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) 80 return std::malloc(size);
84 void*
Realloc(
void* originalPtr,
size_t originalSize,
size_t newSize) {
87 std::free(originalPtr);
90 return std::realloc(originalPtr, newSize);
92 static void Free(
void *ptr) { std::free(ptr); }
114 template <
typename BaseAllocator = CrtAllocator>
124 chunkHead_(0), chunk_capacity_(chunkSize), userBuffer_(0), baseAllocator_(baseAllocator), ownBaseAllocator_(0)
139 chunkHead_(0), chunk_capacity_(chunkSize), userBuffer_(buffer), baseAllocator_(baseAllocator), ownBaseAllocator_(0)
143 chunkHead_ =
reinterpret_cast<ChunkHeader*
>(buffer);
145 chunkHead_->size = 0;
146 chunkHead_->next = 0;
159 while (chunkHead_ && chunkHead_ != userBuffer_) {
161 baseAllocator_->Free(chunkHead_);
164 if (chunkHead_ && chunkHead_ == userBuffer_)
165 chunkHead_->
size = 0;
174 capacity +=
c->capacity;
194 if (chunkHead_ == 0 || chunkHead_->size + size > chunkHead_->capacity)
195 if (!AddChunk(chunk_capacity_ > size ? chunk_capacity_ : size))
199 chunkHead_->size += size;
204 void*
Realloc(
void* originalPtr,
size_t originalSize,
size_t newSize) {
205 if (originalPtr == 0)
215 if (originalSize >= newSize)
219 if (originalPtr == reinterpret_cast<char *>(chunkHead_) +
RAPIDJSON_ALIGN(
sizeof(
ChunkHeader)) + chunkHead_->size - originalSize) {
220 size_t increment =
static_cast<size_t>(newSize - originalSize);
221 if (chunkHead_->size + increment <= chunkHead_->capacity) {
222 chunkHead_->size += increment;
228 if (
void* newBuffer =
Malloc(newSize)) {
230 std::memcpy(newBuffer, originalPtr, originalSize);
238 static void Free(
void *ptr) { (void)ptr; }
252 ownBaseAllocator_ = baseAllocator_ =
RAPIDJSON_NEW(BaseAllocator)();
254 chunk->capacity = capacity;
256 chunk->next = chunkHead_;
284 #endif // RAPIDJSON_ENCODINGS_H_ size_t Capacity() const
Computes the total capacity of allocated memory chunks.
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.
decltype(auto) constexpr size(T &&obj)
ADL-aware version of std::size.
#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
#define RAPIDJSON_ALLOCATOR_DEFAULT_CHUNK_CAPACITY
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.