Skip to content
Myr

stb_ds: Allow STBDS_REALLOC and STBDS_FREE defines to customize memory management.

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Andy Durdin · committed by Andy Durdin · parent 5a2a8e8 · Browse files · 1 file+26 added−9 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+26 added−9 removed

  • other:+26 added−9 removed

1 file+26 added−9 removed

modified:stb_ds.h+26 added-9 removed
39 unmodified lines
40 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.4344 #define STBDS_REALLOC better_realloc45 #define STBDS_FREE better_free4647 These defines need to be set globally.4849 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.5253LICENSE5455 Placed in the public domain and also MIT licensed.56 See end of file for detailed license information.
272 unmodified lines
329330#ifndef INCLUDE_STB_DS_H331#define INCLUDE_STB_DS_H332324#include <stdlib.h>333#include <stddef.h>334#include <string.h>335336#ifndef STBDS_NO_SHORT_NAMES
44 unmodified lines
381382#define stralloc stbds_stralloc383#define strreset stbds_strreset384#endif 385386#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#endif394395#ifdef __cplusplus396extern "C" {397#endif398
67 unmodified lines
466#define stbds_arrpush stbds_arrput // synonym467#define stbds_arrpop(a) (stbds_header(a)->length--, (a)[stbds_header(a)->length])468#define stbds_arraddn(a,n) (stbds_arrmaybegrow(a,n), stbds_header(a)->length += (n))469#define stbds_arrlast(a) ((a)[stbds_header(a)->length-1])452#define stbds_arrfree(a) ((void) ((a) ? free(stbds_header(a)) : (void)0), (a)=NULL)470#define stbds_arrfree(a) ((void) ((a) ? STBDS_FREE(stbds_header(a)) : (void)0), (a)=NULL)471#define stbds_arrdel(a,i) stbds_arrdeln(a,i,1)472#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))473#define stbds_arrdelswap(a,i) ((a)[i] = stbds_arrlast(a), stbds_header(a)->length -= 1)474#define stbds_arrinsn(a,i,n) (stbds_arraddn((a),(n)), memmove(&(a)[(i)+(n)], &(a)[i], sizeof *(a) * (stbds_header(a)->length-(n)-(i))))
187 unmodified lines
662 min_cap = 2 * stbds_arrcap(a);663 else if (min_cap < 4)664 min_cap = 4;665648 b = realloc((a) ? stbds_header(a) : 0, elemsize * min_cap + sizeof(stbds_array_header));666 b = STBDS_REALLOC((a) ? stbds_header(a) : 0, elemsize * min_cap + sizeof(stbds_array_header));667 b = (char *) b + sizeof(stbds_array_header);668 if (a == NULL) {669 stbds_header(b)->length = 0;670 stbds_header(b)->hash_table = 0;
83 unmodified lines
754755static stbds_hash_index *stbds_make_hash_index(size_t slot_count, stbds_hash_index *ot)756{757 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);758 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->storage = (stbds_hash_bucket *) STBDS_ALIGN_FWD((size_t) (t+1), STBDS_CACHE_LINE_SIZE);760 t->slot_count = slot_count;761 t->slot_count_log2 = stbds_log2(slot_count);762 t->tombstone_count = 0;
307 unmodified lines
1070}10711072#define STBDS_HASH_TO_ARR(x,elemsize) ((char*) (x) - (elemsize))1073#define STBDS_ARR_TO_HASH(x,elemsize) ((char*) (x) + (elemsize))1056#define STBDS_FREE(x) free(x)10741075#define stbds_hash_table(a) ((stbds_hash_index *) stbds_header(a)->hash_table)1076 1077void stbds_hmfree_func(void *a, size_t elemsize, size_t keyoff)
312 unmodified lines
1390{1391 // to keep replaceable allocator simple, we don't want to use strdup.1392 // rolling our own also avoids problem of strdup vs _strdup1393 size_t len = strlen(str)+1;1377 char *p = (char*) realloc(0, len);1394 char *p = (char*) STBDS_REALLOC(0, len);1395 memmove(p, str, len);1396 return p;1397}1398
24 unmodified lines
1423 // if string is larger than blocksize, then just allocate the full size.1424 // note that we still advance string_block so block size will continue1425 // increasing, so e.g. if somebody only calls this with 1000-long strings,1426 // eventually the arena will start doubling and handling those as well1410 stbds_string_block *sb = (stbds_string_block *) realloc(0, sizeof(*sb)-8 + len);1427 stbds_string_block *sb = (stbds_string_block *) STBDS_REALLOC(0, sizeof(*sb)-8 + len);1428 memmove(sb->storage, str, len);1429 if (a->storage) {1430 // insert it after the first element, so that we don't waste the space there1431 sb->next = a->storage->next;
4 unmodified lines
1436 a->remaining = 0; // this is redundant, but good for clarity1437 }1438 return sb->storage;1439 } else {1423 stbds_string_block *sb = (stbds_string_block *) realloc(0, sizeof(*sb)-8 + blocksize);1440 stbds_string_block *sb = (stbds_string_block *) STBDS_REALLOC(0, sizeof(*sb)-8 + blocksize);1441 sb->next = a->storage;1442 a->storage = sb;1443 a->remaining = blocksize;1444 }
11 unmodified lines
1456 stbds_string_block *x,*y;1457 x = a->storage;1458 while (x) {1459 y = x->next;1443 free(x);1460 STBDS_FREE(x);1461 x = y;1462 }1463 memset(a, 0, sizeof(*a));1464}
222 unmodified lines