michaelrsweet / mxml

Tiny XML library.
https://www.msweet.org/mxml
Apache License 2.0
426 stars 157 forks source link

Memory controls have been added #300

Closed Kadiryanik closed 2 months ago

Kadiryanik commented 1 year ago

I made a small change besides the memory controls.

if (!ind->alloc_nodes)
  temp = malloc(64 * sizeof(mxml_node_t *));
else
  temp = realloc(ind->nodes, (ind->alloc_nodes + 64) * sizeof(mxml_node_t *));

Since the ind is allocated with the calloc, ind->nodes contains NULL and _ind->allocnodes contains 0. realloc works as malloc if the first parameter is NULL. Your size is also same for both. (_ind->allocnodes=0 + 64 = 64)

So we can rid of the if statement and we can use realloc for both case.

michaelrsweet commented 1 year ago

realloc(NULL, bytes) is a C99 guarantee, but currently Mini-XML is still limited to the original ANSI C (C89). But a future version of Mini-XML will move the bar to C99 so I'll hold this for 4.0...

Kadiryanik commented 1 year ago

Thank you for the update. I didn't know there was such a difference in c89. In the sources below, it is said that it behaves like malloc when calling with NULL. Could you please check it again, if it is as you say, could you share the link of your source?

l1:c89

4.10.3.4 The realloc function void realloc(void ptr, size_t size); ... If ptr is a null pointer, the realloc function behaves like the malloc function for the specified size.

l2:c89

7.10.3.4 The realloc function ... If ptr is a null pointer, the realloc function behaves like the malloc function for the specified size.

michaelrsweet commented 1 year ago

Older Unix platforms do not support passing NULL to realloc yet claim C89 conformance. The easiest to verify are the last Sun Solaris and SunOS releases but AIX, HP-UX, IRIX, and Tru64 all had the same problem.

Like I said, I’m ending support for those old platforms with the next major release of Mini-XML…

Kadiryanik commented 1 year ago

I got your concern now, thanks.

michaelrsweet commented 2 months ago

[master c07a57e] Use realloc for everything (Issue #300)