Commit 4fa43bd4 authored by Milo Yip's avatar Milo Yip

Merge pull request #183 from pah/feature/wrapped-new-delete

Add customization macros for global new/delete
parents dea1cdca c557b230
...@@ -105,7 +105,7 @@ public: ...@@ -105,7 +105,7 @@ public:
chunkHead_(0), chunk_capacity_(chunkSize), userBuffer_(0), baseAllocator_(baseAllocator), ownBaseAllocator_(0) chunkHead_(0), chunk_capacity_(chunkSize), userBuffer_(0), baseAllocator_(baseAllocator), ownBaseAllocator_(0)
{ {
if (!baseAllocator_) if (!baseAllocator_)
ownBaseAllocator_ = baseAllocator_ = new BaseAllocator(); ownBaseAllocator_ = baseAllocator_ = RAPIDJSON_NEW(BaseAllocator());
AddChunk(chunk_capacity_); AddChunk(chunk_capacity_);
} }
...@@ -135,7 +135,7 @@ public: ...@@ -135,7 +135,7 @@ public:
*/ */
~MemoryPoolAllocator() { ~MemoryPoolAllocator() {
Clear(); Clear();
delete ownBaseAllocator_; RAPIDJSON_DELETE(ownBaseAllocator_);
} }
//! Deallocates all memory chunks, excluding the user-supplied buffer. //! Deallocates all memory chunks, excluding the user-supplied buffer.
......
...@@ -1634,7 +1634,7 @@ public: ...@@ -1634,7 +1634,7 @@ public:
allocator_(allocator), ownAllocator_(0), stack_(stackAllocator, stackCapacity), parseResult_() allocator_(allocator), ownAllocator_(0), stack_(stackAllocator, stackCapacity), parseResult_()
{ {
if (!allocator_) if (!allocator_)
ownAllocator_ = allocator_ = new Allocator(); ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator());
} }
#if RAPIDJSON_HAS_CXX11_RVALUE_REFS #if RAPIDJSON_HAS_CXX11_RVALUE_REFS
...@@ -1875,7 +1875,7 @@ private: ...@@ -1875,7 +1875,7 @@ private:
} }
void Destroy() { void Destroy() {
delete ownAllocator_; RAPIDJSON_DELETE(ownAllocator_);
} }
static const size_t kDefaultStackCapacity = 1024; static const size_t kDefaultStackCapacity = 1024;
......
...@@ -38,7 +38,7 @@ public: ...@@ -38,7 +38,7 @@ public:
Stack(Allocator* allocator, size_t stackCapacity) : allocator_(allocator), ownAllocator(0), stack_(0), stackTop_(0), stackEnd_(0), initialCapacity_(stackCapacity) { Stack(Allocator* allocator, size_t stackCapacity) : allocator_(allocator), ownAllocator(0), stack_(0), stackTop_(0), stackEnd_(0), initialCapacity_(stackCapacity) {
RAPIDJSON_ASSERT(stackCapacity > 0); RAPIDJSON_ASSERT(stackCapacity > 0);
if (!allocator_) if (!allocator_)
ownAllocator = allocator_ = new Allocator(); ownAllocator = allocator_ = RAPIDJSON_NEW(Allocator());
} }
#if RAPIDJSON_HAS_CXX11_RVALUE_REFS #if RAPIDJSON_HAS_CXX11_RVALUE_REFS
...@@ -162,7 +162,7 @@ private: ...@@ -162,7 +162,7 @@ private:
void Destroy() { void Destroy() {
Allocator::Free(stack_); 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. // Prohibit copy constructor & assignment operator.
......
...@@ -400,6 +400,18 @@ template<int x> struct StaticAssertTest {}; ...@@ -400,6 +400,18 @@ template<int x> struct StaticAssertTest {};
//!@endcond //!@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 // Allocators and Encodings
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment