21 #ifndef RAPIDJSON_ALLOCATORS_H_ 22 #define RAPIDJSON_ALLOCATORS_H_ 70 static const bool kNeedFree =
true;
71 void* Malloc(
size_t size) {
return std::malloc(size); }
72 void* Realloc(
void* originalPtr,
size_t originalSize,
size_t newSize) { (void)originalSize;
return std::realloc(originalPtr, newSize); }
73 static void Free(
void *ptr) { std::free(ptr); }
95 template <
typename BaseAllocator = CrtAllocator>
98 static const bool kNeedFree =
false;
105 chunkHead_(0), chunk_capacity_(chunkSize), userBuffer_(0), baseAllocator_(baseAllocator), ownBaseAllocator_(0)
119 MemoryPoolAllocator(
void *buffer,
size_t size,
size_t chunkSize = kDefaultChunkCapacity, BaseAllocator* baseAllocator = 0) :
120 chunkHead_(0), chunk_capacity_(chunkSize), userBuffer_(buffer), baseAllocator_(baseAllocator), ownBaseAllocator_(0)
124 chunkHead_ =
reinterpret_cast<ChunkHeader*
>(buffer);
125 chunkHead_->capacity = size -
sizeof(ChunkHeader);
126 chunkHead_->size = 0;
127 chunkHead_->next = 0;
140 while(chunkHead_ != 0 && chunkHead_ != userBuffer_) {
141 ChunkHeader* next = chunkHead_->next;
142 baseAllocator_->Free(chunkHead_);
152 for (ChunkHeader* c = chunkHead_; c != 0; c = c->next)
153 capacity += c->capacity;
162 for (ChunkHeader* c = chunkHead_; c != 0; c = c->next)
170 if (chunkHead_ == 0 || chunkHead_->size + size > chunkHead_->capacity)
171 AddChunk(chunk_capacity_ > size ? chunk_capacity_ : size);
173 void *buffer =
reinterpret_cast<char *
>(chunkHead_ + 1) + chunkHead_->size;
174 chunkHead_->size += size;
179 void*
Realloc(
void* originalPtr,
size_t originalSize,
size_t newSize) {
180 if (originalPtr == 0)
181 return Malloc(newSize);
184 if (originalSize >= newSize)
188 if (originalPtr == (
char *)(chunkHead_ + 1) + chunkHead_->size - originalSize) {
189 size_t increment =
static_cast<size_t>(newSize - originalSize);
191 if (chunkHead_->size + increment <= chunkHead_->capacity) {
192 chunkHead_->size += increment;
198 void* newBuffer = Malloc(newSize);
200 return std::memcpy(newBuffer, originalPtr, originalSize);
204 static void Free(
void *ptr) { (void)ptr; }
215 void AddChunk(
size_t capacity) {
217 ownBaseAllocator_ = baseAllocator_ =
RAPIDJSON_NEW(BaseAllocator());
218 ChunkHeader* chunk =
reinterpret_cast<ChunkHeader*
>(baseAllocator_->Malloc(
sizeof(ChunkHeader) + capacity));
219 chunk->capacity = capacity;
221 chunk->next = chunkHead_;
225 static const int kDefaultChunkCapacity = 64 * 1024;
236 ChunkHeader *chunkHead_;
237 size_t chunk_capacity_;
239 BaseAllocator* baseAllocator_;
240 BaseAllocator* ownBaseAllocator_;
245 #endif // RAPIDJSON_ENCODINGS_H_ ~MemoryPoolAllocator()
Destructor.
Definition: allocators.h:133
void * Realloc(void *originalPtr, size_t originalSize, size_t newSize)
Resizes a memory block (concept Allocator)
Definition: allocators.h:179
MemoryPoolAllocator(void *buffer, size_t size, size_t chunkSize=kDefaultChunkCapacity, BaseAllocator *baseAllocator=0)
Constructor with user-supplied buffer.
Definition: allocators.h:119
size_t Capacity() const
Computes the total capacity of allocated memory chunks.
Definition: allocators.h:150
void Clear()
Deallocates all memory chunks, excluding the user-supplied buffer.
Definition: allocators.h:139
MemoryPoolAllocator(size_t chunkSize=kDefaultChunkCapacity, BaseAllocator *baseAllocator=0)
Constructor with chunkSize.
Definition: allocators.h:104
C-runtime library allocator.
Definition: allocators.h:68
void * Malloc(size_t size)
Allocates a memory block. (concept Allocator)
Definition: allocators.h:168
#define RAPIDJSON_NEW(x)
! customization point for global new
Definition: rapidjson.h:408
#define RAPIDJSON_ALIGN(x)
Data alignment of the machine.
Definition: rapidjson.h:173
#define RAPIDJSON_DELETE(x)
! customization point for global delete
Definition: rapidjson.h:412
main RapidJSON namespace
Definition: rapidjson.h:241
common definitions and configuration
size_t Size() const
Computes the memory blocks allocated.
Definition: allocators.h:160
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:204
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:269