ucx/map.c

changeset 173
31a8682fffb7
parent 147
1aa598f36872
child 177
11ad03783baf
equal deleted inserted replaced
172:7084e8e8433c 173:31a8682fffb7
42 42
43 if(!allocator) { 43 if(!allocator) {
44 allocator = ucx_default_allocator(); 44 allocator = ucx_default_allocator();
45 } 45 }
46 46
47 UcxMap *map = (UcxMap*)allocator->malloc(allocator->pool, sizeof(UcxMap)); 47 UcxMap *map = (UcxMap*)almalloc(allocator, sizeof(UcxMap));
48 if (!map) { 48 if (!map) {
49 return NULL; 49 return NULL;
50 } 50 }
51 51
52 map->allocator = allocator; 52 map->allocator = allocator;
53 map->map = (UcxMapElement**)allocator->calloc( 53 map->map = (UcxMapElement**)alcalloc(
54 allocator->pool, 54 allocator, size, sizeof(UcxMapElement*));
55 size,
56 sizeof(UcxMapElement*));
57 if(map->map == NULL) { 55 if(map->map == NULL) {
58 allocator->free(allocator->pool, map); 56 alfree(allocator, map);
59 return NULL; 57 return NULL;
60 } 58 }
61 map->size = size; 59 map->size = size;
62 map->count = 0; 60 map->count = 0;
63 61
68 for (size_t n = 0 ; n < map->size ; n++) { 66 for (size_t n = 0 ; n < map->size ; n++) {
69 UcxMapElement *elem = map->map[n]; 67 UcxMapElement *elem = map->map[n];
70 if (elem != NULL) { 68 if (elem != NULL) {
71 do { 69 do {
72 UcxMapElement *next = elem->next; 70 UcxMapElement *next = elem->next;
73 map->allocator->free(map->allocator->pool, elem->key.data); 71 alfree(map->allocator, elem->key.data);
74 map->allocator->free(map->allocator->pool, elem); 72 alfree(map->allocator, elem);
75 elem = next; 73 elem = next;
76 } while (elem != NULL); 74 } while (elem != NULL);
77 } 75 }
78 } 76 }
79 map->allocator->free(map->allocator->pool, map->map); 77 alfree(map->allocator, map->map);
80 } 78 }
81 79
82 void ucx_map_free(UcxMap *map) { 80 void ucx_map_free(UcxMap *map) {
83 ucx_map_free_elmlist(map); 81 ucx_map_free_elmlist(map);
84 map->allocator->free(map->allocator->pool, map); 82 alfree(map->allocator, map);
85 } 83 }
86 84
87 int ucx_map_copy(UcxMap *restrict from, UcxMap *restrict to, 85 int ucx_map_copy(UcxMap *restrict from, UcxMap *restrict to,
88 copy_func fnc, void *data) { 86 copy_func fnc, void *data) {
89 UcxMapIterator i = ucx_map_iterator(from); 87 UcxMapIterator i = ucx_map_iterator(from);
114 oldmap.size = map->size; 112 oldmap.size = map->size;
115 oldmap.count = map->count; 113 oldmap.count = map->count;
116 oldmap.allocator = map->allocator; 114 oldmap.allocator = map->allocator;
117 115
118 map->size = (map->count * 5) >> 1; 116 map->size = (map->count * 5) >> 1;
119 map->map = (UcxMapElement**)map->allocator->calloc( 117 map->map = (UcxMapElement**)alcalloc(
120 map->allocator->pool, 118 map->allocator, map->size, sizeof(UcxMapElement*));
121 map->size,
122 sizeof(UcxMapElement*));
123 if (!map->map) { 119 if (!map->map) {
124 *map = oldmap; 120 *map = oldmap;
125 return 1; 121 return 1;
126 } 122 }
127 map->count = 0; 123 map->count = 0;
148 prev = elm; 144 prev = elm;
149 elm = elm->next; 145 elm = elm->next;
150 } 146 }
151 147
152 if (!elm || elm->key.hash != key.hash) { 148 if (!elm || elm->key.hash != key.hash) {
153 UcxMapElement *e = (UcxMapElement*)allocator->malloc( 149 UcxMapElement *e = (UcxMapElement*)almalloc(
154 allocator->pool, 150 allocator, sizeof(UcxMapElement));
155 sizeof(UcxMapElement));
156 if (!e) { 151 if (!e) {
157 return -1; 152 return -1;
158 } 153 }
159 e->key.data = NULL; 154 e->key.data = NULL;
160 if (prev) { 155 if (prev) {
165 e->next = elm; 160 e->next = elm;
166 elm = e; 161 elm = e;
167 } 162 }
168 163
169 if (!elm->key.data) { 164 if (!elm->key.data) {
170 void *kd = allocator->malloc(allocator->pool, key.len); 165 void *kd = almalloc(allocator, key.len);
171 if (!kd) { 166 if (!kd) {
172 return -1; 167 return -1;
173 } 168 }
174 memcpy(kd, key.data, key.len); 169 memcpy(kd, key.data, key.len);
175 key.data = kd; 170 key.data = kd;
198 if (pelm) { 193 if (pelm) {
199 pelm->next = elm->next; 194 pelm->next = elm->next;
200 } else { 195 } else {
201 map->map[slot] = elm->next; 196 map->map[slot] = elm->next;
202 } 197 }
203 map->allocator->free(map->allocator->pool, elm->key.data); 198 alfree(map->allocator, elm->key.data);
204 map->allocator->free(map->allocator->pool, elm); 199 alfree(map->allocator, elm);
205 map->count--; 200 map->count--;
206 } 201 }
207 202
208 return data; 203 return data;
209 } 204 }

mercurial