on master help sign in
Redo node_alloc to return a suitable node
commit0f72a8228ce80b29d1e788d218f565c695575869 authorNeil Mitchell <ndmitchell@gmail.com> dateThu 4 Jan 2018, 12:27 (9 years ago) parentfdd831a
1 file changed+29−21
gen/test.c+29−21
gen/test.c+29−21
@@ -211,19 +211,29 @@ node* hexml_node_child(const document* d, const node* parent, const node* prev,
211211/////////////////////////////////////////////////////////////////////
212212// PARSING CODE
213213
214−static inline void node_alloc(node_buffer* b, int ask)
214+static inline node* node_alloc(node_buffer* b)
215215{
216+ // Invariant: node_commit requires at least one spare spot
216217 int space = b->size - b->used_back - b->used_front;
217− if (space >= ask) return;
218− int size2 = (b->size + 1000 + ask) * 2;
219− node* buf2 = malloc(size2 * sizeof(node));
220− assert(buf2);
221− memcpy(buf2, b->nodes, b->used_front * sizeof(node));
222− memcpy(&buf2[size2 - b->used_back], &b->nodes[b->size - b->used_back], b->used_back * sizeof(node));
223− free(b->alloc);
224− b->size = size2;
225− b->nodes = buf2;
226− b->alloc = buf2;
218+ if (space < 2)
219+ {
220+ int size2 = (b->size + 1000) * 2;
221+ node* buf2 = malloc(size2 * sizeof(node));
222+ assert(buf2);
223+ memcpy(buf2, b->nodes, b->used_front * sizeof(node));
224+ memcpy(&buf2[size2 - b->used_back], &b->nodes[b->size - b->used_back], b->used_back * sizeof(node));
225+ free(b->alloc);
226+ b->size = size2;
227+ b->nodes = buf2;
228+ b->alloc = buf2;
229+ }
230+ b->used_back++;
231+ return &b->nodes[b->size - b->used_back - 1];
232+}
233+
234+static inline void node_commit(node_buffer* b)
235+{
236+
227237}
228238
229239static inline attr* attr_alloc(attr_buffer* b)
@@ -260,17 +270,15 @@ static inline str gap(const char* ref, const char* start, const char* end)
260270
261271#define P_Abort(x) return x
262272#define P_Tag \
263− node_alloc(&d->nodes, 1); \
264− d->nodes.used_back++; \
265− me = &d->nodes.nodes[d->nodes.size - d->nodes.used_back]; \
266− me->outer = gap(d->body, p, p);
273+ node = node_alloc(&d->nodes); \
274+ node->outer = gap(d->body, p, p);
267275#define P_NameStart name_start = p
268276#define P_NameEnd name_end = p
269277#define P_AttribsStart \
270− me->attrs.start = d->attrs.used; \
271− me->name = gap(d->body, name_start, name_end);
278+ node->attrs.start = d->attrs.used; \
279+ node->name = gap(d->body, name_start, name_end);
272280#define P_AttribsEnd \
273− me->attrs = start_end(me->attrs.start, d->attrs.used);
281+ node->attrs = start_end(node->attrs.start, d->attrs.used);
274282#define P_QuoteStart quote_start = p
275283#define P_QuoteEnd \
276284 quote_end = p; \
@@ -281,8 +289,8 @@ static inline str gap(const char* ref, const char* start, const char* end)
281289#define P_TagOpen printf("TagOpen %s\n", p)
282290#define P_TagClose printf("TagClose %s\n", p)
283291#define P_TagOpenClose \
284− me->nodes = start_end(0, 0); \
285− me->outer.length = gap(d->body, p, p).length;
292+ node->nodes = start_end(0, 0); \
293+ node->outer.length = gap(d->body, p, p).length;
286294
287295// Given the parsed string, return either NULL (success) or an error message (failure)
288296static const char* parser(const char* p, document* d)
@@ -290,7 +298,7 @@ static const char* parser(const char* p, document* d)
290298 const char *name_start, *name_end; // Where a name is <[foo] or <foo [bar]=
291299 const char *quote_start, *quote_end; // Where an attribute quote is <foo bar='[123]'>
292300 attr* attr; // The current attribute I'm working on
293− node* me; // The current node I'm working on
301+ node* node; // The current node I'm working on
294302# include "test.h"
295303 return NULL;
296304}