TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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...
 
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...
 
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 = 64 * 1024
 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

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.
110  :
111  chunkHead_(0), chunk_capacity_(chunkSize), userBuffer_(0), baseAllocator_(baseAllocator), ownBaseAllocator_(0)
112  {
113  }
BaseAllocator * ownBaseAllocator_
base allocator created by this object.
Definition: allocators.h:256
void * userBuffer_
User supplied buffer.
Definition: allocators.h:254
ChunkHeader * chunkHead_
Head of the chunk linked-list. Only the head chunk serves allocation.
Definition: allocators.h:252
BaseAllocator * baseAllocator_
base allocator for allocating memory chunks.
Definition: allocators.h:255
size_t chunk_capacity_
The minimum capacity of chunk when they are allocated.
Definition: allocators.h:253
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.
125  :
126  chunkHead_(0), chunk_capacity_(chunkSize), userBuffer_(buffer), baseAllocator_(baseAllocator), ownBaseAllocator_(0)
127  {
128  RAPIDJSON_ASSERT(buffer != 0);
129  RAPIDJSON_ASSERT(size > sizeof(ChunkHeader));
130  chunkHead_ = reinterpret_cast<ChunkHeader*>(buffer);
131  chunkHead_->capacity = size - sizeof(ChunkHeader);
132  chunkHead_->size = 0;
133  chunkHead_->next = 0;
134  }
BaseAllocator * ownBaseAllocator_
base allocator created by this object.
Definition: allocators.h:256
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:344
void * userBuffer_
User supplied buffer.
Definition: allocators.h:254
ChunkHeader * chunkHead_
Head of the chunk linked-list. Only the head chunk serves allocation.
Definition: allocators.h:252
BaseAllocator * baseAllocator_
base allocator for allocating memory chunks.
Definition: allocators.h:255
size_t size
Current size of allocated memory in bytes.
Definition: allocators.h:248
size_t capacity
Capacity of the chunk in bytes (excluding the header itself).
Definition: allocators.h:247
size_t chunk_capacity_
The minimum capacity of chunk when they are allocated.
Definition: allocators.h:253
ChunkHeader * next
Next chunk in the linked list.
Definition: allocators.h:249
template<typename BaseAllocator = CrtAllocator>
MemoryPoolAllocator< BaseAllocator >::~MemoryPoolAllocator ( )
inline

Destructor.

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

139  {
140  Clear();
142  }
BaseAllocator * ownBaseAllocator_
base allocator created by this object.
Definition: allocators.h:256
void Clear()
Deallocates all memory chunks, excluding the user-supplied buffer.
Definition: allocators.h:145
#define RAPIDJSON_DELETE(x)
! customization point for global delete
Definition: rapidjson.h:484

+ Here is the call graph for this function:

template<typename BaseAllocator = CrtAllocator>
MemoryPoolAllocator< BaseAllocator >::MemoryPoolAllocator ( const MemoryPoolAllocator< BaseAllocator > &  rhs)
private

Copy constructor is not permitted.

Member Function Documentation

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

Creates a new chunk.

Parameters
capacityCapacity of the chunk in bytes.
231  {
232  if (!baseAllocator_)
233  ownBaseAllocator_ = baseAllocator_ = RAPIDJSON_NEW(BaseAllocator());
234  ChunkHeader* chunk = reinterpret_cast<ChunkHeader*>(baseAllocator_->Malloc(sizeof(ChunkHeader) + capacity));
235  chunk->capacity = capacity;
236  chunk->size = 0;
237  chunk->next = chunkHead_;
238  chunkHead_ = chunk;
239  }
BaseAllocator * ownBaseAllocator_
base allocator created by this object.
Definition: allocators.h:256
Definition: adtfile.h:57
ChunkHeader * chunkHead_
Head of the chunk linked-list. Only the head chunk serves allocation.
Definition: allocators.h:252
#define RAPIDJSON_NEW(x)
! customization point for global new
Definition: rapidjson.h:480
BaseAllocator * baseAllocator_
base allocator for allocating memory chunks.
Definition: allocators.h:255

+ Here is the caller graph for this function:

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

Computes the total capacity of allocated memory chunks.

Returns
total capacity in bytes.
158  {
159  size_t capacity = 0;
160  for (ChunkHeader* c = chunkHead_; c != 0; c = c->next)
161  capacity += c->capacity;
162  return capacity;
163  }
ChunkHeader * chunkHead_
Head of the chunk linked-list. Only the head chunk serves allocation.
Definition: allocators.h:252
ChunkHeader * next
Next chunk in the linked list.
Definition: allocators.h:249
template<typename BaseAllocator = CrtAllocator>
void MemoryPoolAllocator< BaseAllocator >::Clear ( )
inline

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

145  {
146  while (chunkHead_ && chunkHead_ != userBuffer_) {
147  ChunkHeader* next = chunkHead_->next;
148  baseAllocator_->Free(chunkHead_);
149  chunkHead_ = next;
150  }
152  chunkHead_->size = 0; // Clear user buffer
153  }
void * userBuffer_
User supplied buffer.
Definition: allocators.h:254
int next(int i, int n)
Definition: RecastContour.cpp:469
ChunkHeader * chunkHead_
Head of the chunk linked-list. Only the head chunk serves allocation.
Definition: allocators.h:252
BaseAllocator * baseAllocator_
base allocator for allocating memory chunks.
Definition: allocators.h:255
size_t size
Current size of allocated memory in bytes.
Definition: allocators.h:248
ChunkHeader * next
Next chunk in the linked list.
Definition: allocators.h:249

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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

Frees a memory block (concept Allocator)

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

Allocates a memory block. (concept Allocator)

176  {
177  if (!size)
178  return NULL;
179 
180  size = RAPIDJSON_ALIGN(size);
181  if (chunkHead_ == 0 || chunkHead_->size + size > chunkHead_->capacity)
182  AddChunk(chunk_capacity_ > size ? chunk_capacity_ : size);
183 
184  void *buffer = reinterpret_cast<char *>(chunkHead_ + 1) + chunkHead_->size;
185  chunkHead_->size += size;
186  return buffer;
187  }
arena_t NULL
Definition: jemalloc_internal.h:624
ChunkHeader * chunkHead_
Head of the chunk linked-list. Only the head chunk serves allocation.
Definition: allocators.h:252
void AddChunk(size_t capacity)
Creates a new chunk.
Definition: allocators.h:231
size_t size
Current size of allocated memory in bytes.
Definition: allocators.h:248
size_t capacity
Capacity of the chunk in bytes (excluding the header itself).
Definition: allocators.h:247
size_t chunk_capacity_
The minimum capacity of chunk when they are allocated.
Definition: allocators.h:253
#define RAPIDJSON_ALIGN(x)
Data alignment of the machine.
Definition: rapidjson.h:247

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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)

190  {
191  if (originalPtr == 0)
192  return Malloc(newSize);
193 
194  if (newSize == 0)
195  return NULL;
196 
197  // Do not shrink if new size is smaller than original
198  if (originalSize >= newSize)
199  return originalPtr;
200 
201  // Simply expand it if it is the last allocation and there is sufficient space
202  if (originalPtr == (char *)(chunkHead_ + 1) + chunkHead_->size - originalSize) {
203  size_t increment = static_cast<size_t>(newSize - originalSize);
204  increment = RAPIDJSON_ALIGN(increment);
205  if (chunkHead_->size + increment <= chunkHead_->capacity) {
206  chunkHead_->size += increment;
207  return originalPtr;
208  }
209  }
210 
211  // Realloc process: allocate and copy memory, do not free original buffer.
212  void* newBuffer = Malloc(newSize);
213  RAPIDJSON_ASSERT(newBuffer != 0); // Do not handle out-of-memory explicitly.
214  if (originalSize)
215  std::memcpy(newBuffer, originalPtr, originalSize);
216  return newBuffer;
217  }
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:344
void * Malloc(size_t size)
Allocates a memory block. (concept Allocator)
Definition: allocators.h:176
arena_t NULL
Definition: jemalloc_internal.h:624
ChunkHeader * chunkHead_
Head of the chunk linked-list. Only the head chunk serves allocation.
Definition: allocators.h:252
size_t size
Current size of allocated memory in bytes.
Definition: allocators.h:248
#define RAPIDJSON_ALIGN(x)
Data alignment of the machine.
Definition: rapidjson.h:247

+ Here is the call graph for this function:

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

Computes the memory blocks allocated.

Returns
total used bytes.
168  {
169  size_t size = 0;
170  for (ChunkHeader* c = chunkHead_; c != 0; c = c->next)
171  size += c->size;
172  return size;
173  }
ChunkHeader * chunkHead_
Head of the chunk linked-list. Only the head chunk serves allocation.
Definition: allocators.h:252
ChunkHeader * next
Next chunk in the linked list.
Definition: allocators.h:249

Member Data Documentation

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

base allocator for allocating memory chunks.

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

The minimum capacity of chunk when they are allocated.

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

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

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

Default chunk capacity.

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

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

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

base allocator created by this object.

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

User supplied buffer.


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