16#ifndef RAPIDJSON_ALLOCATORS_H_
17#define RAPIDJSON_ALLOCATORS_H_
64#ifndef RAPIDJSON_ALLOCATOR_DEFAULT_CHUNK_CAPACITY
65#define RAPIDJSON_ALLOCATOR_DEFAULT_CHUNK_CAPACITY (64 * 1024)
78 static const bool kNeedFree =
true;
79 void* Malloc(
size_t size) {
81 return std::malloc(size);
85 void* Realloc(
void* originalPtr,
size_t originalSize,
size_t newSize) {
88 std::free(originalPtr);
91 return std::realloc(originalPtr, newSize);
93 static void Free(
void *ptr) { std::free(ptr); }
115template <
typename BaseAllocator = CrtAllocator>
175 capacity += c->capacity;
205 void*
Realloc(
void* originalPtr,
size_t originalSize,
size_t newSize) {
206 if (originalPtr == 0)
216 if (originalSize >= newSize)
221 size_t increment =
static_cast<size_t>(newSize - originalSize);
229 if (
void* newBuffer =
Malloc(newSize)) {
231 std::memcpy(newBuffer, originalPtr, originalSize);
239 static void Free(
void *ptr) { (void)ptr; }
255 chunk->capacity = capacity;
C-runtime library allocator.
Default memory allocator used by the parser and DOM.
MemoryPoolAllocator(void *buffer, size_t size, size_t chunkSize=kDefaultChunkCapacity, BaseAllocator *baseAllocator=0)
Constructor with user-supplied buffer.
BaseAllocator * ownBaseAllocator_
base allocator created by this object.
bool AddChunk(size_t capacity)
Creates a new chunk.
size_t Capacity() const
Computes the total capacity of allocated memory chunks.
ChunkHeader * chunkHead_
Head of the chunk linked-list. Only the head chunk serves allocation.
void Clear()
Deallocates all memory chunks, excluding the user-supplied buffer.
BaseAllocator * baseAllocator_
base allocator for allocating memory chunks.
size_t chunk_capacity_
The minimum capacity of chunk when they are allocated.
static void Free(void *ptr)
Frees a memory block (concept Allocator)
MemoryPoolAllocator(const MemoryPoolAllocator &rhs)
Copy constructor is not permitted.
void * Realloc(void *originalPtr, size_t originalSize, size_t newSize)
Resizes a memory block (concept Allocator)
void * Malloc(size_t size)
Allocates a memory block. (concept Allocator)
void * userBuffer_
User supplied buffer.
static const int kDefaultChunkCapacity
Default chunk capacity.
static const bool kNeedFree
Tell users that no need to call Free() with this allocator. (concept Allocator)
~MemoryPoolAllocator()
Destructor.
MemoryPoolAllocator & operator=(const MemoryPoolAllocator &rhs)
Copy assignment operator is not permitted.
size_t Size() const
Computes the memory blocks allocated.
MemoryPoolAllocator(size_t chunkSize=kDefaultChunkCapacity, BaseAllocator *baseAllocator=0)
Constructor with chunkSize.
#define RAPIDJSON_ALIGN(x)
Data alignment of the machine.
#define RAPIDJSON_ALLOCATOR_DEFAULT_CHUNK_CAPACITY
User-defined kDefaultChunkCapacity definition.
#define RAPIDJSON_ASSERT(x)
Assertion.
#define RAPIDJSON_NAMESPACE_BEGIN
provide custom rapidjson namespace (opening expression)
#define RAPIDJSON_NAMESPACE_END
provide custom rapidjson namespace (closing expression)
common definitions and configuration
#define RAPIDJSON_DELETE(x)
! customization point for global delete
#define RAPIDJSON_NEW(TypeName)
! customization point for global new