Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
Aleph::ArenaTreeAllocator< Node > Struct Template Reference

Allocator of the nodes of a tree in an arena. More...

#include <tpl_dynSetTree.H>

Inheritance diagram for Aleph::ArenaTreeAllocator< Node >:
[legend]
Collaboration diagram for Aleph::ArenaTreeAllocator< Node >:
[legend]

Classes

struct  Free_Slot
 The room of a released node, in the list of those to give again. More...
 

Public Member Functions

 ArenaTreeAllocator (const size_t &sz=1024 *1024)
 Build an arena of sz bytes.
 
 ArenaTreeAllocator (const char *base_addr, const size_t &sz)
 Build an arena on the sz bytes at base_addr, owned by the caller.
 
Node * alloc_lval (const typename Node::key_type &key) override
 Build a node with a copy of key; throw bad_alloc if it does not fit.
 
Node * alloc_rval (typename Node::key_type &&key) override
 Build a node with key moved into it; throw bad_alloc if it does not fit.
 
void unalloc (Node *p) override
 Destroy the node p and keep its room, if it is of this arena.
 
void pin () noexcept
 Note that some node of this arena is now in another set.
 
bool is_pinned () const noexcept
 Return true if some node of this arena was given to another set.
 
void reset () noexcept
 Make the whole arena available again.
 
size_t allocated_size () const noexcept
 Return the bytes taken from the arena, released nodes included.
 
size_t available_size () const noexcept
 Return the bytes of the arena that were never taken.
 
 ~ArenaTreeAllocator ()=default
 
- Public Member Functions inherited from Aleph::AbstractTreeNodeAllocator< Node >
 AbstractTreeNodeAllocator ()=default
 
virtual ~AbstractTreeNodeAllocator ()=default
 

Public Attributes

AhArenaAllocator arena
 The memory of the nodes.
 

Private Member Functions

void * take () noexcept
 Return the room of a node, or nullptr if the arena has none left.
 
void give_back (void *p) noexcept
 Release a room: the arena takes back its last block, the list any other.
 
template<class K >
Node * build (K &&key)
 Build a node of key in a free room; throw bad_alloc if there is none.
 

Private Attributes

Free_Slot * free_slots = nullptr
 The released rooms.
 
bool pinned = false
 Whether some node was given to another set.
 

Detailed Description

template<typename Node>
struct Aleph::ArenaTreeAllocator< Node >

Allocator of the nodes of a tree in an arena.

The arena only takes back the last block that it gave. As every node has the same size, the allocator keeps the other released nodes in a list and gives them again before it takes more of the arena.

A set may give its nodes to another one, with join() or a split. From then on its arena is pinned: those nodes live in it, so it is never reset. And a node of another arena is destroyed, but its room is never reused.

Template Parameters
Nodethe type of the nodes.
Note
The arena lasts as long as its set: a set that receives nodes from a set with an arena must be destroyed before it.

Definition at line 136 of file tpl_dynSetTree.H.

Constructor & Destructor Documentation

◆ ArenaTreeAllocator() [1/2]

template<typename Node >
Aleph::ArenaTreeAllocator< Node >::ArenaTreeAllocator ( const size_t &  sz = 1024 * 1024)
inline

Build an arena of sz bytes.

Definition at line 141 of file tpl_dynSetTree.H.

◆ ArenaTreeAllocator() [2/2]

template<typename Node >
Aleph::ArenaTreeAllocator< Node >::ArenaTreeAllocator ( const char *  base_addr,
const size_t &  sz 
)
inline

Build an arena on the sz bytes at base_addr, owned by the caller.

Definition at line 148 of file tpl_dynSetTree.H.

◆ ~ArenaTreeAllocator()

template<typename Node >
Aleph::ArenaTreeAllocator< Node >::~ArenaTreeAllocator ( )
default

Member Function Documentation

◆ alloc_lval()

template<typename Node >
Node * Aleph::ArenaTreeAllocator< Node >::alloc_lval ( const typename Node::key_type &  key)
inlineoverridevirtual

Build a node with a copy of key; throw bad_alloc if it does not fit.

Implements Aleph::AbstractTreeNodeAllocator< Node >.

Definition at line 155 of file tpl_dynSetTree.H.

References Aleph::ArenaTreeAllocator< Node >::build().

◆ alloc_rval()

template<typename Node >
Node * Aleph::ArenaTreeAllocator< Node >::alloc_rval ( typename Node::key_type &&  key)
inlineoverridevirtual

Build a node with key moved into it; throw bad_alloc if it does not fit.

Implements Aleph::AbstractTreeNodeAllocator< Node >.

Definition at line 161 of file tpl_dynSetTree.H.

References Aleph::ArenaTreeAllocator< Node >::build().

◆ allocated_size()

template<typename Node >
size_t Aleph::ArenaTreeAllocator< Node >::allocated_size ( ) const
inlinenoexcept

Return the bytes taken from the arena, released nodes included.

Definition at line 193 of file tpl_dynSetTree.H.

References Aleph::AhArenaAllocator::allocated_size(), and Aleph::ArenaTreeAllocator< Node >::arena.

◆ available_size()

template<typename Node >
size_t Aleph::ArenaTreeAllocator< Node >::available_size ( ) const
inlinenoexcept

Return the bytes of the arena that were never taken.

Definition at line 199 of file tpl_dynSetTree.H.

References Aleph::ArenaTreeAllocator< Node >::arena, and Aleph::AhArenaAllocator::available_size().

◆ build()

template<typename Node >
template<class K >
Node * Aleph::ArenaTreeAllocator< Node >::build ( K &&  key)
inlineprivate

Build a node of key in a free room; throw bad_alloc if there is none.

Definition at line 241 of file tpl_dynSetTree.H.

References ah_bad_alloc_if, Aleph::ArenaTreeAllocator< Node >::give_back(), and Aleph::ArenaTreeAllocator< Node >::take().

Referenced by Aleph::ArenaTreeAllocator< Node >::alloc_lval(), and Aleph::ArenaTreeAllocator< Node >::alloc_rval().

◆ give_back()

template<typename Node >
void Aleph::ArenaTreeAllocator< Node >::give_back ( void *  p)
inlineprivatenoexcept

◆ is_pinned()

template<typename Node >
bool Aleph::ArenaTreeAllocator< Node >::is_pinned ( ) const
inlinenoexcept

Return true if some node of this arena was given to another set.

Definition at line 179 of file tpl_dynSetTree.H.

◆ pin()

template<typename Node >
void Aleph::ArenaTreeAllocator< Node >::pin ( )
inlinenoexcept

Note that some node of this arena is now in another set.

Definition at line 176 of file tpl_dynSetTree.H.

References Aleph::ArenaTreeAllocator< Node >::pinned.

◆ reset()

template<typename Node >
void Aleph::ArenaTreeAllocator< Node >::reset ( )
inlinenoexcept

Make the whole arena available again.

Note
Only for an arena that is not pinned and whose nodes are all destroyed, or have their keys destroyed.

Definition at line 185 of file tpl_dynSetTree.H.

References Aleph::ArenaTreeAllocator< Node >::arena, Aleph::ArenaTreeAllocator< Node >::free_slots, Aleph::ArenaTreeAllocator< Node >::pinned, and Aleph::AhArenaAllocator::reset().

◆ take()

template<typename Node >
void * Aleph::ArenaTreeAllocator< Node >::take ( )
inlineprivatenoexcept

◆ unalloc()

template<typename Node >
void Aleph::ArenaTreeAllocator< Node >::unalloc ( Node *  p)
inlineoverridevirtual

Destroy the node p and keep its room, if it is of this arena.

Implements Aleph::AbstractTreeNodeAllocator< Node >.

Definition at line 167 of file tpl_dynSetTree.H.

References Aleph::ArenaTreeAllocator< Node >::arena, Aleph::AhArenaAllocator::contains(), and Aleph::ArenaTreeAllocator< Node >::give_back().

Member Data Documentation

◆ arena

◆ free_slots

template<typename Node >
Free_Slot* Aleph::ArenaTreeAllocator< Node >::free_slots = nullptr
private

◆ pinned

template<typename Node >
bool Aleph::ArenaTreeAllocator< Node >::pinned = false
private

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