A general purpose bump allocator.
More...
#include <MemoryArena.h>
|
| 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:
|
|
MemoryArena & | operator= (MemoryArena &&rhs)=default |
|
A general purpose bump allocator.
◆ 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
-
blockSizeHintInBytes | Size 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. |
numDefaultBlocks | Number of pre-allocated blocks. |
◆ MemoryArena() [3/3]
Move another arena into this one. The moved-from arena cannot be used unless:
- yet another arena has moved into it
clear()
has been called.
◆ ~MemoryArena()
ph::MemoryArena::~MemoryArena |
( |
| ) |
|
◆ 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
-
T | Type 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
-
T | Type 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
-
numBytes | Number of bytes to allocate. |
alignmentInBytes | Alignment requirement of the allocation, in bytes. |
- Returns
- Allocated memory, never
nullptr
.
- Exceptions
-
std::bad_alloc | If 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
-
T | Type for the object created. |
- Parameters
-
args | Arguments 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=()
The documentation for this class was generated from the following files: