Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Rationale
Due to the design of
PAR_REALLOC
it can't work with some kinds of allocators that are not designed to supportrealloc
. A good way to solve this, as seen in other libraries such asstb
, is to have thePAR_REALLOC
macro also provide the old size of the buffer. I specifically had this issue withpar_shapes
so I only provided a solution for that header.Changes
I changed the
PAR_REALLOC
macro to accept an extra parameterOLD_SZ
that represents the old size.The default implementation of the macro remains unchanged.
I then searched for all the uses of
PAR_REALLOC
inpar_shapes
and added the old size of the buffers to thePAR_REALLOC
call. This sometimes involved creating new variables such asint old_dst_npoints = dst->npoints;
.Testing
I tested my solution by adding a small
realloc
wrapper intest_shapes.c
which usesmalloc
andfree
and overriding thePAR_*
allocation macros.All tests have passed when I ran them after I applied my changes.
Considerations
This will affect people who already override
PAR_REALLOC
since they will have to add the extra parameter to the macro, however, if theirPAR_REALLOC
implementation already works without needing the old size then they can just ignore theOLD_SZ
parameter, this is what the default implementation forPAR_REALLOC
does too.This will cause an inconsistency in the API however since all other
par
headers will still have the old-stylerealloc
. If needed I can provide this change for the otherpar
headers as well.