ucx/map.c

Fri, 12 Oct 2012 12:00:06 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Fri, 12 Oct 2012 12:00:06 +0200
changeset 70
6721482eaf8e
parent 69
fb59270b1de3
child 79
cf3757c60c8f
permissions
-rw-r--r--

fixed memory leak in ucx_map_rehash

olaf@20 1 /*
olaf@20 2 *
olaf@20 3 */
olaf@2 4
olaf@20 5 #include <stdlib.h>
olaf@20 6 #include <string.h>
olaf@20 7
olaf@20 8 #include "map.h"
olaf@20 9
olaf@20 10 UcxMap *ucx_map_new(size_t size) {
olaf@45 11 if(size == 0) {
olaf@45 12 size = 16;
olaf@45 13 }
olaf@45 14
olaf@20 15 UcxMap *map = (UcxMap*)malloc(sizeof(UcxMap));
olaf@20 16 if(map == NULL) {
olaf@20 17 return NULL;
olaf@20 18 }
olaf@20 19
universe@29 20 map->map = (UcxMapElement**)calloc(size, sizeof(UcxMapElement*));
olaf@20 21 if(map->map == NULL) {
olaf@20 22 free(map);
olaf@20 23 return NULL;
olaf@20 24 }
olaf@20 25 map->size = size;
olaf@45 26 map->count = 0;
olaf@20 27
olaf@20 28 return map;
olaf@20 29 }
olaf@20 30
olaf@70 31 void ucx_map_free_elmlist(UcxMap *map) {
universe@29 32 for (size_t n = 0 ; n < map->size ; n++) {
universe@29 33 UcxMapElement *elem = map->map[n];
universe@29 34 if (elem != NULL) {
universe@29 35 do {
universe@29 36 UcxMapElement *next = elem->next;
olaf@30 37 free(elem->key.data);
universe@29 38 free(elem);
universe@29 39 elem = next;
universe@29 40 } while (elem != NULL);
universe@29 41 }
universe@29 42 }
olaf@30 43 free(map->map);
olaf@70 44 }
olaf@70 45
olaf@70 46 void ucx_map_free(UcxMap *map) {
olaf@70 47 ucx_map_free_elmlist(map);
universe@29 48 free(map);
universe@29 49 }
universe@29 50
universe@67 51 int ucx_map_copy(UcxMap *restrict from, UcxMap *restrict to,
universe@67 52 copy_func fnc, void *data) {
olaf@52 53 UcxMapIterator i = ucx_map_iterator(from);
olaf@52 54 void *value;
olaf@52 55 UCX_MAP_FOREACH(value, i) {
olaf@52 56 int ret = ucx_map_put(to, i.cur->key, fnc ? fnc(value, data) : value);
olaf@52 57 if(ret != 0) {
olaf@52 58 return 1;
olaf@52 59 }
olaf@52 60 }
olaf@52 61 return 0;
olaf@52 62 }
olaf@52 63
olaf@44 64 UcxMap *ucx_map_clone(UcxMap *map, copy_func fnc, void *data) {
universe@51 65 size_t bs = (map->count * 5) >> 1;
olaf@45 66 UcxMap *newmap = ucx_map_new(bs > map->size ? bs : map->size);
olaf@52 67 if(newmap == NULL) {
olaf@52 68 return NULL;
olaf@44 69 }
olaf@52 70 ucx_map_copy(map, newmap, fnc, data);
olaf@44 71 return newmap;
olaf@44 72 }
olaf@44 73
olaf@52 74 int ucx_map_rehash(UcxMap *map) {
universe@51 75 size_t load = (map->size * 3) >> 2;
universe@51 76 if (map->count > load) {
olaf@52 77 UcxMap oldmap;
olaf@52 78 oldmap.map = map->map;
olaf@52 79 oldmap.size = map->size;
olaf@52 80 oldmap.count = map->count;
olaf@52 81
olaf@52 82 map->size = (map->count * 5) >> 1;
olaf@52 83 map->map = (UcxMapElement**)calloc(map->size, sizeof(UcxMapElement*));
olaf@52 84 if(map->map == NULL) {
olaf@52 85 *map = oldmap;
olaf@52 86 return 1;
olaf@52 87 }
olaf@52 88 map->count = 0;
olaf@52 89 ucx_map_copy(&oldmap, map, NULL, NULL);
olaf@70 90
olaf@70 91 /* free the UcxMapElement list of oldmap */
olaf@70 92 ucx_map_free_elmlist(&oldmap);
universe@51 93 }
olaf@52 94 return 0;
universe@51 95 }
universe@51 96
olaf@20 97 int ucx_map_put(UcxMap *map, UcxKey key, void *data) {
olaf@20 98 if(key.hash == 0) {
olaf@20 99 key.hash = ucx_hash((char*)key.data, key.len);
olaf@20 100 }
olaf@20 101
universe@29 102 size_t slot = key.hash%map->size;
universe@67 103 UcxMapElement *restrict elm = map->map[slot];
universe@67 104 UcxMapElement *restrict prev = NULL;
universe@29 105
universe@29 106 while (elm != NULL && elm->key.hash < key.hash) {
universe@29 107 prev = elm;
universe@29 108 elm = elm->next;
universe@29 109 }
universe@29 110
universe@29 111 if (elm == NULL || elm->key.hash != key.hash) {
olaf@20 112 UcxMapElement *e = (UcxMapElement*)malloc(sizeof(UcxMapElement));
olaf@20 113 if(e == NULL) {
olaf@20 114 return -1;
olaf@20 115 }
olaf@30 116 e->key.data = NULL;
universe@53 117 if (prev) {
universe@53 118 prev->next = e;
universe@53 119 } else {
universe@29 120 map->map[slot] = e;
universe@29 121 }
universe@29 122 e->next = elm;
olaf@20 123 elm = e;
olaf@20 124 }
universe@29 125
olaf@30 126 if(elm->key.data == NULL) {
olaf@30 127 void *kd = malloc(key.len);
olaf@30 128 if (kd == NULL) {
olaf@30 129 return -1;
olaf@30 130 }
olaf@30 131 memcpy(kd, key.data, key.len);
olaf@30 132 key.data = kd;
olaf@30 133 elm->key = key;
olaf@45 134 map->count++;
olaf@30 135 }
olaf@20 136 elm->data = data;
olaf@20 137
olaf@20 138 return 0;
olaf@20 139 }
olaf@20 140
universe@53 141 void* ucx_map_get_and_remove(UcxMap *map, UcxKey key, _Bool remove) {
olaf@20 142 if(key.hash == 0) {
olaf@20 143 key.hash = ucx_hash((char*)key.data, key.len);
olaf@20 144 }
olaf@20 145
universe@53 146 size_t slot = key.hash%map->size;
universe@67 147 UcxMapElement *restrict elm = map->map[slot];
universe@67 148 UcxMapElement *restrict pelm = NULL;
universe@53 149 while (elm && elm->key.hash <= key.hash) {
olaf@20 150 if(elm->key.hash == key.hash) {
olaf@20 151 int n = (key.len > elm->key.len) ? elm->key.len : key.len;
universe@29 152 if (memcmp(elm->key.data, key.data, n) == 0) {
universe@53 153 void *data = elm->data;
universe@53 154 if (remove) {
universe@53 155 if (pelm) {
universe@53 156 pelm->next = elm->next;
universe@53 157 } else {
universe@53 158 map->map[slot] = elm->next;
universe@53 159 }
universe@53 160 free(elm);
universe@53 161 map->count--;
universe@53 162 }
universe@53 163
universe@53 164 return data;
olaf@20 165 }
olaf@20 166 }
universe@53 167 pelm = elm;
universe@53 168 elm = pelm->next;
olaf@20 169 }
olaf@20 170
olaf@20 171 return NULL;
olaf@20 172 }
olaf@20 173
universe@53 174 void *ucx_map_get(UcxMap *map, UcxKey key) {
universe@53 175 return ucx_map_get_and_remove(map, key, 0);
universe@53 176 }
universe@53 177
universe@53 178 void *ucx_map_remove(UcxMap *map, UcxKey key) {
universe@53 179 return ucx_map_get_and_remove(map, key, 1);
universe@53 180 }
universe@53 181
olaf@20 182 UcxKey ucx_key(void *data, size_t len) {
olaf@20 183 UcxKey key;
olaf@20 184 key.data = data;
olaf@20 185 key.len = len;
universe@69 186 key.hash = ucx_hash((const char*) data, len);
olaf@20 187 return key;
olaf@20 188 }
olaf@20 189
olaf@20 190
universe@67 191 int ucx_hash(const char *data, size_t len) {
olaf@20 192 /* murmur hash 2 */
olaf@20 193
olaf@20 194 int m = 0x5bd1e995;
olaf@20 195 int r = 24;
olaf@20 196
olaf@20 197 int h = 25 ^ len;
olaf@20 198
olaf@20 199 int i = 0;
olaf@20 200 while (len >= 4) {
olaf@20 201 int k = data[i + 0] & 0xFF;
olaf@20 202 k |= (data[i + 1] & 0xFF) << 8;
olaf@20 203 k |= (data[i + 2] & 0xFF) << 16;
olaf@20 204 k |= (data[i + 3] & 0xFF) << 24;
olaf@20 205
olaf@20 206 k *= m;
olaf@20 207 k ^= k >> r;
olaf@20 208 k *= m;
olaf@20 209
olaf@20 210 h *= m;
olaf@20 211 h ^= k;
olaf@20 212
olaf@20 213 i += 4;
olaf@20 214 len -= 4;
olaf@20 215 }
olaf@20 216
olaf@20 217 switch (len) {
olaf@20 218 case 3: h ^= (data[i + 2] & 0xFF) << 16;
universe@38 219 /* no break */
olaf@20 220 case 2: h ^= (data[i + 1] & 0xFF) << 8;
universe@38 221 /* no break */
olaf@20 222 case 1: h ^= (data[i + 0] & 0xFF); h *= m;
universe@38 223 /* no break */
olaf@20 224 }
olaf@20 225
olaf@20 226 h ^= h >> 13;
olaf@20 227 h *= m;
olaf@20 228 h ^= h >> 15;
olaf@20 229
olaf@20 230 return h;
olaf@20 231 }
olaf@31 232
olaf@31 233 UcxMapIterator ucx_map_iterator(UcxMap *map) {
olaf@31 234 UcxMapIterator i;
olaf@31 235 i.map = map;
olaf@31 236 i.cur = NULL;
olaf@31 237 i.index = 0;
olaf@31 238 return i;
olaf@31 239 }
olaf@31 240
olaf@31 241 int ucx_map_iter_next(UcxMapIterator *i, void **elm) {
olaf@31 242 UcxMapElement *e = i->cur;
olaf@31 243
olaf@31 244 if(e == NULL) {
olaf@31 245 e = i->map->map[0];
olaf@31 246 } else {
olaf@31 247 e = e->next;
olaf@31 248 }
olaf@31 249
olaf@31 250 while(i->index < i->map->size) {
olaf@31 251 if(e != NULL) {
olaf@31 252 if(e->data != NULL) {
olaf@31 253 i->cur = e;
olaf@31 254 *elm = e->data;
olaf@31 255 return 0;
olaf@31 256 }
olaf@31 257
olaf@31 258 e = e->next;
olaf@31 259 } else {
olaf@31 260 i->index++;
olaf@31 261
olaf@31 262 if(i->index < i->map->size) {
olaf@31 263 e = i->map->map[i->index];
olaf@31 264 }
olaf@31 265 }
olaf@31 266 }
olaf@31 267
olaf@31 268 return 1;
olaf@31 269 }
universe@42 270
universe@48 271 int ucx_map_load_enc(UcxMap *map, FILE *f, UcxAllocator allocator,
universe@48 272 ucx_map_coder decoder, void* decdata) {
universe@42 273
universe@43 274 int c; int r, n;
universe@42 275
universe@42 276 char *key, *value;
universe@42 277
universe@43 278 while ((c = fgetc(f)) > 0) {
universe@42 279 /* Discard leading spaces and comments */
universe@43 280 if (c < 33) continue;
universe@42 281 if (c == '#' || c == '!') {
universe@42 282 while ((c = (char) fgetc(f)) > 0) {
universe@42 283 if (c == '\n') break;
universe@42 284 }
universe@42 285 continue;
universe@42 286 }
universe@42 287
universe@42 288 /* read into key buffer */
universe@42 289 n = 16;
universe@69 290 key = (char*) malloc(n);
universe@42 291 r = 0;
universe@42 292 do {
universe@42 293 if (c == '=') break;
universe@42 294 if (r > n - 2) {
universe@42 295 n *= 2;
universe@69 296 key = (char*) realloc(key, n);
universe@42 297 }
universe@42 298 key[r] = c;
universe@42 299 r++;
universe@43 300 } while ((c = fgetc(f)) > 0);
universe@43 301 if (c <= 0) {
universe@42 302 free(key);
universe@42 303 return 1;
universe@42 304 }
universe@42 305 key[r] = 0;
universe@43 306 while (key[--r] == ' ') key[r] = 0;
universe@43 307
universe@43 308 /* skip whitespaces */
universe@43 309 while ((c = fgetc(f)) > 0) {
universe@43 310 if (c > 32) break;
universe@43 311 }
universe@43 312 if (c <= 0) {
universe@43 313 free(key);
universe@43 314 return 1;
universe@43 315 }
universe@42 316
universe@42 317 /* read into value buffer */
universe@42 318 n = 64;
universe@69 319 value = (char*) malloc(n);
universe@42 320 r = 0;
universe@43 321 do {
universe@42 322 if (c == '\n') break;
universe@43 323 if (r > n - 2) {
universe@42 324 n *= 2;
universe@69 325 value = (char*) realloc(value, n);
universe@42 326 }
universe@42 327 value[r] = c;
universe@42 328 r++;
universe@43 329 } while ((c = fgetc(f)) > 0);
universe@42 330 value[r] = 0;
universe@43 331 while (value[--r] < 33) value[r] = 0;
universe@46 332
universe@48 333 if (decoder) {
universe@48 334 size_t decodedSize;
universe@48 335 void *decoded = decoder(value, decdata, &decodedSize);
universe@46 336 free(value);
universe@69 337 value = (char*) decoded;
universe@48 338 r = decodedSize;
universe@48 339 } else {
universe@48 340 r += 2;
universe@69 341 value = (char*) realloc(value, r);
universe@48 342 }
universe@48 343
universe@48 344 if (allocator.pool) {
universe@48 345 void *pooledValue = allocator.malloc(allocator.pool, r);
universe@48 346 memcpy(pooledValue, value, r);
universe@48 347 free(value);
universe@69 348 value = (char*) pooledValue;
universe@46 349 }
universe@42 350
universe@42 351 ucx_map_cstr_put(map, key, value);
universe@42 352 free(key);
universe@42 353 }
universe@42 354
universe@42 355 return 0;
universe@42 356 }
universe@42 357
universe@48 358 int ucx_map_store_enc(UcxMap *map, FILE *f,
universe@48 359 ucx_map_coder encoder, void *encdata) {
universe@42 360 UcxMapIterator iter = ucx_map_iterator(map);
universe@42 361 char *k, *v;
universe@42 362 sstr_t key, value;
universe@42 363 int written;
universe@42 364
universe@42 365 UCX_MAP_FOREACH(v, iter) {
universe@42 366 k = (char*) iter.cur->key.data;
universe@46 367 key = sstr(k);
universe@48 368 if (encoder) {
universe@48 369 size_t encodedSize;
universe@48 370 void *encoded = encoder(v, encdata, &encodedSize);
universe@69 371 value = sstrn((char*) encoded,encodedSize - 1);
universe@48 372 } else {
universe@46 373 value = sstr(v);
universe@46 374 }
universe@42 375
universe@42 376 written = 0;
universe@42 377 written += fwrite(key.ptr, 1, key.length, f);
universe@42 378 written += fwrite(" = ", 1, 3, f);
universe@42 379 written += fwrite(value.ptr, 1, value.length, f);
universe@42 380 written += fwrite("\n", 1, 1, f);
universe@42 381
universe@48 382 if (encoder) {
universe@46 383 free(value.ptr);
universe@46 384 }
universe@46 385
universe@42 386 if (written != key.length + value.length + 4) return 1;
universe@42 387 }
universe@42 388
universe@42 389 return 0;
universe@42 390 }

mercurial