FE 0.15.0
A C++23 toolkit for writing compiler/interpreter frontends.
Loading...
Searching...
No Matches

An arena pre-allocates so-called pages of size Arena::page_size_. More...

#include <fe/arena.h>

Classes

class  MemoryResource
 A memory resource bridge in order to use this Arena for pmr containers. More...
struct  Allocator
 An allocator in order to use this Arena for containers. More...
struct  Deleter
class  Ref
 A non-owning pointer into an Arena. More...

Public Types

template<class T>
using Ptr = std::unique_ptr<T, Deleter<T>>
using State = std::pair<size_t, size_t>

Public Member Functions

Construction
 Arena (const Arena &)=delete
 Arena (size_t page_size=Default_Page_Size)
 Arena (Arena &&other) noexcept
Arenaoperator= (Arena)=delete
template<class T>
Allocator< T > allocator () noexcept
 Create Allocator from Arena.
std::pmr::memory_resourceresource () noexcept
const std::pmr::memory_resourceresource () const noexcept
template<class T, class... Args>
Ptr< T > mk (Args &&... args)
 This is a std::unique_ptr that uses the Arena under the hood and whose Deleter will only invoke the destructor but not delete anything; memory will be released upon destruction of the Arena.
template<class T, class... Args>
Ref< T > ref (Args &&... args)
 Like Arena::mk, but yields a Ref: nothing will ever destroy the object.
template<std::ranges::input_range R, class T = std::ranges::range_value_t<R>>
Span< T > copy (const R &range)
 An Arena-allocated copy of range.
Allocate
void * allocate (size_t num_bytes, size_t align)
 Get n bytes of fresh memory.
template<class T>
T * allocate (size_t num_elems)
Deallocate

Deallocate memory again in reverse order.

Use like this:

auto state = arena.state();
auto ptr = arena.allocate(n);
if (/* I don't want that */) arena.deallocate(state);
State state() const noexcept
Definition arena.h:223
Warning
Only use, if you really know what you are doing.
void deallocate (size_t num_bytes) noexcept
 Removes num_bytes again.
State state () const noexcept
void deallocate (State state) noexcept

Static Public Member Functions

static constexpr size_t align (size_t i, size_t a) noexcept
 Align i to a.

Static Public Attributes

static constexpr size_t Default_Page_Size = 1024 * 1024
 1MB.

Friends

void swap (Arena &a1, Arena &a2) noexcept

Detailed Description

An arena pre-allocates so-called pages of size Arena::page_size_.

You can use Arena::allocate to obtain memory from this. When a page runs out of memory, the next page will be (pre-)allocated. You cannot directly release memory obtained via this method. Instead, all memory acquired via this Arena will be released as soon as this Arena will be destroyed. As an exception, you can Arena::deallocate memory that has just been acquired.

Definition at line 25 of file arena.h.

Member Typedef Documentation

◆ Ptr

template<class T>
using fe::Arena::Ptr = std::unique_ptr<T, Deleter<T>>

Definition at line 115 of file arena.h.

◆ State

using fe::Arena::State = std::pair<size_t, size_t>

Definition at line 116 of file arena.h.

Constructor & Destructor Documentation

◆ Arena() [1/3]

fe::Arena::Arena ( const Arena & )
delete

◆ Arena() [2/3]

fe::Arena::Arena ( size_t page_size = Default_Page_Size)
inlineexplicit

Definition at line 121 of file arena.h.

References Default_Page_Size.

◆ Arena() [3/3]

fe::Arena::Arena ( Arena && other)
inlinenoexcept

Definition at line 125 of file arena.h.

References Arena(), and swap.

Member Function Documentation

◆ align()

constexpr size_t fe::Arena::align ( size_t i,
size_t a )
inlinestaticconstexprnoexcept

Align i to a.

Definition at line 245 of file arena.h.

Referenced by allocate().

◆ allocate() [1/2]

void * fe::Arena::allocate ( size_t num_bytes,
size_t align )
inlinenodiscard

Get n bytes of fresh memory.

Note
When a fresh page is allocated, its base is only aligned to the align of the allocation that triggered it. A later allocation in the same page that requests a larger alignment has its offset aligned but may still be under-aligned relative to its request. This is a non-issue for the default (max-aligned) page size and for arenas with uniform alignment; only tiny custom arenas mixing alignments can hit it.

Definition at line 186 of file arena.h.

References align().

Referenced by allocate(), fe::Arena::Allocator< T >::allocate(), and copy().

◆ allocate() [2/2]

template<class T>
T * fe::Arena::allocate ( size_t num_elems)
inlinenodiscard

Definition at line 202 of file arena.h.

References allocate().

◆ allocator()

template<class T>
Allocator< T > fe::Arena::allocator ( )
inlinenoexcept

Create Allocator from Arena.

Definition at line 133 of file arena.h.

References fe::Arena::Allocator< T >::Allocator().

Referenced by fe::Arena::Allocator< T >::Allocator().

◆ copy()

template<std::ranges::input_range R, class T = std::ranges::range_value_t<R>>
Span< T > fe::Arena::copy ( const R & range)
inlinenodiscard

An Arena-allocated copy of range.

Definition at line 168 of file arena.h.

References allocate().

◆ deallocate() [1/2]

void fe::Arena::deallocate ( size_t num_bytes)
inlinenoexcept

Removes num_bytes again.

Definition at line 219 of file arena.h.

◆ deallocate() [2/2]

void fe::Arena::deallocate ( State state)
inlinenoexcept

Definition at line 225 of file arena.h.

References state().

◆ mk()

template<class T, class... Args>
Ptr< T > fe::Arena::mk ( Args &&... args)
inline

This is a std::unique_ptr that uses the Arena under the hood and whose Deleter will only invoke the destructor but not delete anything; memory will be released upon destruction of the Arena.

Use like this:

auto ptr = arena.mk<Foo>(a, b, c); // new Foo(a, b, c) placed into arena

Definition at line 150 of file arena.h.

◆ operator=()

Arena & fe::Arena::operator= ( Arena )
delete

References Arena().

◆ ref()

template<class T, class... Args>
Ref< T > fe::Arena::ref ( Args &&... args)
inline

Like Arena::mk, but yields a Ref: nothing will ever destroy the object.

Use like this:

auto ref = arena.ref<Foo>(a, b, c); // new Foo(a, b, c) placed into arena, never destroyed
Ref< T > ref(Args &&... args)
Like Arena::mk, but yields a Ref: nothing will ever destroy the object.
Definition arena.h:160

Definition at line 160 of file arena.h.

References fe::Arena::Ref< T >::Ref().

Referenced by fe::Arena::Ref< T >::Ref().

◆ resource() [1/2]

const std::pmr::memory_resource * fe::Arena::resource ( ) const
inlinenoexcept

Definition at line 138 of file arena.h.

◆ resource() [2/2]

std::pmr::memory_resource * fe::Arena::resource ( )
inlinenoexcept

Definition at line 137 of file arena.h.

Referenced by fe::Arena::MemoryResource::do_is_equal().

◆ state()

State fe::Arena::state ( ) const
inlinenodiscardnoexcept

Definition at line 223 of file arena.h.

Referenced by deallocate().

◆ swap

void swap ( Arena & a1,
Arena & a2 )
friend

Definition at line 235 of file arena.h.

References Arena(), and swap.

Referenced by Arena(), and swap.

Member Data Documentation

◆ Default_Page_Size

size_t fe::Arena::Default_Page_Size = 1024 * 1024
staticconstexpr

1MB.

Definition at line 27 of file arena.h.

Referenced by Arena(), and fe::Patricia< D, KT, K, N >::Patricia().


The documentation for this class was generated from the following file: