summary refs log tree commit diff
path: root/util.c
diff options
context:
space:
mode:
authorQuentin Carbonneaux <quentin.carbonneaux@yale.edu>2017-01-12 22:31:51 -0500
committerQuentin Carbonneaux <quentin.carbonneaux@yale.edu>2017-01-12 22:31:51 -0500
commit2b4ece6f99c18df090a127ec20c60ff05cbc0705 (patch)
treee8ef009433b47325520cd6b7f429409cccedce6b /util.c
parente38da51c14d9eaf8c53b58b3d2e33d7b37768f29 (diff)
downloadroux-2b4ece6f99c18df090a127ec20c60ff05cbc0705.tar.gz
use a less obtuse api for vnew()
Diffstat (limited to 'util.c')
-rw-r--r--util.c14
1 files changed, 8 insertions, 6 deletions
diff --git a/util.c b/util.c
index 380f139..9b73771 100644
--- a/util.c
+++ b/util.c
@@ -6,7 +6,7 @@ typedef struct Vec Vec;
 
 struct Vec {
 	ulong mag;
-	void *(*alloc)();
+	Pool pool;
 	size_t esz;
 	ulong cap;
 	union {
@@ -119,18 +119,20 @@ icpy(Ins *d, Ins *s, ulong n)
 }
 
 void *
-vnew(ulong len, size_t esz, void *alloc(size_t))
+vnew(ulong len, size_t esz, Pool pool)
 {
+	void *(*f)(size_t);
 	ulong cap;
 	Vec *v;
 
 	for (cap=VMin; cap<len; cap*=2)
 		;
-	v = alloc(cap * esz + sizeof(Vec));
+	f = pool == Pheap ? emalloc : alloc;
+	v = f(cap * esz + sizeof(Vec));
 	v->mag = VMag;
 	v->cap = cap;
 	v->esz = esz;
-	v->alloc = alloc;
+	v->pool = pool;
 	return v + 1;
 }
 
@@ -141,7 +143,7 @@ vfree(void *p)
 
 	v = (Vec *)p - 1;
 	assert(v->mag == VMag);
-	if (v->alloc == emalloc) {
+	if (v->pool == Pheap) {
 		v->mag = 0;
 		free(v);
 	}
@@ -157,7 +159,7 @@ vgrow(void *vp, ulong len)
 	assert(v+1 && v->mag == VMag);
 	if (v->cap >= len)
 		return;
-	v1 = vnew(len, v->esz, v->alloc);
+	v1 = vnew(len, v->esz, v->pool);
 	memcpy(v1, v+1, v->cap * v->esz);
 	vfree(v+1);
 	*(Vec **)vp = v1;