diff --git a/panda/src/putil/uniqueIdAllocator.cxx b/panda/src/putil/uniqueIdAllocator.cxx index eae422e289..13eaa123b7 100644 --- a/panda/src/putil/uniqueIdAllocator.cxx +++ b/panda/src/putil/uniqueIdAllocator.cxx @@ -63,7 +63,7 @@ UniqueIdAllocator(U32 min, U32 max) for (U32 i=0; i<_size; ++i) { _table[i]=i+1; } - _table[_size-1]=-1; + _table[_size-1]=IndexEnd; _next_free=0; _last_free=_size-1; _free=_size; @@ -86,18 +86,19 @@ UniqueIdAllocator:: // Access: // Description: Receive an id between _min and _max (that were passed // to the constructor). -// -1 is returned if no ids are available. +// IndexEnd is returned if no ids are available. //////////////////////////////////////////////////////////////////// U32 UniqueIdAllocator:: allocate() { - if (_next_free==-1) { + if (_next_free==IndexEnd) { // ...all ids allocated. uniqueIdAllocator_warning("allocate Error: no more free ids."); - return -1; + return IndexEnd; } U32 id=_min+_next_free; _next_free=_table[_next_free]; - nassertr(_table[id-_min]=-2, -1); // this assignment is debug only. + // This assert will not fire because it is assigning not comparing. This is intentional. + nassertr(_table[id-_min]=IndexAllocated, IndexEnd); // this assignment is debug only. --_free; uniqueIdAllocator_debug("allocate() returning "<=_min); // Attempt to free out-of-range id. nassertv(index<=_max); // Attempt to free out-of-range id. index=index-_min; // Convert to _table index. - nassertv(_table[index]==-2); // Attempt to free non-allocated id. - _table[index]=-1; // Mark this element as the end of the list. + nassertv(_table[index]==IndexAllocated); // Attempt to free non-allocated id. + _table[index]=IndexEnd; // Mark this element as the end of the list. _table[_last_free]=index; - if (_next_free==-1) { + if (_next_free==IndexEnd) { // ...the free list was empty. _next_free=index; } diff --git a/panda/src/putil/uniqueIdAllocator.h b/panda/src/putil/uniqueIdAllocator.h index 347359b4ff..9d20e8b5d9 100644 --- a/panda/src/putil/uniqueIdAllocator.h +++ b/panda/src/putil/uniqueIdAllocator.h @@ -36,6 +36,10 @@ typedef unsigned long U32; // 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 @@ -52,7 +56,11 @@ PUBLISHED: float percent_used() const; void output(ostream& os, bool verbose=false) const; +public: + static const U32 IndexEnd=(U32)-1; + protected: + static const U32 IndexAllocated=(U32)-2; U32* _table; U32 _min; U32 _max;