rapidjson::MemoryPoolAllocator< BaseAllocator > Class Template Reference

Default memory allocator used by the parser and DOM. More...

#include <rapidjson.h>

+ Inheritance diagram for rapidjson::MemoryPoolAllocator< BaseAllocator >:

Classes

struct  ChunkHeader
 Chunk header for perpending to each chunk. More...
 

Public Member Functions

 MemoryPoolAllocator (size_t chunkSize=kDefaultChunkCapacity, BaseAllocator *baseAllocator=0)
 Constructor with chunkSize. More...
 
 MemoryPoolAllocator (char *buffer, size_t size, size_t chunkSize=kDefaultChunkCapacity, BaseAllocator *baseAllocator=0)
 Constructor with user-supplied buffer. More...
 
 ~MemoryPoolAllocator ()
 Destructor. More...
 
void Clear ()
 Deallocates all memory chunks, excluding the user-supplied buffer. More...
 
size_t Capacity ()
 Computes the total capacity of allocated memory chunks. More...
 
size_t Size ()
 Computes the memory blocks allocated. More...
 
void * Malloc (size_t size)
 Allocates a memory block. (concept Allocator) More...
 
void * Realloc (void *originalPtr, size_t originalSize, size_t newSize)
 Resizes a memory block (concept Allocator) More...
 

Static Public Member Functions

static void Free (void *)
 Frees a memory block (concept Allocator) More...
 

Static Public Attributes

static const bool kNeedFree = false
 Tell users that no need to call Free() with this allocator. (concept Allocator) More...
 

Private Member Functions

void AddChunk (size_t capacity)
 Creates a new chunk. More...
 

Private Attributes

ChunkHeaderchunkHead_
 Head of the chunk linked-list. Only the head chunk serves allocation. More...
 
size_t chunk_capacity_
 The minimum capacity of chunk when they are allocated. More...
 
charuserBuffer_
 User supplied buffer. More...
 
BaseAllocator * baseAllocator_
 base allocator for allocating memory chunks. More...
 
BaseAllocator * ownBaseAllocator_
 base allocator created by this object. More...
 

Static Private Attributes

static const int kDefaultChunkCapacity = 64 * 1024
 Default chunk capacity. More...
 

Detailed Description

template<typename BaseAllocator = CrtAllocator>
class rapidjson::MemoryPoolAllocator< BaseAllocator >

Default memory allocator used by the parser and DOM.

This allocator allocate memory blocks from pre-allocated memory chunks.

It does not free memory blocks. And Realloc() only allocate new memory.

The memory chunks are allocated by BaseAllocator, which is CrtAllocator by default.

User may also supply a buffer as the first chunk.

If the user-buffer is full then additional chunks are allocated by BaseAllocator.

The user-buffer is not deallocated by this allocator.

Template Parameters
BaseAllocatorthe allocator type for allocating memory chunks. Default is CrtAllocator.

Constructor & Destructor Documentation

◆ MemoryPoolAllocator() [1/2]

template<typename BaseAllocator = CrtAllocator>
rapidjson::MemoryPoolAllocator< BaseAllocator >::MemoryPoolAllocator ( size_t  chunkSize = kDefaultChunkCapacity,
BaseAllocator *  baseAllocator = 0 
)
inline

Constructor with chunkSize.

Parameters
chunkSizeThe size of memory chunk. The default is kDefaultChunkSize.
baseAllocatorThe allocator for allocating memory chunks.
193  :
194  chunkHead_(0), chunk_capacity_(chunkSize), userBuffer_(0), baseAllocator_(baseAllocator), ownBaseAllocator_(0)
195  {
196  if (!baseAllocator_)
197  ownBaseAllocator_ = baseAllocator_ = new BaseAllocator();
199  }
char * userBuffer_
User supplied buffer.
Definition: rapidjson.h:327
size_t chunk_capacity_
The minimum capacity of chunk when they are allocated.
Definition: rapidjson.h:326
ChunkHeader * chunkHead_
Head of the chunk linked-list. Only the head chunk serves allocation.
Definition: rapidjson.h:325
void AddChunk(size_t capacity)
Creates a new chunk.
Definition: rapidjson.h:306
BaseAllocator * baseAllocator_
base allocator for allocating memory chunks.
Definition: rapidjson.h:328
BaseAllocator * ownBaseAllocator_
base allocator created by this object.
Definition: rapidjson.h:329

References rapidjson::MemoryPoolAllocator< BaseAllocator >::AddChunk(), rapidjson::MemoryPoolAllocator< BaseAllocator >::baseAllocator_, rapidjson::MemoryPoolAllocator< BaseAllocator >::chunk_capacity_, and rapidjson::MemoryPoolAllocator< BaseAllocator >::ownBaseAllocator_.

◆ MemoryPoolAllocator() [2/2]

template<typename BaseAllocator = CrtAllocator>
rapidjson::MemoryPoolAllocator< BaseAllocator >::MemoryPoolAllocator ( char buffer,
size_t  size,
size_t  chunkSize = kDefaultChunkCapacity,
BaseAllocator *  baseAllocator = 0 
)
inline

Constructor with user-supplied buffer.

The user buffer will be used firstly. When it is full, memory pool allocates new chunk with chunk size.

The user buffer will not be deallocated when this allocator is destructed.

Parameters
bufferUser supplied buffer.
sizeSize of the buffer in bytes. It must at least larger than sizeof(ChunkHeader).
chunkSizeThe size of memory chunk. The default is kDefaultChunkSize.
baseAllocatorThe allocator for allocating memory chunks.
211  :
212  chunkHead_(0), chunk_capacity_(chunkSize), userBuffer_(buffer), baseAllocator_(baseAllocator), ownBaseAllocator_(0)
213  {
214  RAPIDJSON_ASSERT(buffer != 0);
215  RAPIDJSON_ASSERT(size > sizeof(ChunkHeader));
216  chunkHead_ = (ChunkHeader*)buffer;
217  chunkHead_->capacity = size - sizeof(ChunkHeader);
218  chunkHead_->size = 0;
219  chunkHead_->next = 0;
220  }
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:105
size_t capacity
Capacity of the chunk in bytes (excluding the header itself).
Definition: rapidjson.h:320
ChunkHeader * next
Next chunk in the linked list.
Definition: rapidjson.h:322
size_t size
Current size of allocated memory in bytes.
Definition: rapidjson.h:321

References rapidjson::MemoryPoolAllocator< BaseAllocator >::ChunkHeader::capacity, rapidjson::MemoryPoolAllocator< BaseAllocator >::chunkHead_, rapidjson::MemoryPoolAllocator< BaseAllocator >::ChunkHeader::next, RAPIDJSON_ASSERT, and rapidjson::MemoryPoolAllocator< BaseAllocator >::ChunkHeader::size.

◆ ~MemoryPoolAllocator()

template<typename BaseAllocator = CrtAllocator>
rapidjson::MemoryPoolAllocator< BaseAllocator >::~MemoryPoolAllocator ( )
inline

Destructor.

This deallocates all memory chunks, excluding the user-supplied buffer.

225  {
226  Clear();
227  delete ownBaseAllocator_;
228  }
void Clear()
Deallocates all memory chunks, excluding the user-supplied buffer.
Definition: rapidjson.h:231

References rapidjson::MemoryPoolAllocator< BaseAllocator >::Clear(), and rapidjson::MemoryPoolAllocator< BaseAllocator >::ownBaseAllocator_.

Member Function Documentation

◆ AddChunk()

template<typename BaseAllocator = CrtAllocator>
void rapidjson::MemoryPoolAllocator< BaseAllocator >::AddChunk ( size_t  capacity)
inlineprivate

◆ Capacity()

template<typename BaseAllocator = CrtAllocator>
size_t rapidjson::MemoryPoolAllocator< BaseAllocator >::Capacity ( )
inline

Computes the total capacity of allocated memory chunks.

Returns
total capacity in bytes.
242  {
243  size_t capacity = 0;
244  for (ChunkHeader* c = chunkHead_; c != 0; c = c->next)
245  capacity += c->capacity;
246  return capacity;
247  }

References rapidjson::MemoryPoolAllocator< BaseAllocator >::chunkHead_, and rapidjson::MemoryPoolAllocator< BaseAllocator >::ChunkHeader::next.

◆ Clear()

template<typename BaseAllocator = CrtAllocator>
void rapidjson::MemoryPoolAllocator< BaseAllocator >::Clear ( )
inline

◆ Free()

template<typename BaseAllocator = CrtAllocator>
static void rapidjson::MemoryPoolAllocator< BaseAllocator >::Free ( void *  )
inlinestatic

Frees a memory block (concept Allocator)

300 {} // Do nothing

◆ Malloc()

template<typename BaseAllocator = CrtAllocator>
void* rapidjson::MemoryPoolAllocator< BaseAllocator >::Malloc ( size_t  size)
inline

Allocates a memory block. (concept Allocator)

260  {
261  size = (size + 3) & ~3; // Force aligning size to 4
262 
263  if (chunkHead_->size + size > chunkHead_->capacity)
264  AddChunk(chunk_capacity_ > size ? chunk_capacity_ : size);
265 
266  char *buffer = (char *)(chunkHead_ + 1) + chunkHead_->size;
267  RAPIDJSON_ASSERT(((uintptr_t)buffer & 3) == 0); // returned buffer is aligned to 4
268  chunkHead_->size += size;
269 
270  return buffer;
271  }

References rapidjson::MemoryPoolAllocator< BaseAllocator >::AddChunk(), rapidjson::MemoryPoolAllocator< BaseAllocator >::ChunkHeader::capacity, rapidjson::MemoryPoolAllocator< BaseAllocator >::chunk_capacity_, rapidjson::MemoryPoolAllocator< BaseAllocator >::chunkHead_, RAPIDJSON_ASSERT, and rapidjson::MemoryPoolAllocator< BaseAllocator >::ChunkHeader::size.

Referenced by rapidjson::MemoryPoolAllocator< BaseAllocator >::Realloc().

◆ Realloc()

template<typename BaseAllocator = CrtAllocator>
void* rapidjson::MemoryPoolAllocator< BaseAllocator >::Realloc ( void *  originalPtr,
size_t  originalSize,
size_t  newSize 
)
inline

Resizes a memory block (concept Allocator)

274  {
275  if (originalPtr == 0)
276  return Malloc(newSize);
277 
278  // Do not shrink if new size is smaller than original
279  if (originalSize >= newSize)
280  return originalPtr;
281 
282  // Simply expand it if it is the last allocation and there is sufficient space
283  if (originalPtr == (char *)(chunkHead_ + 1) + chunkHead_->size - originalSize) {
284  size_t increment = newSize - originalSize;
285  increment = (increment + 3) & ~3; // Force aligning size to 4
286  if (chunkHead_->size + increment <= chunkHead_->capacity) {
287  chunkHead_->size += increment;
288  RAPIDJSON_ASSERT(((uintptr_t)originalPtr & 3) == 0); // returned buffer is aligned to 4
289  return originalPtr;
290  }
291  }
292 
293  // Realloc process: allocate and copy memory, do not free original buffer.
294  void* newBuffer = Malloc(newSize);
295  RAPIDJSON_ASSERT(newBuffer != 0); // Do not handle out-of-memory explicitly.
296  return memcpy(newBuffer, originalPtr, originalSize);
297  }
void * Malloc(size_t size)
Allocates a memory block. (concept Allocator)
Definition: rapidjson.h:260

References rapidjson::MemoryPoolAllocator< BaseAllocator >::chunkHead_, rapidjson::MemoryPoolAllocator< BaseAllocator >::Malloc(), RAPIDJSON_ASSERT, and rapidjson::MemoryPoolAllocator< BaseAllocator >::ChunkHeader::size.

◆ Size()

template<typename BaseAllocator = CrtAllocator>
size_t rapidjson::MemoryPoolAllocator< BaseAllocator >::Size ( )
inline

Computes the memory blocks allocated.

Returns
total used bytes.
252  {
253  size_t size = 0;
254  for (ChunkHeader* c = chunkHead_; c != 0; c = c->next)
255  size += c->size;
256  return size;
257  }

References rapidjson::MemoryPoolAllocator< BaseAllocator >::chunkHead_, and rapidjson::MemoryPoolAllocator< BaseAllocator >::ChunkHeader::next.

Member Data Documentation

◆ baseAllocator_

template<typename BaseAllocator = CrtAllocator>
BaseAllocator* rapidjson::MemoryPoolAllocator< BaseAllocator >::baseAllocator_
private

◆ chunk_capacity_

template<typename BaseAllocator = CrtAllocator>
size_t rapidjson::MemoryPoolAllocator< BaseAllocator >::chunk_capacity_
private

◆ chunkHead_

◆ kDefaultChunkCapacity

template<typename BaseAllocator = CrtAllocator>
const int rapidjson::MemoryPoolAllocator< BaseAllocator >::kDefaultChunkCapacity = 64 * 1024
staticprivate

Default chunk capacity.

◆ kNeedFree

template<typename BaseAllocator = CrtAllocator>
const bool rapidjson::MemoryPoolAllocator< BaseAllocator >::kNeedFree = false
static

Tell users that no need to call Free() with this allocator. (concept Allocator)

◆ ownBaseAllocator_

template<typename BaseAllocator = CrtAllocator>
BaseAllocator* rapidjson::MemoryPoolAllocator< BaseAllocator >::ownBaseAllocator_
private

◆ userBuffer_

template<typename BaseAllocator = CrtAllocator>
char* rapidjson::MemoryPoolAllocator< BaseAllocator >::userBuffer_
private

User supplied buffer.

Referenced by rapidjson::MemoryPoolAllocator< BaseAllocator >::Clear().


The documentation for this class was generated from the following file: