changeset 38:b156910b59b2

Wrap free() in dofree() to allow instrumenting it for debugging.
author David A. Holland
date Sat, 30 Mar 2013 21:02:25 -0400
parents 70902cac4170
children 337110e7240a
files array.c array.h directive.c eval.c files.c macro.c main.c place.c utils.c utils.h
diffstat 10 files changed, 38 insertions(+), 29 deletions(-) [+]
line wrap: on
line diff
--- a/array.c	Sat Mar 30 20:52:59 2013 -0400
+++ b/array.c	Sat Mar 30 21:02:25 2013 -0400
@@ -47,7 +47,7 @@
 array_destroy(struct array *a)
 {
 	array_cleanup(a);
-	free(a);
+	dofree(a);
 }
 
 void
@@ -61,7 +61,7 @@
 array_cleanup(struct array *a)
 {
 	arrayassert(a->num == 0);
-	free(a->v);
+	dofree(a->v);
 #ifdef ARRAYS_CHECKED
 	a->v = NULL;
 #endif
--- a/array.h	Sat Mar 30 20:52:59 2013 -0400
+++ b/array.h	Sat Mar 30 21:02:25 2013 -0400
@@ -179,7 +179,7 @@
 	ARRAY##_destroy(struct ARRAY *a)			\
 	{							\
 		ARRAY##_cleanup(a);				\
-		free(a);					\
+		dofree(a);					\
 	}							\
 								\
 	INLINE unsigned						\
--- a/directive.c	Sat Mar 30 20:52:59 2013 -0400
+++ b/directive.c	Sat Mar 30 21:02:25 2013 -0400
@@ -94,7 +94,7 @@
 void
 ifstate_destroy(struct ifstate *is)
 {
-	free(is);
+	dofree(is);
 }
 
 static
@@ -126,7 +126,7 @@
 	expr = macroexpand(p2, line, len, true);
 	val = eval(&p3, expr);
 	ifstate_push(p, val);
-	free(expr);
+	dofree(expr);
 }
 
 static
@@ -163,7 +163,7 @@
 		expr = macroexpand(p2, line, len, true);
 		ifstate->curtrue = eval(&p3, expr);
 		ifstate->evertrue = ifstate->curtrue;
-		free(expr);
+		dofree(expr);
 	}
 }
 
@@ -298,10 +298,10 @@
 	}
 	text = macroexpand(p2, line, len, false);
 	if (tryinclude(p, text, strlen(text))) {
-		free(text);
+		dofree(text);
 		return;
 	}
-	free(text);
+	dofree(text);
 	complain(p, "Illegal #include directive");
 	complain_fail();
 }
@@ -328,7 +328,7 @@
 	if (mode.werror) {
 		complain_fail();
 	}
-	free(msg);
+	dofree(msg);
 }
 
 static
@@ -340,7 +340,7 @@
 	msg = macroexpand(p2, line, len, false);
 	complain(p, "#error: %s", msg);
 	complain_fail();
-	free(msg);
+	dofree(msg);
 }
 
 ////////////////////////////////////////////////////////////
--- a/eval.c	Sat Mar 30 20:52:59 2013 -0400
+++ b/eval.c	Sat Mar 30 21:02:25 2013 -0400
@@ -149,7 +149,7 @@
 void
 token_destroy(struct token *t)
 {
-	free(t);
+	dofree(t);
 }
 
 DESTROYALL_ARRAY(token, );
--- a/files.c	Sat Mar 30 20:52:59 2013 -0400
+++ b/files.c	Sat Mar 30 21:02:25 2013 -0400
@@ -70,7 +70,7 @@
 void
 incdir_destroy(struct incdir *id)
 {
-	free(id);
+	dofree(id);
 }
 
 void
@@ -242,7 +242,7 @@
 	if (toplevel) {
 		directive_goteof(&linestartplace);
 	}
-	free(buf);
+	dofree(buf);
 }
 
 ////////////////////////////////////////////////////////////
@@ -308,11 +308,11 @@
 		if (fd >= 0) {
 			pf = place_addfile(place, file, id->issystem);
 			file_read(pf, fd, file, false);
-			free(file);
+			dofree(file);
 			close(fd);
 			return;
 		}
-		free(file);
+		dofree(file);
 	}
 	complain(place, "Include file %s not found", name);
 	complain_fail();
--- a/macro.c	Sat Mar 30 20:52:59 2013 -0400
+++ b/macro.c	Sat Mar 30 21:02:25 2013 -0400
@@ -110,9 +110,9 @@
 expansionitem_destroy(struct expansionitem *ei)
 {
 	if (ei->isstring) {
-		free(ei->string);
+		dofree(ei->string);
 	}
-	free(ei);
+	dofree(ei);
 }
 
 static
@@ -162,8 +162,8 @@
 {
 	expansionitemarray_destroyall(&m->expansion);
 	expansionitemarray_cleanup(&m->expansion);
-	free(m->name);
-	free(m);
+	dofree(m->name);
+	dofree(m);
 }
 
 static
@@ -634,7 +634,7 @@
 	assert(es->state == ES_NORMAL);
 	stringarray_cleanup(&es->args);
 	if (es->buf) {
-		free(es->buf);
+		dofree(es->buf);
 	}
 }
 
@@ -646,7 +646,7 @@
 
 	num = stringarray_num(&es->args);
 	for (i=0; i<num; i++) {
-		free(stringarray_get(&es->args,  i));
+		dofree(stringarray_get(&es->args,  i));
 	}
 	stringarray_setsize(&es->args, 0);
 }
@@ -656,6 +656,7 @@
 expand_send(struct expstate *es, struct place *p, const char *buf, size_t len)
 {
 	if (es->tobuf) {
+		assert(es->bufpos <= es->bufmax);
 		if (es->bufpos + len > es->bufmax) {
 			if (es->bufmax == 0) {
 				es->bufmax = 64;
@@ -667,6 +668,7 @@
 		}
 		memcpy(es->buf + es->bufpos, buf, len);
 		es->bufpos += len;
+		assert(es->bufpos <= es->bufmax);
 	} else {
 		output(p, buf, len);
 	}
@@ -796,10 +798,10 @@
 
 	newbuf = expand_substitute(p, es);
 	newbuf2 = macroexpand(p, newbuf, strlen(newbuf), false);
-	free(newbuf);
+	dofree(newbuf);
 	expstate_destroyargs(es);
 	doexpand(es, p, newbuf2, strlen(newbuf2));
-	free(newbuf2);
+	dofree(newbuf2);
 
 	es->curmacro->inuse = false;
 }
@@ -849,7 +851,7 @@
 			newbuf = macroexpand(p, ei->string,
 					     strlen(ei->string), false);
 			doexpand(es, p, newbuf, strlen(newbuf));
-			free(newbuf);
+			dofree(newbuf);
 			m->inuse = false;
 		} else {
 			es->curmacro = m;
--- a/main.c	Sat Mar 30 20:52:59 2013 -0400
+++ b/main.c	Sat Mar 30 21:02:25 2013 -0400
@@ -167,7 +167,7 @@
 		} else {
 			macro_undef(cm->macro);
 		}
-		free(cm);
+		dofree(cm);
 	}
 	array_setsize(&commandline_macros, 0);
 }
@@ -287,7 +287,7 @@
 		} else {
 			file_readquote(&cf->where, cf->name);
 		}
-		free(cf);
+		dofree(cf);
 	}
 	array_setsize(&commandline_files, 0);
 }
@@ -926,7 +926,7 @@
 
 	num = stringarray_num(&freestrings);
 	for (i=0; i<num; i++) {
-		free(stringarray_get(&freestrings, i));
+		dofree(stringarray_get(&freestrings, i));
 	}
 	stringarray_setsize(&freestrings, 0);
 	stringarray_cleanup(&freestrings);
--- a/place.c	Sat Mar 30 20:52:59 2013 -0400
+++ b/place.c	Sat Mar 30 21:02:25 2013 -0400
@@ -74,8 +74,8 @@
 void
 placefile_destroy(struct placefile *pf)
 {
-	free(pf->name);
-	free(pf);
+	dofree(pf->name);
+	dofree(pf);
 }
 
 DESTROYALL_ARRAY(placefile, );
--- a/utils.c	Sat Mar 30 20:52:59 2013 -0400
+++ b/utils.c	Sat Mar 30 21:02:25 2013 -0400
@@ -69,6 +69,12 @@
 	return ret;
 }
 
+void
+dofree(void *ptr)
+{
+	free(ptr);
+}
+
 char *
 dostrdup(const char *s)
 {
--- a/utils.h	Sat Mar 30 20:52:59 2013 -0400
+++ b/utils.h	Sat Mar 30 21:02:25 2013 -0400
@@ -41,6 +41,7 @@
 
 void *domalloc(size_t len);
 void *dorealloc(void *ptr, size_t len);
+void dofree(void *ptr);
 
 char *dostrdup(const char *s);
 char *dostrdup2(const char *s, const char *t);