comparison 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
comparison
equal deleted inserted replaced
38:b156910b59b2 39:337110e7240a
108 static 108 static
109 void 109 void
110 expansionitem_destroy(struct expansionitem *ei) 110 expansionitem_destroy(struct expansionitem *ei)
111 { 111 {
112 if (ei->isstring) { 112 if (ei->isstring) {
113 dofree(ei->string); 113 dostrfree(ei->string);
114 } 114 }
115 dofree(ei); 115 dofree(ei, sizeof(*ei));
116 } 116 }
117 117
118 static 118 static
119 bool 119 bool
120 expansionitem_eq(const struct expansionitem *ei1, 120 expansionitem_eq(const struct expansionitem *ei1,
160 void 160 void
161 macro_destroy(struct macro *m) 161 macro_destroy(struct macro *m)
162 { 162 {
163 expansionitemarray_destroyall(&m->expansion); 163 expansionitemarray_destroyall(&m->expansion);
164 expansionitemarray_cleanup(&m->expansion); 164 expansionitemarray_cleanup(&m->expansion);
165 dofree(m->name); 165 dostrfree(m->name);
166 dofree(m); 166 dofree(m, sizeof(*m));
167 } 167 }
168 168
169 static 169 static
170 bool 170 bool
171 macro_eq(const struct macro *m1, const struct macro *m2) 171 macro_eq(const struct macro *m1, const struct macro *m2)
632 expstate_cleanup(struct expstate *es) 632 expstate_cleanup(struct expstate *es)
633 { 633 {
634 assert(es->state == ES_NORMAL); 634 assert(es->state == ES_NORMAL);
635 stringarray_cleanup(&es->args); 635 stringarray_cleanup(&es->args);
636 if (es->buf) { 636 if (es->buf) {
637 dofree(es->buf); 637 dofree(es->buf, es->bufmax);
638 } 638 }
639 } 639 }
640 640
641 static 641 static
642 void 642 void
644 { 644 {
645 unsigned i, num; 645 unsigned i, num;
646 646
647 num = stringarray_num(&es->args); 647 num = stringarray_num(&es->args);
648 for (i=0; i<num; i++) { 648 for (i=0; i<num; i++) {
649 dofree(stringarray_get(&es->args, i)); 649 dostrfree(stringarray_get(&es->args, i));
650 } 650 }
651 stringarray_setsize(&es->args, 0); 651 stringarray_setsize(&es->args, 0);
652 } 652 }
653 653
654 static 654 static
655 void 655 void
656 expand_send(struct expstate *es, struct place *p, const char *buf, size_t len) 656 expand_send(struct expstate *es, struct place *p, const char *buf, size_t len)
657 { 657 {
658 size_t oldmax;
659
658 if (es->tobuf) { 660 if (es->tobuf) {
659 assert(es->bufpos <= es->bufmax); 661 assert(es->bufpos <= es->bufmax);
660 if (es->bufpos + len > es->bufmax) { 662 if (es->bufpos + len > es->bufmax) {
663 oldmax = es->bufmax;
661 if (es->bufmax == 0) { 664 if (es->bufmax == 0) {
662 es->bufmax = 64; 665 es->bufmax = 64;
663 } 666 }
664 while (es->bufpos + len > es->bufmax) { 667 while (es->bufpos + len > es->bufmax) {
665 es->bufmax *= 2; 668 es->bufmax *= 2;
666 } 669 }
667 es->buf = dorealloc(es->buf, es->bufmax); 670 es->buf = dorealloc(es->buf, oldmax, es->bufmax);
668 } 671 }
669 memcpy(es->buf + es->bufpos, buf, len); 672 memcpy(es->buf + es->bufpos, buf, len);
670 es->bufpos += len; 673 es->bufpos += len;
671 assert(es->bufpos <= es->bufmax); 674 assert(es->bufpos <= es->bufmax);
672 } else { 675 } else {
707 num = stringarray_num(&es->args); 710 num = stringarray_num(&es->args);
708 assert(num > 0); 711 assert(num > 0);
709 712
710 text = stringarray_get(&es->args, num - 1); 713 text = stringarray_get(&es->args, num - 1);
711 oldlen = strlen(text); 714 oldlen = strlen(text);
712 text = dorealloc(text, oldlen + len + 1); 715 text = dorealloc(text, oldlen + 1, oldlen + len + 1);
713 memcpy(text + oldlen, buf, len); 716 memcpy(text + oldlen, buf, len);
714 text[oldlen+len] = '\0'; 717 text[oldlen+len] = '\0';
715 stringarray_set(&es->args, num - 1, text); 718 stringarray_set(&es->args, num - 1, text);
716 } 719 }
717 720
796 assert(es->curmacro->inuse == false); 799 assert(es->curmacro->inuse == false);
797 es->curmacro->inuse = true; 800 es->curmacro->inuse = true;
798 801
799 newbuf = expand_substitute(p, es); 802 newbuf = expand_substitute(p, es);
800 newbuf2 = macroexpand(p, newbuf, strlen(newbuf), false); 803 newbuf2 = macroexpand(p, newbuf, strlen(newbuf), false);
801 dofree(newbuf); 804 dostrfree(newbuf);
802 expstate_destroyargs(es); 805 expstate_destroyargs(es);
803 doexpand(es, p, newbuf2, strlen(newbuf2)); 806 doexpand(es, p, newbuf2, strlen(newbuf2));
804 dofree(newbuf2); 807 dostrfree(newbuf2);
805 808
806 es->curmacro->inuse = false; 809 es->curmacro->inuse = false;
807 } 810 }
808 811
809 static 812 static
849 ei = expansionitemarray_get(&m->expansion, 0); 852 ei = expansionitemarray_get(&m->expansion, 0);
850 assert(ei->isstring); 853 assert(ei->isstring);
851 newbuf = macroexpand(p, ei->string, 854 newbuf = macroexpand(p, ei->string,
852 strlen(ei->string), false); 855 strlen(ei->string), false);
853 doexpand(es, p, newbuf, strlen(newbuf)); 856 doexpand(es, p, newbuf, strlen(newbuf));
854 dofree(newbuf); 857 dostrfree(newbuf);
855 m->inuse = false; 858 m->inuse = false;
856 } else { 859 } else {
857 es->curmacro = m; 860 es->curmacro = m;
858 es->state = ES_WANTLPAREN; 861 es->state = ES_WANTLPAREN;
859 } 862 }
1099 expstate_init(&es, true, honordefined); 1102 expstate_init(&es, true, honordefined);
1100 doexpand(&es, p, buf, len); 1103 doexpand(&es, p, buf, len);
1101 expand_got_eof(&es, p); 1104 expand_got_eof(&es, p);
1102 ret = es.buf; 1105 ret = es.buf;
1103 es.buf = NULL; 1106 es.buf = NULL;
1107 /* trim to fit, so the malloc debugging won't complain */
1108 ret = dorealloc(es.buf, es.bufmax, strlen(es.buf) + 1);
1109 es.bufpos = es.bufmax = 0;
1104 expstate_cleanup(&es); 1110 expstate_cleanup(&es);
1105 1111
1106 return ret; 1112 return ret;
1107 } 1113 }
1108 1114