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
{
20 // Construct with given block size
21 CPooledAlloc(size_t iBlockSize
);
24 // Return an uninitialized object
27 // Return an object to the pool
32 // Use simple pooled alloc for the blocked allocation
33 CSimplePooledAlloc
< T
> m_Alloc
;
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();
50 return m_Alloc
.Alloc();
53 template<typename T
> void CPooledAlloc
<T
>::Free(T
*pFree
) {
54 m_vpFree
.push_back(pFree
);