Skip to content

strata::AlignedAllocator

Part of Memory.

template <typename T, std::size_t Alignment = default_alignment>
class strata::AlignedAllocator;

A stateless allocator like std::allocator, but aligned to Alignment bytes.

In constant evaluation it is std::allocator, so that dynamic tensors work in constexpr code; at run time it is std::malloc with room to round up, as Eigen does, the offset back to the start of the block kept in the byte just before the aligned one: the aligned operator new takes a slower path in common allocators, which costs small tensors up to twice their time. One byte holds offsets up to 256, hence the bound. Both allocate and deallocate pick the same branch, since memory allocated in constant evaluation cannot outlive it.

Template parameters

Name Description
T The element type.
Alignment A power of two, at least alignof(T) and at most 256.

Members

alignment

static constexpr std::size_t alignment = Alignment

The alignment of the memory, in bytes.

AlignedAllocator

template <typename U>
constexpr AlignedAllocator(const AlignedAllocator<U, Alignment> &) noexcept

Converts from the allocator of another element type.

Implicit, as that of std::allocator: containers convert rebound allocators without asking.

allocate

constexpr T * allocate(std::size_t count)

Memory for count elements, aligned to Alignment.

Exceptions

Name Description
std::bad_array_new_length when the size overflows.
std::bad_alloc when there is no memory.

deallocate

constexpr void deallocate(T *pointer, std::size_t count) noexcept

Returns memory from allocate.

operator==

constexpr bool operator==(const AlignedAllocator &, const AlignedAllocator &) noexcept=default

All allocators of one type are equal.