29template <std::
size_t N, std::
size_t alignment = alignof(std::max_align_t)>
32 alignas(alignment)
char buf_[N];
37 arena() noexcept : ptr_(buf_) {}
41 template <std::
size_t ReqAlign>
char*
allocate(std::size_t n);
44 static constexpr std::size_t
size() noexcept {
return N;}
45 std::size_t
used() const noexcept {
return static_cast<std::size_t
>(ptr_ - buf_);}
46 void reset() noexcept {ptr_ = buf_;}
51 align_up(std::size_t n)
noexcept
52 {
return (n + (alignment-1)) & ~(alignment-1);}
55 pointer_in_buffer(
char* p)
noexcept
56 {
return buf_ <= p && p <= buf_ + N;}
59template <std::
size_t N, std::
size_t alignment>
60template <std::
size_t ReqAlign>
64 static_assert(ReqAlign <= alignment,
"alignment is too small for this arena");
65 assert(pointer_in_buffer(ptr_) &&
"short_alloc has outlived arena");
66 auto const aligned_n = align_up(n);
67 if (
static_cast<decltype(aligned_n)
>(buf_ + N - ptr_) >= aligned_n)
74 static_assert(alignment <=
alignof(std::max_align_t),
"you've chosen an "
75 "alignment that is larger than alignof(std::max_align_t), and "
76 "cannot be guaranteed by normal operator new");
77 return static_cast<char*
>(::operator
new(n));
80template <std::
size_t N, std::
size_t alignment>
84 assert(pointer_in_buffer(ptr_) &&
"short_alloc has outlived arena");
85 if (pointer_in_buffer(p))
97template <
class T, std::
size_t N, std::
size_t Align = alignof(std::max_align_t)>
103 static auto constexpr size = N;
116 "size N needs to be a multiple of alignment Align");
130 a_.deallocate(
reinterpret_cast<char*
>(p), n*
sizeof(
T));
133 template <
class T1, std::size_t N1, std::size_t A1,
134 class U, std::size_t
M, std::size_t A2>
139 template <
class U, std::
size_t M, std::
size_t A>
friend class short_alloc;
142template <
class T, std::
size_t N, std::
size_t A1,
class U, std::
size_t M, std::
size_t A2>
147 return N ==
M && A1 == A2 && &x.a_ == &y.a_;
150template <
class T, std::
size_t N, std::
size_t A1,
class U, std::
size_t M, std::
size_t A2>
Definition short_alloc.hpp:31
arena(const arena &)=delete
char * allocate(std::size_t n)
Definition short_alloc.hpp:62
void deallocate(char *p, std::size_t n) noexcept
Definition short_alloc.hpp:82
void reset() noexcept
Definition short_alloc.hpp:46
~arena()
Definition short_alloc.hpp:36
std::size_t used() const noexcept
Definition short_alloc.hpp:45
static constexpr std::size_t size() noexcept
Definition short_alloc.hpp:44
arena & operator=(const arena &)=delete
arena() noexcept
Definition short_alloc.hpp:37
Definition short_alloc.hpp:99
short_alloc(arena_type &a) noexcept
Definition short_alloc.hpp:113
short_alloc(const short_alloc< U, N, alignment > &a) noexcept
Definition short_alloc.hpp:119
static auto constexpr size
Definition short_alloc.hpp:103
short_alloc & operator=(const short_alloc &)=delete
T value_type
Definition short_alloc.hpp:101
arena< size, alignment > arena_type
Definition short_alloc.hpp:104
T * allocate(std::size_t n)
Definition short_alloc.hpp:124
void deallocate(T *p, std::size_t n) noexcept
Definition short_alloc.hpp:128
friend bool operator==(const short_alloc< T1, N1, A1 > &x, const short_alloc< U, M, A2 > &y) noexcept
static auto constexpr alignment
Definition short_alloc.hpp:102
short_alloc(const short_alloc &)=default
friend class short_alloc
Definition short_alloc.hpp:139
bool operator==(const short_alloc< T, N, A1 > &x, const short_alloc< U, M, A2 > &y) noexcept
Definition short_alloc.hpp:145
bool operator!=(const short_alloc< T, N, A1 > &x, const short_alloc< U, M, A2 > &y) noexcept
Definition short_alloc.hpp:153
Definition short_alloc.hpp:122
short_alloc< _Up, N, alignment > other
Definition short_alloc.hpp:122