Dobby
3.0
Dobby “Docker based Thingy” is a tool for managing and running OCI containers using crun
|
Class used to generate unique numbers. More...
#include <IDGenerator.h>
Public Member Functions | |
IDGenerator (unsigned offset=0) | |
int | get () |
bool | put (int id) |
void | clear () |
Private Attributes | |
const unsigned | mOffset |
unsigned | mLfsr |
std::mutex | mLock |
std::bitset< mSize > | mUsed |
Static Private Attributes | |
static const unsigned | mSize = (1u << N) |
static const unsigned | mPolynomial |
Class used to generate unique numbers.
Why? there a few places in the code were we create some resource and then return a unique 'id' for it, this is typically done for resources created over dbus.
This class is guaranteed to return unique id numbers in a non-linear sequence.
The N template parameters refers to the number of bits in the generator range. Avoid large values for N, as for each entry we allocate a bit to tell if it's in use or not, in addition in the worst case we have to iterate through all 2^N possible values to find a free one.
The api has a get() and a put() operation, obviously get() returns a new id and put() releases the id back to the pool. The id's returned are not sequential, instead they are created using a pseudo random repeating sequence (fibonacci LFSR).
When the pool is exhausted get() will return -1.
|
staticprivate |