![]() |
dfx 0.1.0
Linux-based dynamic dataflow executor
|
ID allocator that can recycle released identifiers. More...
#include <dfx-utilities/RecyclableIdAllocator.hpp>
Public Types | |
| using | value_type = T |
| Type used for allocated identifiers. | |
Public Member Functions | |
| value_type | next () noexcept |
| Allocate and return an identifier. | |
| void | release (value_type id) |
| Release an identifier back to the allocator. | |
| void | reserve (value_type id) |
| Mark an identifier as in-use without returning it from next(). | |
| void | reset () noexcept |
| Reset the allocator to its initial state. | |
ID allocator that can recycle released identifiers.
RecyclableIdAllocator hands out integral identifiers and supports releasing them back to the allocator so they can be reused later.
When the allocator observes that all allocated/reserved IDs have been released (tracked via an internal active counter), it automatically resets itself back to the initial state. This keeps memory usage bounded and avoids retaining large free-lists after bursty allocations.
| T | Integral type used for identifiers (e.g. uint32_t, uint64_t). |
| using dfx::Utils::RecyclableIdAllocator< T >::value_type = T |
Type used for allocated identifiers.
|
inlinenoexcept |
Allocate and return an identifier.
If there are released IDs, returns the smallest available released ID. Otherwise returns the next fresh ID from the end-of-sequence counter.
|
inline |
Release an identifier back to the allocator.
After release(id), the allocator may return id again in a future call to next().
Behavior details:
id was never allocated/reserved by this allocator (i.e. id >= _nextId), the call is ignored.id has already been released (double-free), the call is ignored.id is the last end-of-sequence ID (i.e. _nextId == id + 1), the allocator shrinks _nextId instead of inserting into the heap.| id | Identifier to release. |
|
inline |
Mark an identifier as in-use without returning it from next().
This is useful when you need to "skip" or "pin" a specific ID because it is already used externally (e.g. restoring from persisted state, importing objects with fixed IDs, or reserving a well-known ID).
Two cases:
id is beyond the current allocation range (id >= _nextId), the allocator:id as active (reserved).id is within range (id < _nextId), then id must currently be free (previously released). In that case it is removed from the free lists.| id | Identifier to reserve. |
| Whatever | B_ASSERT throws/aborts with if id < _nextId but the ID is not actually free. |
|
inlinenoexcept |
Reset the allocator to its initial state.
After reset(), the next call to next() returns 0.
This clears: