shithub: sl

Download patch

ref: 165915b065fae1eab1a3eb21d65f782eecd7206b
parent: 7ff18f7668116d3b7d6e4346ec601f83529a77d1
author: Sigrid Solveig Haflínudóttir <sigrid@ftrv.se>
date: Wed Feb 5 12:27:20 EST 2025

start with a buffer of 512 finalizers

Add the current number of those printed in vm-stats.

--- a/src/cvalues.c
+++ b/src/cvalues.c
@@ -11,12 +11,9 @@
 add_finalizer(cvalue_t *cv)
 {
 	if(FL(nfinalizers) == FL(maxfinalizers)){
-		size_t nn = FL(maxfinalizers) == 0 ? 256 : FL(maxfinalizers)*2;
-		cvalue_t **temp = MEM_REALLOC(FL(finalizers), nn*sizeof(cvalue_t*));
-		if(temp == nil)
-			lerrorf(FL_MemoryError, "out of memory");
-		FL(finalizers) = temp;
-		FL(maxfinalizers) = nn;
+		FL(maxfinalizers) *= 2;
+		FL(finalizers) = MEM_REALLOC(FL(finalizers), FL(maxfinalizers)*sizeof(FL(finalizers)));
+		assert(FL(finalizers) != nil);
 	}
 	FL(finalizers)[FL(nfinalizers)++] = cv;
 }
@@ -71,19 +68,13 @@
 	return CVALUE_NWORDS;
 }
 
-static void
-autorelease(cvalue_t *cv)
+void
+cv_autorelease(cvalue_t *cv)
 {
 	cv->type = (fltype_t*)(((uintptr_t)cv->type) | CV_OWNED_BIT);
 	add_finalizer(cv);
 }
 
-void
-cv_autorelease(cvalue_t *cv)
-{
-	autorelease(cv);
-}
-
 static value_t
 cprim(fltype_t *type, size_t sz)
 {
@@ -97,19 +88,18 @@
 value_t
 cvalue_(fltype_t *type, size_t sz, bool nofinalize)
 {
-	cvalue_t *pcv;
-	int str = 0;
-
 	assert(type != nil);
 	if(valid_numtype(type->numtype) && type->numtype != T_MPINT)
 		return cprim(type, sz);
 
+	bool str = false;
 	if(type->eltype == FL(bytetype)){
 		if(sz == 0)
 			return FL(the_empty_string);
 		sz++;
-		str = 1;
+		str = true;
 	}
+	cvalue_t *pcv;
 	if(sz <= MAX_INL_SIZE){
 		size_t nw = CVALUE_NWORDS - 1 + NWORDS(sz) + (sz == 0 ? 1 : 0);
 		pcv = alloc_words(nw);
@@ -123,7 +113,7 @@
 		pcv = alloc_words(CVALUE_NWORDS);
 		pcv->type = type;
 		pcv->data = MEM_ALLOC(sz);
-		autorelease(pcv);
+		cv_autorelease(pcv);
 		FL(malloc_pressure) += sz;
 	}
 	if(str)
@@ -208,7 +198,7 @@
 	void *data = MEM_ALLOC(sz);
 	memcpy(data, cv_data(cv), sz);
 	cv->data = data;
-	autorelease(cv);
+	cv_autorelease(cv);
 }
 
 #define num_init(ctype, cnvt, tag) \
@@ -642,7 +632,7 @@
 			len++;
 		ncv->data = MEM_ALLOC(len);
 		memcpy(ncv->data, cv_data(cv), len);
-		autorelease(ncv);
+		cv_autorelease(ncv);
 		if(hasparent(cv)){
 			ncv->type = (fltype_t*)(((uintptr_t)ncv->type) & ~CV_PARENT_BIT);
 			ncv->parent = FL_nil;
--- a/src/flisp.c
+++ b/src/flisp.c
@@ -1261,6 +1261,7 @@
 	ios_printf(ios_stderr, "heap used      %10"PRIuPTR"\n", (uintptr_t)(FL(curheap)-FL(fromspace)));
 	ios_printf(ios_stderr, "stack          %10"PRIu64"\n", (uint64_t)FL(nstack)*sizeof(value_t));
 	ios_printf(ios_stderr, "gc calls       %10"PRIu64"\n", (uint64_t)FL(gccalls));
+	ios_printf(ios_stderr, "finalizers     %10"PRIu32"\n", (uint32_t)FL(nfinalizers));
 	ios_printf(ios_stderr, "max finalizers %10"PRIu32"\n", (uint32_t)FL(maxfinalizers));
 	ios_printf(ios_stderr, "opcodes        %10d\n", N_OPCODES);
 	return FL_void;
@@ -1291,6 +1292,7 @@
 		MEM_FREE(FL(tospace));
 		MEM_FREE(FL(consflags));
 		MEM_FREE(FL(stack));
+		MEM_FREE(FL(finalizers));
 		htable_free(&FL(printconses));
 		MEM_FREE(fl);
 		return -1;
@@ -1305,6 +1307,9 @@
 	FL(lim) = FL(curheap)+FL(heapsize)-sizeof(cons_t);
 	FL(nstack) = 4096;
 	if((FL(stack) = MEM_ALLOC(FL(nstack)*sizeof(value_t))) == nil)
+		goto failed;
+	FL(maxfinalizers) = 512;
+	if((FL(finalizers) = MEM_ALLOC(FL(maxfinalizers) * sizeof(*FL(finalizers)))) == nil)
 		goto failed;
 	comparehash_init();