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 <gaspard.petit@eidosmontreal.com>
This commit is contained in:
Gaspard Petit 2019-02-10 00:32:26 -05:00
parent b94c2a1203
commit ad2e5369b9
2 changed files with 20 additions and 4 deletions

View File

@ -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); }
};
///////////////////////////////////////////////////////////////////////////////

View File

@ -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