FE 0.6.0
A header-only C++ library for writing frontends
Loading...
Searching...
No Matches
fe::Arena Class Reference

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

#include <fe/arena.h>

Classes

struct  Allocator
 
struct  Deleter
 

Public Member Functions

Allocator

An allocator in order to use this Arena for containers.

Construct it via Arena::allocator.

template<class T >
Allocator< T > allocator ()
 Create Allocator from Arena.
 
Construction/Destruction
 Arena (size_t page_size=Default_Page_Size)
 
 Arena (const Arena &)=delete
 
 Arena (Arena &&other) noexcept
 
Arenaoperator= (Arena)=delete
 
Allocate
Arenaalign (size_t a)
 Align next allocate(size_t) to a.
 
void * allocate (size_t num_bytes)
 Get n bytes of fresh memory.
 
template<class T >
T * allocate (size_t num_elems)
 

Static Public Attributes

static constexpr size_t Default_Page_Size = 1024 * 1024
 1MB.
 

Friends

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

Smart Pointer

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
template<class T >
using Ptr = std::unique_ptr<T, Deleter<T>>
 
template<class T , class... Args>
Ptr< T > mk (Args &&... args)
 

Deallocate

Deallocate memory again in reverse order.

using State = std::pair<size_t, size_t>
 Goes back to state in Arena.
 
void deallocate (size_t num_bytes)
 
State state () const
 
void deallocate (State state)
 

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 just as been acquired.

Definition at line 17 of file arena.h.

Member Typedef Documentation

◆ Ptr

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

Definition at line 67 of file arena.h.

◆ State

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

Goes back to state in Arena.

Use like this:

auto state = arena.state();
auto ptr = arena.allocate(n);
if (/* I don't want that *&zwj;/) arena.deallocate(state);
State state() const
Definition arena.h:127
Warning
Only use, if you really know what you are doing.

Definition at line 126 of file arena.h.

Constructor & Destructor Documentation

◆ Arena() [1/3]

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

Definition at line 76 of file arena.h.

◆ Arena() [2/3]

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

◆ Arena() [3/3]

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

Definition at line 81 of file arena.h.

References swap.

Member Function Documentation

◆ align()

Arena & fe::Arena::align ( size_t a)
inline

Align next allocate(size_t) to a.

Definition at line 92 of file arena.h.

Referenced by allocate(), and fe::SymPool::sym().

◆ allocate() [1/2]

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

Get n bytes of fresh memory.

Definition at line 95 of file arena.h.

Referenced by allocate(), fe::Arena::Allocator< T >::allocate(), mk(), and fe::SymPool::sym().

◆ allocate() [2/2]

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

Definition at line 106 of file arena.h.

References align(), and allocate().

◆ allocator()

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

Create Allocator from Arena.

Definition at line 47 of file arena.h.

◆ deallocate() [1/2]

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

Removes num_bytes again.

Definition at line 116 of file arena.h.

Referenced by fe::SymPool::sym().

◆ deallocate() [2/2]

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

Definition at line 128 of file arena.h.

References state().

◆ mk()

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

Definition at line 68 of file arena.h.

References allocate().

◆ operator=()

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

◆ state()

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

Definition at line 127 of file arena.h.

Referenced by deallocate(), and fe::SymPool::sym().

Friends And Related Symbol Documentation

◆ swap

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

Definition at line 136 of file arena.h.

Referenced by Arena().

Member Data Documentation

◆ Default_Page_Size

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

1MB.

Definition at line 19 of file arena.h.


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