src/hash_map.c

changeset 1635
4983b6a34996
parent 1632
f74e4fc496a2
equal deleted inserted replaced
1634:006e076a8db7 1635:4983b6a34996
398 cx_hash_map_get, 398 cx_hash_map_get,
399 cx_hash_map_remove, 399 cx_hash_map_remove,
400 cx_hash_map_iterator, 400 cx_hash_map_iterator,
401 }; 401 };
402 402
403 static int cx_map_cmpfunc2_safe_memcmp(const void *a, const void *b, void *c) {
404 // it is not safe to store a pointer to the size in the list
405 // because the entire list structure might get reallocated
406 size_t elem_size = (size_t)(uintptr_t)c;
407 return memcmp(a, b, elem_size);
408 }
409
403 CxMap *cxHashMapCreate( 410 CxMap *cxHashMapCreate(
404 const CxAllocator *allocator, 411 const CxAllocator *allocator,
405 size_t itemsize, 412 size_t itemsize,
406 size_t buckets 413 size_t buckets
407 ) { 414 ) {
430 map->base.cl = &cx_hash_map_class; 437 map->base.cl = &cx_hash_map_class;
431 map->base.collection.allocator = allocator; 438 map->base.collection.allocator = allocator;
432 439
433 if (itemsize > 0) { 440 if (itemsize > 0) {
434 map->base.collection.elem_size = itemsize; 441 map->base.collection.elem_size = itemsize;
435 map->base.collection.advanced_cmp = cx_ccmp_memcmp; 442 map->base.collection.advanced_cmp = cx_map_cmpfunc2_safe_memcmp;
436 map->base.collection.cmp_data = &map->base.collection.elem_size; 443 map->base.collection.cmp_data = (void*)(uintptr_t)itemsize;
437 } else { 444 } else {
438 map->base.collection.elem_size = sizeof(void *); 445 map->base.collection.elem_size = sizeof(void *);
439 map->base.collection.store_pointer = true; 446 map->base.collection.store_pointer = true;
440 map->base.collection.simple_cmp = cx_cmp_ptr; 447 map->base.collection.simple_cmp = cx_cmp_ptr;
441 } 448 }

mercurial