67 template<
class T>
using Ptr = std::unique_ptr<T, Deleter<T>>;
68 template<
class T,
class... Args>
Ptr<T> mk(Args&&... args) {
69 auto ptr =
new (
allocate(
sizeof(T))) T(std::forward<Args&&>(args)...);
77 : page_size_(page_size) {
78 pages_.emplace_back(page_size);
92 Arena&
align(
size_t a) {
return index_ = (index_ + (a - 1)) & ~(a - 1), *
this; }
95 [[nodiscard]]
void*
allocate(
size_t num_bytes) {
96 if (index_ + num_bytes > pages_.back().size) {
97 pages_.emplace_back(std::max(page_size_, num_bytes));
101 auto result = pages_.back().buffer.get() + index_;
106 template<
class T> [[nodiscard]] T*
allocate(
size_t num_elems) {
108 return static_cast<T*
>(
allocate(num_elems * std::max(
sizeof(T),
alignof(T))));
126 using State = std::pair<size_t, size_t>;
129 if (
state.first == pages_.size())
130 index_ =
state.second;
139 swap(a1.pages_, a2.pages_);
140 swap(a1.page_size_, a2.page_size_);
141 swap(a1.index_, a2.index_);
149 , buffer(std::make_unique<char[]>(size)) {}
151 std::unique_ptr<char[]> buffer;
154 std::list<Page> pages_;
An arena pre-allocates so-called pages of size Arena::page_size_.
void deallocate(State state)
Arena(const Arena &)=delete
Arena & align(size_t a)
Align next allocate(size_t) to a.
void deallocate(size_t num_bytes)
Arena(Arena &&other) noexcept
std::pair< size_t, size_t > State
Goes back to state in Arena.
void * allocate(size_t num_bytes)
Get n bytes of fresh memory.
Arena(size_t page_size=Default_Page_Size)
Allocator< T > allocator()
Create Allocator from Arena.
T * allocate(size_t num_elems)
friend void swap(Arena &a1, Arena &a2) noexcept
Arena & operator=(Arena)=delete
std::unique_ptr< T, Deleter< T > > Ptr
Ptr< T > mk(Args &&... args)
static constexpr size_t Default_Page_Size
1MB.
bool operator!=(const Allocator< U > &a) const noexcept
T * allocate(size_t num_elems)
void deallocate(T *, size_t) noexcept
bool operator==(const Allocator< U > &a) const noexcept
Allocator(Arena &arena) noexcept
Allocator(const Arena::Allocator< U > &allocator) noexcept
constexpr Deleter() noexcept=default