Boost logo

Boost Users :

From: wugui (yg-boost-users_at_[hidden])
Date: 2002-12-06 01:45:16


> Destroying single objects that were created from an object_pool uses
> ordered_free, as you discovered. An object_pool will keep its free list
> ordered, so that it can run through the "list of allocated objects" in
O(N)
> time instead of O(N**2) time in ~object_pool. It's a performance tradeoff
> with object_pool::destroy; object_pool was designed more for a "allocate a
> bunch of objects and then destroy all of them" type of scenario.
>
> -Steve
>
thanks.

if i only construct one and destroy exactly.
is the order_xxx not need now? and (maybe?) i don't care the performance
in ~object_pool.

i use pool to avoid mem-piece.
so i implement a simple ObjectPool (see below).
i think random construct and destroy is fast now.
is right?

#include <boost/pool/pool.hpp>

template <typename T>
class ObjectPool
{
public:
    ObjectPool() : rawpool(sizeof(T)) {}
    T * construct()
    {
        T * ret = static_cast<T *>(rawpool.malloc());
        if (ret == 0)
            return ret;
        try { new (ret) T(); }
        catch (...) { rawpool.free(ret); throw; }
        return ret;
    }
    void destroy(T *p)
    {
        p->~T();
        rawpool.free(p);
    }
private:
    boost::pool<> rawpool;
};


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