shithub: femtolisp

Download patch

ref: 1fa5ecb395cf9771300fae5da9e38b69e3aa48a9
parent: ba512ae01309f1ab20f138564df42f4b17cd5e23
author: Sigrid Solveig Haflínudóttir <sigrid@ftrv.se>
date: Tue Dec 10 21:14:26 EST 2024

for-each and hash tables: fix after relocation, reduce size limit to uint32, add an assert

--- a/flisp.c
+++ b/flisp.c
@@ -2128,8 +2128,7 @@
 			}
 			if(ishashtable(v)){
 				htable_t *h = totable(v);
-				if(n == 0)
-					h->i = 0;
+				assert(n != 0 || h->i == 0);
 				void **table = h->table;
 				for(; h->i < h->size; h->i += 2){
 					if(table[h->i+1] != HT_NOTFOUND)
@@ -2142,6 +2141,7 @@
 					h->i += 2;
 					continue;
 				}
+				h->i = 0;
 			}
 			POPN(pargs+1);
 			return FL(t);
--- a/htable.c
+++ b/htable.c
@@ -24,6 +24,7 @@
 	size_t i;
 	for(i = 0; i < size; i++)
 		h->table[i] = HT_NOTFOUND;
+	h->i = 0;
 	return h;
 }
 
--- a/htable.h
+++ b/htable.h
@@ -3,13 +3,12 @@
 #define HT_N_INLINE 32
 
 typedef struct {
-	size_t size;
-	void **table;
-	void *_space[HT_N_INLINE];
-
+	uint32_t size;
 	// this is to skip over non-items in for-each
 	// FIXME(sigrid): in a multithreaded environment this isn't enough
-	size_t i;
+	uint32_t i;
+	void **table;
+	void *_space[HT_N_INLINE];
 }htable_t;
 
 // define this to be an invalid key/value
--- a/table.c
+++ b/table.c
@@ -53,6 +53,7 @@
 	if(oldh->table == &oldh->_space[0])
 		h->table = &h->_space[0];
 	size_t i;
+	h->i = oldh->i;
 	for(i = 0; i < h->size; i++){
 		if(h->table[i] != HT_NOTFOUND)
 			h->table[i] = (void*)relocate((value_t)h->table[i]);