ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
Arena.h File Reference

An arena of objects allocated in blocks, beyond the book: make() builds an object in the current block, a new block of \( n \) objects is taken when it is full, and all the objects die with the arena. More...

#include "ADS/Contract.h"
#include "ADS/Portable.h"
#include <algorithm>
#include <bit>
#include <compare>
#include <cstddef>
#include <cstdint>
#include <functional>
#include <iterator>
#include <limits>
#include <memory>
#include <new>
#include <type_traits>
#include <utility>
#include <vector>
#include "ADS/memory/impl/Arena.tpp"

Go to the source code of this file.

Classes

class  ADS::Arena< T, A >
 an arena of objects of type T in blocks of \( n \) More...
class  ADS::Arena< T, A >::Iterator< Const >
 the iterator, on constant objects if Const More...
class  ADS::ArenaResource< A >
 an arena of bytes in blocks of \( n \), the resource of a PoolAllocator More...

Namespaces

namespace  ADS

Detailed description

An arena of objects allocated in blocks, beyond the book: make() builds an object in the current block, a new block of \( n \) objects is taken when it is full, and all the objects die with the arena.

Allocating costs a pointer bump instead of a call to the allocator, the objects made one after another are next to each other in memory, and their addresses never change, which the pointers among them (the parents of the nodes of a search) rely on; the price is that an object cannot be freed by itself. The blocks come from the allocator A, which may give, e.g., huge pages.

clear() destroys all the objects at once and keeps the blocks, which the next objects take again in their order, so that a search restarted many times (an incremental search that gives up its repair) drops its nodes in \( O( 1 ) \) time when they are trivially destructible, instead of freeing them one by one or giving the blocks back to A. We call an epoch the time from one clear() (or from the construction) to the next one: no object survives a new epoch, and a pointer made in an epoch is stale in the next ones, whose make() may give its place to another object. Using a stale pointer violates the contract; fresh( p ) says whether \( p \) is a place given in the current epoch, which a stale pointer is until its place is given again, and under the sanitizer of addresses the places dropped by clear() stay forbidden until make() gives them again, so that the first access through a stale pointer to one of them is stopped there (see Epochs of the arenas and of the pools). An arena belongs to one thread: other threads may read its objects, but each of their accesses must happen before the clear() of the owner (e.g., by a barrier), which takes no lock and has no atomic; with clear() never called, the arena is the same as before, function by function.

ArenaResource is the same arena for the bytes of any type, the resource of a PoolAllocator [see PoolAllocator.h]: a request of \( b \) bytes aligned to \( a \) moves a pointer in the current block, and a new block is taken from the allocator A when it does not fit, of \( n \) bytes (or of \( b + a \), if more); a deallocation does nothing, and the blocks go back to A only with release() or with the resource, so that a container on PoolAllocator< T , ArenaResource<> > takes its nodes, places and blocks from a few large blocks and frees them all at once, as std::pmr::monotonic_buffer_resource does, with no virtual call. The resource must outlive the containers and the allocators made from it, and belongs to one thread.

The contracts [see Contract.h]: a block holds at least one object; the invariant is that size() objects have been made in the epoch; the alignment of a request of ArenaResource is a power of 2. The operations are defined in Arena.tpp.

See also
D. R. Hanson, Fast allocation and deallocation of memory based on object lifetimes, Software: Practice and Experience 20(1), 5-12, 1990, doi:10.1002/spe.4380200104
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa