From c557b230a65c1305c90080a350d924c2362e7424 Mon Sep 17 00:00:00 2001 From: "Philipp A. Hartmann" Date: Thu, 30 Oct 2014 11:20:46 +0100 Subject: [PATCH] Add customization macros for global new/delete As mentioned in #181, some environments may require adaptations to the internal calls to the global `new`/`delete` operators, like adding explicit `NULL` checks to `delete. This patch adds two new macros * RAPIDJSON_NEW(x) * RAPIDJSON_DELETE(x) to allow user-defined expressions in these cases. This fixes #181 in an alternative manner. --- include/rapidjson/allocators.h | 4 ++-- include/rapidjson/document.h | 4 ++-- include/rapidjson/internal/stack.h | 4 ++-- include/rapidjson/rapidjson.h | 12 ++++++++++++ 4 files changed, 18 insertions(+), 6 deletions(-) diff --git a/include/rapidjson/allocators.h b/include/rapidjson/allocators.h index c99485e50..04fab687b 100644 --- a/include/rapidjson/allocators.h +++ b/include/rapidjson/allocators.h @@ -105,7 +105,7 @@ class MemoryPoolAllocator { chunkHead_(0), chunk_capacity_(chunkSize), userBuffer_(0), baseAllocator_(baseAllocator), ownBaseAllocator_(0) { if (!baseAllocator_) - ownBaseAllocator_ = baseAllocator_ = new BaseAllocator(); + ownBaseAllocator_ = baseAllocator_ = RAPIDJSON_NEW(BaseAllocator()); AddChunk(chunk_capacity_); } @@ -135,7 +135,7 @@ class MemoryPoolAllocator { */ ~MemoryPoolAllocator() { Clear(); - delete ownBaseAllocator_; + RAPIDJSON_DELETE(ownBaseAllocator_); } //! Deallocates all memory chunks, excluding the user-supplied buffer. diff --git a/include/rapidjson/document.h b/include/rapidjson/document.h index 0f7974738..2b9fe6383 100644 --- a/include/rapidjson/document.h +++ b/include/rapidjson/document.h @@ -1634,7 +1634,7 @@ class GenericDocument : public GenericValue { allocator_(allocator), ownAllocator_(0), stack_(stackAllocator, stackCapacity), parseResult_() { if (!allocator_) - ownAllocator_ = allocator_ = new Allocator(); + ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator()); } #if RAPIDJSON_HAS_CXX11_RVALUE_REFS @@ -1875,7 +1875,7 @@ class GenericDocument : public GenericValue { } void Destroy() { - delete ownAllocator_; + RAPIDJSON_DELETE(ownAllocator_); } static const size_t kDefaultStackCapacity = 1024; diff --git a/include/rapidjson/internal/stack.h b/include/rapidjson/internal/stack.h index ea722c6f1..fbf05b4d5 100644 --- a/include/rapidjson/internal/stack.h +++ b/include/rapidjson/internal/stack.h @@ -38,7 +38,7 @@ class Stack { Stack(Allocator* allocator, size_t stackCapacity) : allocator_(allocator), ownAllocator(0), stack_(0), stackTop_(0), stackEnd_(0), initialCapacity_(stackCapacity) { RAPIDJSON_ASSERT(stackCapacity > 0); if (!allocator_) - ownAllocator = allocator_ = new Allocator(); + ownAllocator = allocator_ = RAPIDJSON_NEW(Allocator()); } #if RAPIDJSON_HAS_CXX11_RVALUE_REFS @@ -162,7 +162,7 @@ class Stack { void Destroy() { Allocator::Free(stack_); - delete ownAllocator; // Only delete if it is owned by the stack + RAPIDJSON_DELETE(ownAllocator); // Only delete if it is owned by the stack } // Prohibit copy constructor & assignment operator. diff --git a/include/rapidjson/rapidjson.h b/include/rapidjson/rapidjson.h index 3f743234d..d4c7f03e2 100644 --- a/include/rapidjson/rapidjson.h +++ b/include/rapidjson/rapidjson.h @@ -400,6 +400,18 @@ template struct StaticAssertTest {}; //!@endcond +/////////////////////////////////////////////////////////////////////////////// +// new/delete + +#ifndef RAPIDJSON_NEW +///! customization point for global \c new +#define RAPIDJSON_NEW(x) new x +#endif +#ifndef RAPIDJSON_DELETE +///! customization point for global \c delete +#define RAPIDJSON_DELETE(x) delete x +#endif + /////////////////////////////////////////////////////////////////////////////// // Allocators and Encodings