15 #ifndef RAPIDJSON_ALLOCATORS_H_
16 #define RAPIDJSON_ALLOCATORS_H_
20 RAPIDJSON_NAMESPACE_BEGIN
64 static const bool kNeedFree =
true;
65 void* Malloc(
size_t size) {
67 return std::malloc(size);
71 void* Realloc(
void* originalPtr,
size_t originalSize,
size_t newSize) {
74 std::free(originalPtr);
77 return std::realloc(originalPtr, newSize);
79 static void Free(
void *ptr) { std::free(ptr); }
101 template <
typename BaseAllocator = CrtAllocator>
104 static const bool kNeedFree =
false;
111 chunkHead_(0), chunk_capacity_(chunkSize), userBuffer_(0), baseAllocator_(baseAllocator), ownBaseAllocator_(0)
125 MemoryPoolAllocator(
void *buffer,
size_t size,
size_t chunkSize = kDefaultChunkCapacity, BaseAllocator* baseAllocator = 0) :
126 chunkHead_(0), chunk_capacity_(chunkSize), userBuffer_(buffer), baseAllocator_(baseAllocator), ownBaseAllocator_(0)
130 chunkHead_ =
reinterpret_cast<ChunkHeader*
>(buffer);
131 chunkHead_->capacity = size -
sizeof(ChunkHeader);
132 chunkHead_->size = 0;
133 chunkHead_->next = 0;
146 while (chunkHead_ && chunkHead_ != userBuffer_) {
147 ChunkHeader* next = chunkHead_->next;
148 baseAllocator_->Free(chunkHead_);
151 if (chunkHead_ && chunkHead_ == userBuffer_)
152 chunkHead_->size = 0;
160 for (ChunkHeader* c = chunkHead_; c != 0; c = c->next)
161 capacity += c->capacity;
170 for (ChunkHeader* c = chunkHead_; c != 0; c = c->next)
181 if (chunkHead_ == 0 || chunkHead_->size + size > chunkHead_->capacity)
182 AddChunk(chunk_capacity_ > size ? chunk_capacity_ : size);
184 void *buffer =
reinterpret_cast<char *
>(chunkHead_) +
RAPIDJSON_ALIGN(
sizeof(ChunkHeader)) + chunkHead_->size;
185 chunkHead_->size += size;
190 void*
Realloc(
void* originalPtr,
size_t originalSize,
size_t newSize) {
191 if (originalPtr == 0)
192 return Malloc(newSize);
198 if (originalSize >= newSize)
202 if (originalPtr == (
char *)(chunkHead_) +
RAPIDJSON_ALIGN(
sizeof(ChunkHeader)) + chunkHead_->size - originalSize) {
203 size_t increment =
static_cast<size_t>(newSize - originalSize);
205 if (chunkHead_->size + increment <= chunkHead_->capacity) {
206 chunkHead_->size += increment;
212 void* newBuffer = Malloc(newSize);
215 std::memcpy(newBuffer, originalPtr, originalSize);
220 static void Free(
void *ptr) { (void)ptr; }
231 void AddChunk(
size_t capacity) {
233 ownBaseAllocator_ = baseAllocator_ =
RAPIDJSON_NEW(BaseAllocator());
234 ChunkHeader* chunk =
reinterpret_cast<ChunkHeader*
>(baseAllocator_->Malloc(
RAPIDJSON_ALIGN(
sizeof(ChunkHeader)) + capacity));
235 chunk->capacity = capacity;
237 chunk->next = chunkHead_;
241 static const int kDefaultChunkCapacity = 64 * 1024;
252 ChunkHeader *chunkHead_;
253 size_t chunk_capacity_;
255 BaseAllocator* baseAllocator_;
256 BaseAllocator* ownBaseAllocator_;
259 RAPIDJSON_NAMESPACE_END
261 #endif // RAPIDJSON_ENCODINGS_H_
~MemoryPoolAllocator()
Destructor.
Definition: allocators.h:139
void * Realloc(void *originalPtr, size_t originalSize, size_t newSize)
Resizes a memory block (concept Allocator)
Definition: allocators.h:190
MemoryPoolAllocator(void *buffer, size_t size, size_t chunkSize=kDefaultChunkCapacity, BaseAllocator *baseAllocator=0)
Constructor with user-supplied buffer.
Definition: allocators.h:125
size_t Capacity() const
Computes the total capacity of allocated memory chunks.
Definition: allocators.h:158
size_t Size() const
Computes the memory blocks allocated.
Definition: allocators.h:168
void Clear()
Deallocates all memory chunks, excluding the user-supplied buffer.
Definition: allocators.h:145
MemoryPoolAllocator(size_t chunkSize=kDefaultChunkCapacity, BaseAllocator *baseAllocator=0)
Constructor with chunkSize.
Definition: allocators.h:110
C-runtime library allocator.
Definition: allocators.h:62
void * Malloc(size_t size)
Allocates a memory block. (concept Allocator)
Definition: allocators.h:176
#define RAPIDJSON_NEW(x)
! customization point for global new
Definition: rapidjson.h:480
#define RAPIDJSON_ALIGN(x)
Data alignment of the machine.
Definition: rapidjson.h:247
#define RAPIDJSON_DELETE(x)
! customization point for global delete
Definition: rapidjson.h:484
common definitions and configuration
Default memory allocator used by the parser and DOM.
Definition: allocators.h:102
static void Free(void *ptr)
Frees a memory block (concept Allocator)
Definition: allocators.h:220
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:344