Skip to content

Commit 88bd956

Browse files
authored
Merge pull request Tencent#1453 from eidosmontreal/custom_malloc
Adding a single customization point that ensures all allocations within rapidjson can be performed with a custom memory allocator
2 parents 1a80382 + 004e8e6 commit 88bd956

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
@@ -639,6 +639,22 @@ RAPIDJSON_NAMESPACE_END
639639
#endif // RAPIDJSON_ASSERT_THROWS
640640
#endif // RAPIDJSON_NOEXCEPT_ASSERT
641641

642+
///////////////////////////////////////////////////////////////////////////////
643+
// malloc/realloc/free
644+
645+
#ifndef RAPIDJSON_MALLOC
646+
///! customization point for global \c malloc
647+
#define RAPIDJSON_MALLOC(size) std::malloc(size)
648+
#endif
649+
#ifndef RAPIDJSON_REALLOC
650+
///! customization point for global \c realloc
651+
#define RAPIDJSON_REALLOC(ptr, new_size) std::realloc(ptr, new_size)
652+
#endif
653+
#ifndef RAPIDJSON_FREE
654+
///! customization point for global \c free
655+
#define RAPIDJSON_FREE(ptr) std::free(ptr)
656+
#endif
657+
642658
///////////////////////////////////////////////////////////////////////////////
643659
// new/delete
644660

0 commit comments

Comments
 (0)