Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory controls have been added #300

Closed
wants to merge 1 commit into from

Conversation

Kadiryanik
Copy link

@Kadiryanik Kadiryanik commented Dec 28, 2022

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->alloc_nodes contains 0. realloc works as malloc if the first parameter is NULL. Your size is also same for both. (ind->alloc_nodes=0 + 64 = 64)

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

@michaelrsweet michaelrsweet self-assigned this Dec 28, 2022
@michaelrsweet michaelrsweet added enhancement New feature or request priority-low labels Dec 28, 2022
@michaelrsweet michaelrsweet added this to the v4.0 milestone Dec 28, 2022
@michaelrsweet
Copy link
Owner

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
Copy link
Author

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
Copy link
Owner

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
Copy link
Author

I got your concern now, thanks.

michaelrsweet added a commit that referenced this pull request Mar 6, 2024
@michaelrsweet
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request priority-low
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants