Skip to content

Commit ad2e536

Browse files
author
Gaspard Petit
committed
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 <[email protected]>
1 parent b94c2a1 commit ad2e536

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

include/rapidjson/allocators.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,19 +77,19 @@ class CrtAllocator {
7777
static const bool kNeedFree = true;
7878
void* Malloc(size_t size) {
7979
if (size) // behavior of malloc(0) is implementation defined.
80-
return std::malloc(size);
80+
return RAPIDJSON_MALLOC(size);
8181
else
8282
return NULL; // standardize to returning NULL.
8383
}
8484
void* Realloc(void* originalPtr, size_t originalSize, size_t newSize) {
8585
(void)originalSize;
8686
if (newSize == 0) {
87-
std::free(originalPtr);
87+
RAPIDJSON_FREE(originalPtr);
8888
return NULL;
8989
}
90-
return std::realloc(originalPtr, newSize);
90+
return RAPIDJSON_REALLOC(originalPtr, newSize);
9191
}
92-
static void Free(void *ptr) { std::free(ptr); }
92+
static void Free(void *ptr) { RAPIDJSON_FREE(ptr); }
9393
};
9494

9595
///////////////////////////////////////////////////////////////////////////////

include/rapidjson/rapidjson.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,22 @@ RAPIDJSON_NAMESPACE_END
617617
#define RAPIDJSON_NOEXCEPT_ASSERT(x) RAPIDJSON_ASSERT(x)
618618
#endif // RAPIDJSON_ASSERT_THROWS
619619

620+
///////////////////////////////////////////////////////////////////////////////
621+
// malloc/realloc/free
622+
623+
#ifndef RAPIDJSON_MALLOC
624+
///! customization point for global \c malloc
625+
#define RAPIDJSON_MALLOC std::malloc
626+
#endif
627+
#ifndef RAPIDJSON_REALLOC
628+
///! customization point for global \c realloc
629+
#define RAPIDJSON_REALLOC std::realloc
630+
#endif
631+
#ifndef RAPIDJSON_FREE
632+
///! customization point for global \c free
633+
#define RAPIDJSON_FREE std::free
634+
#endif
635+
620636
///////////////////////////////////////////////////////////////////////////////
621637
// new/delete
622638

0 commit comments

Comments
 (0)