ref: 4813458af595305b5018c8f79b8f618e5c97189f
parent: f8a9a3fbf021dbc5f2aae0256bc1ab468b747438
author: qwx <qwx@sciops.net>
date: Thu Dec 16 22:08:54 EST 2021
add estrdup and have erealloc zero out new memory
--- a/asif.h
+++ b/asif.h
@@ -44,8 +44,9 @@
void four1(double*, int, int);
void realft(double*, int, int);
-void* erealloc(void*, ulong);
+char* estrdup(char*);
+void* erealloc(void*, ulong, ulong);
void* emalloc(ulong);
-#define MIN(a,b) ((a) < (b) ? (a) : (b))
-#define MAX(a,b) ((a) > (b) ? (a) : (b))
+#define MIN(a,b) ((a) <= (b) ? (a) : (b))
+#define MAX(a,b) ((a) >= (b) ? (a) : (b))
--- a/emalloc.c
+++ b/emalloc.c
@@ -2,12 +2,23 @@
#include <libc.h>
#include "asif.h"
+char *
+estrdup(char *s)
+{
+ if((s = strdup(s)) == nil)
+ sysfatal("estrdup: %r");
+ setmalloctag(s, getcallerpc(&s));
+ return s;
+}
+
void *
-erealloc(void *p, ulong n)
+erealloc(void *p, ulong n, ulong oldn)
{
if((p = realloc(p, n)) == nil)
sysfatal("realloc: %r");
setrealloctag(p, getcallerpc(&p));
+ if(n > oldn)
+ memset((uchar *)p + oldn, 0, n - oldn);
return p;
}
--- a/varray.c
+++ b/varray.c
@@ -9,6 +9,8 @@
void
vfree(VArray *v)
{
+ if(v == nil)
+ return;
free(v->p);
free(v);
}
@@ -18,10 +20,11 @@
{
int off;
+ assert(v != nil && u != nil);
off = v->n * v->elsize;
if(v->n++ >= v->bufsize){
+ v->p = erealloc(v->p, v->bufsize * 2, v->bufsize);
v->bufsize *= 2;
- v->p = erealloc(v->p, v->bufsize);
v->vsize *= 2;
}
memcpy((char*)v->p+off, u, v->elsize);