Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

UniqueIdAllocator Class Reference

Manage a set of ID values from min to max inclusive. More...

#include <uniqueIdAllocator.h>

List of all members.

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


Detailed Description

Manage a set of ID values from min to max inclusive.

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.


Constructor & Destructor Documentation

UniqueIdAllocator::UniqueIdAllocator U32  min = 0,
U32  max = 20
 

Create a free id pool in the range [min:max].

Definition at line 62 of file uniqueIdAllocator.cxx.

References U32.

UniqueIdAllocator::~UniqueIdAllocator  ) 
 

Definition at line 87 of file uniqueIdAllocator.cxx.

References _free, _min, _next_free, _table, IndexAllocated, IndexEnd, U32, uniqueIdAllocator_debug, and uniqueIdAllocator_warning.


Member Function Documentation

U32 UniqueIdAllocator::allocate  ) 
 

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.

void UniqueIdAllocator::free U32  index  ) 
 

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.

References _free, and _size.

void UniqueIdAllocator::output ostream &  os,
bool  verbose = false
const
 

...intended for debugging only.

Definition at line 176 of file uniqueIdAllocator.cxx.

float UniqueIdAllocator::percent_used  )  const
 

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.


Member Data Documentation

U32 UniqueIdAllocator::_free [protected]
 

Definition at line 94 of file uniqueIdAllocator.h.

Referenced by free(), and ~UniqueIdAllocator().

U32 UniqueIdAllocator::_last_free [protected]
 

Definition at line 92 of file uniqueIdAllocator.h.

U32 UniqueIdAllocator::_max [protected]
 

Definition at line 90 of file uniqueIdAllocator.h.

U32 UniqueIdAllocator::_min [protected]
 

Definition at line 89 of file uniqueIdAllocator.h.

Referenced by ~UniqueIdAllocator().

U32 UniqueIdAllocator::_next_free [protected]
 

Definition at line 91 of file uniqueIdAllocator.h.

Referenced by ~UniqueIdAllocator().

U32 UniqueIdAllocator::_size [protected]
 

Definition at line 93 of file uniqueIdAllocator.h.

Referenced by free().

U32* UniqueIdAllocator::_table [protected]
 

Definition at line 88 of file uniqueIdAllocator.h.

Referenced by ~UniqueIdAllocator().

const U32 UniqueIdAllocator::IndexAllocated = (U32)-2 [static, protected]
 

Definition at line 87 of file uniqueIdAllocator.h.

Referenced by ~UniqueIdAllocator().

const U32 UniqueIdAllocator::IndexEnd = (U32)-1 [static]
 

Definition at line 84 of file uniqueIdAllocator.h.

Referenced by ~UniqueIdAllocator().


The documentation for this class was generated from the following files:
Generated on Fri May 2 00:56:02 2003 for Panda by doxygen1.3