Skip to content

Commit

Permalink
Merge pull request #1453 from eidosmontreal/custom_malloc
Browse files Browse the repository at this point in the history
Adding a single customization point that ensures all allocations within rapidjson can be performed with a custom memory allocator
  • Loading branch information
miloyip committed Jun 23, 2020
2 parents 1a80382 + 004e8e6 commit 88bd956
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
8 changes: 4 additions & 4 deletions include/rapidjson/allocators.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,19 +77,19 @@ class CrtAllocator {
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); }
};

///////////////////////////////////////////////////////////////////////////////
Expand Down
16 changes: 16 additions & 0 deletions include/rapidjson/rapidjson.h
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,22 @@ RAPIDJSON_NAMESPACE_END
#endif // RAPIDJSON_ASSERT_THROWS
#endif // RAPIDJSON_NOEXCEPT_ASSERT

///////////////////////////////////////////////////////////////////////////////
// malloc/realloc/free

#ifndef RAPIDJSON_MALLOC
///! customization point for global \c malloc
#define RAPIDJSON_MALLOC(size) std::malloc(size)
#endif
#ifndef RAPIDJSON_REALLOC
///! customization point for global \c 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(ptr) std::free(ptr)
#endif

///////////////////////////////////////////////////////////////////////////////
// new/delete

Expand Down

0 comments on commit 88bd956

Please sign in to comment.