From 2ae297a7c7df3254ddf24f9e9172b5288ad48714 Mon Sep 17 00:00:00 2001 From: Dave Schuyler Date: Sat, 15 Mar 2003 03:40:49 +0000 Subject: [PATCH] no more exit() calls --- panda/src/putil/test_uniqueIdAllocator.cxx | 93 ++++--- panda/src/putil/uniqueIdAllocator.cxx | 302 +++++++++++---------- panda/src/putil/uniqueIdAllocator.h | 101 +++---- 3 files changed, 261 insertions(+), 235 deletions(-) diff --git a/panda/src/putil/test_uniqueIdAllocator.cxx b/panda/src/putil/test_uniqueIdAllocator.cxx index d325f814d4..27b931006c 100644 --- a/panda/src/putil/test_uniqueIdAllocator.cxx +++ b/panda/src/putil/test_uniqueIdAllocator.cxx @@ -1,47 +1,46 @@ - - -#include "stdafx.h" - -#include -#include -#include -using namespace std; - -#include "uniqueIdAllocator.h" - - -int _tmain(int argc, _TCHAR* argv[]) { - cout <<"UniqueIdAllocator Test"< +#include +#include +using namespace std; + +#include "uniqueIdAllocator.h" + + +int _tmain(int argc, _TCHAR* argv[]) { + cout <<"UniqueIdAllocator Test"< 0. - _table=new U32[_size]; - assert(_table); // This should be redundant if new throws an exception. - for (U32 i=0; i<_size; ++i) { - _table[i]=i+1; - } - _table[_size-1]=-1; - _next_free=0; - _last_free=_size-1; - _free=_size; -} - -//////////////////////////////////////////////////////////////////// -// Function: -// Access: -// Description: -//////////////////////////////////////////////////////////////////// -UniqueIdAllocator:: -~UniqueIdAllocator() { - //cout<<"UniqueIdAllocator::~UniqueIdAllocator()"<>2)) { - // ...under 1/4 of the ids are free. - cerr<<"UniqueIdAllocator Error: 75% of ids allocated."<=_min); // Attempt to free out-of-range id. - assert(index<=_max); // Attempt to free out-of-range id. - index=index-_min; - assert(_table[index]==-2); // Attempt to free non-allocated id. - _table[index]=-1; - _table[_last_free]=index; - #if 0 //[ - // This is only necessary if the free pool is allowed to go empty. - // Since we don't allow that, it is an optimization to comment - // this out. - if (_next_free==-1) { - _next_free=index; - } - #endif //] - _last_free=index; - ++_free; -} - - -//////////////////////////////////////////////////////////////////// -// Function: -// Access: -// Description: ...intended for debugging only. -//////////////////////////////////////////////////////////////////// -void UniqueIdAllocator:: -printTo(ostream& os) const { - os <<"[_next_free: "<debug() << msg << endl; \ + } else {} + + #define uniqueIdAllocator_info(msg) \ + uniqueIdAllocator_cat->info() << msg << endl + + #define uniqueIdAllocator_warning(msg) \ + uniqueIdAllocator_cat->warning() << msg << endl +#else //][ + // Release build: + #define uniqueIdAllocator_debug(msg) ((void)0) + #define uniqueIdAllocator_info(msg) ((void)0) + #define uniqueIdAllocator_warning(msg) ((void)0) +#endif //] + +#define audio_error(msg) \ + audio_cat->error() << msg << endl + +//////////////////////////////////////////////////////////////////// +// Function: +// Access: +// Description: Create a free id pool in the range [min:max]. +//////////////////////////////////////////////////////////////////// +UniqueIdAllocator:: +UniqueIdAllocator(U32 min, U32 max) + : _min(min), _max(max) { + uniqueIdAllocator_debug("UniqueIdAllocator("< 0. + _table=new U32[_size]; + assert(_table); // This should be redundant if new throws an exception. + for (U32 i=0; i<_size; ++i) { + _table[i]=i+1; + } + _table[_size-1]=-1; + _next_free=0; + _last_free=_size-1; + _free=_size; +} + +//////////////////////////////////////////////////////////////////// +// Function: +// Access: +// Description: +//////////////////////////////////////////////////////////////////// +UniqueIdAllocator:: +~UniqueIdAllocator() { + uniqueIdAllocator_debug("~UniqueIdAllocator()"); + delete [] _table; +} + + +//////////////////////////////////////////////////////////////////// +// Function: +// Access: +// Description: Receive an id between _min and _max (that were passed +// to the constructor). +// -1 is returned if no ids are available. +//////////////////////////////////////////////////////////////////// +U32 UniqueIdAllocator:: +allocate() { + if (_next_free==-1) { + // ...all ids allocated. + uniqueIdAllocator_warning("allocate Error: no more free ids."); + return -1; + } + U32 id=_min+_next_free; + _next_free=_table[_next_free]; + assert(_table[id-_min]=-2); // this assignment is debug only. + --_free; + uniqueIdAllocator_debug("allocate() returning "<=_min); // Attempt to free out-of-range id. + assert(index<=_max); // Attempt to free out-of-range id. + index=index-_min; // Convert to _table index. + assert(_table[index]==-2); // Attempt to free non-allocated id. + _table[index]=-1; // Mark this element as the end of the list. + _table[_last_free]=index; + if (_next_free==-1) { + // ...the free list was empty. + _next_free=index; + } + _last_free=index; + ++_free; +} + + +//////////////////////////////////////////////////////////////////// +// Function: +// Access: +// Description: return what percentage of the pool is used. The +// range is 0 to 1.0, so 75% would be 0.75, for example. +//////////////////////////////////////////////////////////////////// +float UniqueIdAllocator:: +percent_used() const { + return float(_size-_free)/_size; +} + + +//////////////////////////////////////////////////////////////////// +// Function: +// Access: +// Description: ...intended for debugging only. +//////////////////////////////////////////////////////////////////// +void UniqueIdAllocator:: +print_to(ostream& os, bool verbose) const { + os <<"[_next_free: "<