shithub: asif

Download patch

ref: 3c2760234f8ea7293e8cc678f238fd068d18cf44
parent: 672e6e2a22e2b3c94aabd4f591fcc20a7634b93f
author: qwx <qwx@sciops.net>
date: Sun Jul 17 12:52:39 EDT 2022

varray: maybe avoid exponential memory usage eh, how's that sound

--- a/varray.c
+++ b/varray.c
@@ -3,7 +3,7 @@
 #include "asif.h"
 
 enum{
-	VAdefsize = 1024,
+	VAinc = 2048,
 };
 
 void
@@ -32,9 +32,9 @@
 	assert(v != nil && u != nil);
 	off = v->n * v->elsize;
 	if(v->n++ >= v->vsize){
-		v->p = erealloc(v->p, v->bufsize * 2, v->bufsize);
-		v->bufsize *= 2;
-		v->vsize *= 2;
+		v->p = erealloc(v->p, v->bufsize + VAinc * v->elsize, v->bufsize);
+		v->bufsize += VAinc * v->elsize;
+		v->vsize += VAinc;
 	}
 	memcpy((char*)v->p+off, u, v->elsize);
 }
@@ -48,8 +48,8 @@
 	v->n = 0;
 	v->elsize = elsize;
 	if(n <= 0)
-		n = VAdefsize;
-	v->vsize = MIN(n, VAdefsize);
+		n = VAinc;
+	v->vsize = MIN(n, VAinc);
 	v->bufsize = v->vsize * elsize;
 	v->p = emalloc(v->bufsize);
 	return v;