Skip to content

Commit 51a0b80

Browse files
vtjnashKristofferC
authored andcommitted
fix ptrhash_remove (#42009)
Same bug as 5e57c21 (#26833), same fix. (cherry picked from commit 82c4a27)
1 parent fc1fcef commit 51a0b80

File tree

2 files changed

+63
-53
lines changed

2 files changed

+63
-53
lines changed

src/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,10 +257,10 @@ $(addprefix $(BUILDDIR)/,threading.o threading.dbg.obj gc.o gc.dbg.obj init.c in
257257
$(addprefix $(BUILDDIR)/,APInt-C.o APInt-C.dbg.obj runtime_intrinsics.o runtime_intrinsics.dbg.obj): $(SRCDIR)/APInt-C.h
258258

259259
# archive library file rules
260-
$(BUILDDIR)/support/libsupport.a: $(addprefix $(SRCDIR)/support/,*.h *.c *.S) $(SRCDIR)/support/*.c
260+
$(BUILDDIR)/support/libsupport.a: $(addprefix $(SRCDIR)/support/,*.h *.c *.S *.inc) $(SRCDIR)/support/*.c
261261
$(MAKE) -C $(SRCDIR)/support BUILDDIR='$(abspath $(BUILDDIR)/support)'
262262

263-
$(BUILDDIR)/support/libsupport-debug.a: $(addprefix $(SRCDIR)/support/,*.h *.c *.S) $(SRCDIR)/support/*.c
263+
$(BUILDDIR)/support/libsupport-debug.a: $(addprefix $(SRCDIR)/support/,*.h *.c *.S *.inc) $(SRCDIR)/support/*.c
264264
$(MAKE) -C $(SRCDIR)/support debug BUILDDIR='$(abspath $(BUILDDIR)/support)'
265265

266266
$(FLISP_EXECUTABLE_release): $(BUILDDIR)/flisp/libflisp.a

src/support/htable.inc

Lines changed: 61 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -13,67 +13,77 @@
1313
static void **HTNAME##_lookup_bp_r(htable_t *h, void *key, void *ctx) \
1414
{ \
1515
uint_t hv; \
16-
size_t i, orig, index, iter; \
16+
size_t i, orig, index, iter, empty_slot; \
1717
size_t newsz, sz = hash_size(h); \
1818
size_t maxprobe = max_probe(sz); \
1919
void **tab = h->table; \
2020
void **ol; \
2121
\
2222
hv = HFUNC((uintptr_t)key, ctx); \
23-
retry_bp: \
24-
iter = 0; \
25-
index = (size_t)(hv & (sz-1)) * 2; \
26-
sz *= 2; \
27-
orig = index; \
28-
\
29-
do { \
30-
if (tab[index+1] == HT_NOTFOUND) { \
31-
tab[index] = key; \
32-
return &tab[index+1]; \
23+
while (1) { \
24+
iter = 0; \
25+
index = (size_t)(hv & (sz-1)) * 2; \
26+
sz *= 2; \
27+
orig = index; \
28+
empty_slot = -1; \
29+
\
30+
do { \
31+
if (tab[index] == HT_NOTFOUND) { \
32+
if (empty_slot == -1) \
33+
empty_slot = index; \
34+
break; \
35+
} \
36+
if (tab[index+1] == HT_NOTFOUND) { \
37+
if (empty_slot == -1) \
38+
empty_slot = index; \
39+
} \
40+
\
41+
if (EQFUNC(key, tab[index], ctx)) \
42+
return &tab[index+1]; \
43+
\
44+
index = (index+2) & (sz-1); \
45+
iter++; \
46+
if (iter > maxprobe) \
47+
break; \
48+
} while (index != orig); \
49+
\
50+
if (empty_slot != -1) { \
51+
tab[empty_slot] = key; \
52+
return &tab[empty_slot+1]; \
3353
} \
3454
\
35-
if (EQFUNC(key, tab[index], ctx)) \
36-
return &tab[index+1]; \
37-
\
38-
index = (index+2) & (sz-1); \
39-
iter++; \
40-
if (iter > maxprobe) \
41-
break; \
42-
} while (index != orig); \
43-
\
44-
/* table full */ \
45-
/* quadruple size, rehash, retry the insert */ \
46-
/* it's important to grow the table really fast; otherwise we waste */ \
47-
/* lots of time rehashing all the keys over and over. */ \
48-
sz = h->size; \
49-
ol = h->table; \
50-
if (sz < HT_N_INLINE) \
51-
newsz = HT_N_INLINE; \
52-
else if (sz >= (1<<19) || (sz <= (1<<8))) \
53-
newsz = sz<<1; \
54-
else \
55-
newsz = sz<<2; \
56-
/*printf("trying to allocate %d words.\n", newsz); fflush(stdout);*/ \
57-
tab = (void**)LLT_ALLOC(newsz*sizeof(void*)); \
58-
if (tab == NULL) \
59-
return NULL; \
60-
for(i=0; i < newsz; i++) \
61-
tab[i] = HT_NOTFOUND; \
62-
h->table = tab; \
63-
h->size = newsz; \
64-
for(i=0; i < sz; i+=2) { \
65-
if (ol[i+1] != HT_NOTFOUND) { \
66-
(*HTNAME##_lookup_bp_r(h, ol[i], ctx)) = ol[i+1]; \
55+
/* table full */ \
56+
/* quadruple size, rehash, retry the insert */ \
57+
/* it's important to grow the table really fast; otherwise we waste */ \
58+
/* lots of time rehashing all the keys over and over. */ \
59+
sz = h->size; \
60+
ol = h->table; \
61+
if (sz < HT_N_INLINE) \
62+
newsz = HT_N_INLINE; \
63+
else if (sz >= (1<<19) || (sz <= (1<<8))) \
64+
newsz = sz<<1; \
65+
else \
66+
newsz = sz<<2; \
67+
/*printf("trying to allocate %d words.\n", newsz); fflush(stdout);*/ \
68+
tab = (void**)LLT_ALLOC(newsz*sizeof(void*)); \
69+
if (tab == NULL) \
70+
return NULL; \
71+
for (i = 0; i < newsz; i++) \
72+
tab[i] = HT_NOTFOUND; \
73+
h->table = tab; \
74+
h->size = newsz; \
75+
for (i = 0; i < sz; i += 2) { \
76+
if (ol[i+1] != HT_NOTFOUND) { \
77+
(*HTNAME##_lookup_bp_r(h, ol[i], ctx)) = ol[i+1]; \
78+
} \
6779
} \
68-
} \
69-
if (ol != &h->_space[0]) \
70-
LLT_FREE(ol); \
80+
if (ol != &h->_space[0]) \
81+
LLT_FREE(ol); \
7182
\
72-
sz = hash_size(h); \
73-
maxprobe = max_probe(sz); \
74-
tab = h->table; \
75-
\
76-
goto retry_bp; \
83+
sz = hash_size(h); \
84+
maxprobe = max_probe(sz); \
85+
tab = h->table; \
86+
} \
7787
\
7888
return NULL; \
7989
} \

0 commit comments

Comments
 (0)