shithub: sl

Download patch

ref: de1e8f7a3fe80a5c4d14b67440d1b6151354b584
parent: d33305eb8ba0cd5cc806c36881d62514d88f890c
author: Sigrid Solveig Haflínudóttir <sigrid@ftrv.se>
date: Wed Apr 9 15:02:47 EDT 2025

remove static limit on the number of gc handles

--- a/src/sl.c
+++ b/src/sl.c
@@ -375,8 +375,13 @@
 void
 sl_gc_handle(sl_v *pv)
 {
-	if(sl_unlikely(slg.ngchandles >= N_GC_HANDLES))
-		cthrow(lerrorf(sl_errmem, "out of gc handles"), pv);
+	if(sl_unlikely(slg.ngchandles == slg.maxgchandles)){
+		sl_v **gh = MEM_REALLOC(slg.gchandles, slg.maxgchandles*2*sizeof(sl_v*));
+		if(gh == nil)
+			cthrow(lerrorf(sl_errmem, "out of gc handles"), pv);
+		slg.maxgchandles *= 2;
+		slg.gchandles = gh;
+	}
 	slg.gchandles[slg.ngchandles++] = pv;
 }
 
@@ -1220,6 +1225,7 @@
 	ios_printf(io, "finalizers     %10"PRIu32"\n", (u32int)slg.nfinalizers);
 	ios_printf(io, "max finalizers %10"PRIu32"\n", (u32int)slg.maxfinalizers);
 	ios_printf(io, "gc handles     %10"PRIu32"\n", (u32int)slg.ngchandles);
+	ios_printf(io, "max gc handles %10"PRIu32"\n", (u32int)slg.maxgchandles);
 	ios_printf(io, "gc calls       %10"PRIu64"\n", (u64int)slg.gccalls);
 	ios_printf(io, "opcodes        %10d\n", N_OPCODES);
 	return sl_void;
@@ -1248,6 +1254,7 @@
 failed:
 		MEM_FREE(sl.consflags);
 		MEM_FREE(slg.finalizers);
+		MEM_FREE(slg.gchandles);
 		sl_segfree(slg.fromspace, slg.heapsize);
 		sl_segfree(slg.tospace, slg.heapsize);
 		sl_segfree(sl.stack, stacksize*sizeof(sl_v));
@@ -1268,6 +1275,9 @@
 
 	slg.maxfinalizers = 512;
 	if((slg.finalizers = MEM_ALLOC(slg.maxfinalizers * sizeof(*slg.finalizers))) == nil)
+		goto failed;
+	slg.maxgchandles = 16;
+	if((slg.gchandles = MEM_ALLOC(slg.maxgchandles * sizeof(sl_v*))) == nil)
 		goto failed;
 
 	if((sl.consflags = bitvector_new(slg.heapsize/sizeof(sl_cons), 1)) == nil)
--- a/src/sl.h
+++ b/src/sl.h
@@ -367,10 +367,6 @@
 	sl_eof = builtin(OP_DUMMY_EOF),
 };
 
-enum {
-	N_GC_HANDLES = 1024,
-};
-
 typedef struct Sl Sl;
 typedef struct Slg Slg;
 
@@ -417,7 +413,8 @@
 	bool exiting;
 	bool grew;
 	int ngchandles;
-	sl_v *gchandles[N_GC_HANDLES];
+	int maxgchandles;
+	sl_v **gchandles;
 	usize gccalls;
 	sl_htable reverse_dlsym_lookup;
 	sl_htable types;