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

size_t Capacity () const
 Computes the total capacity of allocated memory chunks. More...
 
void Clear ()
 Deallocates all memory chunks, excluding the user-supplied buffer. More...
 
void * Malloc (size_t size)
 Allocates a memory block. (concept Allocator) More...
 
 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...
 
void * Realloc (void *originalPtr, size_t originalSize, size_t newSize)
 Resizes a memory block (concept Allocator) More...
 
size_t Size () const
 Computes the memory blocks allocated. More...
 
 ~MemoryPoolAllocator ()
 Destructor. More...
 

Static Public Member Functions

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

Static Public Attributes

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

Private Member Functions

bool AddChunk (size_t capacity)
 Creates a new chunk. More...
 
 MemoryPoolAllocator (const MemoryPoolAllocator &rhs)
 Copy constructor is not permitted. More...
 
MemoryPoolAllocatoroperator= (const MemoryPoolAllocator &rhs)
 Copy assignment operator is not permitted. More...
 

Private Attributes

BaseAllocator * baseAllocator_
 base allocator for allocating memory chunks. More...
 
size_t chunk_capacity_
 The minimum capacity of chunk when they are allocated. More...
 
ChunkHeaderchunkHead_
 Head of the chunk linked-list. Only the head chunk serves allocation. More...
 
BaseAllocator * ownBaseAllocator_
 base allocator created by this object. More...
 
void * userBuffer_
 User supplied buffer. 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 121 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 131 of file allocators.h.

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 146 of file allocators.h.

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

Destructor.

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

Definition at line 165 of file allocators.h.

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 277 of file allocators.h.

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 187 of file allocators.h.

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

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

Definition at line 172 of file allocators.h.

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

Frees a memory block (concept Allocator)

Definition at line 262 of file allocators.h.

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

Allocates a memory block. (concept Allocator)

Definition at line 207 of file allocators.h.

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 223 of file allocators.h.

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

Computes the memory blocks allocated.

Returns
total used bytes.

Definition at line 198 of file allocators.h.

Member Data Documentation

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

base allocator for allocating memory chunks.

Definition at line 309 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 307 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 306 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 294 of file allocators.h.

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

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

Definition at line 124 of file allocators.h.

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

base allocator created by this object.

Definition at line 310 of file allocators.h.

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

User supplied buffer.

Definition at line 308 of file allocators.h.


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


xbot_talker
Author(s): wangxiaoyun
autogenerated on Sat Oct 10 2020 03:27:54