//////////////////////////////////////////////////////////////////////////////////////// // RAVEN STANDARD TEMPLATE LIBRARY // (c) 2002 Activision // // // Handle Pool // ----------- // The memory pool class is a simple routine for constant time allocation and deallocation // from a fixed size pool of objects. The class uses a simple array to hold the actual // data, a queue for the free list, and a bit field to mark which spots in the array // are allocated. // // In addition to the standard memory pool features, this Handle Pool provides a fast // iterator, asserts on attempting to access unused data, and a unique ID "handle" for // the external system to use. // // // // NOTES: // // // //////////////////////////////////////////////////////////////////////////////////////// #if !defined(RATL_HANDLE_POOL_VS_INC) #define RATL_HANDLE_POOL_VS_INC //////////////////////////////////////////////////////////////////////////////////////// // Includes //////////////////////////////////////////////////////////////////////////////////////// #if !defined(RATL_COMMON_INC) #include "ratl_common.h" #endif #if !defined(RATL_POOL_VS_INC) #include "pool_vs.h" #endif namespace ratl { template class handle_pool_base : public pool_root { public: typedef typename T TStorageTraits; typedef typename T::TValue TTValue; //////////////////////////////////////////////////////////////////////////////////// // Capacity Enum //////////////////////////////////////////////////////////////////////////////////// enum { CAPACITY = T::CAPACITY }; private: int mHandles[CAPACITY]; int mMASK_HANDLE_TO_INDEX; int mMASK_NUM_BITS; void IncHandle(int index) { mHandles[index] += (1<>= 1; mMASK_HANDLE_TO_INDEX <<= 1; mMASK_HANDLE_TO_INDEX |= 1; mMASK_NUM_BITS++; } for (int i=0; i::clear(); // note that we do not refill the handles cause we want old handles to still be stale for (int i=0; i::free_index(index); IncHandle(index); } //////////////////////////////////////////////////////////////////////////////////// // The Deallocator, by handle //////////////////////////////////////////////////////////////////////////////////// void free(int handle) { assert(is_used(handle)); free_index(handle&mMASK_HANDLE_TO_INDEX); } //////////////////////////////////////////////////////////////////////////////////// // The Deallocator, by pointer //////////////////////////////////////////////////////////////////////////////////// void free(TTValue *me) { free_index(pointer_to_index(me)); } //////////////////////////////////////////////////////////////////////////////////// // Convert a handle to a raw index, not generally something you should use //////////////////////////////////////////////////////////////////////////////////// int handle_to_index(int handle) const { assert(is_used(handle)); return (handle&mMASK_HANDLE_TO_INDEX); } //////////////////////////////////////////////////////////////////////////////////// // FInd the handle for a given index, this cannot check for stale handles, so it is ill advised //////////////////////////////////////////////////////////////////////////////////// int index_to_handle(int index) const { assert(index>=0 && index::iterator at(int handle) { assert(is_used(handle)); return at_index(handle&mMASK_HANDLE_TO_INDEX); } //////////////////////////////////////////////////////////////////////////////////// // Get An Iterator To The Object At handle //////////////////////////////////////////////////////////////////////////////////// pool_root::const_iterator at(int handle) const { assert(is_used(handle)); return at_index(handle&mMASK_HANDLE_TO_INDEX); } }; template class handle_pool_vs : public handle_pool_base > { public: typedef typename storage::value_semantics TStorageTraits; typedef typename TStorageTraits::TValue TTValue; enum { CAPACITY = ARG_CAPACITY }; handle_pool_vs() {} }; template class handle_pool_os : public handle_pool_base > { public: typedef typename storage::object_semantics TStorageTraits; typedef typename TStorageTraits::TValue TTValue; enum { CAPACITY = ARG_CAPACITY }; handle_pool_os() {} }; template class handle_pool_is : public handle_pool_base > { public: typedef typename storage::virtual_semantics TStorageTraits; typedef typename TStorageTraits::TValue TTValue; enum { CAPACITY = ARG_CAPACITY, MAX_CLASS_SIZE = ARG_MAX_CLASS_SIZE }; handle_pool_is() {} }; } #endif