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

A pool of objects of one type, beyond the book: make() builds an object in a slot of the pool and free() destroys it and keeps its slot, which the next make() takes again (the last one freed first), so that the nodes of a linked structure cost no call to the allocator, the slot just freed is still in the cache when it is used again, and the objects made one after another are next to each other in memory. More...

#include "ADS/Contract.h"
#include "ADS/Portable.h"
#include "ADS/memory/HugePageAllocator.h"
#include <algorithm>
#include <cstddef>
#include <functional>
#include <memory>
#include <new>
#include <type_traits>
#include <utility>
#include "ADS/memory/impl/ObjectPool.tpp"

Go to the source code of this file.

Classes

class  ADS::ObjectPool< T, A >
 a pool of objects of type T in blocks of at most \( n \) slots More...

Namespaces

namespace  ADS

Detailed description

A pool of objects of one type, beyond the book: make() builds an object in a slot of the pool and free() destroys it and keeps its slot, which the next make() takes again (the last one freed first), so that the nodes of a linked structure cost no call to the allocator, the slot just freed is still in the cache when it is used again, and the objects made one after another are next to each other in memory.

The slots come in blocks taken from the allocator A, the first of one slot and each next one twice the last up to \( n \) slots, so that a small structure takes no more than it holds (at most twice its largest size, plus one block of \( n \)); a block begins with a header that links it to the one before, so that the pool is seven words and allocates nothing but its blocks, which go back to A only with the pool: a structure that shrinks keeps the memory of its largest size. The default allocator gives the blocks of 2 MB or more on huge pages [see HugePageAllocator.h], which the nodes of a large linked structure, reached in no useful order, need not to miss the TLB at each step. Unlike Arena [see Arena.h], an object can be freed by itself, and the pool does not destroy the objects left in it, which their owner destroys (or does not, if their destructor is trivial) before the pool dies. Each operation costs \( O( 1 ) \) time, but for the allocation of a block.

allocate() and deallocate() give and take back the raw slots, the objects built and destroyed by the caller, as with std::allocator.

clear() drops all the objects at once in \( O( 1 ) \) time, with no destructor called (as at the death of the pool), and keeps the blocks, which the next objects take again from the last one down, before a new block is taken: a worker of a parallel search, which owns a pool, drops its nodes at a restart without freeing them one by one. We call an epoch the time from one clear() (or from the construction) to the next one: nothing survives a new epoch, neither an object nor a free slot, and a pointer of an old epoch is stale, its slot given again by the make() of the new one. Using a stale pointer, free() included, violates the contract; fresh( p ) says whether \( p \) is a slot given in the current epoch, which free() and deallocate() check at the level of the audits (a stale pointer passes only when its slot has been given again), and under the sanitizer of addresses the slots dropped by clear() stay forbidden until they are given again (see Epochs of the arenas and of the pools). A pool belongs to one thread: an object is freed by the thread that made it, and an object that another thread wants to drop goes back to the owner by a message, to be freed there (or is dropped by the next clear()); other threads may read the objects, each access happening before the clear() of the owner (e.g., by a barrier). ConcurrentPoolResource [see ConcurrentPool.h] takes the frees of any thread instead. With clear() never called, make() and free() are the same as before.

The pool moves and swaps with its allocator, which gives back the blocks it gave; an allocator that cannot be assigned, as std::pmr::polymorphic_allocator, is moved by building the pools again.

The contracts [see Contract.h]: a block holds at least one slot, free() and deallocate() need an object (a slot) of the pool given in the epoch (audit); the invariant is that size() objects are alive. The operations are defined in ObjectPool.tpp.

See also
D. E. Knuth, The Art of Computer Programming, vol. 1, 3rd ed., Addison-Wesley, 1997, section 2.2.3 (the list AVAIL)
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa