Mercurial > ~dholland > hg > tradcpp > index.cgi
diff macro.c @ 39:337110e7240a
Pass the size to free; it makes debug checking easier.
author | David A. Holland |
---|---|
date | Sat, 30 Mar 2013 21:17:47 -0400 |
parents | b156910b59b2 |
children | 291fefe664f2 |
line wrap: on
line diff
--- a/macro.c Sat Mar 30 21:02:25 2013 -0400 +++ b/macro.c Sat Mar 30 21:17:47 2013 -0400 @@ -110,9 +110,9 @@ expansionitem_destroy(struct expansionitem *ei) { if (ei->isstring) { - dofree(ei->string); + dostrfree(ei->string); } - dofree(ei); + dofree(ei, sizeof(*ei)); } static @@ -162,8 +162,8 @@ { expansionitemarray_destroyall(&m->expansion); expansionitemarray_cleanup(&m->expansion); - dofree(m->name); - dofree(m); + dostrfree(m->name); + dofree(m, sizeof(*m)); } static @@ -634,7 +634,7 @@ assert(es->state == ES_NORMAL); stringarray_cleanup(&es->args); if (es->buf) { - dofree(es->buf); + dofree(es->buf, es->bufmax); } } @@ -646,7 +646,7 @@ num = stringarray_num(&es->args); for (i=0; i<num; i++) { - dofree(stringarray_get(&es->args, i)); + dostrfree(stringarray_get(&es->args, i)); } stringarray_setsize(&es->args, 0); } @@ -655,16 +655,19 @@ void expand_send(struct expstate *es, struct place *p, const char *buf, size_t len) { + size_t oldmax; + if (es->tobuf) { assert(es->bufpos <= es->bufmax); if (es->bufpos + len > es->bufmax) { + oldmax = es->bufmax; if (es->bufmax == 0) { es->bufmax = 64; } while (es->bufpos + len > es->bufmax) { es->bufmax *= 2; } - es->buf = dorealloc(es->buf, es->bufmax); + es->buf = dorealloc(es->buf, oldmax, es->bufmax); } memcpy(es->buf + es->bufpos, buf, len); es->bufpos += len; @@ -709,7 +712,7 @@ text = stringarray_get(&es->args, num - 1); oldlen = strlen(text); - text = dorealloc(text, oldlen + len + 1); + text = dorealloc(text, oldlen + 1, oldlen + len + 1); memcpy(text + oldlen, buf, len); text[oldlen+len] = '\0'; stringarray_set(&es->args, num - 1, text); @@ -798,10 +801,10 @@ newbuf = expand_substitute(p, es); newbuf2 = macroexpand(p, newbuf, strlen(newbuf), false); - dofree(newbuf); + dostrfree(newbuf); expstate_destroyargs(es); doexpand(es, p, newbuf2, strlen(newbuf2)); - dofree(newbuf2); + dostrfree(newbuf2); es->curmacro->inuse = false; } @@ -851,7 +854,7 @@ newbuf = macroexpand(p, ei->string, strlen(ei->string), false); doexpand(es, p, newbuf, strlen(newbuf)); - dofree(newbuf); + dostrfree(newbuf); m->inuse = false; } else { es->curmacro = m; @@ -1101,6 +1104,9 @@ expand_got_eof(&es, p); ret = es.buf; es.buf = NULL; + /* trim to fit, so the malloc debugging won't complain */ + ret = dorealloc(es.buf, es.bufmax, strlen(es.buf) + 1); + es.bufpos = es.bufmax = 0; expstate_cleanup(&es); return ret;