468,119 Members | 2,108 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,119 developers. It's quick & easy.

boost pool per class...

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::pool 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<SmallObjectx( new SmallObject );
}
Nov 17 '07 #1
1 1737
On Nov 17, 11:32 am, werasm <wer...@gmail.comwrote:
//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>
{

};
Excuse me for making a habit of replying to my own posts :-|. I
should probably add operator new[] and delete[] operators as
well. Consider it done. Any other comments welcome.
Nov 17 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by porschberg | last post: by
1 post views Thread by Noah Roberts | last post: by
3 posts views Thread by Chris Jones | last post: by
3 posts views Thread by neino | last post: by
4 posts views Thread by mavrik | last post: by
6 posts views Thread by Juha Nieminen | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.