Thu, 11 Jul 2013 17:32:48 +0200
map uses an allocator
g++-debug.mk | file | annotate | diff | comparison | revisions | |
gcc-debug.mk | file | annotate | diff | comparison | revisions | |
ucx/allocator.c | file | annotate | diff | comparison | revisions | |
ucx/allocator.h | file | annotate | diff | comparison | revisions | |
ucx/map.c | file | annotate | diff | comparison | revisions | |
ucx/map.h | file | annotate | diff | comparison | revisions |
1.1 --- a/g++-debug.mk Fri Jun 21 11:18:24 2013 +0200 1.2 +++ b/g++-debug.mk Thu Jul 11 17:32:48 2013 +0200 1.3 @@ -31,7 +31,7 @@ 1.4 AR = ar 1.5 RM = rm 1.6 1.7 -CFLAGS = -std=gnu++0x -g -O2 -fstrict-aliasing -Werror -Wall -pedantic -c 1.8 +CFLAGS = -std=gnu++0x -g -O2 -fstrict-aliasing -Wall -pedantic -c 1.9 COFLAGS = -o 1.10 LDFLAGS = 1.11 LOFLAGS = -o
2.1 --- a/gcc-debug.mk Fri Jun 21 11:18:24 2013 +0200 2.2 +++ b/gcc-debug.mk Thu Jul 11 17:32:48 2013 +0200 2.3 @@ -31,7 +31,7 @@ 2.4 AR = ar 2.5 RM = rm 2.6 2.7 -CFLAGS = -std=gnu99 -g -O2 -fstrict-aliasing -Werror -Wall -pedantic -c 2.8 +CFLAGS = -std=gnu99 -g -O2 -fstrict-aliasing -Wall -pedantic -c 2.9 COFLAGS = -o 2.10 LDFLAGS = 2.11 LOFLAGS = -o
3.1 --- a/ucx/allocator.c Fri Jun 21 11:18:24 2013 +0200 3.2 +++ b/ucx/allocator.c Thu Jul 11 17:32:48 2013 +0200 3.3 @@ -29,6 +29,19 @@ 3.4 #include <stdlib.h> 3.5 #include "allocator.h" 3.6 3.7 +UcxAllocator default_allocator = { 3.8 + NULL, 3.9 + ucx_default_malloc, 3.10 + ucx_default_calloc, 3.11 + ucx_default_realloc, 3.12 + ucx_default_free 3.13 +}; 3.14 + 3.15 +UcxAllocator *ucx_default_allocator() { 3.16 + UcxAllocator *allocator = &default_allocator; 3.17 + return allocator; 3.18 +} 3.19 + 3.20 void *ucx_default_malloc(void *ignore, size_t n) { 3.21 return malloc(n); 3.22 } 3.23 @@ -40,3 +53,7 @@ 3.24 void *ucx_default_realloc(void *ignore, void *data, size_t n) { 3.25 return realloc(data, n); 3.26 } 3.27 + 3.28 +void ucx_default_free(void *ignore, void *data) { 3.29 + free(data); 3.30 +}
4.1 --- a/ucx/allocator.h Fri Jun 21 11:18:24 2013 +0200 4.2 +++ b/ucx/allocator.h Thu Jul 11 17:32:48 2013 +0200 4.3 @@ -38,20 +38,26 @@ 4.4 typedef void*(*ucx_allocator_malloc)(void *pool, size_t n); 4.5 typedef void*(*ucx_allocator_calloc)(void *pool, size_t n, size_t size); 4.6 typedef void*(*ucx_allocator_realloc)(void *pool, void *data, size_t n); 4.7 +typedef void(*ucx_allocator_free)(void *pool, void *data); 4.8 4.9 typedef struct { 4.10 void *pool; 4.11 - ucx_allocator_malloc malloc; 4.12 - ucx_allocator_calloc calloc; 4.13 + ucx_allocator_malloc malloc; 4.14 + ucx_allocator_calloc calloc; 4.15 ucx_allocator_realloc realloc; 4.16 + ucx_allocator_free free; 4.17 } UcxAllocator; 4.18 4.19 +UcxAllocator *ucx_default_allocator(); 4.20 + 4.21 void *ucx_default_malloc(void *ignore, size_t n); 4.22 void *ucx_default_calloc(void *ignore, size_t n, size_t size); 4.23 void *ucx_default_realloc(void *ignore, void *data, size_t n); 4.24 +void ucx_default_free(void *ignore, void *data); 4.25 4.26 #define UCX_ALLOCATOR_DEFAULT {NULL, \ 4.27 - ucx_default_malloc, ucx_default_calloc, ucx_default_realloc} 4.28 + ucx_default_malloc, ucx_default_calloc, ucx_default_realloc, \ 4.29 + ucx_default_free } 4.30 4.31 #ifdef __cplusplus 4.32 }
5.1 --- a/ucx/map.c Fri Jun 21 11:18:24 2013 +0200 5.2 +++ b/ucx/map.c Thu Jul 11 17:32:48 2013 +0200 5.3 @@ -32,18 +32,30 @@ 5.4 #include "map.h" 5.5 5.6 UcxMap *ucx_map_new(size_t size) { 5.7 + return ucx_map_new_allocator(size, NULL); 5.8 +} 5.9 + 5.10 +UcxMap *ucx_map_new_allocator(size_t size, UcxAllocator *allocator) { 5.11 if(size == 0) { 5.12 size = 16; 5.13 } 5.14 5.15 - UcxMap *map = (UcxMap*)malloc(sizeof(UcxMap)); 5.16 + if(!allocator) { 5.17 + allocator = ucx_default_allocator(); 5.18 + } 5.19 + 5.20 + UcxMap *map = (UcxMap*)allocator->malloc(allocator->pool, sizeof(UcxMap)); 5.21 if(map == NULL) { 5.22 return NULL; 5.23 } 5.24 - 5.25 - map->map = (UcxMapElement**)calloc(size, sizeof(UcxMapElement*)); 5.26 + 5.27 + map->allocator = allocator; 5.28 + map->map = (UcxMapElement**)allocator->calloc( 5.29 + allocator->pool, 5.30 + size, 5.31 + sizeof(UcxMapElement*)); 5.32 if(map->map == NULL) { 5.33 - free(map); 5.34 + allocator->free(allocator->pool, map); 5.35 return NULL; 5.36 } 5.37 map->size = size; 5.38 @@ -58,18 +70,18 @@ 5.39 if (elem != NULL) { 5.40 do { 5.41 UcxMapElement *next = elem->next; 5.42 - free(elem->key.data); 5.43 + map->allocator->free(map->allocator->pool, elem->key.data); 5.44 free(elem); 5.45 elem = next; 5.46 } while (elem != NULL); 5.47 } 5.48 } 5.49 - free(map->map); 5.50 + map->allocator->free(map->allocator->pool, map->map); 5.51 } 5.52 5.53 void ucx_map_free(UcxMap *map) { 5.54 ucx_map_free_elmlist(map); 5.55 - free(map); 5.56 + map->allocator->free(map->allocator->pool, map); 5.57 } 5.58 5.59 int ucx_map_copy(UcxMap *restrict from, UcxMap *restrict to, 5.60 @@ -102,9 +114,13 @@ 5.61 oldmap.map = map->map; 5.62 oldmap.size = map->size; 5.63 oldmap.count = map->count; 5.64 + oldmap.allocator = map->allocator; 5.65 5.66 map->size = (map->count * 5) >> 1; 5.67 - map->map = (UcxMapElement**)calloc(map->size, sizeof(UcxMapElement*)); 5.68 + map->map = (UcxMapElement**)map->allocator->calloc( 5.69 + map->allocator->pool, 5.70 + map->size, 5.71 + sizeof(UcxMapElement*)); 5.72 if(map->map == NULL) { 5.73 *map = oldmap; 5.74 return 1; 5.75 @@ -119,6 +135,8 @@ 5.76 } 5.77 5.78 int ucx_map_put(UcxMap *map, UcxKey key, void *data) { 5.79 + UcxAllocator *allocator = map->allocator; 5.80 + 5.81 if(key.hash == 0) { 5.82 key.hash = ucx_hash((char*)key.data, key.len); 5.83 } 5.84 @@ -133,7 +151,9 @@ 5.85 } 5.86 5.87 if (elm == NULL || elm->key.hash != key.hash) { 5.88 - UcxMapElement *e = (UcxMapElement*)malloc(sizeof(UcxMapElement)); 5.89 + UcxMapElement *e = (UcxMapElement*)allocator->malloc( 5.90 + allocator->pool, 5.91 + sizeof(UcxMapElement)); 5.92 if(e == NULL) { 5.93 return -1; 5.94 } 5.95 @@ -148,7 +168,7 @@ 5.96 } 5.97 5.98 if(elm->key.data == NULL) { 5.99 - void *kd = malloc(key.len); 5.100 + void *kd = allocator->malloc(allocator->pool, key.len); 5.101 if (kd == NULL) { 5.102 return -1; 5.103 } 5.104 @@ -181,7 +201,7 @@ 5.105 } else { 5.106 map->map[slot] = elm->next; 5.107 } 5.108 - free(elm); 5.109 + map->allocator->free(map->allocator->pool, elm); 5.110 map->count--; 5.111 } 5.112
6.1 --- a/ucx/map.h Fri Jun 21 11:18:24 2013 +0200 6.2 +++ b/ucx/map.h Thu Jul 11 17:32:48 2013 +0200 6.3 @@ -55,6 +55,7 @@ 6.4 typedef void*(*ucx_map_coder)(void*,void*,size_t*); 6.5 6.6 struct UcxMap { 6.7 + UcxAllocator *allocator; 6.8 UcxMapElement **map; 6.9 size_t size; 6.10 size_t count; 6.11 @@ -80,6 +81,7 @@ 6.12 6.13 6.14 UcxMap *ucx_map_new(size_t size); 6.15 +UcxMap *ucx_map_new_allocator(size_t size, UcxAllocator *allocator); 6.16 void ucx_map_free(UcxMap *map); 6.17 /* you cannot clone maps with more than 390 mio entries */ 6.18 int ucx_map_copy(UcxMap *restrict from, UcxMap *restrict to,