Merge branch 'use-free' of https://github.com/adurdin/stb into working
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 · parents eddae8c, 2cddbc9 · Browse files · 1 file·+33 added−10 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+33 added−10 removed
- other:+33 added−10 removed
Provenance
- Commit
258c6e1- Tree
3b88d8d- Author
Sean Barrett <sean@nothings.org>- Committer
Sean Barrett <sean@nothings.org>- Parent 1
eddae8c- Parent 2
2cddbc9
1 file changed+33 added−10 removed
modified:stb_ds.h+33 added-10 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_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.52+53 LICENSE54 55 Placed in the public domain and also MIT licensed.56 See end of file for detailed license information.183 unmodified lines240 sh_new_strdup241 void sh_new_strdup(T*);242 Overwrites the existing pointer with a newly allocated243 string hashmap which will automatically allocate and free235- each string key using malloc/free244+ each string key using realloc/free245 246 sh_new_arena247 void sh_new_arena(T*);248 Overwrites the existing pointer with a newly allocated80 unmodified lines329 330 #ifndef INCLUDE_STB_DS_H331 #define INCLUDE_STB_DS_H332 324-#include <stdlib.h>333 #include <stddef.h>334 #include <string.h>335 336 #ifndef STBDS_NO_SHORT_NAMES44 unmodified lines381 382 #define stralloc stbds_stralloc383 #define strreset stbds_strreset384 #endif 385+386+#if defined(STBDS_REALLOC) && !defined(STBDS_FREE) || !defined(STBDS_REALLOC) && defined(STBDS_FREE)387+#error "You must define both STBDS_REALLOC and STBDS_FREE, or neither."388+#endif389+#if !defined(STBDS_REALLOC) && !defined(STBDS_FREE)390+#include <stdlib.h>391+#define STBDS_REALLOC realloc392+#define STBDS_FREE free393+#endif394+395 #ifdef __cplusplus396 extern "C" {397 #endif398 23 unmodified lines422 extern void * stbds_hmput_default(void *a, size_t elemsize);423 extern void * stbds_hmput_key(void *a, size_t elemsize, void *key, size_t keysize, int mode);424 extern void * stbds_hmdel_key(void *a, size_t elemsize, void *key, size_t keysize, size_t keyoffset, int mode);425 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])452-#define stbds_arrfree(a) ((void) ((a) ? realloc(stbds_header(a),0) : 0), (a)=NULL)471+#define stbds_arrfree(a) ((void) ((a) ? stbds_free(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+650 //651 // stbds_arr implementation652 //653 14 unmodified lines668 min_cap = 2 * stbds_arrcap(a);669 else if (min_cap < 4)670 min_cap = 4;671 648- b = realloc((a) ? stbds_header(a) : 0, elemsize * min_cap + sizeof(stbds_array_header));672+ b = STBDS_REALLOC((a) ? stbds_header(a) : 0, elemsize * min_cap + sizeof(stbds_array_header));673 b = (char *) b + sizeof(stbds_array_header);674 if (a == NULL) {675 stbds_header(b)->length = 0;676 stbds_header(b)->hash_table = 0;83 unmodified lines760 761 static stbds_hash_index *stbds_make_hash_index(size_t slot_count, stbds_hash_index *ot)762 {763 stbds_hash_index *t;740- t = (stbds_hash_index *) realloc(0,(slot_count >> STBDS_BUCKET_SHIFT) * sizeof(stbds_hash_bucket) + sizeof(stbds_hash_index) + STBDS_CACHE_LINE_SIZE-1);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);765 t->storage = (stbds_hash_bucket *) STBDS_ALIGN_FWD((size_t) (t+1), STBDS_CACHE_LINE_SIZE);766 t->slot_count = slot_count;767 t->slot_count_log2 = stbds_log2(slot_count);768 t->tombstone_count = 0;307 unmodified lines1076 }1077 1078 #define STBDS_HASH_TO_ARR(x,elemsize) ((char*) (x) - (elemsize))1079 #define STBDS_ARR_TO_HASH(x,elemsize) ((char*) (x) + (elemsize))1056-#define STBDS_FREE(x) realloc(x,0)1080 1081 #define stbds_hash_table(a) ((stbds_hash_index *) stbds_header(a)->hash_table)1082 1083 void stbds_hmfree_func(void *a, size_t elemsize, size_t keyoff)312 unmodified lines1396 {1397 // to keep replaceable allocator simple, we don't want to use strdup.1398 // rolling our own also avoids problem of strdup vs _strdup1399 size_t len = strlen(str)+1;1377- char *p = (char*) realloc(0, len);1400+ char *p = (char*) STBDS_REALLOC(0, len);1401 memmove(p, str, len);1402 return p;1403 }1404 24 unmodified lines1429 // if string is larger than blocksize, then just allocate the full size.1430 // note that we still advance string_block so block size will continue1431 // increasing, so e.g. if somebody only calls this with 1000-long strings,1432 // eventually the arena will start doubling and handling those as well1410- stbds_string_block *sb = (stbds_string_block *) realloc(0, sizeof(*sb)-8 + len);1433+ stbds_string_block *sb = (stbds_string_block *) STBDS_REALLOC(0, sizeof(*sb)-8 + len);1434 memmove(sb->storage, str, len);1435 if (a->storage) {1436 // insert it after the first element, so that we don't waste the space there1437 sb->next = a->storage->next;4 unmodified lines1442 a->remaining = 0; // this is redundant, but good for clarity1443 }1444 return sb->storage;1445 } else {1423- stbds_string_block *sb = (stbds_string_block *) realloc(0, sizeof(*sb)-8 + blocksize);1446+ stbds_string_block *sb = (stbds_string_block *) STBDS_REALLOC(0, sizeof(*sb)-8 + blocksize);1447 sb->next = a->storage;1448 a->storage = sb;1449 a->remaining = blocksize;1450 }11 unmodified lines1462 stbds_string_block *x,*y;1463 x = a->storage;1464 while (x) {1465 y = x->next;1443- realloc(x,0);1466+ STBDS_FREE(x);1467 x = y;1468 }1469 memset(a, 0, sizeof(*a));1470 }222 unmodified lines