00001 // Filename: uniqueIdAllocator.h 00002 // Created by: schuyler 2003-03-13 00003 // 00004 //////////////////////////////////////////////////////////////////// 00005 // 00006 // PANDA 3D SOFTWARE 00007 // Copyright (c) 2001, Disney Enterprises, Inc. All rights reserved 00008 // 00009 // All use of this software is subject to the terms of the Panda 3d 00010 // Software license. You should have received a copy of this license 00011 // along with this source code; you will also find a current copy of 00012 // the license at http://www.panda3d.org/license.txt . 00013 // 00014 // To contact the maintainers of this program write to 00015 // panda3d@yahoogroups.com . 00016 // 00017 //////////////////////////////////////////////////////////////////// 00018 // 00019 00020 #ifndef _UNIQUEIDALLOCATOR_H //[ 00021 #define _UNIQUEIDALLOCATOR_H 00022 00023 #include "pandabase.h" 00024 00025 typedef unsigned long U32; 00026 00027 00028 //////////////////////////////////////////////////////////////////// 00029 // Class : UniqueIdAllocator 00030 // Description : Manage a set of ID values from min to max inclusive. 00031 // The ID numbers that are freed will be allocated 00032 // (reused) in the same order. I.e. the oldest ID numbers 00033 // will be allocated. 00034 // 00035 // This implementation will use 4 bytes per id number, 00036 // plus a few bytes of management data. e.g. 10,000 00037 // ID numbers will use 40KB. 00038 // 00039 // Also be advised that ID -1 and -2 are used internally by 00040 // the allocator. If allocate returns IndexEnd (-1) then 00041 // the allocator is out of free ID numbers. 00042 // 00043 // There are other implementations that can better leverage 00044 // runs of used or unused IDs or use bit arrays for the 00045 // IDs. But, it takes extra work to track the age of 00046 // freed IDs, which is required for what we wanted. If 00047 // you would like to kick around other implementation 00048 // ideas, please contact Schuyler. 00049 //////////////////////////////////////////////////////////////////// 00050 class EXPCL_PANDA UniqueIdAllocator { 00051 PUBLISHED: 00052 UniqueIdAllocator(U32 min=0, U32 max=20); 00053 ~UniqueIdAllocator(); 00054 U32 allocate(); 00055 void free(U32 index); 00056 float percent_used() const; 00057 void output(ostream& os, bool verbose=false) const; 00058 00059 public: 00060 static const U32 IndexEnd=(U32)-1; 00061 00062 protected: 00063 static const U32 IndexAllocated=(U32)-2; 00064 U32* _table; 00065 U32 _min; 00066 U32 _max; 00067 U32 _next_free; 00068 U32 _last_free; 00069 U32 _size; 00070 U32 _free; 00071 }; 00072 00073 #endif //]