19 #ifndef RAPIDJSON_ALLOCATORS_H_ 20 #define RAPIDJSON_ALLOCATORS_H_ 69 #ifndef RAPIDJSON_ALLOCATOR_DEFAULT_CHUNK_CAPACITY 70 #define RAPIDJSON_ALLOCATOR_DEFAULT_CHUNK_CAPACITY (64 * 1024) 85 return std::malloc(size);
89 void *
Realloc(
void *originalPtr,
size_t originalSize,
size_t newSize) {
92 std::free(originalPtr);
95 return std::realloc(originalPtr, newSize);
97 static void Free(
void *ptr) { std::free(ptr); }
121 template <
typename BaseAllocator = CrtAllocator>
134 BaseAllocator *baseAllocator = 0)
136 chunk_capacity_(chunkSize),
138 baseAllocator_(baseAllocator),
139 ownBaseAllocator_(0) {}
154 size_t chunkSize = kDefaultChunkCapacity,
155 BaseAllocator *baseAllocator = 0)
157 chunk_capacity_(chunkSize),
159 baseAllocator_(baseAllocator),
160 ownBaseAllocator_(0) {
163 chunkHead_ =
reinterpret_cast<ChunkHeader *
>(buffer);
165 chunkHead_->size = 0;
166 chunkHead_->next = 0;
179 while (chunkHead_ && chunkHead_ != userBuffer_) {
181 baseAllocator_->Free(chunkHead_);
184 if (chunkHead_ && chunkHead_ == userBuffer_)
185 chunkHead_->
size = 0;
194 capacity += c->capacity;
203 for (
ChunkHeader *c = chunkHead_; c != 0; c = c->
next) size += c->size;
209 if (!size)
return NULL;
212 if (chunkHead_ == 0 || chunkHead_->size + size > chunkHead_->capacity)
213 if (!AddChunk(chunk_capacity_ > size ? chunk_capacity_ : size))
216 void *buffer =
reinterpret_cast<char *
>(chunkHead_) +
218 chunkHead_->size += size;
223 void *
Realloc(
void *originalPtr,
size_t originalSize,
size_t newSize) {
224 if (originalPtr == 0)
return Malloc(newSize);
226 if (newSize == 0)
return NULL;
232 if (originalSize >= newSize)
return originalPtr;
237 reinterpret_cast<char *>(chunkHead_) +
240 size_t increment =
static_cast<size_t>(newSize - originalSize);
241 if (chunkHead_->size + increment <= chunkHead_->capacity) {
242 chunkHead_->size += increment;
248 if (
void *newBuffer =
Malloc(newSize)) {
249 if (originalSize) std::memcpy(newBuffer, originalPtr, originalSize);
256 static void Free(
void *ptr) { (void)ptr; }
270 ownBaseAllocator_ = baseAllocator_ =
RAPIDJSON_NEW(BaseAllocator)();
272 reinterpret_cast<ChunkHeader *>(baseAllocator_->Malloc(
274 chunk->capacity = capacity;
276 chunk->next = chunkHead_;
283 static const int kDefaultChunkCapacity =
297 size_t chunk_capacity_;
308 #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)
#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.
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)
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.