Boost logo

Boost Users :

Subject: [Boost-users] Boost.Pool: Writing a base class for per-class pool allocation
From: Freddie Witherden (freddie_at_[hidden])
Date: 2011-11-03 08:04:11


Hi all,

I am currently trying to write a base class which overloads new and
delete so that they allocate from a boost pool:

template<typename T> class pool_object {
public:
    static void* operator new(size_t) { return pool_.malloc(); }

    static void operator delete(void *p) { pool_.free(p); }

private:
    static boost::pool<> pool_;
};

template<typename T>
boost::pool<> pool_object<T>::pool_(sizeof(T));

The idea is that one can write: class foo : public pool_object<foo> { ... };

The problem with this is that boost::pool is not thread-safe. The
thread-safe variant is a singleton pool. However, this is parametrized
by a size: boost::singleton_pool<sizeof(T)> -- which is a problem as T
is incomplete.

Are there any solutions other than sticking my own mutex into the class?

Regards, Freddie.




Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net