Photon Engine 2.0.0-beta
A physically based renderer.
Loading...
Searching...
No Matches
ph::MemoryArena Class Referencefinal

A general purpose bump allocator. More...

#include <MemoryArena.h>

Inheritance diagram for ph::MemoryArena:
ph::IMoveOnly

Public Member Functions

 MemoryArena ()
 Empty arena without any allocation performed yet.
 
 MemoryArena (std::size_t blockSizeHintInBytes, std::size_t numDefaultBlocks)
 
 ~MemoryArena ()
 
std::byte * allocRaw (std::size_t numBytes, std::size_t alignmentInBytes=alignof(std::max_align_t))
 Allocate raw memory. Generally speaking, the memory returned contains no object–placement new is required before any use of the memory content for most object types, otherwise it is UB by C++ standard. An exception is implicit-lifetime types. This method implicitly create objects of implicit-lifetime type in the memory returned.
 
void clear ()
 Reset the usage of the arena. All memory handed out are effectively deallocated/deleted after this call. The arena then transitions to its initial state and is ready for allocation again. Destructors are called if required.
 
std::size_t numUsedBytes () const
 
std::size_t numAllocatedBytes () const
 
std::size_t getBlockSizeInBytes () const
 
std::size_t numAllocatedBlocks () const
 
template<typename T >
T * alloc ()
 Allocate raw memory for type T. Convenient method for allocating raw memory for object of type T. Alignment is handled automatically. See allocRaw(std::size_t, std::size_t) for details.
 
template<typename T >
TSpan< T > allocArray (const std::size_t arraySize)
 Allocate raw memory for array of type T. Convenient method for allocating raw memory for array of type T. Alignment is handled automatically. See allocRaw(std::size_t, std::size_t) for details.
 
template<typename T , typename... Args>
T * make (Args &&... args)
 Make an object of type T. Convenient method for creating an object without needing a placement new later. Equivalent to allocate then placement new for an object of type T. Alignment is handled automatically. Additionally, destructors will be automatically called (if needed) when clearing the arena.
 
 MemoryArena (MemoryArena &&other)=default
 Move another arena into this one. The moved-from arena cannot be used unless:
 
MemoryArenaoperator= (MemoryArena &&rhs)=default
 

Detailed Description

A general purpose bump allocator.

Constructor & Destructor Documentation

◆ MemoryArena() [1/3]

ph::MemoryArena::MemoryArena ( )

Empty arena without any allocation performed yet.

◆ MemoryArena() [2/3]

ph::MemoryArena::MemoryArena ( std::size_t blockSizeHintInBytes,
std::size_t numDefaultBlocks )
Parameters
blockSizeHintInBytesSize of each memory block in the arena. This imposed a limit on the maximum size of a single allocation. The parameter is only a hint, actual size may be larger for performance reasons.
numDefaultBlocksNumber of pre-allocated blocks.

◆ MemoryArena() [3/3]

ph::MemoryArena::MemoryArena ( MemoryArena && other)
inlinedefault

Move another arena into this one. The moved-from arena cannot be used unless:

  1. yet another arena has moved into it
  2. clear() has been called.

◆ ~MemoryArena()

ph::MemoryArena::~MemoryArena ( )

Member Function Documentation

◆ alloc()

template<typename T >
T * ph::MemoryArena::alloc ( )
inline

Allocate raw memory for type T. Convenient method for allocating raw memory for object of type T. Alignment is handled automatically. See allocRaw(std::size_t, std::size_t) for details.

Template Parameters
TType for the raw memory allocated, must be trivially destructible.
Note
Provides storage only. Placement new is required before any use of the memory content. For const types, laundering is needed to access the created object via the returned pointer ([basic.life] section 8.3).

◆ allocArray()

template<typename T >
TSpan< T > ph::MemoryArena::allocArray ( const std::size_t arraySize)
inline

Allocate raw memory for array of type T. Convenient method for allocating raw memory for array of type T. Alignment is handled automatically. See allocRaw(std::size_t, std::size_t) for details.

Template Parameters
TType for the raw array memory allocated, must be trivially destructible.
Note
Provides storage only. Placement new is required before any use of the memory content. For const types, laundering is needed to access the created object via the returned pointer ([basic.life] section 8.3).

◆ allocRaw()

std::byte * ph::MemoryArena::allocRaw ( std::size_t numBytes,
std::size_t alignmentInBytes = alignof(std::max_align_t) )

Allocate raw memory. Generally speaking, the memory returned contains no object–placement new is required before any use of the memory content for most object types, otherwise it is UB by C++ standard. An exception is implicit-lifetime types. This method implicitly create objects of implicit-lifetime type in the memory returned.

Parameters
numBytesNumber of bytes to allocate.
alignmentInBytesAlignment requirement of the allocation, in bytes.
Returns
Allocated memory, never nullptr.
Exceptions
std::bad_allocIf the allocation failed.

◆ clear()

void ph::MemoryArena::clear ( )

Reset the usage of the arena. All memory handed out are effectively deallocated/deleted after this call. The arena then transitions to its initial state and is ready for allocation again. Destructors are called if required.

◆ getBlockSizeInBytes()

std::size_t ph::MemoryArena::getBlockSizeInBytes ( ) const
inline

◆ make()

template<typename T , typename... Args>
T * ph::MemoryArena::make ( Args &&... args)
inline

Make an object of type T. Convenient method for creating an object without needing a placement new later. Equivalent to allocate then placement new for an object of type T. Alignment is handled automatically. Additionally, destructors will be automatically called (if needed) when clearing the arena.

Template Parameters
TType for the object created.
Parameters
argsArguments for calling the constructor of T.

◆ numAllocatedBlocks()

std::size_t ph::MemoryArena::numAllocatedBlocks ( ) const
inline

◆ numAllocatedBytes()

std::size_t ph::MemoryArena::numAllocatedBytes ( ) const
inline

◆ numUsedBytes()

std::size_t ph::MemoryArena::numUsedBytes ( ) const
inline

◆ operator=()

MemoryArena & ph::MemoryArena::operator= ( MemoryArena && rhs)
inlinedefault

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