on master help sign in
Change attr_alloc to be simpler, actual do the allocation
commit48395bef5a1a49848c7b2aec1d1f7def5b18e2fe authorNeil Mitchell <ndmitchell@gmail.com> dateThu 4 Jan 2018, 12:18 (9 years ago) parent370ce52
1 file changed+18−15
gen/test.c+18−15
gen/test.c+18−15
@@ -226,18 +226,21 @@ static inline void node_alloc(node_buffer* b, int ask)
226226 b->alloc = buf2;
227227}
228228
229−static inline void attr_alloc(attr_buffer* b, int ask)
229+static inline attr* attr_alloc(attr_buffer* b)
230230{
231− int space = b->size - b->used;
232− if (space >= ask) return;
233− int size2 = (b->size + 1000 + ask) * 2;
234− attr* buf2 = malloc(size2 * sizeof(attr));
235− assert(buf2);
236− memcpy(buf2, b->attrs, b->used * sizeof(attr));
237− free(b->alloc);
238− b->size = size2;
239− b->attrs = buf2;
240− b->alloc = buf2;
231+ if (b->size <= b->used)
232+ {
233+ int size2 = (b->size + 1000) * 2;
234+ attr* buf2 = malloc(size2 * sizeof(attr));
235+ assert(buf2);
236+ memcpy(buf2, b->attrs, b->used * sizeof(attr));
237+ free(b->alloc);
238+ b->size = size2;
239+ b->attrs = buf2;
240+ b->alloc = buf2;
241+ }
242+ b->used++;
243+ return &b->attrs[b->used - 1];
241244}
242245
243246static void set_error(document* d, const char* msg)
@@ -269,10 +272,9 @@ static inline str gap(const char* ref, const char* start, const char* end)
269272#define P_QuoteStart quote_start = p
270273#define P_QuoteEnd \
271274 quote_end = p; \
272− attr_alloc(&d->attrs, 1); \
273− d->attrs.attrs[d->attrs.used].name = gap(d->body, name_start, name_end); \
274− d->attrs.attrs[d->attrs.used].value = gap(d->body, quote_start, quote_end); \
275− d->attrs.used++;
275+ attr = attr_alloc(&d->attrs); \
276+ attr->name = gap(d->body, name_start, name_end); \
277+ attr->value = gap(d->body, quote_start, quote_end);
276278#define P_TagComment printf("TagComment %s\n", p)
277279#define P_TagOpen printf("TagOpen %s\n", p)
278280#define P_TagClose printf("TagClose %s\n", p)
@@ -285,6 +287,7 @@ static const char* parser(const char* p, document* d)
285287{
286288 const char *name_start, *name_end; // Where a name is <[foo] or <foo [bar]=
287289 const char *quote_start, *quote_end; // Where an attribute quote is <foo bar='[123]'>
290+ attr* attr; // The current attribute I'm working on
288291 node* me; // The current node I'm working on
289292# include "test.h"
290293 return NULL;