Dobby  3.0
Dobby “Docker based Thingy” is a tool for managing and running OCI containers using crun
Public Member Functions | Private Attributes | Static Private Attributes | List of all members
AICommon::IDGenerator< N > Class Template Reference

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
 

Detailed Description

template<std::size_t N>
class AICommon::IDGenerator< N >

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.

Member Data Documentation

◆ mPolynomial

template<std::size_t N>
const unsigned AICommon::IDGenerator< N >::mPolynomial
staticprivate
Initial value:
= (N == 4) ? 0x9 :
(N == 5) ? 0x1B :
(N == 6) ? 0x36 :
(N == 7) ? 0x5F :
(N == 8) ? 0xE1 :
(N == 9) ? 0x1B0 :
(N == 10) ? 0x3A6 :
(N == 11) ? 0x574 :
(N == 12) ? 0xC48 :
(N == 13) ? 0x11D4 :
(N == 14) ? 0x214E :
(N == 15) ? 0x41A6 :
(N == 16) ? 0x84BE :
(N == 17) ? 0x1022E :
(N == 18) ? 0x20196 :
(N == 19) ? 0x4032F :
(N == 20) ? 0x80534 : 0

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