on master help sign in
Move to separate allocation vs use buffers
commitb8098d39b6094d012347b478656290f3b85f8382 authorNeil Mitchell <ndmitchell@gmail.com> dateMon 28 Nov 2016, 18:25 (10 years ago) parentf97ced2
1 file changed+8−3
hexml.c+8−3
hexml.c+8−3
@@ -31,6 +31,7 @@ typedef struct
3131 int size;
3232 int used;
3333 attr* attrs; // dynamically allocated buffer
34+ attr* alloc; // what to call free on
3435} attr_buffer;
3536
3637
@@ -54,6 +55,7 @@ typedef struct
5455 int used_front; // front entries, stored for good
5556 int used_back; // back entries, stack based, copied into front
5657 node* nodes; // dynamically allocated buffer
58+ node* alloc; // what to call free on
5759} node_buffer;
5860
5961
@@ -182,8 +184,8 @@ str node_outer(node* n){return n->outer;}
182184void document_free(document* d)
183185{
184186 free(d->error_message);
185− free(d->nodes.nodes);
186− free(d->attrs.attrs);
187+ free(d->nodes.alloc);
188+ free(d->attrs.alloc);
187189 free(d);
188190}
189191
@@ -306,8 +308,9 @@ void static inline attr_alloc(attr_buffer* b, int ask)
306308 int size2 = (b->size + 1000 + ask) * 2;
307309 attr* buf2 = malloc(size2 * sizeof(attr));
308310 memcpy(buf2, b->attrs, b->used * sizeof(attr));
309− free(b->attrs);
311+ free(b->alloc);
310312 b->attrs = buf2;
313+ b->alloc = buf2;
311314}
312315
313316// you now expect a name, perhaps preceeded by whitespace
@@ -471,10 +474,12 @@ document* document_parse(char* s, int slen)
471474 d->attrs.size = 0;
472475 d->attrs.used = 0;
473476 d->attrs.attrs = NULL;
477+ d->attrs.alloc = NULL;
474478 d->nodes.size = 1000;
475479 d->nodes.used_back = 0;
476480 d->nodes.used_front = 1;
477481 d->nodes.nodes = malloc(sizeof(node) * 1000);
482+ d->nodes.alloc = d->nodes.nodes;
478483
479484 d->nodes.nodes[0].name = start_length(0, 0);
480485 d->nodes.nodes[0].outer = start_length(0, slen);