use nix build & meson & zlog
This commit is contained in:
parent
0123623742
commit
346768a144
60 changed files with 13739 additions and 0 deletions
147
src/lib/xstring.c
Normal file
147
src/lib/xstring.c
Normal file
|
|
@ -0,0 +1,147 @@
|
|||
#include <errno.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "htab.h"
|
||||
#include "utils.h"
|
||||
#include "xstring.h"
|
||||
|
||||
static void* xstring_create(objmgr_t*, objtype_t*, va_list);
|
||||
static void xstring_fini(void*);
|
||||
static void xstring_type_fini(objtype_t*);
|
||||
|
||||
static xstring_typedata_t xstring_typedata;
|
||||
|
||||
static objtype_t xstring = {
|
||||
.type = "xstring",
|
||||
.fn_create = (obj_create_fn_t)xstring_create,
|
||||
.fn_fini = xstring_fini,
|
||||
.fn_type_fini = (objtype_fini_fn_t)xstring_type_fini,
|
||||
.typedata = &xstring_typedata
|
||||
};
|
||||
|
||||
static void xstring_obj_killer(myobj_t* obj)
|
||||
{
|
||||
if (obj == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
memset(obj, 0x0, sizeof(*obj));
|
||||
free(obj);
|
||||
}
|
||||
|
||||
int register_xstring(objmgr_t* mgr,
|
||||
int nslots,
|
||||
int delta)
|
||||
{
|
||||
ht_init(&(xstring_typedata.tab), nslots,
|
||||
string_hash_key);
|
||||
zarray_init(&(xstring_typedata.strings), delta,
|
||||
(zarray_item_killer_t)xstring_obj_killer);
|
||||
|
||||
return objmgr_register(mgr, &xstring);
|
||||
}
|
||||
|
||||
static void* xstring_create(objmgr_t* mgr,
|
||||
objtype_t* type,
|
||||
va_list ap)
|
||||
{
|
||||
char* s = va_arg(ap, char*);
|
||||
char* p = NULL;
|
||||
myobj_t* obj = NULL;
|
||||
hent_t* e = NULL;
|
||||
xstring_typedata_t* typedata = NULL;
|
||||
int32_t idx;
|
||||
|
||||
if (s == NULL) {
|
||||
SET_ERROR(EINVAL);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
typedata = (xstring_typedata_t*)type->typedata;
|
||||
|
||||
e = ht_get((htab_t*)&(typedata->tab), (void*)s);
|
||||
if (e != NULL) {
|
||||
idx = e->data;
|
||||
obj = (myobj_t*)zarray_get(&(typedata->strings), idx);
|
||||
|
||||
if (obj != NULL) {
|
||||
p = MYOBJ_TO_OBJ(char*, obj);
|
||||
return obj_ref(p, OBJREF_NOP);
|
||||
}
|
||||
|
||||
SET_ERROR(EINVAL);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
obj = (myobj_t*)calloc(1, sizeof(*obj) + strlen(s) + 1);
|
||||
if (obj == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
idx = zarray_store(&(typedata->strings), obj);
|
||||
if (idx < 0) {
|
||||
free(obj);
|
||||
SET_ERROR(-idx);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
obj_init(mgr, type, idx, obj);
|
||||
|
||||
p = MYOBJ_TO_OBJ(char*, obj);
|
||||
|
||||
strcpy(p, s);
|
||||
|
||||
e = ht_add((htab_t*)&(typedata->tab), (void*)s);
|
||||
if (e == NULL) {
|
||||
obj_deref(p);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
e->data = idx;
|
||||
|
||||
return p;
|
||||
}
|
||||
|
||||
/* this will ONLY be called by obj_deref() */
|
||||
static void xstring_fini(void* s)
|
||||
{
|
||||
myobj_t* myobj;
|
||||
objtype_t* typeval;
|
||||
xstring_typedata_t* typedata;
|
||||
int32_t idx;
|
||||
|
||||
if (s == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
myobj = TO_MYOBJ(s);
|
||||
typeval = myobj->type;
|
||||
typedata = (xstring_typedata_t*)typeval->typedata;
|
||||
|
||||
if (typedata == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
ht_del(&(typedata->tab), (void*)s);
|
||||
|
||||
idx = (int32_t)myobj->exdata;
|
||||
if (idx >= 0) {
|
||||
zarray_del(&(typedata->strings), idx);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
static void xstring_type_fini(objtype_t* typeval)
|
||||
{
|
||||
xstring_typedata_t* typedata = (xstring_typedata_t*)typeval->typedata;
|
||||
|
||||
if (typedata == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
ht_fini(&(typedata->tab));
|
||||
zarray_fini(&(typedata->strings));
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue