Simple vector-based memory pool allocator.
Simple vector-based memory pool allocator This template can be used to provide simple pooled memory allocation from a container class that adheres to the stl random access container concept. Note that alloc/free works with indices into the container!
vector_pool<type> myPool;
int nIdx=myPool.alloc();
myPool[nIdx] = myVal;
... do stuff ...
myPool.free(nIdx);
#ifndef INCLUDED_O3TL_VECTOR_POOL_HXX
#define INCLUDED_O3TL_VECTOR_POOL_HXX
#include <utility>
#include <vector>
{
{
template<typename ValueType, class Container> class simple_pool_impl :
public Container
{
typedef typename Container::value_type
value_type;
public:
{}
{
}
{
{
this->at(nIdx).value = rCopy;
this->at(nIdx).nextFree = -1;
return nIdx;
}
else
{
}
}
void free( std::ptrdiff_t nIdx )
{
}
{
return this->operator[](nIdx).value;
}
{
return this->operator[](nIdx).value;
}
};
template< typename ValueType > struct struct_from_value
{
{
{}
{}
};
};
}
template<typename ValueType> struct vector_pool :
public detail::simple_pool_impl<ValueType,
std::vector<typename detail::struct_from_value<ValueType>::type > >
{};
}
#endif
Container::value_type value_type
void free(std::ptrdiff_t nIdx)
std::ptrdiff_t store(const ValueType &rCopy)
const ValueType & get(std::ptrdiff_t nIdx) const
std::ptrdiff_t mnFirstFreeIndex