Facilitate replacement of standard library memory management functions #224
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.
This PR is intended to address #75
This change allows consumers to replace standard library memory management functions (
malloc
andfree
) with their own memory allocator functions without changing the tinycbor code. Since this seems most useful in an embedded context, this change is scoped to the cborparser_dup_string.c parser file only.One goal of this update is to be fully backward compatible with existing code. Current consumers of this library who want to continue to use the standard library memory management functions do not need to make any changes to their code or project configuration.
Consumers who want to replace
malloc
andfree
should create a header file that definescbor_malloc
andcbor_free
. Then, they set this header file as the value of theCBOR_CUSTOM_ALLOC_INCLUDE
macro. For example, if the header file is named cbor_alloc.h, they would add this to the project settings:-DCBOR_CUSTOM_ALLOC_INCLUDE=\"cbor_alloc.h\"
(This implementation pattern is used in other projects such as mbedtls to facilitate customization where the default implementation does not meet the project's needs.)
For cases where the replacement functions have the same function signature as
malloc
andfree
, macro replacement can be used to definecbor_malloc
andcbor_free
. This allows replacement with no additional cost to the call stack. For example, consumers who wish to replacemalloc
andfree
with FreeRTOS heap functions can create the following simple header:For cases where the replacement functions have different function signatures than
malloc
andfree
,cbor_malloc
andcbor_free
can be defined as functions that wrap the replacement functions. Function prototypes in the header would look like this: