Skip to content

Commit 3bb8404

Browse files
committed
htable: fix implementation of htable_reset
1 parent 38d6247 commit 3bb8404

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

src/support/htable.c

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ extern "C" {
2020

2121
htable_t *htable_new(htable_t *h, size_t size)
2222
{
23-
if (size <= HT_N_INLINE/2) {
23+
if (size <= HT_N_INLINE / 2) {
2424
h->size = size = HT_N_INLINE;
2525
h->table = &h->_space[0];
2626
}
@@ -29,11 +29,12 @@ htable_t *htable_new(htable_t *h, size_t size)
2929
size *= 2; // 2 pointers per key/value pair
3030
size *= 2; // aim for 50% occupancy
3131
h->size = size;
32-
h->table = (void**)LLT_ALLOC(size*sizeof(void*));
32+
h->table = (void**)LLT_ALLOC(size * sizeof(void*));
3333
}
34-
if (h->table == NULL) return NULL;
34+
if (h->table == NULL)
35+
return NULL;
3536
size_t i;
36-
for(i=0; i < size; i++)
37+
for (i = 0; i < size; i++)
3738
h->table[i] = HT_NOTFOUND;
3839
return h;
3940
}
@@ -48,15 +49,17 @@ void htable_free(htable_t *h)
4849
void htable_reset(htable_t *h, size_t sz)
4950
{
5051
sz = next_power_of_two(sz);
51-
if (h->size > sz*4 && h->size > HT_N_INLINE) {
52-
size_t newsz = sz*4;
53-
void **newtab = (void**)LLT_REALLOC(h->table, newsz*sizeof(void*));
54-
h->size = newsz;
55-
h->table = newtab;
52+
if (h->size > sz * 4 && h->size > HT_N_INLINE) {
53+
LLT_FREE(h->table);
54+
h->table = NULL;
55+
if (htable_new(h, sz) == NULL)
56+
htable_new(h, 0);
57+
}
58+
else {
59+
size_t i, hsz = h->size;
60+
for (i = 0; i < hsz; i++)
61+
h->table[i] = HT_NOTFOUND;
5662
}
57-
size_t i, hsz=h->size;
58-
for(i=0; i < hsz; i++)
59-
h->table[i] = HT_NOTFOUND;
6063
}
6164

6265
#ifdef __cplusplus

0 commit comments

Comments
 (0)