15 #ifndef RAPIDJSON_ALLOCATORS_H_ 16 #define RAPIDJSON_ALLOCATORS_H_ 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>
111 chunkHead_(0), chunk_capacity_(chunkSize), userBuffer_(0), baseAllocator_(baseAllocator), ownBaseAllocator_(0)
126 chunkHead_(0), chunk_capacity_(chunkSize), userBuffer_(buffer), baseAllocator_(baseAllocator), ownBaseAllocator_(0)
132 chunkHead_->size = 0;
133 chunkHead_->next = 0;
146 while (chunkHead_ && chunkHead_ != userBuffer_) {
148 baseAllocator_->Free(chunkHead_);
151 if (chunkHead_ && chunkHead_ == userBuffer_)
152 chunkHead_->
size = 0;
161 capacity +=
c->capacity;
181 if (chunkHead_ == 0 || chunkHead_->size + size > chunkHead_->capacity)
182 AddChunk(chunk_capacity_ > size ? chunk_capacity_ : size);
185 chunkHead_->size += size;
190 void*
Realloc(
void* originalPtr,
size_t originalSize,
size_t newSize) {
191 if (originalPtr == 0)
198 if (originalSize >= newSize)
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);
233 ownBaseAllocator_ = baseAllocator_ =
RAPIDJSON_NEW(BaseAllocator());
237 chunk->
next = chunkHead_;
241 static const int kDefaultChunkCapacity = 64 * 1024;
261 #endif // RAPIDJSON_ENCODINGS_H_ ChunkHeader * chunkHead_
Head of the chunk linked-list. Only the head chunk serves allocation.
void * Realloc(void *originalPtr, size_t originalSize, size_t newSize)
void * Malloc(size_t size)
Allocates a memory block. (concept Allocator)
static void Free(void *ptr)
Frees a memory block (concept Allocator)
C-runtime library allocator.
void Clear()
Deallocates all memory chunks, excluding the user-supplied buffer.
void * Realloc(void *originalPtr, size_t originalSize, size_t newSize)
Resizes a memory block (concept Allocator)
MemoryPoolAllocator(void *buffer, size_t size, size_t chunkSize=kDefaultChunkCapacity, BaseAllocator *baseAllocator=0)
Constructor with user-supplied buffer.
#define RAPIDJSON_NAMESPACE_BEGIN
provide custom rapidjson namespace (opening expression)
typedef void(GLAPIENTRY *PFNGLCOPYTEXSUBIMAGE3DPROC)(GLenum target
size_t Capacity() const
Computes the total capacity of allocated memory chunks.
~MemoryPoolAllocator()
Destructor.
void * Malloc(size_t size)
Default memory allocator used by the parser and DOM.
#define RAPIDJSON_NEW(x)
! customization point for global new
#define RAPIDJSON_NAMESPACE_END
provide custom rapidjson namespace (closing expression)
#define RAPIDJSON_ALIGN(x)
Data alignment of the machine.
#define RAPIDJSON_DELETE(x)
! customization point for global delete
static const bool kNeedFree
BaseAllocator * baseAllocator_
base allocator for allocating memory chunks.
void AddChunk(size_t capacity)
Creates a new chunk.
static void Free(void *ptr)
MemoryPoolAllocator(size_t chunkSize=kDefaultChunkCapacity, BaseAllocator *baseAllocator=0)
Constructor with chunkSize.
common definitions and configuration
size_t Size() const
Computes the memory blocks allocated.
void * userBuffer_
User supplied buffer.
BaseAllocator * ownBaseAllocator_
base allocator created by this object.
size_t chunk_capacity_
The minimum capacity of chunk when they are allocated.
#define RAPIDJSON_ASSERT(x)
Assertion.