Sat, 19 Jan 2013 20:13:07 +0100
fixed some memory leaks
/* * */ #ifndef MAP_H #define MAP_H #include "ucx.h" #include "string.h" #include "mempool.h" #include <stdio.h> #ifdef __cplusplus extern "C" { #endif #define UCX_MAP_FOREACH(elm,iter) \ for(;ucx_map_iter_next(&iter,(void*)&elm)==0;) typedef struct UcxMap UcxMap; typedef struct UcxKey UcxKey; typedef struct UcxMapElement UcxMapElement; typedef struct UcxMapIterator UcxMapIterator; /* * param 1: element * param 2: additional data * param 3: size of encoded data will be stored here * returns encoded / decoded string or NULL on failure */ typedef void*(*ucx_map_coder)(void*,void*,size_t*); struct UcxMap { UcxMapElement **map; size_t size; size_t count; }; struct UcxKey { void *data; size_t len; int hash; }; struct UcxMapElement { void *data; UcxMapElement *next; UcxKey key; }; struct UcxMapIterator { UcxMap *map; UcxMapElement *cur; int index; }; UcxMap *ucx_map_new(size_t size); void ucx_map_free(UcxMap *map); /* you cannot clone maps with more than 390 mio entries */ int ucx_map_copy(UcxMap *from, UcxMap *to, copy_func fnc, void *data); UcxMap *ucx_map_clone(UcxMap *map, copy_func fnc, void *data); int ucx_map_rehash(UcxMap *map); int ucx_map_put(UcxMap *map, UcxKey key, void *data); void* ucx_map_get(UcxMap *map, UcxKey key); #define ucx_map_sstr_put(m, s, d) ucx_map_put(m, ucx_key(s.ptr, s.length), d) #define ucx_map_cstr_put(m, s, d) ucx_map_put(m, ucx_key(s, 1+strlen(s)), d) #define ucx_map_sstr_get(m, s) ucx_map_get(m, ucx_key(s.ptr, s.length)) #define ucx_map_cstr_get(m, s) ucx_map_get(m, ucx_key(s, 1+strlen(s))) UcxKey ucx_key(void *data, size_t len); int ucx_hash(char *data, size_t len); UcxMapIterator ucx_map_iterator(UcxMap *map); int ucx_map_iter_next(UcxMapIterator *i, void **elm); /* use macros for string maps only, values are not encoded */ #define ucx_map_load(map, f, alloc) ucx_map_load_enc(map, f, alloc, NULL, NULL) #define ucx_map_store(map, f) ucx_map_store_enc(map, f, NULL, NULL) int ucx_map_load_enc(UcxMap *map, FILE *f, UcxAllocator allocator, ucx_map_coder decoder, void* decdata); /* encoders shall provide null terminated strings*/ int ucx_map_store_enc(UcxMap *map, FILE *f, ucx_map_coder encoder, void* encdata); #ifdef __cplusplus } #endif #endif /* MAP_H */