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.
168  :
169  chunkHead_(0), chunk_capacity_(chunkSize), userBuffer_(0), baseAllocator_(baseAllocator), ownBaseAllocator_(0)
170  {
171  if (!baseAllocator_)
172  ownBaseAllocator_ = baseAllocator_ = new BaseAllocator();
174  }
char * userBuffer_
User supplied buffer.
Definition: rapidjson.h:302
size_t chunk_capacity_
The minimum capacity of chunk when they are allocated.
Definition: rapidjson.h:301
ChunkHeader * chunkHead_
Head of the chunk linked-list. Only the head chunk serves allocation.
Definition: rapidjson.h:300
void AddChunk(size_t capacity)
Creates a new chunk.
Definition: rapidjson.h:281
BaseAllocator * baseAllocator_
base allocator for allocating memory chunks.
Definition: rapidjson.h:303
BaseAllocator * ownBaseAllocator_
base allocator created by this object.
Definition: rapidjson.h:304

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.
186  :
187  chunkHead_(0), chunk_capacity_(chunkSize), userBuffer_(buffer), baseAllocator_(baseAllocator), ownBaseAllocator_(0)
188  {
189  RAPIDJSON_ASSERT(buffer != 0);
190  RAPIDJSON_ASSERT(size > sizeof(ChunkHeader));
191  chunkHead_ = (ChunkHeader*)buffer;
192  chunkHead_->capacity = size - sizeof(ChunkHeader);
193  chunkHead_->size = 0;
194  chunkHead_->next = 0;
195  }
Scalar Scalar int size
Definition: benchVecAdd.cpp:17
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:80
size_t capacity
Capacity of the chunk in bytes (excluding the header itself).
Definition: rapidjson.h:295
ChunkHeader * next
Next chunk in the linked list.
Definition: rapidjson.h:297
size_t size
Current size of allocated memory in bytes.
Definition: rapidjson.h:296

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

◆ ~MemoryPoolAllocator()

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

Destructor.

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

200  {
201  Clear();
202  delete ownBaseAllocator_;
203  }
void Clear()
Deallocates all memory chunks, excluding the user-supplied buffer.
Definition: rapidjson.h:206

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.
217  {
218  size_t capacity = 0;
219  for (ChunkHeader* c = chunkHead_; c != 0; c = c->next)
220  capacity += c->capacity;
221  return capacity;
222  }
int c
Definition: calibrate.py:100

References calibrate::c, and rapidjson::MemoryPoolAllocator< BaseAllocator >::chunkHead_.

◆ 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)

275 {} // Do nothing

◆ Malloc()

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

Allocates a memory block. (concept Allocator)

235  {
236  size = (size + 3) & ~3; // Force aligning size to 4
237 
240 
241  char *buffer = (char *)(chunkHead_ + 1) + chunkHead_->size;
242  RAPIDJSON_ASSERT(((uintptr_t)buffer & 3) == 0); // returned buffer is aligned to 4
243  chunkHead_->size += size;
244 
245  return buffer;
246  }

References rapidjson::MemoryPoolAllocator< BaseAllocator >::AddChunk(), rapidjson::MemoryPoolAllocator< BaseAllocator >::ChunkHeader::capacity, rapidjson::MemoryPoolAllocator< BaseAllocator >::chunk_capacity_, rapidjson::MemoryPoolAllocator< BaseAllocator >::chunkHead_, RAPIDJSON_ASSERT, rapidjson::MemoryPoolAllocator< BaseAllocator >::ChunkHeader::size, and 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)

249  {
250  if (originalPtr == 0)
251  return Malloc(newSize);
252 
253  // Do not shrink if new size is smaller than original
254  if (originalSize >= newSize)
255  return originalPtr;
256 
257  // Simply expand it if it is the last allocation and there is sufficient space
258  if (originalPtr == (char *)(chunkHead_ + 1) + chunkHead_->size - originalSize) {
259  size_t increment = newSize - originalSize;
260  increment = (increment + 3) & ~3; // Force aligning size to 4
261  if (chunkHead_->size + increment <= chunkHead_->capacity) {
262  chunkHead_->size += increment;
263  RAPIDJSON_ASSERT(((uintptr_t)originalPtr & 3) == 0); // returned buffer is aligned to 4
264  return originalPtr;
265  }
266  }
267 
268  // Realloc process: allocate and copy memory, do not free original buffer.
269  void* newBuffer = Malloc(newSize);
270  RAPIDJSON_ASSERT(newBuffer != 0); // Do not handle out-of-memory explicitly.
271  return memcpy(newBuffer, originalPtr, originalSize);
272  }
void * Malloc(size_t size)
Allocates a memory block. (concept Allocator)
Definition: rapidjson.h:235

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.
227  {
228  size_t size = 0;
229  for (ChunkHeader* c = chunkHead_; c != 0; c = c->next)
230  size += c->size;
231  return size;
232  }

References calibrate::c, rapidjson::MemoryPoolAllocator< BaseAllocator >::chunkHead_, and size.

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: