tagging release
[dasher.git] / Src / Common / Allocators / PooledAlloc.h
blob7ea72acbe5a9a54cd09e400bed87928db0cbecd4
1 // PooledAlloc.h
2 //
3 // Copyright (c) 2005 David Ward
5 #ifndef __PooledAlloc_h__
6 #define __PooledAlloc_h__
8 // CPooledAlloc allocates objects T in fixed-size blocks (specified in the constructor)
9 // Alloc returns an uninitialized T*
10 // Free returns an object to the pool
12 #include "SimplePooledAlloc.h"
14 /////////////////////////////////////////////////////////////////////////////
16 template<typename T> class CPooledAlloc {
18 public:
20 // Construct with given block size
21 CPooledAlloc(size_t iBlockSize);
22 ~CPooledAlloc();
24 // Return an uninitialized object
25 T *Alloc();
27 // Return an object to the pool
28 void Free(T * pFree);
30 private:
32 // Use simple pooled alloc for the blocked allocation
33 CSimplePooledAlloc < T > m_Alloc;
35 // The free list
36 std::vector < T * >m_vpFree;
40 template<typename T> CPooledAlloc<T>::CPooledAlloc(size_t iSize):m_Alloc(iSize) {}
42 template<typename T> CPooledAlloc<T>::~CPooledAlloc() {}
44 template<typename T> T * CPooledAlloc < T >::Alloc() {
45 if(m_vpFree.size() > 0) {
46 T *pLast = m_vpFree.back();
47 m_vpFree.pop_back();
48 return pLast;
50 return m_Alloc.Alloc();
53 template<typename T> void CPooledAlloc<T>::Free(T *pFree) {
54 m_vpFree.push_back(pFree);
57 #endif // __include__