stb_ds: fix shputs() for strdup and arena hash table
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Srđan Stipić · committed by Srđan Stipić · parent f54acd4 · Browse files · 1 file·+37 added−5 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+37 added−5 removed
- other:+37 added−5 removed
Provenance
- Commit
1e400e2- Tree
ef99002- Author
Srđan Stipić <srdjan.stipic@gmail.com>- Committer
Srđan Stipić <srdjan.stipic@gmail.com>- Parent
f54acd4
1 file changed+37 added−5 removed
modified:stb_ds.h+37 added-5 removed
510 unmodified lines511 #define STBDS_OFFSETOF(var,field) ((char *) &(var)->field - (char *) (var))512 513 #define stbds_header(t) ((stbds_array_header *) (t) - 1)514 #define stbds_temp(t) stbds_header(t)->temp515+#define stbds_temp_key(t) stbds_header(t)->temp_key516 517 #define stbds_arrsetcap(a,n) (stbds_arrgrow(a,0,n))518 #define stbds_arrsetlen(a,n) ((stbds_arrcap(a) < (size_t) (n) ? stbds_arrsetcap((a),(size_t)(n)),0 : 0), (a) ? stbds_header(a)->length = (size_t) (n) : 0)519 #define stbds_arrcap(a) ((a) ? stbds_header(a)->capacity : 0)67 unmodified lines587 (t)[stbds_temp((t)-1)].value = (v), stbds_temp((t)-1))588 589 #define stbds_shputs(t, s) \590 ((t) = stbds_hmput_key_wrapper((t), sizeof *(t), (void*) (s).key, sizeof (s).key, STBDS_HM_STRING), \590- (t)[stbds_temp((t)-1)] = (s))591+ (t)[stbds_temp((t)-1)] = (s), \592+ (t)[stbds_temp((t)-1)].key = stbds_temp_key((t)-1))593 594 #define stbds_pshput(t, p) \595 ((t) = stbds_hmput_key_wrapper((t), sizeof *(t), (void*) (p)->key, sizeof (p)->key, STBDS_HM_PTR_TO_STRING), \596 (t)[stbds_temp((t)-1)] = (p))38 unmodified lines635 size_t length;636 size_t capacity;637 void * hash_table;638 ptrdiff_t temp;639+ char * temp_key;640 } stbds_array_header;641 642 typedef struct stbds_string_block643 {768 unmodified lines1412 bucket->index[pos & STBDS_BUCKET_MASK] = i-1;1413 stbds_temp(a) = i-1;1414 1415 switch (table->string.mode) {1413- case STBDS_SH_STRDUP: *(char **) ((char *) a + elemsize*i) = stbds_strdup((char*) key); break;1414- case STBDS_SH_ARENA: *(char **) ((char *) a + elemsize*i) = stbds_stralloc(&table->string, (char*)key); break;1415- case STBDS_SH_DEFAULT: *(char **) ((char *) a + elemsize*i) = (char *) key; break;1416+ case STBDS_SH_STRDUP: stbds_temp_key(a) = *(char **) ((char *) a + elemsize*i) = stbds_strdup((char*) key); break;1417+ case STBDS_SH_ARENA: stbds_temp_key(a) = *(char **) ((char *) a + elemsize*i) = stbds_stralloc(&table->string, (char*)key); break;1418+ case STBDS_SH_DEFAULT: stbds_temp_key(a) = *(char **) ((char *) a + elemsize*i) = (char *) key; break;1419 default: memcpy((char *) a + elemsize*i, key, keysize); break;1420 }1421 }1422 return STBDS_ARR_TO_HASH(a,elemsize);196 unmodified lines1619 const int testsize = 100000;1620 const int testsize2 = testsize/20;1621 int *arr=NULL;1622 struct { int key; int value; } *intmap = NULL;1620- struct { char *key; int value; } *strmap = NULL;1623+ struct { char *key; int value; } *strmap = NULL, s;1624 struct { stbds_struct key; int value; } *map = NULL;1625 stbds_struct *map2 = NULL;1626 stbds_struct2 *map3 = NULL;1627 stbds_string_arena sa = { 0 };74 unmodified lines1702 for (i=0; i < testsize; ++i)1703 stralloc(&sa, strkey(i));1704 strreset(&sa);1705 1706+ {1707+ s.key = "a", s.value = 1;1708+ shputs(strmap, s);1709+ STBDS_ASSERT(*strmap[0].key == 'a');1710+ STBDS_ASSERT(strmap[0].key == s.key);1711+ STBDS_ASSERT(strmap[0].value == s.value);1712+ shfree(strmap);1713+ }1714+1715+ {1716+ s.key = "a", s.value = 1;1717+ sh_new_strdup(strmap);1718+ shputs(strmap, s);1719+ STBDS_ASSERT(*strmap[0].key == 'a');1720+ STBDS_ASSERT(strmap[0].key != s.key);1721+ STBDS_ASSERT(strmap[0].value == s.value);1722+ shfree(strmap);1723+ }1724+1725+ {1726+ s.key = "a", s.value = 1;1727+ sh_new_arena(strmap);1728+ shputs(strmap, s);1729+ STBDS_ASSERT(*strmap[0].key == 'a');1730+ STBDS_ASSERT(strmap[0].key != s.key);1731+ STBDS_ASSERT(strmap[0].value == s.value);1732+ shfree(strmap);1733+ }1734+1735 for (j=0; j < 2; ++j) {1736 STBDS_ASSERT(shgeti(strmap,"foo") == -1);1737 if (j == 0)1738 sh_new_strdup(strmap);122 unmodified lines