DART 6.13.2
Loading...
Searching...
No Matches
dart::common::PoolAllocator Class Reference

Memory allocator optimized for allocating many objects of the same or similar sizes. More...

#include <PoolAllocator.hpp>

Inheritance diagram for dart::common::PoolAllocator:
dart::common::MemoryAllocator dart::common::Castable< MemoryAllocator >

Classes

struct  MemoryBlock
 
struct  MemoryUnit
 

Public Types

using Debug = MemoryAllocatorDebugger< PoolAllocator >
 

Public Member Functions

 PoolAllocator (MemoryAllocator &baseAllocator=MemoryAllocator::GetDefault())
 Constructor.
 
 ~PoolAllocator () override
 Destructor.
 
const std::string & getType () const override
 Returns type string.
 
void _ANONYMOUS_FUNCTION_1 ()
 
const MemoryAllocatorgetBaseAllocator () const
 Returns the base allocator.
 
MemoryAllocatorgetBaseAllocator ()
 Returns the base allocator.
 
int getNumAllocatedMemoryBlocks () const
 Returns the count of allocated memory blocks.
 
void * allocate (size_t bytes) noexcept override
 Allocates size bytes of uninitialized storage.
 
void deallocate (void *pointer, size_t bytes) override
 Deallocates the storage referenced by the pointer p, which must be a pointer obtained by an earlier cal to allocate().
 
void print (std::ostream &os=std::cout, int indent=0) const override
 Prints state of the memory allocator.
 
template<typename T >
T * allocateAs (size_t n=1) noexcept
 Allocates object(s) without calling the constructor.
 
template<typename T , typename... Args>
T * construct (Args &&... args) noexcept
 Allocates uninitialized storage and constructs an object of type T to the allocated storage.
 
template<typename T , typename... Args>
T * constructAt (void *pointer, Args &&... args)
 
template<typename T , typename... Args>
T * constructAt (T *pointer, Args &&... args)
 
template<typename T >
void destroy (T *object) noexcept
 Calls the destructor of the object and deallocate the storage.
 
bool is () const
 Returns true if the types of this Base and the template parameter (a base class) are identical.
 
const Derived * as () const
 Casts to pointer of Derived if Base is its base class.
 
Derived * as ()
 Casts to pointer of Derived if Base is its base class.
 
const Derived & asRef () const
 Casts to reference of Derived if Base is its base class. UB otherwise.
 
Derived & asRef ()
 Casts to reference of Derived if Base is its base class. UB otherwise.
 

Static Public Member Functions

static const std::string & getStaticType ()
 Returns type string.
 
static MemoryAllocatorGetDefault ()
 Returns the default memory allocator.
 

Private Member Functions

const MemoryAllocatorbase () const
 Casts to Base const-reference.
 
MemoryAllocatorbase ()
 Casts to Base reference.
 

Private Attributes

MemoryAllocatormBaseAllocator
 The base allocator to allocate memory chunk.
 
std::mutex mMutex
 Mutex for for mNumAllocatedMemoryBlocks, mNumMemoryBlocks, mFreeMemoryUnits, and mAllocatedMemoryBlocks.
 
MemoryBlockmMemoryBlocks
 The array of memory blocks.
 
int mMemoryBlocksSize
 The size of mMemoryBlocks.
 
int mCurrentMemoryBlockIndex
 The count of the allocated memory blocks in use.
 
std::array< MemoryUnit *, HEAP_COUNTmFreeMemoryUnits
 List of free memory units.
 

Static Private Attributes

static constexpr int HEAP_COUNT = 128
 
static constexpr size_t MAX_UNIT_SIZE = 1024
 
static constexpr size_t BLOCK_SIZE = 16 * MAX_UNIT_SIZE
 
static std::array< size_t, HEAP_COUNTmUnitSizes
 
static std::array< int, MAX_UNIT_SIZE+1 > mMapSizeToHeapIndex
 
static bool mInitialized = false
 

Detailed Description

Memory allocator optimized for allocating many objects of the same or similar sizes.

Member Typedef Documentation

◆ Debug

Constructor & Destructor Documentation

◆ PoolAllocator()

dart::common::PoolAllocator::PoolAllocator ( MemoryAllocator baseAllocator = MemoryAllocator::GetDefault())
explicit

Constructor.

Parameters
[in]baseAllocator(optional) Base memory allocator.
[in]initialAllocation(optional) Bytes to initially allocate.

◆ ~PoolAllocator()

dart::common::PoolAllocator::~PoolAllocator ( )
override

Destructor.

Member Function Documentation

◆ _ANONYMOUS_FUNCTION_1()

void dart::common::PoolAllocator::_ANONYMOUS_FUNCTION_1 ( )

◆ allocate()

void * dart::common::PoolAllocator::allocate ( size_t  bytes)
overridevirtualnoexcept

Allocates size bytes of uninitialized storage.

Parameters
[in]bytesThe byte size to allocate sotrage for.
Returns
On success, the pointer to the beginning of newly allocated memory.
On failure, a null pointer

Implements dart::common::MemoryAllocator.

◆ allocateAs()

template<typename T >
T * dart::common::MemoryAllocator::allocateAs ( size_t  n = 1)
noexceptinherited

Allocates object(s) without calling the constructor.

This is identical to static_cast<T*>(allocate(n * sizeof(T))).

Parameters
[in]nThe number of objects to allocate.

◆ as() [1/2]

Derived * dart::common::Castable< MemoryAllocator >::as ( )
inherited

Casts to pointer of Derived if Base is its base class.

Returns nullptr otherwise.

◆ as() [2/2]

const Derived * dart::common::Castable< MemoryAllocator >::as ( ) const
inherited

Casts to pointer of Derived if Base is its base class.

Returns nullptr otherwise.

◆ asRef() [1/2]

Derived & dart::common::Castable< MemoryAllocator >::asRef ( )
inherited

Casts to reference of Derived if Base is its base class. UB otherwise.

◆ asRef() [2/2]

const Derived & dart::common::Castable< MemoryAllocator >::asRef ( ) const
inherited

Casts to reference of Derived if Base is its base class. UB otherwise.

◆ base() [1/2]

MemoryAllocator & dart::common::Castable< MemoryAllocator >::base ( )
privateinherited

Casts to Base reference.

◆ base() [2/2]

const MemoryAllocator & dart::common::Castable< MemoryAllocator >::base ( ) const
privateinherited

Casts to Base const-reference.

◆ construct()

template<typename T , typename... Args>
T * dart::common::MemoryAllocator::construct ( Args &&...  args)
noexceptinherited

Allocates uninitialized storage and constructs an object of type T to the allocated storage.

Template Parameters
TThe object type to construct.
Args...The argument types to pass to the object constructor.
Parameters
[in]argsThe constructor arguments to use.

◆ constructAt() [1/2]

template<typename T , typename... Args>
T * dart::common::MemoryAllocator::constructAt ( T *  pointer,
Args &&...  args 
)
inherited

◆ constructAt() [2/2]

template<typename T , typename... Args>
T * dart::common::MemoryAllocator::constructAt ( void *  pointer,
Args &&...  args 
)
inherited

◆ deallocate()

void dart::common::PoolAllocator::deallocate ( void *  pointer,
size_t  bytes 
)
overridevirtual

Deallocates the storage referenced by the pointer p, which must be a pointer obtained by an earlier cal to allocate().

Parameters
[in]pointerPointer obtained from allocate().
[in]bytesThe bytes of the allocated memory.

Implements dart::common::MemoryAllocator.

◆ destroy()

template<typename T >
void dart::common::MemoryAllocator::destroy ( T *  object)
noexceptinherited

Calls the destructor of the object and deallocate the storage.

◆ getBaseAllocator() [1/2]

MemoryAllocator & dart::common::PoolAllocator::getBaseAllocator ( )

Returns the base allocator.

◆ getBaseAllocator() [2/2]

const MemoryAllocator & dart::common::PoolAllocator::getBaseAllocator ( ) const

Returns the base allocator.

◆ GetDefault()

MemoryAllocator & dart::common::MemoryAllocator::GetDefault ( )
staticinherited

Returns the default memory allocator.

◆ getNumAllocatedMemoryBlocks()

int dart::common::PoolAllocator::getNumAllocatedMemoryBlocks ( ) const

Returns the count of allocated memory blocks.

◆ getStaticType()

static const std::string & dart::common::PoolAllocator::getStaticType ( )
inlinestatic

Returns type string.

◆ getType()

const std::string & dart::common::PoolAllocator::getType ( ) const
inlineoverridevirtual

Returns type string.

Implements dart::common::MemoryAllocator.

◆ is()

bool dart::common::Castable< MemoryAllocator >::is ( ) const
inherited

Returns true if the types of this Base and the template parameter (a base class) are identical.

This function is a syntactic sugar, which is identical to: (getType() == ShapeType::getStaticType()).

Example code:

if (shape->is<Sphere>())
std::cout << "The shape type is sphere!\n";

◆ print()

void dart::common::PoolAllocator::print ( std::ostream &  os = std::cout,
int  indent = 0 
) const
overridevirtual

Prints state of the memory allocator.

Reimplemented from dart::common::MemoryAllocator.

Member Data Documentation

◆ BLOCK_SIZE

constexpr size_t dart::common::PoolAllocator::BLOCK_SIZE = 16 * MAX_UNIT_SIZE
inlinestaticconstexprprivate

◆ HEAP_COUNT

constexpr int dart::common::PoolAllocator::HEAP_COUNT = 128
inlinestaticconstexprprivate

◆ MAX_UNIT_SIZE

constexpr size_t dart::common::PoolAllocator::MAX_UNIT_SIZE = 1024
inlinestaticconstexprprivate

◆ mBaseAllocator

MemoryAllocator& dart::common::PoolAllocator::mBaseAllocator
private

The base allocator to allocate memory chunk.

◆ mCurrentMemoryBlockIndex

int dart::common::PoolAllocator::mCurrentMemoryBlockIndex
private

The count of the allocated memory blocks in use.

◆ mFreeMemoryUnits

std::array<MemoryUnit*, HEAP_COUNT> dart::common::PoolAllocator::mFreeMemoryUnits
private

List of free memory units.

The size of mFreeMemoryUnits is fixed to HEAP_COUNT where each element is for a specific memory size. For example, the first element has the free memory unit for 8 bytes while the last element has the free memory unit for 1024 bytes.

The size must be the same of mUnitSizes.

◆ mInitialized

bool dart::common::PoolAllocator::mInitialized = false
inlinestaticprivate

◆ mMapSizeToHeapIndex

std::array<int, MAX_UNIT_SIZE + 1> dart::common::PoolAllocator::mMapSizeToHeapIndex
inlinestaticprivate

◆ mMemoryBlocks

MemoryBlock* dart::common::PoolAllocator::mMemoryBlocks
private

The array of memory blocks.

This array is a placeholder of allocated memory blocks. Initially this contains nullptr as the elements.

◆ mMemoryBlocksSize

int dart::common::PoolAllocator::mMemoryBlocksSize
private

The size of mMemoryBlocks.

This is simply the current size of mMemoryBlocks. The value doesn't mean the actual count of the allocated memory blocks.

◆ mMutex

std::mutex dart::common::PoolAllocator::mMutex
mutableprivate

Mutex for for mNumAllocatedMemoryBlocks, mNumMemoryBlocks, mFreeMemoryUnits, and mAllocatedMemoryBlocks.

◆ mUnitSizes

std::array<size_t, HEAP_COUNT> dart::common::PoolAllocator::mUnitSizes
inlinestaticprivate