Skip to content

Commit

Permalink
Merge pull request Tencent#183 from pah/feature/wrapped-new-delete
Browse files Browse the repository at this point in the history
Add customization macros for global new/delete
  • Loading branch information
miloyip committed Oct 30, 2014
2 parents dea1cdc + c557b23 commit 4fa43bd
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 6 deletions.
4 changes: 2 additions & 2 deletions include/rapidjson/allocators.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_);
}

Expand Down Expand Up @@ -135,7 +135,7 @@ class MemoryPoolAllocator {
*/
~MemoryPoolAllocator() {
Clear();
delete ownBaseAllocator_;
RAPIDJSON_DELETE(ownBaseAllocator_);
}

//! Deallocates all memory chunks, excluding the user-supplied buffer.
Expand Down
4 changes: 2 additions & 2 deletions include/rapidjson/document.h
Original file line number Diff line number Diff line change
Expand Up @@ -1634,7 +1634,7 @@ class GenericDocument : public GenericValue<Encoding, Allocator> {
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
Expand Down Expand Up @@ -1875,7 +1875,7 @@ class GenericDocument : public GenericValue<Encoding, Allocator> {
}

void Destroy() {
delete ownAllocator_;
RAPIDJSON_DELETE(ownAllocator_);
}

static const size_t kDefaultStackCapacity = 1024;
Expand Down
4 changes: 2 additions & 2 deletions include/rapidjson/internal/stack.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
12 changes: 12 additions & 0 deletions include/rapidjson/rapidjson.h
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,18 @@ template<int x> 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

Expand Down

0 comments on commit 4fa43bd

Please sign in to comment.