Go to the documentation of this file.
9 #ifndef EAGINE_MEMORY_STD_ALLOC_HPP
10 #define EAGINE_MEMORY_STD_ALLOC_HPP
12 #include "../assert.hpp"
17 namespace eagine::memory {
21 class std_allocator :
public block_owner {
24 auto _get_sba() const -> auto& {
31 using propagate_on_container_move_assignment = std::true_type;
32 using propagate_on_container_copy_assignment = std::true_type;
33 using propagate_on_container_swap = std::true_type;
34 using is_always_equal = std::false_type;
38 using other = std_allocator<U>;
42 std_allocator(
const std_allocator<U>& that)
43 : _sba{that._get_sba()} {}
45 std_allocator(shared_byte_allocator sba) noexcept
46 : _sba{std::move(sba)} {}
48 std_allocator() noexcept
49 : _sba{default_byte_allocator()} {}
51 template <
typename ByteAlloc>
52 auto as() -> ByteAlloc& {
53 return _sba.as<ByteAlloc>();
56 auto address(T& r) noexcept -> T* {
57 return std::allocator<T>().address(r);
60 auto address(
const T& r) noexcept ->
const T* {
61 return std::allocator<T>().address(r);
64 auto max_size() const noexcept -> size_type {
65 return _sba.max_size(
alignof(T));
68 auto allocate(size_type n,
const void* =
nullptr) -> T* {
69 owned_block b = _sba.allocate(span_size_of<T>(n), span_align_of<T>());
72 throw std::bad_alloc();
76 EAGINE_ASSERT(b.size() >= span_size_of<T>(n));
78 T* p =
static_cast<T*
>(b.addr());
85 void deallocate(T* p, size_type n) {
93 operator==(
const std_allocator& a,
const std_allocator& b) noexcept {
94 return (a._sba == b._sba);
98 operator!=(
const std_allocator& a,
const std_allocator& b) noexcept {
99 return (a._sba != b._sba);
102 template <
class U,
class... A>
103 static inline void construct(U* p, A&&... a) {
104 ::new(
static_cast<void*
>(p)) U(std::forward<A>(a)...);
107 template <
typename U>
108 static inline void destroy(U* p) noexcept(noexcept(p->~U())) {
113 shared_byte_allocator _sba{};
118 #endif // EAGINE_MEMORY_STD_ALLOC_HPP
static constexpr auto is_aligned_to(const_address addr, span_size_t alignment) noexcept
Indicates if a memory address aligned to the specified alignment.
Definition: address.hpp:229
value_type
Value tree value element data type enumeration.
Definition: interface.hpp:27
std::ptrdiff_t span_size_t
Signed span size type used by eagine.
Definition: types.hpp:36
static constexpr auto as_bytes(basic_span< T, P, S > spn) noexcept -> basic_block< std::is_const_v< T >>
Converts a span into a basic_block.
Definition: block.hpp:39
static constexpr auto cover(T *addr, S size) noexcept -> span_if_mutable< T >
Creates a span starting at the specified pointer and specified length.
Definition: span.hpp:465
basic_address< false > address
Type alias for non-const memory address values.
Definition: address.hpp:203
static constexpr auto operator!=(const valid_if< T, P1 > &v1, const valid_if< T, P2 > &v2) noexcept -> tribool
Non-equality comparison of two conditionally valid values.
Definition: decl.hpp:169
static void release_block(owned_block &&b) noexcept
Should be called to release the ownership of a memory block.
Definition: block.hpp:117
auto operator==(message_id l, static_message_id< ClassId, MethodId > r) noexcept
Equality comparison between message_id and static_message_id.
Definition: message_id.hpp:131
static auto acquire_block(block b) noexcept -> owned_block
Should be called to take the ownership of a memory block.
Definition: block.hpp:112