21 #ifndef RAPIDJSON_ALLOCATORS_H_
22 #define RAPIDJSON_ALLOCATORS_H_
70 static const bool kNeedFree =
true;
71 void* Malloc(
size_t size) {
return malloc(size); }
72 void* Realloc(
void* originalPtr,
size_t originalSize,
size_t newSize) { (void)originalSize;
return realloc(originalPtr, newSize); }
73 static void Free(
void *ptr) { free(ptr); }
95 template <
typename BaseAllocator = CrtAllocator>
105 chunkHead_(0), chunk_capacity_(chunkSize), userBuffer_(0), baseAllocator_(baseAllocator), ownBaseAllocator_(0)
108 ownBaseAllocator_ = baseAllocator_ =
new BaseAllocator();
109 AddChunk(chunk_capacity_);
122 MemoryPoolAllocator(
void *buffer,
size_t size,
size_t chunkSize = kDefaultChunkCapacity, BaseAllocator* baseAllocator = 0) :
123 chunkHead_(0), chunk_capacity_(chunkSize), userBuffer_(buffer), baseAllocator_(baseAllocator), ownBaseAllocator_(0)
127 chunkHead_ =
reinterpret_cast<ChunkHeader*
>(buffer);
128 chunkHead_->capacity = size -
sizeof(ChunkHeader);
129 chunkHead_->size = 0;
130 chunkHead_->next = 0;
138 delete ownBaseAllocator_;
143 while(chunkHead_ != 0 && chunkHead_ != userBuffer_) {
144 ChunkHeader* next = chunkHead_->next;
145 baseAllocator_->Free(chunkHead_);
155 for (ChunkHeader* c = chunkHead_; c != 0; c = c->next)
156 capacity += c->capacity;
165 for (ChunkHeader* c = chunkHead_; c != 0; c = c->next)
173 if (chunkHead_->size + size > chunkHead_->capacity)
174 AddChunk(chunk_capacity_ > size ? chunk_capacity_ : size);
176 void *buffer =
reinterpret_cast<char *
>(chunkHead_ + 1) + chunkHead_->size;
177 chunkHead_->size += size;
182 void*
Realloc(
void* originalPtr,
size_t originalSize,
size_t newSize) {
183 if (originalPtr == 0)
187 if (originalSize >= newSize)
191 if (originalPtr == (
char *)(chunkHead_ + 1) + chunkHead_->size - originalSize) {
192 size_t increment =
static_cast<size_t>(newSize - originalSize);
194 if (chunkHead_->size + increment <= chunkHead_->capacity) {
195 chunkHead_->size += increment;
201 void* newBuffer =
Malloc(newSize);
203 return memcpy(newBuffer, originalPtr, originalSize);
207 static void Free(
void *ptr) { (void)ptr; }
218 void AddChunk(
size_t capacity) {
219 ChunkHeader* chunk =
reinterpret_cast<ChunkHeader*
>(baseAllocator_->Malloc(
sizeof(ChunkHeader) + capacity));
220 chunk->capacity = capacity;
222 chunk->next = chunkHead_;
226 static const int kDefaultChunkCapacity = 64 * 1024;
237 ChunkHeader *chunkHead_;
238 size_t chunk_capacity_;
240 BaseAllocator* baseAllocator_;
241 BaseAllocator* ownBaseAllocator_;
246 #endif // RAPIDJSON_ENCODINGS_H_
~MemoryPoolAllocator()
Destructor.
Definition: allocators.h:136
void * Realloc(void *originalPtr, size_t originalSize, size_t newSize)
Resizes a memory block (concept Allocator)
Definition: allocators.h:182
MemoryPoolAllocator(void *buffer, size_t size, size_t chunkSize=kDefaultChunkCapacity, BaseAllocator *baseAllocator=0)
Constructor with user-supplied buffer.
Definition: allocators.h:122
size_t Capacity() const
Computes the total capacity of allocated memory chunks.
Definition: allocators.h:153
size_t Size() const
Computes the memory blocks allocated.
Definition: allocators.h:163
void Clear()
Deallocates all memory chunks, excluding the user-supplied buffer.
Definition: allocators.h:142
#define RAPIDJSON_ALIGN(x)
Endianness of the machine.
Definition: rapidjson.h:125
MemoryPoolAllocator(size_t chunkSize=kDefaultChunkCapacity, BaseAllocator *baseAllocator=0)
Constructor with chunkSize.
Definition: allocators.h:104
C-runtime library allocator.
Definition: allocators.h:68
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:175
void * Malloc(size_t size)
Allocates a memory block. (concept Allocator)
Definition: allocators.h:171
common definitions and configuration
static const bool kNeedFree
Tell users that no need to call Free() with this allocator. (concept Allocator)
Definition: allocators.h:98
Default memory allocator used by the parser and DOM.
Definition: allocators.h:96
static void Free(void *ptr)
Frees a memory block (concept Allocator)
Definition: allocators.h:207