Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef TMemoryPool_h__
- #define TMemoryPool_h__
- #include "Globals.h"
- #include <vector>
- template <class T>
- class TMemoryPool
- {
- public:
- TMemoryPool() { /* ... */ };
- ~TMemoryPool() { /* ... */ };
- // Initializes the memory pool.
- tgBool InitializeMemoryPool(tgUInt32 BlockSize = 32);
- // Destroys the memory pool.
- tgBool DestroyMemoryPool();
- // Acquires a free resource from the memory pool.
- T* AcquireResource();
- // Drops a used resource from the memory pool.
- tgBool DropResource(T* Resource);
- protected:
- // Allocates a new block size worth of memory.
- void AllocateResourceBlock();
- // Block size for our memory pool.
- tgUInt32 m_BlockSize;
- // Memory pool handle used internally.
- template <class T>
- class TMemoryPoolHandle
- {
- public:
- TMemoryPoolHandle() : Used(FALSE)
- {
- Resource = tgNew T;
- #ifdef _DEBUG
- tgLogPrint("MemoryPool ::: Resource, reporting for duty!");
- #endif
- }
- ~TMemoryPoolHandle()
- {
- tgDelete Resource;
- }
- T* Resource;
- tgBool Used;
- };
- // Memory pool typedef.
- typedef std::vector < TMemoryPoolHandle < T > * > MemoryPool;
- // Our memory pool.
- MemoryPool m_Pool;
- };
- template <class T>
- tgBool TMemoryPool<T>::InitializeMemoryPool( tgUInt32 BlockSize /*= 32*/ )
- {
- m_BlockSize = BlockSize;
- // Allocate an initial block.
- AllocateResourceBlock();
- return TRUE;
- }
- template <class T>
- tgBool TMemoryPool<T>::DestroyMemoryPool()
- {
- MemoryPool::iterator it = m_Pool.begin();
- while (it != m_Pool.end())
- {
- tgDelete (*it);
- it = m_Pool.erase(it);
- }
- // Destroy all allocated resources.
- //m_Pool.clear();
- return TRUE;
- }
- template <class T>
- T* TMemoryPool<T>::AcquireResource()
- {
- MemoryPool::iterator it = m_Pool.begin();
- while (it != m_Pool.end())
- {
- T& Handle = (*it);
- if (Handle.Used == FALSE)
- {
- Handle.Used = TRUE;
- return Handle.Resource;
- }
- }
- // If we got this far, allocate more memory.
- // Store the current size of our pool; this is where our newly allocated
- // memory block will start.
- const tgUInt32 NewBlockStart = m_Pool.size();
- // Allocate the new block.
- AllocateResourceBlock();
- // Retrieve the most recent resource handle.
- T& Handle = m_Pool[NewBlockStart];
- // Mark as used, return resouce.
- Handle.Used = TRUE;
- return Handle.Resource;
- }
- template <class T>
- tgBool TMemoryPool<T>::DropResource( T* Resource )
- {
- MemoryPool::iterator it = m_Pool.begin();
- while (it != m_Pool.end())
- {
- T& Handle = (*it);
- if (Handle.Resource == Resource)
- {
- Handle.Used = FALSE;
- return TRUE;
- }
- }
- return FALSE;
- }
- template <class T>
- void TMemoryPool<T>::AllocateResourceBlock()
- {
- // Reserve space in our vector.
- m_Pool.reserve(m_Pool.size() + m_BlockSize);
- // Allocate new resources.
- for (tgUInt32 i = 0; i < m_BlockSize; ++i)
- {
- m_Pool.push_back(tgNew TMemoryPoolHandle<T>);
- }
- #ifdef _DEBUG
- tgLogPrint("MemoryPool ::: New block allocated - total size is %u", m_Pool.size());
- #endif
- }
- #endif // TMemoryPool_h__
Add Comment
Please, Sign In to add comment