OGLplus  (0.59.0) a C++ wrapper for rendering APIs

std_alloc.hpp
Go to the documentation of this file.
1 
9 #ifndef EAGINE_MEMORY_STD_ALLOC_HPP
10 #define EAGINE_MEMORY_STD_ALLOC_HPP
11 
12 #include "../assert.hpp"
13 #include "default_alloc.hpp"
14 #include "shared_alloc.hpp"
15 #include <memory>
16 
17 namespace eagine::memory {
18 
19 // std_allocator
20 template <typename T>
21 class std_allocator : public block_owner {
22 
23 public:
24  auto _get_sba() const -> auto& {
25  return _sba;
26  }
27 
28  using value_type = T;
29  using size_type = span_size_t;
30 
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;
35 
36  template <typename U>
37  struct rebind {
38  using other = std_allocator<U>;
39  };
40 
41  template <typename U>
42  std_allocator(const std_allocator<U>& that)
43  : _sba{that._get_sba()} {}
44 
45  std_allocator(shared_byte_allocator sba) noexcept
46  : _sba{std::move(sba)} {}
47 
48  std_allocator() noexcept
49  : _sba{default_byte_allocator()} {}
50 
51  template <typename ByteAlloc>
52  auto as() -> ByteAlloc& {
53  return _sba.as<ByteAlloc>();
54  }
55 
56  auto address(T& r) noexcept -> T* {
57  return std::allocator<T>().address(r);
58  }
59 
60  auto address(const T& r) noexcept -> const T* {
61  return std::allocator<T>().address(r);
62  }
63 
64  auto max_size() const noexcept -> size_type {
65  return _sba.max_size(alignof(T));
66  }
67 
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>());
70 
71  if(!b) {
72  throw std::bad_alloc();
73  }
74 
75  EAGINE_ASSERT(is_aligned_to(b.addr(), span_align_of<T>()));
76  EAGINE_ASSERT(b.size() >= span_size_of<T>(n));
77 
78  T* p = static_cast<T*>(b.addr());
79 
80  release_block(std::move(b));
81 
82  return p;
83  }
84 
85  void deallocate(T* p, size_type n) {
86  if(p && n) {
87  _sba.deallocate(
88  acquire_block(as_bytes(cover(p, n))), span_align_of<T>());
89  }
90  }
91 
92  friend auto
93  operator==(const std_allocator& a, const std_allocator& b) noexcept {
94  return (a._sba == b._sba);
95  }
96 
97  friend auto
98  operator!=(const std_allocator& a, const std_allocator& b) noexcept {
99  return (a._sba != b._sba);
100  }
101 
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)...);
105  }
106 
107  template <typename U>
108  static inline void destroy(U* p) noexcept(noexcept(p->~U())) {
109  return p->~U();
110  }
111 
112 private:
113  shared_byte_allocator _sba{};
114 };
115 
116 } // namespace eagine::memory
117 
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

Copyright © 2015-2021 Matúš Chochlík.
<chochlik -at -gmail.com>
Documentation generated on Tue Apr 13 2021 by Doxygen (version 1.8.17).