Classes | Public Member Functions | Static Public Member Functions | Static Public Attributes | Private Member Functions | Private Attributes | Static Private Attributes | List of all members
MemoryPoolAllocator< BaseAllocator > Class Template Reference

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

#include <allocators.h>

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 (void *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 () const
 Computes the total capacity of allocated memory chunks. More...
 
size_t Size () const
 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 *ptr)
 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

 MemoryPoolAllocator (const MemoryPoolAllocator &rhs)
 Copy constructor is not permitted. More...
 
MemoryPoolAllocatoroperator= (const MemoryPoolAllocator &rhs)
 Copy assignment operator is not permitted. More...
 
bool 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...
 
void * userBuffer_
 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 = RAPIDJSON_ALLOCATOR_DEFAULT_CHUNK_CAPACITY
 Default chunk capacity. More...
 

Detailed Description

template<typename BaseAllocator = CrtAllocator>
class 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.
Note
implements Allocator concept

Definition at line 115 of file allocators.h.

Constructor & Destructor Documentation

template<typename BaseAllocator = CrtAllocator>
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.

Definition at line 123 of file allocators.h.

123  :
124  chunkHead_(0), chunk_capacity_(chunkSize), userBuffer_(0), baseAllocator_(baseAllocator), ownBaseAllocator_(0)
125  {
126  }
BaseAllocator * ownBaseAllocator_
base allocator created by this object.
Definition: allocators.h:279
void * userBuffer_
User supplied buffer.
Definition: allocators.h:277
ChunkHeader * chunkHead_
Head of the chunk linked-list. Only the head chunk serves allocation.
Definition: allocators.h:275
BaseAllocator * baseAllocator_
base allocator for allocating memory chunks.
Definition: allocators.h:278
size_t chunk_capacity_
The minimum capacity of chunk when they are allocated.
Definition: allocators.h:276
template<typename BaseAllocator = CrtAllocator>
MemoryPoolAllocator< BaseAllocator >::MemoryPoolAllocator ( void *  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.

Definition at line 138 of file allocators.h.

138  :
139  chunkHead_(0), chunk_capacity_(chunkSize), userBuffer_(buffer), baseAllocator_(baseAllocator), ownBaseAllocator_(0)
140  {
141  RAPIDJSON_ASSERT(buffer != 0);
142  RAPIDJSON_ASSERT(size > sizeof(ChunkHeader));
143  chunkHead_ = reinterpret_cast<ChunkHeader*>(buffer);
144  chunkHead_->capacity = size - sizeof(ChunkHeader);
145  chunkHead_->size = 0;
146  chunkHead_->next = 0;
147  }
BaseAllocator * ownBaseAllocator_
base allocator created by this object.
Definition: allocators.h:279
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:406
void * userBuffer_
User supplied buffer.
Definition: allocators.h:277
decltype(auto) constexpr size(T &&obj)
ADL-aware version of std::size.
Definition: StdUtils.h:92
ChunkHeader * chunkHead_
Head of the chunk linked-list. Only the head chunk serves allocation.
Definition: allocators.h:275
BaseAllocator * baseAllocator_
base allocator for allocating memory chunks.
Definition: allocators.h:278
size_t size
Current size of allocated memory in bytes.
Definition: allocators.h:271
size_t capacity
Capacity of the chunk in bytes (excluding the header itself).
Definition: allocators.h:270
size_t chunk_capacity_
The minimum capacity of chunk when they are allocated.
Definition: allocators.h:276
ChunkHeader * next
Next chunk in the linked list.
Definition: allocators.h:272
template<typename BaseAllocator = CrtAllocator>
MemoryPoolAllocator< BaseAllocator >::~MemoryPoolAllocator ( )
inline

Destructor.

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

Definition at line 152 of file allocators.h.

152  {
153  Clear();
155  }
BaseAllocator * ownBaseAllocator_
base allocator created by this object.
Definition: allocators.h:279
void Clear()
Deallocates all memory chunks, excluding the user-supplied buffer.
Definition: allocators.h:158
#define RAPIDJSON_DELETE(x)
! customization point for global delete
Definition: rapidjson.h:605
template<typename BaseAllocator = CrtAllocator>
MemoryPoolAllocator< BaseAllocator >::MemoryPoolAllocator ( const MemoryPoolAllocator< BaseAllocator > &  rhs)
private

Copy constructor is not permitted.

Member Function Documentation

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

Creates a new chunk.

Parameters
capacityCapacity of the chunk in bytes.
Returns
true if success.

Definition at line 250 of file allocators.h.

250  {
251  if (!baseAllocator_)
252  ownBaseAllocator_ = baseAllocator_ = RAPIDJSON_NEW(BaseAllocator)();
253  if (ChunkHeader* chunk = reinterpret_cast<ChunkHeader*>(baseAllocator_->Malloc(RAPIDJSON_ALIGN(sizeof(ChunkHeader)) + capacity))) {
254  chunk->capacity = capacity;
255  chunk->size = 0;
256  chunk->next = chunkHead_;
257  chunkHead_ = chunk;
258  return true;
259  }
260  else
261  return false;
262  }
BaseAllocator * ownBaseAllocator_
base allocator created by this object.
Definition: allocators.h:279
#define RAPIDJSON_NEW(TypeName)
! customization point for global new
Definition: rapidjson.h:601
ChunkHeader * chunkHead_
Head of the chunk linked-list. Only the head chunk serves allocation.
Definition: allocators.h:275
BaseAllocator * baseAllocator_
base allocator for allocating memory chunks.
Definition: allocators.h:278
#define RAPIDJSON_ALIGN(x)
Data alignment of the machine.
Definition: rapidjson.h:276
template<typename BaseAllocator = CrtAllocator>
size_t MemoryPoolAllocator< BaseAllocator >::Capacity ( ) const
inline

Computes the total capacity of allocated memory chunks.

Returns
total capacity in bytes.

Definition at line 171 of file allocators.h.

171  {
172  size_t capacity = 0;
173  for (ChunkHeader* c = chunkHead_; c != 0; c = c->next)
174  capacity += c->capacity;
175  return capacity;
176  }
ChunkHeader * chunkHead_
Head of the chunk linked-list. Only the head chunk serves allocation.
Definition: allocators.h:275
template<typename BaseAllocator = CrtAllocator>
void MemoryPoolAllocator< BaseAllocator >::Clear ( )
inline

Deallocates all memory chunks, excluding the user-supplied buffer.

Definition at line 158 of file allocators.h.

158  {
159  while (chunkHead_ && chunkHead_ != userBuffer_) {
160  ChunkHeader* next = chunkHead_->next;
161  baseAllocator_->Free(chunkHead_);
162  chunkHead_ = next;
163  }
165  chunkHead_->size = 0; // Clear user buffer
166  }
void * userBuffer_
User supplied buffer.
Definition: allocators.h:277
ChunkHeader * chunkHead_
Head of the chunk linked-list. Only the head chunk serves allocation.
Definition: allocators.h:275
BaseAllocator * baseAllocator_
base allocator for allocating memory chunks.
Definition: allocators.h:278
size_t size
Current size of allocated memory in bytes.
Definition: allocators.h:271
ChunkHeader * next
Next chunk in the linked list.
Definition: allocators.h:272
template<typename BaseAllocator = CrtAllocator>
static void MemoryPoolAllocator< BaseAllocator >::Free ( void *  ptr)
inlinestatic

Frees a memory block (concept Allocator)

Definition at line 238 of file allocators.h.

238 { (void)ptr; } // Do nothing
template<typename BaseAllocator = CrtAllocator>
void* MemoryPoolAllocator< BaseAllocator >::Malloc ( size_t  size)
inline

Allocates a memory block. (concept Allocator)

Definition at line 189 of file allocators.h.

189  {
190  if (!size)
191  return NULL;
192 
194  if (chunkHead_ == 0 || chunkHead_->size + size > chunkHead_->capacity)
196  return NULL;
197 
198  void *buffer = reinterpret_cast<char *>(chunkHead_) + RAPIDJSON_ALIGN(sizeof(ChunkHeader)) + chunkHead_->size;
199  chunkHead_->size += size;
200  return buffer;
201  }
decltype(auto) constexpr size(T &&obj)
ADL-aware version of std::size.
Definition: StdUtils.h:92
bool AddChunk(size_t capacity)
Creates a new chunk.
Definition: allocators.h:250
ChunkHeader * chunkHead_
Head of the chunk linked-list. Only the head chunk serves allocation.
Definition: allocators.h:275
size_t size
Current size of allocated memory in bytes.
Definition: allocators.h:271
size_t capacity
Capacity of the chunk in bytes (excluding the header itself).
Definition: allocators.h:270
size_t chunk_capacity_
The minimum capacity of chunk when they are allocated.
Definition: allocators.h:276
#define RAPIDJSON_ALIGN(x)
Data alignment of the machine.
Definition: rapidjson.h:276
template<typename BaseAllocator = CrtAllocator>
MemoryPoolAllocator& MemoryPoolAllocator< BaseAllocator >::operator= ( const MemoryPoolAllocator< BaseAllocator > &  rhs)
private

Copy assignment operator is not permitted.

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

Resizes a memory block (concept Allocator)

Definition at line 204 of file allocators.h.

204  {
205  if (originalPtr == 0)
206  return Malloc(newSize);
207 
208  if (newSize == 0)
209  return NULL;
210 
211  originalSize = RAPIDJSON_ALIGN(originalSize);
212  newSize = RAPIDJSON_ALIGN(newSize);
213 
214  // Do not shrink if new size is smaller than original
215  if (originalSize >= newSize)
216  return originalPtr;
217 
218  // Simply expand it if it is the last allocation and there is sufficient space
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;
223  return originalPtr;
224  }
225  }
226 
227  // Realloc process: allocate and copy memory, do not free original buffer.
228  if (void* newBuffer = Malloc(newSize)) {
229  if (originalSize)
230  std::memcpy(newBuffer, originalPtr, originalSize);
231  return newBuffer;
232  }
233  else
234  return NULL;
235  }
void * Malloc(size_t size)
Allocates a memory block. (concept Allocator)
Definition: allocators.h:189
ChunkHeader * chunkHead_
Head of the chunk linked-list. Only the head chunk serves allocation.
Definition: allocators.h:275
size_t size
Current size of allocated memory in bytes.
Definition: allocators.h:271
#define RAPIDJSON_ALIGN(x)
Data alignment of the machine.
Definition: rapidjson.h:276
template<typename BaseAllocator = CrtAllocator>
size_t MemoryPoolAllocator< BaseAllocator >::Size ( ) const
inline

Computes the memory blocks allocated.

Returns
total used bytes.

Definition at line 181 of file allocators.h.

181  {
182  size_t size = 0;
183  for (ChunkHeader* c = chunkHead_; c != 0; c = c->next)
184  size += c->size;
185  return size;
186  }
decltype(auto) constexpr size(T &&obj)
ADL-aware version of std::size.
Definition: StdUtils.h:92
ChunkHeader * chunkHead_
Head of the chunk linked-list. Only the head chunk serves allocation.
Definition: allocators.h:275

Member Data Documentation

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

base allocator for allocating memory chunks.

Definition at line 278 of file allocators.h.

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

The minimum capacity of chunk when they are allocated.

Definition at line 276 of file allocators.h.

template<typename BaseAllocator = CrtAllocator>
ChunkHeader* MemoryPoolAllocator< BaseAllocator >::chunkHead_
private

Head of the chunk linked-list. Only the head chunk serves allocation.

Definition at line 275 of file allocators.h.

template<typename BaseAllocator = CrtAllocator>
const int MemoryPoolAllocator< BaseAllocator >::kDefaultChunkCapacity = RAPIDJSON_ALLOCATOR_DEFAULT_CHUNK_CAPACITY
staticprivate

Default chunk capacity.

Definition at line 264 of file allocators.h.

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

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

Definition at line 117 of file allocators.h.

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

base allocator created by this object.

Definition at line 279 of file allocators.h.

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

User supplied buffer.

Definition at line 277 of file allocators.h.


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