24#ifndef STELLARLIB_EXT_MEMORY_HPP
25#define STELLARLIB_EXT_MEMORY_HPP
27#include <stellarlib/ext/type_traits.hpp>
29#include <SDL3/SDL_stdinc.h>
48template <
typename T,
typename SizeType = std::
size_t>
142 capacity += capacity / 4;
146 capacity = std::bit_ceil(capacity);
149 for (
const auto src : std::views::iota(begin, begin + size)) {
150 std::construct_at(src + (dst - begin), std::move(*src));
151 std::destroy_at(src);
256 template <typename T>
260 if (
const auto ptr{std::align(
alignof(T),
sizeof(T), _cursor, _size)}) {
261 _cursor =
static_cast<T *
>(_cursor) + 1;
263 return static_cast<T *
>(ptr);
266 return static_cast<T *
>(
nullptr);
287 value_type *_cursor{SDL_aligned_alloc(page_alignment, _capacity)};
361 template <typename T>
365 if (
const auto ptr{_begin[_cursor].allocate<T>()}) {
369 _begin =
static_cast<arena *
>(std::realloc(_begin, (++_cursor + 1) *
sizeof(
arena)));
370 std::construct_at(_begin + _cursor,
sizeof(T) <= _capacity ? _capacity :
sizeof(T));
371 _capacity += _begin[_cursor].capacity();
372 return _begin[_cursor].allocate<T>();
390 arena *_begin{std::construct_at(
static_cast<arena *
>(std::malloc(
sizeof(
arena))), 0)};
constexpr auto allocate() noexcept
Returns uninitialized memory for type T.
Definition memory.hpp:363
std::allocator< void >::propagate_on_container_move_assignment propagate_on_container_move_assignment
Whether the allocator can be propagated on container move assignment.
Definition memory.hpp:316
auto operator==(const arena_allocator &other) const noexcept -> bool
Comparison operator.
std::allocator< void >::value_type value_type
Value type of the allocator.
Definition memory.hpp:301
std::allocator< void >::size_type size_type
Size type of the allocator.
Definition memory.hpp:306
void deallocate() noexcept
Resets the arena.
std::allocator< void >::difference_type difference_type
Difference type of the allocator.
Definition memory.hpp:311
arena_allocator() noexcept
Default constructor.
Fixed-size, page-aligned memory arena.
Definition memory.hpp:182
arena(size_type capacity) noexcept
Parameterized constructor.
constexpr auto allocate() noexcept
Returns uninitialized memory for type T.
Definition memory.hpp:258
void deallocate() noexcept
Resets the arena.
auto operator==(const arena &other) const noexcept -> bool
Comparison operator.
auto operator=(arena &&other) noexcept -> arena &
Move assignment operator.
std::allocator< void >::size_type size_type
Size type of the allocator.
Definition memory.hpp:192
arena(arena &&other) noexcept
Move constructor.
std::allocator< void >::value_type value_type
Value type of the allocator.
Definition memory.hpp:187
constexpr arena(const arena &) noexcept=delete
Deleted copy constructor.
~arena() noexcept
Destructor.
constexpr auto operator=(const arena &) noexcept -> arena &=delete
Deleted copy assignment operator.
std::allocator< void >::difference_type difference_type
Difference type of the allocator.
Definition memory.hpp:197
std::allocator< void >::propagate_on_container_move_assignment propagate_on_container_move_assignment
Whether the allocator can be propagated on container move assignment.
Definition memory.hpp:202
auto capacity() const noexcept -> size_type
Returns the remaining capacity of the arena in bytes.
std::allocator< T >::value_type value_type
Value type of the allocator.
Definition memory.hpp:55
constexpr void reallocate(value_type *&begin, const size_type capacity) const noexcept
Reallocates uninitialized memory for N instances of type T via bit-wise relocation.
Definition memory.hpp:128
constexpr void reallocate(value_type *&begin, const size_type size, size_type &capacity) const noexcept
Reallocates uninitialized memory for at least N instances of type T via element-wise move.
Definition memory.hpp:139
constexpr void allocate(value_type *&begin, const size_type capacity) const noexcept
Allocates uninitialized memory for N instances of type T.
Definition memory.hpp:118
constexpr vector_allocator() noexcept=default
Default constructor.
constexpr void deallocate(value_type *begin) const noexcept
Deallocates uninitialized memory.
Definition memory.hpp:172
std::allocator< value_type >::difference_type difference_type
Difference type of the allocator.
Definition memory.hpp:65
SizeType size_type
Size type of the allocator.
Definition memory.hpp:60
constexpr auto operator==(const vector_allocator &other) const noexcept -> bool=default
Comparison operator.
std::allocator< value_type >::propagate_on_container_move_assignment propagate_on_container_move_assignment
Whether the allocator can be propagated on container move assignment.
Definition memory.hpp:70
Standard library extensions.
Definition bit.hpp:33
constexpr bool is_trivially_relocatable_v
Evaluates whether T is eligible for bit-wise relocation.
Definition type_traits.hpp:65