#include <uniqueIdAllocator.h>
Public Member Functions | |
UniqueIdAllocator (U32 min=0, U32 max=20) | |
Create a free id pool in the range [min:max]. | |
~UniqueIdAllocator () | |
U32 | allocate () |
Receive an id between _min and _max (that were passed to the constructor). | |
void | free (U32 index) |
Free an allocated index (index must be between _min and _max that were passed to the constructor). | |
float | percent_used () const |
return what percentage of the pool is used. | |
void | output (ostream &os, bool verbose=false) const |
...intended for debugging only. | |
Static Public Attributes | |
const U32 | IndexEnd = (U32)-1 |
Protected Attributes | |
U32 * | _table |
U32 | _min |
U32 | _max |
U32 | _next_free |
U32 | _last_free |
U32 | _size |
U32 | _free |
Static Protected Attributes | |
const U32 | IndexAllocated = (U32)-2 |
The ID numbers that are freed will be allocated (reused) in the same order. I.e. the oldest ID numbers will be allocated.
This implementation will use 4 bytes per id number, plus a few bytes of management data. e.g. 10,000 ID numbers will use 40KB.
Also be advised that ID -1 and -2 are used internally by the allocator. If allocate returns IndexEnd (-1) then the allocator is out of free ID numbers.
There are other implementations that can better leverage runs of used or unused IDs or use bit arrays for the IDs. But, it takes extra work to track the age of freed IDs, which is required for what we wanted. If you would like to kick around other implementation ideas, please contact Schuyler.
Definition at line 74 of file uniqueIdAllocator.h.
|
Create a free id pool in the range [min:max].
Definition at line 62 of file uniqueIdAllocator.cxx. References U32. |
|
Definition at line 87 of file uniqueIdAllocator.cxx. References _free, _min, _next_free, _table, IndexAllocated, IndexEnd, U32, uniqueIdAllocator_debug, and uniqueIdAllocator_warning. |
|
Receive an id between _min and _max (that were passed to the constructor). IndexEnd is returned if no ids are available. Definition at line 106 of file uniqueIdAllocator.cxx. |
|
Free an allocated index (index must be between _min and _max that were passed to the constructor).
Definition at line 134 of file uniqueIdAllocator.cxx. |
|
...intended for debugging only.
Definition at line 176 of file uniqueIdAllocator.cxx. |
|
return what percentage of the pool is used. The range is 0 to 1.0, so 75% would be 0.75, for example. Definition at line 162 of file uniqueIdAllocator.cxx. |
|
Definition at line 94 of file uniqueIdAllocator.h. Referenced by free(), and ~UniqueIdAllocator(). |
|
Definition at line 92 of file uniqueIdAllocator.h. |
|
Definition at line 90 of file uniqueIdAllocator.h. |
|
Definition at line 89 of file uniqueIdAllocator.h. Referenced by ~UniqueIdAllocator(). |
|
Definition at line 91 of file uniqueIdAllocator.h. Referenced by ~UniqueIdAllocator(). |
|
Definition at line 93 of file uniqueIdAllocator.h. Referenced by free(). |
|
Definition at line 88 of file uniqueIdAllocator.h. Referenced by ~UniqueIdAllocator(). |
|
Definition at line 87 of file uniqueIdAllocator.h. Referenced by ~UniqueIdAllocator(). |
|
Definition at line 84 of file uniqueIdAllocator.h. Referenced by ~UniqueIdAllocator(). |