git: 9front

Download patch

ref: 13c0412184b045b45181d41ef8eb7ff606294591
parent: ed3c7d4918d967d5d9ed8b6d1d5099d2416ee14e
author: cinap_lenrek <cinap_lenrek@felloff.net>
date: Sat Dec 27 18:28:44 EST 2025

cc: reuse memory for Typetab by using allocn()

Using malloc() and free() wont free any memory
with the hunk allocator. Just make the Typetab
static (in signature()) to reuse the memory.

--- a/sys/src/cmd/cc/dcl.c
+++ b/sys/src/cmd/cc/dcl.c
@@ -1071,9 +1071,8 @@
 }
 
 typedef struct Typetab Typetab;
-
 struct Typetab{
-	int n;
+	int m, n;
 	Type **a;
 };
 
@@ -1080,23 +1079,19 @@
 static int
 sigind(Type *t, Typetab *tt)
 {
-	int n;
-	Type **a, **na, **p, **e;
+	Type **p, **e;
 
-	n = tt->n;
-	a = tt->a;
-	e = a+n;
 	/* linear search seems ok */
-	for(p = a ; p < e; p++)
+	e = tt->a+tt->n;
+	for(p = tt->a; p < e; p++){
 		if(sametype(*p, t))
-			return p-a;
-	if((n&15) == 0){
-		na = malloc((n+16)*sizeof(Type*));
-		memmove(na, a, n*sizeof(Type*));
-		free(a);
-		a = tt->a = na;
+			return p - tt->a;
 	}
-	a[tt->n++] = t;
+	if(tt->n == tt->m){
+		tt->a = allocn(tt->a, tt->n*sizeof(Type*), 16*sizeof(Type*));
+		tt->m += 16;
+	}
+	tt->a[tt->n++] = t;
 	return -1;
 }
 
@@ -1141,14 +1136,10 @@
 ulong
 signature(Type *t)
 {
-	ulong s;
-	Typetab tt;
+	static Typetab tt;
 
-	tt.n = 0;
-	tt.a = nil;
-	s = signat(t, &tt);
-	free(tt.a);
-	return s;
+	tt.n = 0;	/* reset table */
+	return signat(t, &tt);
 }
 
 ulong
--