tweak STBDS_REALLOC/STBDS_FREE change
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Sean Barrett · committed by Sean Barrett · parent 258c6e1 · Browse files · 1 file·+21 added−26 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+21 added−26 removed
- other:+21 added−26 removed
Provenance
- Commit
8f0bade- Tree
c234fe7- Author
Sean Barrett <sean@nothings.org>- Committer
Sean Barrett <sean@nothings.org>- Parent
258c6e1
1 file changed+21 added−26 removed
modified:stb_ds.h+21 added-26 removed
39 unmodified lines40 stb_ds.h to use specification-compliant SipHash-2-4 for all keys. Doing so makes41 hash table insertion about 20% slower on 4- and 8-byte keys, 5% slower on42 64-byte keys, and 10% slower on 256-byte keys on my test computer.43 44- #define STBDS_REALLOC better_realloc45- #define STBDS_FREE better_free44+ #define STBDS_REALLOC(context,ptr,size) better_realloc45+ #define STBDS_FREE(context,ptr) better_free46 47 These defines only need to be set in the file containing #define STB_DS_IMPLEMENTATION.48 49 By default stb_ds uses stdlib realloc() and free() for memory management. You can50- substitute your own functions (with the same signatures) instead by defining these51- symbols. You must either define both, or neither.50+ substitute your own functions instead by defining these symbols. You must either51+ define both, or neither. Note that at the moment, 'context' will always be NULL.52+ @TODO add an array/hash initialization function that takes a memory context pointer.53 54 LICENSE55 56 Placed in the public domain and also MIT licensed.331 unmodified lines388 #error "You must define both STBDS_REALLOC and STBDS_FREE, or neither."389 #endif390 #if !defined(STBDS_REALLOC) && !defined(STBDS_FREE)391 #include <stdlib.h>391-#define STBDS_REALLOC realloc392-#define STBDS_FREE free392+#define STBDS_REALLOC(c,p,s) realloc(p,s)393+#define STBDS_FREE(c,p) free(p)394 #endif395 396 #ifdef __cplusplus397 extern "C" {25 unmodified lines423 extern void * stbds_hmput_default(void *a, size_t elemsize);424 extern void * stbds_hmput_key(void *a, size_t elemsize, void *key, size_t keysize, int mode);425 extern void * stbds_hmdel_key(void *a, size_t elemsize, void *key, size_t keysize, size_t keyoffset, int mode);426 extern void * stbds_shmode_func(size_t elemsize, int mode);426-extern void stbds_free(void *p);427 428 #ifdef __cplusplus429 }430 #endif36 unmodified lines467 #define stbds_arrpush stbds_arrput // synonym468 #define stbds_arrpop(a) (stbds_header(a)->length--, (a)[stbds_header(a)->length])469 #define stbds_arraddn(a,n) (stbds_arrmaybegrow(a,n), stbds_header(a)->length += (n))470 #define stbds_arrlast(a) ((a)[stbds_header(a)->length-1])471-#define stbds_arrfree(a) ((void) ((a) ? stbds_free(stbds_header(a)) : (void)0), (a)=NULL)471+#define stbds_arrfree(a) ((void) ((a) ? STBDS_FREE(NULL,stbds_header(a)) : (void)0), (a)=NULL)472 #define stbds_arrdel(a,i) stbds_arrdeln(a,i,1)473 #define stbds_arrdeln(a,i,n) (memmove(&(a)[i], &(a)[(i)+(n)], sizeof *(a) * (stbds_header(a)->length-(n)-(i))), stbds_header(a)->length -= (n))474 #define stbds_arrdelswap(a,i) ((a)[i] = stbds_arrlast(a), stbds_header(a)->length -= 1)475 #define stbds_arrinsn(a,i,n) (stbds_arraddn((a),(n)), memmove(&(a)[(i)+(n)], &(a)[i], sizeof *(a) * (stbds_header(a)->length-(n)-(i))))165 unmodified lines641 #else642 #define STBDS_STATS(x)643 #endif644 645-void stbds_free(void *p)646-{647- STBDS_FREE(p);648-}649-645 //646 // stbds_arr implementation647 //648 14 unmodified lines663 min_cap = 2 * stbds_arrcap(a);664 else if (min_cap < 4)665 min_cap = 4;666 672- b = STBDS_REALLOC((a) ? stbds_header(a) : 0, elemsize * min_cap + sizeof(stbds_array_header));667+ b = STBDS_REALLOC(NULL, (a) ? stbds_header(a) : 0, elemsize * min_cap + sizeof(stbds_array_header));668 b = (char *) b + sizeof(stbds_array_header);669 if (a == NULL) {670 stbds_header(b)->length = 0;671 stbds_header(b)->hash_table = 0;83 unmodified lines755 756 static stbds_hash_index *stbds_make_hash_index(size_t slot_count, stbds_hash_index *ot)757 {758 stbds_hash_index *t;764- t = (stbds_hash_index *) STBDS_REALLOC(0,(slot_count >> STBDS_BUCKET_SHIFT) * sizeof(stbds_hash_bucket) + sizeof(stbds_hash_index) + STBDS_CACHE_LINE_SIZE-1);759+ t = (stbds_hash_index *) STBDS_REALLOC(NULL,0,(slot_count >> STBDS_BUCKET_SHIFT) * sizeof(stbds_hash_bucket) + sizeof(stbds_hash_index) + STBDS_CACHE_LINE_SIZE-1);760 t->storage = (stbds_hash_bucket *) STBDS_ALIGN_FWD((size_t) (t+1), STBDS_CACHE_LINE_SIZE);761 t->slot_count = slot_count;762 t->slot_count_log2 = stbds_log2(slot_count);763 t->tombstone_count = 0;318 unmodified lines1082 if (stbds_hash_table(a)->string.mode == STBDS_SH_STRDUP) {1083 size_t i;1084 // skip 0th element, which is default1085 for (i=1; i < stbds_header(a)->length; ++i)1091- STBDS_FREE(*(char**) ((char *) a + elemsize*i));1086+ STBDS_FREE(NULL, *(char**) ((char *) a + elemsize*i));1087 }1088 stbds_strreset(&stbds_hash_table(a)->string);1089 }1095- STBDS_FREE(stbds_header(a)->hash_table);1096- STBDS_FREE(stbds_header(a));1090+ STBDS_FREE(NULL, stbds_header(a)->hash_table);1091+ STBDS_FREE(NULL, stbds_header(a));1092 }1093 1094 static ptrdiff_t stbds_hm_find_slot(void *a, size_t elemsize, void *key, size_t keysize, int mode)1095 {117 unmodified lines1213 1214 slot_count = (table == NULL) ? STBDS_BUCKET_LENGTH : table->slot_count*2;1215 nt = stbds_make_hash_index(slot_count, table);1216 if (table) {1222- STBDS_FREE(table);1217+ STBDS_FREE(NULL, table);1218 }1219 stbds_header(a)->hash_table = table = nt;1220 STBDS_STATS(++stbds_hash_grow);1221 }126 unmodified lines1348 b->hash[i] = STBDS_HASH_DELETED;1349 b->index[i] = STBDS_INDEX_DELETED;1350 1351 if (mode == STBDS_HM_STRING && table->string.mode == STBDS_SH_STRDUP)1357- STBDS_FREE(*(char**) ((char *) a+elemsize*old_index));1352+ STBDS_FREE(NULL, *(char**) ((char *) a+elemsize*old_index));1353 1354 // if indices are the same, memcpy is a no-op, but back-pointer-fixup will fail, so skip1355 if (old_index != final_index) {1356 // swap delete 13 unmodified lines1370 stbds_header(raw_a)->length -= 1;1371 1372 if (table->used_count < table->used_count_shrink_threshold && table->slot_count > STBDS_BUCKET_LENGTH) {1373 stbds_header(raw_a)->hash_table = stbds_make_hash_index(table->slot_count>>1, table);1379- STBDS_FREE(table);1374+ STBDS_FREE(NULL, table);1375 STBDS_STATS(++stbds_hash_shrink);1376 } else if (table->tombstone_count > table->tombstone_count_threshold) {1377 stbds_header(raw_a)->hash_table = stbds_make_hash_index(table->slot_count , table);1383- STBDS_FREE(table);1378+ STBDS_FREE(NULL, table);1379 STBDS_STATS(++stbds_hash_rebuild);1380 }1381 1382 return a;8 unmodified lines1391 {1392 // to keep replaceable allocator simple, we don't want to use strdup.1393 // rolling our own also avoids problem of strdup vs _strdup1394 size_t len = strlen(str)+1;1400- char *p = (char*) STBDS_REALLOC(0, len);1395+ char *p = (char*) STBDS_REALLOC(NULL, 0, len);1396 memmove(p, str, len);1397 return p;1398 }1399 24 unmodified lines1424 // if string is larger than blocksize, then just allocate the full size.1425 // note that we still advance string_block so block size will continue1426 // increasing, so e.g. if somebody only calls this with 1000-long strings,1427 // eventually the arena will start doubling and handling those as well1433- stbds_string_block *sb = (stbds_string_block *) STBDS_REALLOC(0, sizeof(*sb)-8 + len);1428+ stbds_string_block *sb = (stbds_string_block *) STBDS_REALLOC(NULL, 0, sizeof(*sb)-8 + len);1429 memmove(sb->storage, str, len);1430 if (a->storage) {1431 // insert it after the first element, so that we don't waste the space there1432 sb->next = a->storage->next;4 unmodified lines1437 a->remaining = 0; // this is redundant, but good for clarity1438 }1439 return sb->storage;1440 } else {1446- stbds_string_block *sb = (stbds_string_block *) STBDS_REALLOC(0, sizeof(*sb)-8 + blocksize);1441+ stbds_string_block *sb = (stbds_string_block *) STBDS_REALLOC(NULL, 0, sizeof(*sb)-8 + blocksize);1442 sb->next = a->storage;1443 a->storage = sb;1444 a->remaining = blocksize;1445 }11 unmodified lines1457 stbds_string_block *x,*y;1458 x = a->storage;1459 while (x) {1460 y = x->next;1466- STBDS_FREE(x);1461+ STBDS_FREE(NULL, x);1462 x = y;1463 }1464 memset(a, 0, sizeof(*a));1465 }222 unmodified lines