DART 6.13.2
Loading...
Searching...
No Matches
dart::common::MemoryAllocator Class Referenceabstract

Base class for std::allocator compatible allocators. More...

#include <MemoryAllocator.hpp>

Inheritance diagram for dart::common::MemoryAllocator:
dart::common::Castable< MemoryAllocator > dart::common::MemoryAllocatorDebugger< FreeListAllocator > dart::common::MemoryAllocatorDebugger< PoolAllocator > dart::common::CAllocator dart::common::FreeListAllocator dart::common::MemoryAllocatorDebugger< T > dart::common::PoolAllocator

Public Member Functions

 MemoryAllocator () noexcept=default
 Default constructor.
 
virtual ~MemoryAllocator ()=default
 Destructor.
 
virtual const std::string & getType () const =0
 Returns type string.
 
virtual void * allocate (size_t bytes) noexcept=0
 Allocates size bytes of uninitialized storage.
 
template<typename T >
T * allocateAs (size_t n=1) noexcept
 Allocates object(s) without calling the constructor.
 
virtual void deallocate (void *pointer, size_t bytes)=0
 Deallocates the storage referenced by the pointer p, which must be a pointer obtained by an earlier cal to allocate().
 
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.
 
virtual void print (std::ostream &os=std::cout, int indent=0) const
 Prints state of the memory allocator.
 
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 MemoryAllocatorGetDefault ()
 Returns the default memory allocator.
 

Private Member Functions

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

Friends

std::ostream & operator<< (std::ostream &os, const MemoryAllocator &allocator)
 Prints state of the memory allocator.
 

Detailed Description

Base class for std::allocator compatible allocators.

Constructor & Destructor Documentation

◆ MemoryAllocator()

dart::common::MemoryAllocator::MemoryAllocator ( )
defaultnoexcept

Default constructor.

◆ ~MemoryAllocator()

virtual dart::common::MemoryAllocator::~MemoryAllocator ( )
virtualdefault

Destructor.

Member Function Documentation

◆ allocate()

virtual void * dart::common::MemoryAllocator::allocate ( size_t  bytes)
pure virtualnoexcept

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

Implemented in dart::common::CAllocator, dart::common::FreeListAllocator, dart::common::MemoryAllocatorDebugger< T >, dart::common::MemoryAllocatorDebugger< FreeListAllocator >, dart::common::MemoryAllocatorDebugger< PoolAllocator >, and dart::common::PoolAllocator.

◆ allocateAs()

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

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)
noexcept

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 
)

◆ constructAt() [2/2]

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

◆ deallocate()

virtual void dart::common::MemoryAllocator::deallocate ( void *  pointer,
size_t  bytes 
)
pure virtual

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.

Implemented in dart::common::CAllocator, dart::common::FreeListAllocator, dart::common::MemoryAllocatorDebugger< T >, dart::common::MemoryAllocatorDebugger< FreeListAllocator >, dart::common::MemoryAllocatorDebugger< PoolAllocator >, and dart::common::PoolAllocator.

◆ destroy()

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

Calls the destructor of the object and deallocate the storage.

◆ GetDefault()

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

Returns the default memory allocator.

◆ getType()

◆ 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::MemoryAllocator::print ( std::ostream &  os = std::cout,
int  indent = 0 
) const
virtual

Friends And Related Symbol Documentation

◆ operator<<

std::ostream & operator<< ( std::ostream &  os,
const MemoryAllocator allocator 
)
friend

Prints state of the memory allocator.