on master help sign in
Change attr_alloc to be simpler, actual do the allocation
48395beNeil Mitchell
gen/test.c+18−15
@@ -226,18 +226,21 @@ static inline void node_alloc(node_buffer* b, int ask)
226 b->alloc = buf2;
226 b->alloc = buf2;
227}
227}
228
228
229−static inline void attr_alloc(attr_buffer* b, int ask)
229+static inline attr* attr_alloc(attr_buffer* b)
230{
230{
231− int space = b->size - b->used;
231+ if (b->size <= b->used)
232− if (space >= ask) return;
232+ {
233− int size2 = (b->size + 1000 + ask) * 2;
233+ int size2 = (b->size + 1000) * 2;
234− attr* buf2 = malloc(size2 * sizeof(attr));
234+ attr* buf2 = malloc(size2 * sizeof(attr));
235− assert(buf2);
235+ assert(buf2);
236− memcpy(buf2, b->attrs, b->used * sizeof(attr));
236+ memcpy(buf2, b->attrs, b->used * sizeof(attr));
237− free(b->alloc);
237+ free(b->alloc);
238− b->size = size2;
238+ b->size = size2;
239− b->attrs = buf2;
239+ b->attrs = buf2;
240− b->alloc = buf2;
240+ b->alloc = buf2;
241+ }
242+ b->used++;
243+ return &b->attrs[b->used - 1];
241}
244}
242
245
243static void set_error(document* d, const char* msg)
246static 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)
269#define P_QuoteStart quote_start = p
272#define P_QuoteStart quote_start = p
270#define P_QuoteEnd \
273#define P_QuoteEnd \
271 quote_end = p; \
274 quote_end = p; \
272− attr_alloc(&d->attrs, 1); \
275+ attr = attr_alloc(&d->attrs); \
273− d->attrs.attrs[d->attrs.used].name = gap(d->body, name_start, name_end); \
276+ attr->name = gap(d->body, name_start, name_end); \
274− d->attrs.attrs[d->attrs.used].value = gap(d->body, quote_start, quote_end); \
277+ attr->value = gap(d->body, quote_start, quote_end);
275− d->attrs.used++;
276#define P_TagComment printf("TagComment %s\n", p)
278#define P_TagComment printf("TagComment %s\n", p)
277#define P_TagOpen printf("TagOpen %s\n", p)
279#define P_TagOpen printf("TagOpen %s\n", p)
278#define P_TagClose printf("TagClose %s\n", p)
280#define P_TagClose printf("TagClose %s\n", p)
@@ -285,6 +287,7 @@ static const char* parser(const char* p, document* d)
285{
287{
286 const char *name_start, *name_end; // Where a name is <[foo] or <foo [bar]=
288 const char *name_start, *name_end; // Where a name is <[foo] or <foo [bar]=
287 const char *quote_start, *quote_end; // Where an attribute quote is <foo bar='[123]'>
289 const char *quote_start, *quote_end; // Where an attribute quote is <foo bar='[123]'>
290+ attr* attr; // The current attribute I'm working on
288 node* me; // The current node I'm working on
291 node* me; // The current node I'm working on
289# include "test.h"
292# include "test.h"
290 return NULL;
293 return NULL;