diff -r a54115d554f7 -r 86b19c98b5fd ucx/map.c --- a/ucx/map.c Fri Jun 21 11:18:24 2013 +0200 +++ b/ucx/map.c Thu Jul 11 17:32:48 2013 +0200 @@ -32,18 +32,30 @@ #include "map.h" UcxMap *ucx_map_new(size_t size) { + return ucx_map_new_allocator(size, NULL); +} + +UcxMap *ucx_map_new_allocator(size_t size, UcxAllocator *allocator) { if(size == 0) { size = 16; } - UcxMap *map = (UcxMap*)malloc(sizeof(UcxMap)); + if(!allocator) { + allocator = ucx_default_allocator(); + } + + UcxMap *map = (UcxMap*)allocator->malloc(allocator->pool, sizeof(UcxMap)); if(map == NULL) { return NULL; } - - map->map = (UcxMapElement**)calloc(size, sizeof(UcxMapElement*)); + + map->allocator = allocator; + map->map = (UcxMapElement**)allocator->calloc( + allocator->pool, + size, + sizeof(UcxMapElement*)); if(map->map == NULL) { - free(map); + allocator->free(allocator->pool, map); return NULL; } map->size = size; @@ -58,18 +70,18 @@ if (elem != NULL) { do { UcxMapElement *next = elem->next; - free(elem->key.data); + map->allocator->free(map->allocator->pool, elem->key.data); free(elem); elem = next; } while (elem != NULL); } } - free(map->map); + map->allocator->free(map->allocator->pool, map->map); } void ucx_map_free(UcxMap *map) { ucx_map_free_elmlist(map); - free(map); + map->allocator->free(map->allocator->pool, map); } int ucx_map_copy(UcxMap *restrict from, UcxMap *restrict to, @@ -102,9 +114,13 @@ oldmap.map = map->map; oldmap.size = map->size; oldmap.count = map->count; + oldmap.allocator = map->allocator; map->size = (map->count * 5) >> 1; - map->map = (UcxMapElement**)calloc(map->size, sizeof(UcxMapElement*)); + map->map = (UcxMapElement**)map->allocator->calloc( + map->allocator->pool, + map->size, + sizeof(UcxMapElement*)); if(map->map == NULL) { *map = oldmap; return 1; @@ -119,6 +135,8 @@ } int ucx_map_put(UcxMap *map, UcxKey key, void *data) { + UcxAllocator *allocator = map->allocator; + if(key.hash == 0) { key.hash = ucx_hash((char*)key.data, key.len); } @@ -133,7 +151,9 @@ } if (elm == NULL || elm->key.hash != key.hash) { - UcxMapElement *e = (UcxMapElement*)malloc(sizeof(UcxMapElement)); + UcxMapElement *e = (UcxMapElement*)allocator->malloc( + allocator->pool, + sizeof(UcxMapElement)); if(e == NULL) { return -1; } @@ -148,7 +168,7 @@ } if(elm->key.data == NULL) { - void *kd = malloc(key.len); + void *kd = allocator->malloc(allocator->pool, key.len); if (kd == NULL) { return -1; } @@ -181,7 +201,7 @@ } else { map->map[slot] = elm->next; } - free(elm); + map->allocator->free(map->allocator->pool, elm); map->count--; }