ucx/map.c

changeset 107
86b19c98b5fd
parent 103
08018864fb91
child 108
d2b1e67b2b48
equal deleted inserted replaced
106:a54115d554f7 107:86b19c98b5fd
30 #include <string.h> 30 #include <string.h>
31 31
32 #include "map.h" 32 #include "map.h"
33 33
34 UcxMap *ucx_map_new(size_t size) { 34 UcxMap *ucx_map_new(size_t size) {
35 return ucx_map_new_allocator(size, NULL);
36 }
37
38 UcxMap *ucx_map_new_allocator(size_t size, UcxAllocator *allocator) {
35 if(size == 0) { 39 if(size == 0) {
36 size = 16; 40 size = 16;
37 } 41 }
38 42
39 UcxMap *map = (UcxMap*)malloc(sizeof(UcxMap)); 43 if(!allocator) {
44 allocator = ucx_default_allocator();
45 }
46
47 UcxMap *map = (UcxMap*)allocator->malloc(allocator->pool, sizeof(UcxMap));
40 if(map == NULL) { 48 if(map == NULL) {
41 return NULL; 49 return NULL;
42 } 50 }
43 51
44 map->map = (UcxMapElement**)calloc(size, sizeof(UcxMapElement*)); 52 map->allocator = allocator;
53 map->map = (UcxMapElement**)allocator->calloc(
54 allocator->pool,
55 size,
56 sizeof(UcxMapElement*));
45 if(map->map == NULL) { 57 if(map->map == NULL) {
46 free(map); 58 allocator->free(allocator->pool, map);
47 return NULL; 59 return NULL;
48 } 60 }
49 map->size = size; 61 map->size = size;
50 map->count = 0; 62 map->count = 0;
51 63
56 for (size_t n = 0 ; n < map->size ; n++) { 68 for (size_t n = 0 ; n < map->size ; n++) {
57 UcxMapElement *elem = map->map[n]; 69 UcxMapElement *elem = map->map[n];
58 if (elem != NULL) { 70 if (elem != NULL) {
59 do { 71 do {
60 UcxMapElement *next = elem->next; 72 UcxMapElement *next = elem->next;
61 free(elem->key.data); 73 map->allocator->free(map->allocator->pool, elem->key.data);
62 free(elem); 74 free(elem);
63 elem = next; 75 elem = next;
64 } while (elem != NULL); 76 } while (elem != NULL);
65 } 77 }
66 } 78 }
67 free(map->map); 79 map->allocator->free(map->allocator->pool, map->map);
68 } 80 }
69 81
70 void ucx_map_free(UcxMap *map) { 82 void ucx_map_free(UcxMap *map) {
71 ucx_map_free_elmlist(map); 83 ucx_map_free_elmlist(map);
72 free(map); 84 map->allocator->free(map->allocator->pool, map);
73 } 85 }
74 86
75 int ucx_map_copy(UcxMap *restrict from, UcxMap *restrict to, 87 int ucx_map_copy(UcxMap *restrict from, UcxMap *restrict to,
76 copy_func fnc, void *data) { 88 copy_func fnc, void *data) {
77 UcxMapIterator i = ucx_map_iterator(from); 89 UcxMapIterator i = ucx_map_iterator(from);
100 if (map->count > load) { 112 if (map->count > load) {
101 UcxMap oldmap; 113 UcxMap oldmap;
102 oldmap.map = map->map; 114 oldmap.map = map->map;
103 oldmap.size = map->size; 115 oldmap.size = map->size;
104 oldmap.count = map->count; 116 oldmap.count = map->count;
117 oldmap.allocator = map->allocator;
105 118
106 map->size = (map->count * 5) >> 1; 119 map->size = (map->count * 5) >> 1;
107 map->map = (UcxMapElement**)calloc(map->size, sizeof(UcxMapElement*)); 120 map->map = (UcxMapElement**)map->allocator->calloc(
121 map->allocator->pool,
122 map->size,
123 sizeof(UcxMapElement*));
108 if(map->map == NULL) { 124 if(map->map == NULL) {
109 *map = oldmap; 125 *map = oldmap;
110 return 1; 126 return 1;
111 } 127 }
112 map->count = 0; 128 map->count = 0;
117 } 133 }
118 return 0; 134 return 0;
119 } 135 }
120 136
121 int ucx_map_put(UcxMap *map, UcxKey key, void *data) { 137 int ucx_map_put(UcxMap *map, UcxKey key, void *data) {
138 UcxAllocator *allocator = map->allocator;
139
122 if(key.hash == 0) { 140 if(key.hash == 0) {
123 key.hash = ucx_hash((char*)key.data, key.len); 141 key.hash = ucx_hash((char*)key.data, key.len);
124 } 142 }
125 143
126 size_t slot = key.hash%map->size; 144 size_t slot = key.hash%map->size;
131 prev = elm; 149 prev = elm;
132 elm = elm->next; 150 elm = elm->next;
133 } 151 }
134 152
135 if (elm == NULL || elm->key.hash != key.hash) { 153 if (elm == NULL || elm->key.hash != key.hash) {
136 UcxMapElement *e = (UcxMapElement*)malloc(sizeof(UcxMapElement)); 154 UcxMapElement *e = (UcxMapElement*)allocator->malloc(
155 allocator->pool,
156 sizeof(UcxMapElement));
137 if(e == NULL) { 157 if(e == NULL) {
138 return -1; 158 return -1;
139 } 159 }
140 e->key.data = NULL; 160 e->key.data = NULL;
141 if (prev) { 161 if (prev) {
146 e->next = elm; 166 e->next = elm;
147 elm = e; 167 elm = e;
148 } 168 }
149 169
150 if(elm->key.data == NULL) { 170 if(elm->key.data == NULL) {
151 void *kd = malloc(key.len); 171 void *kd = allocator->malloc(allocator->pool, key.len);
152 if (kd == NULL) { 172 if (kd == NULL) {
153 return -1; 173 return -1;
154 } 174 }
155 memcpy(kd, key.data, key.len); 175 memcpy(kd, key.data, key.len);
156 key.data = kd; 176 key.data = kd;
179 if (pelm) { 199 if (pelm) {
180 pelm->next = elm->next; 200 pelm->next = elm->next;
181 } else { 201 } else {
182 map->map[slot] = elm->next; 202 map->map[slot] = elm->next;
183 } 203 }
184 free(elm); 204 map->allocator->free(map->allocator->pool, elm);
185 map->count--; 205 map->count--;
186 } 206 }
187 207
188 return data; 208 return data;
189 } 209 }

mercurial