Realloc improvement in par_shapes #44
Open
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_REALLOCit 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_REALLOCmacro also provide the old size of the buffer. I specifically had this issue withpar_shapesso I only provided a solution for that header.Changes
I changed the
PAR_REALLOCmacro to accept an extra parameterOLD_SZthat represents the old size.The default implementation of the macro remains unchanged.
I then searched for all the uses of
PAR_REALLOCinpar_shapesand added the old size of the buffers to thePAR_REALLOCcall. This sometimes involved creating new variables such asint old_dst_npoints = dst->npoints;.Testing
I tested my solution by adding a small
reallocwrapper intest_shapes.cwhich usesmallocandfreeand 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_REALLOCsince they will have to add the extra parameter to the macro, however, if theirPAR_REALLOCimplementation already works without needing the old size then they can just ignore theOLD_SZparameter, this is what the default implementation forPAR_REALLOCdoes too.This will cause an inconsistency in the API however since all other
parheaders will still have the old-stylerealloc. If needed I can provide this change for the otherparheaders as well.