From ad2e5369b9136a57339e978e6a7d38b1f5da60af Mon Sep 17 00:00:00 2001 From: Gaspard Petit Date: Sun, 10 Feb 2019 00:32:26 -0500 Subject: [PATCH 1/2] Adding a single customization point that ensures all allocations within rapidjson can be performed with a custom memory allocator; Introduces the macros RAPIDJSON_MALLOC, RAPIDJSON_REALLOC, and RAPIDJSON_FREE. Signed-off-by: Gaspard Petit --- include/rapidjson/allocators.h | 8 ++++---- include/rapidjson/rapidjson.h | 16 ++++++++++++++++ 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/include/rapidjson/allocators.h b/include/rapidjson/allocators.h index 06b3420..b196c94 100644 --- a/include/rapidjson/allocators.h +++ b/include/rapidjson/allocators.h @@ -77,19 +77,19 @@ public: static const bool kNeedFree = true; void* Malloc(size_t size) { if (size) // behavior of malloc(0) is implementation defined. - return std::malloc(size); + return RAPIDJSON_MALLOC(size); else return NULL; // standardize to returning NULL. } void* Realloc(void* originalPtr, size_t originalSize, size_t newSize) { (void)originalSize; if (newSize == 0) { - std::free(originalPtr); + RAPIDJSON_FREE(originalPtr); return NULL; } - return std::realloc(originalPtr, newSize); + return RAPIDJSON_REALLOC(originalPtr, newSize); } - static void Free(void *ptr) { std::free(ptr); } + static void Free(void *ptr) { RAPIDJSON_FREE(ptr); } }; /////////////////////////////////////////////////////////////////////////////// diff --git a/include/rapidjson/rapidjson.h b/include/rapidjson/rapidjson.h index 065c8bb..12669d3 100644 --- a/include/rapidjson/rapidjson.h +++ b/include/rapidjson/rapidjson.h @@ -617,6 +617,22 @@ RAPIDJSON_NAMESPACE_END #define RAPIDJSON_NOEXCEPT_ASSERT(x) RAPIDJSON_ASSERT(x) #endif // RAPIDJSON_ASSERT_THROWS +/////////////////////////////////////////////////////////////////////////////// +// malloc/realloc/free + +#ifndef RAPIDJSON_MALLOC +///! customization point for global \c malloc +#define RAPIDJSON_MALLOC std::malloc +#endif +#ifndef RAPIDJSON_REALLOC +///! customization point for global \c realloc +#define RAPIDJSON_REALLOC std::realloc +#endif +#ifndef RAPIDJSON_FREE +///! customization point for global \c free +#define RAPIDJSON_FREE std::free +#endif + /////////////////////////////////////////////////////////////////////////////// // new/delete From cef07fb1b3ad9cb6360dff4bc773039604b58a70 Mon Sep 17 00:00:00 2001 From: Gaspard Petit Date: Sun, 10 Feb 2019 01:15:35 -0500 Subject: [PATCH 2/2] Added parameters to RAPIDJSON_MALLOC, RAPIDJSON_REALLOC and RAPIDJSON_FREE Signed-off-by: Gaspard Petit --- include/rapidjson/rapidjson.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/include/rapidjson/rapidjson.h b/include/rapidjson/rapidjson.h index 12669d3..99e8b83 100644 --- a/include/rapidjson/rapidjson.h +++ b/include/rapidjson/rapidjson.h @@ -622,15 +622,15 @@ RAPIDJSON_NAMESPACE_END #ifndef RAPIDJSON_MALLOC ///! customization point for global \c malloc -#define RAPIDJSON_MALLOC std::malloc +#define RAPIDJSON_MALLOC(size) std::malloc(size) #endif #ifndef RAPIDJSON_REALLOC ///! customization point for global \c realloc -#define RAPIDJSON_REALLOC std::realloc +#define RAPIDJSON_REALLOC(ptr, new_size) std::realloc(ptr, new_size) #endif #ifndef RAPIDJSON_FREE ///! customization point for global \c free -#define RAPIDJSON_FREE std::free +#define RAPIDJSON_FREE(ptr) std::free(ptr) #endif ///////////////////////////////////////////////////////////////////////////////