15 #ifndef RAPIDJSON_ALLOCATORS_H_ 16 #define RAPIDJSON_ALLOCATORS_H_ 63 #ifndef RAPIDJSON_ALLOCATOR_DEFAULT_CHUNK_CAPACITY 64 #define RAPIDJSON_ALLOCATOR_DEFAULT_CHUNK_CAPACITY (64 * 1024) 81 return std::malloc(size);
85 void*
Realloc(
void* originalPtr,
size_t originalSize,
size_t newSize)
90 std::free(originalPtr);
93 return std::realloc(originalPtr, newSize);
95 static void Free(
void* ptr)
120 template <
typename BaseAllocator = CrtAllocator>
132 : chunkHead_(0), chunk_capacity_(chunkSize), userBuffer_(0), baseAllocator_(baseAllocator), ownBaseAllocator_(0)
147 BaseAllocator* baseAllocator = 0)
149 , chunk_capacity_(chunkSize)
150 , userBuffer_(buffer)
151 , baseAllocator_(baseAllocator)
152 , ownBaseAllocator_(0)
156 chunkHead_ =
reinterpret_cast<ChunkHeader*
>(buffer);
158 chunkHead_->size = 0;
159 chunkHead_->next = 0;
174 while (chunkHead_ && chunkHead_ != userBuffer_)
177 baseAllocator_->Free(chunkHead_);
180 if (chunkHead_ && chunkHead_ == userBuffer_)
181 chunkHead_->
size = 0;
191 capacity += c->capacity;
213 if (chunkHead_ == 0 || chunkHead_->size + size > chunkHead_->capacity)
214 if (!AddChunk(chunk_capacity_ > size ? chunk_capacity_ : size))
218 chunkHead_->size += size;
223 void*
Realloc(
void* originalPtr,
size_t originalSize,
size_t newSize)
225 if (originalPtr == 0)
235 if (originalSize >= newSize)
242 size_t increment =
static_cast<size_t>(newSize - originalSize);
243 if (chunkHead_->size + increment <= chunkHead_->capacity)
245 chunkHead_->size += increment;
251 if (
void* newBuffer =
Malloc(newSize))
254 std::memcpy(newBuffer, originalPtr, originalSize);
280 ownBaseAllocator_ = baseAllocator_ =
RAPIDJSON_NEW(BaseAllocator)();
284 chunk->capacity = capacity;
286 chunk->next = chunkHead_;
315 #endif // RAPIDJSON_ENCODINGS_H_ size_t Capacity() const
Computes the total capacity of allocated memory chunks.
#define RAPIDJSON_ALLOCATOR_DEFAULT_CHUNK_CAPACITY
User-defined kDefaultChunkCapacity definition.
BaseAllocator * ownBaseAllocator_
base allocator created by this object.
#define RAPIDJSON_ASSERT(x)
Assertion.
void * Malloc(size_t size)
Allocates a memory block. (concept Allocator)
void * userBuffer_
User supplied buffer.
#define RAPIDJSON_NAMESPACE_END
provide custom rapidjson namespace (closing expression)
void * Malloc(size_t size)
size_t Size() const
Computes the memory blocks allocated.
#define RAPIDJSON_NAMESPACE_BEGIN
provide custom rapidjson namespace (opening expression)
void Clear()
Deallocates all memory chunks, excluding the user-supplied buffer.
#define RAPIDJSON_NEW(TypeName)
! customization point for global new
bool AddChunk(size_t capacity)
Creates a new chunk.
ChunkHeader * chunkHead_
Head of the chunk linked-list. Only the head chunk serves allocation.
void * Realloc(void *originalPtr, size_t originalSize, size_t newSize)
#define RAPIDJSON_DELETE(x)
! customization point for global delete
BaseAllocator * baseAllocator_
base allocator for allocating memory chunks.
void * Realloc(void *originalPtr, size_t originalSize, size_t newSize)
Resizes a memory block (concept Allocator)
C-runtime library allocator.
common definitions and configuration
static void Free(void *ptr)
size_t chunk_capacity_
The minimum capacity of chunk when they are allocated.
static void Free(void *ptr)
Frees a memory block (concept Allocator)
static const bool kNeedFree
#define RAPIDJSON_ALIGN(x)
Data alignment of the machine.
MemoryPoolAllocator(size_t chunkSize=kDefaultChunkCapacity, BaseAllocator *baseAllocator=0)
Constructor with chunkSize.
MemoryPoolAllocator(void *buffer, size_t size, size_t chunkSize=kDefaultChunkCapacity, BaseAllocator *baseAllocator=0)
Constructor with user-supplied buffer.
Default memory allocator used by the parser and DOM.
~MemoryPoolAllocator()
Destructor.