From 002c34f458ecfea854e68ce3821d8fbe2603f4f6 Mon Sep 17 00:00:00 2001 From: Michael R Sweet Date: Sun, 21 Apr 2024 17:08:39 -0400 Subject: [PATCH] Add MXML_ALLOC_SIZE define (Issue #318) --- CHANGES.md | 6 ++++-- mxml-attr.c | 12 ++++++++---- mxml-index.c | 4 ++-- mxml-private.h | 3 +++ 4 files changed, 17 insertions(+), 8 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index d04d714..b30e868 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -5,10 +5,12 @@ Changes in Mini-XML Changes in Mini-XML 4.0.3 ------------------------- -- The configure script now defaults the `DSOFLAGS` value to `LDFLAGS` if not - set (Issue #325) +- Now default the `DSOFLAGS` value to `LDFLAGS` in the configure script + (Issue #325) - Now install the man page as "mxml4" to allow parallel installation of Mini-XML 4.x and 3.x (Issue #324) +- Added `MXML_ALLOC_SIZE` define to control the allocation increment for + attributes and indices (Issue #318) - Fixed `mxmlSetDeclarationf` implementation (Issue #322) diff --git a/mxml-attr.c b/mxml-attr.c index 776a934..98dd5f5 100644 --- a/mxml-attr.c +++ b/mxml-attr.c @@ -250,11 +250,15 @@ mxml_set_attr(mxml_node_t *node, // I - Element node } // Add a new attribute... - if ((attr = realloc(node->value.element.attrs, (node->value.element.num_attrs + 1) * sizeof(_mxml_attr_t))) == NULL) - return (false); + if ((node->value.element.num_attrs % MXML_ALLOC_SIZE) == 0) + { + if ((attr = realloc(node->value.element.attrs, (node->value.element.num_attrs + MXML_ALLOC_SIZE) * sizeof(_mxml_attr_t))) == NULL) + return (false); - node->value.element.attrs = attr; - attr += node->value.element.num_attrs; + node->value.element.attrs = attr; + } + + attr = node->value.element.attrs + node->value.element.num_attrs; if ((attr->name = _mxml_strcopy(name)) == NULL) return (false); diff --git a/mxml-index.c b/mxml-index.c index 9847f65..9da244b 100644 --- a/mxml-index.c +++ b/mxml-index.c @@ -247,7 +247,7 @@ mxmlIndexNew(mxml_node_t *node, // I - XML node tree { if (ind->num_nodes >= ind->alloc_nodes) { - if ((temp = realloc(ind->nodes, (ind->alloc_nodes + 64) * sizeof(mxml_node_t *))) == NULL) + if ((temp = realloc(ind->nodes, (ind->alloc_nodes + MXML_ALLOC_SIZE) * sizeof(mxml_node_t *))) == NULL) { // Unable to allocate memory for the index, so abort... mxmlIndexDelete(ind); @@ -255,7 +255,7 @@ mxmlIndexNew(mxml_node_t *node, // I - XML node tree } ind->nodes = temp; - ind->alloc_nodes += 64; + ind->alloc_nodes += MXML_ALLOC_SIZE; } ind->nodes[ind->num_nodes ++] = current; diff --git a/mxml-private.h b/mxml-private.h index 75aa7d7..b2346e1 100644 --- a/mxml-private.h +++ b/mxml-private.h @@ -25,6 +25,9 @@ # else # define MXML_DEBUG(...) # endif // DEBUG +# ifndef MXML_ALLOC_SIZE +# define MXML_ALLOC_SIZE 16 // Allocation increment +# endif // !MXML_ALLOC_SIZE # define MXML_TAB 8 // Tabs every N columns