3 // Copyright (c) 2005 David Ward
5 #ifndef __SimplePooledAlloc_h__
6 #define __SimplePooledAlloc_h__
8 // CSimplePooledAlloc allocates objects T in fixed-size blocks (specified)
9 // Alloc returns a default-constructed T*
10 // Memory is only freed on destruction of the allocator
16 class CSimplePooledAlloc
{
18 // Construct with given block size
19 CSimplePooledAlloc(std::size_t iBlockSize
);
21 ~CSimplePooledAlloc();
23 // Return an uninitialized object
29 CPool(std::size_t iSize
):m_iCurrent(0), m_iSize(iSize
) {
30 m_pData
= new T
[m_iSize
];
36 if(m_iCurrent
< m_iSize
)
37 return &m_pData
[m_iCurrent
++];
41 mutable std::size_t m_iCurrent
;
46 std::vector
< CPool
* >m_vPool
;
48 std::size_t m_iBlockSize
;
52 template < typename T
> CSimplePooledAlloc
< T
>::CSimplePooledAlloc(std::size_t iSize
):m_iBlockSize(iSize
), m_iCurrent(0) {
53 m_vPool
.push_back(new CPool(m_iBlockSize
));
56 template < typename T
> CSimplePooledAlloc
< T
>::~CSimplePooledAlloc() {
57 for(std::size_t i
= 0; i
< m_vPool
.size(); i
++)
61 template < typename T
> T
* CSimplePooledAlloc
< T
>::Alloc() {
62 T
*p
= m_vPool
[m_iCurrent
]->Alloc();
65 m_vPool
.push_back(new CPool(m_iBlockSize
));
67 return m_vPool
.back()->Alloc();