Electroneum
MemoryPoolAllocator< BaseAllocator > Class Template Reference

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

#include <allocators.h>

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...
 

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

◆ MemoryPoolAllocator() [1/2]

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  }

◆ MemoryPoolAllocator() [2/2]

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  }
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:411

◆ ~MemoryPoolAllocator()

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();
154  RAPIDJSON_DELETE(ownBaseAllocator_);
155  }
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:607

Member Function Documentation

◆ Capacity()

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  }
Here is the caller graph for this function:

◆ Clear()

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  }
164  if (chunkHead_ && chunkHead_ == userBuffer_)
165  chunkHead_->size = 0; // Clear user buffer
166  }
Here is the caller graph for this function:

◆ Free()

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

◆ Malloc()

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 
193  size = RAPIDJSON_ALIGN(size);
194  if (chunkHead_ == 0 || chunkHead_->size + size > chunkHead_->capacity)
195  if (!AddChunk(chunk_capacity_ > size ? chunk_capacity_ : size))
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  }
#define RAPIDJSON_ALIGN(x)
Data alignment of the machine.
Definition: rapidjson.h:280
Here is the caller graph for this function:

◆ Realloc()

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
#define RAPIDJSON_ALIGN(x)
Data alignment of the machine.
Definition: rapidjson.h:280
void * memcpy(void *a, const void *b, size_t c)

◆ Size()

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  }
Here is the caller graph for this function:

Member Data Documentation

◆ kNeedFree

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.


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