carb::memory::ArenaAllocator

Defined in carb/memory/ArenaAllocator.h

Structs

template<class T, class FallbackAllocator = std::allocator<T>>
class ArenaAllocator

An allocator that initially allocates from a memory arena (typically on the stack) and falls back to another allocator when that is exhausted.

ArenaAllocator conforms to the C++ Named Requirement of Allocator.

Template Parameters
  • T – The type allocated by this allocator.

  • FallbackAllocator – The Allocator that is used when the arena is exhausted.

Public Types

using pointer = typename std::allocator_traits<FallbackAllocator>::pointer

T*

using const_pointer = typename std::allocator_traits<FallbackAllocator>::const_pointer

T const*

using void_pointer = typename std::allocator_traits<FallbackAllocator>::void_pointer

void*

using const_void_pointer = typename std::allocator_traits<FallbackAllocator>::const_void_pointer

void const*

using value_type = typename std::allocator_traits<FallbackAllocator>::value_type

T

using size_type = typename std::allocator_traits<FallbackAllocator>::size_type

std::size_t

using difference_type = typename std::allocator_traits<FallbackAllocator>::difference_type

std::ptrdiff_t

Public Functions

inline ArenaAllocator()

Default constructor.

Only uses FallbackAllocator as no arena is given.

inline explicit ArenaAllocator(const FallbackAllocator &fallback)

Constructs ArenaAllocator with a specific FallbackAllocator.

Only uses FallbackAllocator as no arena is given.

Parameters

fallback – A FallbackAllocator instance to copy.

inline ArenaAllocator(void *begin, void *end, const FallbackAllocator &fallback = FallbackAllocator())

Constructs ArenaAllocator with an arena and optionally a specific FallbackAllocator.

Warning

It is the caller’s responsibility to ensure that the given memory arena outlives *this and any other ArenaAllocator which it may be moved to.

Parameters
  • begin – A pointer to the beginning of the arena.

  • end – A pointer immediately past the end of the arena.

  • fallback – A FallbackAllocator instance to copy.

inline ArenaAllocator(ArenaAllocator &&other)

Move constructor: constructs ArenaAllocator by moving from a different ArenaAllocator.

Parameters

other – The ArenaAllocator to copy from.

inline ArenaAllocator(const ArenaAllocator &other)

Copy constructor: constructs ArenaAllocator from a copy of a given ArenaAllocator.

Note

Even though other is passed via const-reference, the arena is transferred from other to *this. Further allocations from other will defer to the FallbackAllocator.

Parameters

other – The ArenaAllocator to copy from.

template<class U, class UFallbackAllocator>
inline ArenaAllocator(const ArenaAllocator<U, UFallbackAllocator> &other)

Copy constructor: constructs ArenaAllocator for type T from a copy of a given ArenaAllocator for type U.

Note

This does not copy the arena; that is retained by the original allocator.

Parameters

other – The ArenaAllocator to copy from.

inline pointer allocate(size_type n = 1)

Allocates (but does not construct) memory for one or more instances of value_type.

Parameters

n – The number of contiguous value_type instances to allocate. If the request cannot be serviced by the arena, the FallbackAllocator is used.

Throws

Memory – Any exception that would be thrown by FallbackAllocator.

Returns

An uninitialized memory region that will fit n contiguous instances of value_type.

inline void deallocate(pointer in, size_type n = 1)

Deallocates (but does not destruct) memory for one or more instances of value_type.

Note

If the memory came from the arena, the memory will not be available for reuse unless the memory is the most recent allocation from the arena.

Parameters
  • in – The pointer previously returned from allocate().

  • n – The same n value that was passed to allocate() that produced in.

template<class U>
struct rebind

Rebinds ArenaAllocator to a different type U.

Public Types

using other = ArenaAllocator<U, typename FallbackAllocator::template rebind<U>::other>

The rebound ArenaAllocator.