W
werasm
Hi,
This is not boost related per sé, therefore I'm posing the question
here.
I need to do many small object allocations, and I'm considering
using boost:ool for this. I've contemplated using Coplien's CRTP
to encapsulate the allocation/de-allocation and the pool per type.
Does anybody see a potential caveat here? One potential caveat
would be if the derived class overloads new/delete himself, but
that defeats the point of deriving in the first place. I made
the destructor protected as deletion would be through the
interface of the derived type - so to speak.
Comments welcome.
Werner
Example here:
#include <memory>
//Just to get things to compile...
class Default{};
template <class Alloc = Default>
struct BoostPool
{ explicit BoostPool( unsigned size); };
//Encapsulates small object allocation
// per type.
template <class AllocT>
class BoostPoolAllocatable
{
public:
static void* operator new( size_t );
static void operator delete( void*, size_t );
protected:
virtual ~BoostPoolAllocatable(){ }
private:
static BoostPool<> myPool_;
};
template <class AllocT>
BoostPool<> BoostPoolAllocatable<AllocT>::myPool_( sizeof(AllocT) );
//The small object...
struct SmallObject : BoostPoolAllocatable<SmallObject>
{
};
//Test
int main()
{
std::auto_ptr<SmallObject> x( new SmallObject );
}
This is not boost related per sé, therefore I'm posing the question
here.
I need to do many small object allocations, and I'm considering
using boost:ool for this. I've contemplated using Coplien's CRTP
to encapsulate the allocation/de-allocation and the pool per type.
Does anybody see a potential caveat here? One potential caveat
would be if the derived class overloads new/delete himself, but
that defeats the point of deriving in the first place. I made
the destructor protected as deletion would be through the
interface of the derived type - so to speak.
Comments welcome.
Werner
Example here:
#include <memory>
//Just to get things to compile...
class Default{};
template <class Alloc = Default>
struct BoostPool
{ explicit BoostPool( unsigned size); };
//Encapsulates small object allocation
// per type.
template <class AllocT>
class BoostPoolAllocatable
{
public:
static void* operator new( size_t );
static void operator delete( void*, size_t );
protected:
virtual ~BoostPoolAllocatable(){ }
private:
static BoostPool<> myPool_;
};
template <class AllocT>
BoostPool<> BoostPoolAllocatable<AllocT>::myPool_( sizeof(AllocT) );
//The small object...
struct SmallObject : BoostPoolAllocatable<SmallObject>
{
};
//Test
int main()
{
std::auto_ptr<SmallObject> x( new SmallObject );
}