fixed a bug in mavl_add FossilOrigin-Name: 4b040305182ef40f6f053fe1d84ea15f533e931c9d665b513b9a644592519f3ebsdmakefiles
parent
2055a0d644
commit
c93867811a
@ -0,0 +1,38 @@ |
||||
#include "kvstore.h" |
||||
#include "cw_types.h" |
||||
#include "cw.h" |
||||
|
||||
#include "log.h" |
||||
|
||||
const char * cw_kvstore_add(mavl_t kvstore, const char *key, const struct cw_Type *type, |
||||
const uint8_t * data, int len) |
||||
{ |
||||
mavldata_t mdata, *mresult; |
||||
|
||||
mdata.kv.key=cw_strdup(key); |
||||
if (!mdata.kv.key){ |
||||
cw_log(LOG_ERR, "Can't allocate memory for key %s: %s", |
||||
key,strerror(errno)); |
||||
return NULL; |
||||
} |
||||
|
||||
|
||||
mresult = type->get(&mdata,data,len); |
||||
if (!mresult){ |
||||
cw_log(LOG_ERR, "Can't create kvstore element for key %s of type %s: %s", |
||||
key,type->name, strerror(errno)); |
||||
free(mdata.kv.key); |
||||
return NULL; |
||||
} |
||||
|
||||
mresult = mavl_add(kvstore, &mdata); |
||||
if (mresult != &mdata){ |
||||
cw_log(LOG_ERR, "Element already exists %s", key); |
||||
/* element already exists */ |
||||
free(mdata.kv.key); |
||||
type->del(mresult); |
||||
return key; |
||||
} |
||||
|
||||
return mdata.kv.key; |
||||
} |
@ -0,0 +1,10 @@ |
||||
#include <string.h> |
||||
#include <stdlib.h> |
||||
|
||||
char *cw_strdup(const char *s) { |
||||
size_t size = strlen(s) + 1; |
||||
char *p = malloc(size); |
||||
if (p)
|
||||
memcpy(p, s, size); |
||||
return p; |
||||
} |
@ -0,0 +1,6 @@ |
||||
#include "mavl.h" |
||||
#include "cw_types.h" |
||||
|
||||
void cw_types_del_null(mavldata_t *data){ |
||||
return; |
||||
} |
@ -0,0 +1,15 @@ |
||||
#ifndef __CW_KVSTORE_H |
||||
#define __CW_KVSTORE_H |
||||
|
||||
#include "mavl.h" |
||||
#include "cw_types.h" |
||||
|
||||
void cw_kvstore_mavl_delete(mavldata_t *data); |
||||
const char * cw_kvstore_add(mavl_t kvstore, const char *key, const struct cw_Type *type, |
||||
const uint8_t * data, int len); |
||||
|
||||
#define cw_kvstore_create()\ |
||||
mavl_create(mavl_cmp_kv, cw_kvstore_mavl_delete) |
||||
|
||||
|
||||
#endif /* __CW_KVSTORE_H */ |
Loading…
Reference in new issue