RE: [Boost-Users] object_pool use ordered_malloc ordered_free. wh y?
if construct only, and freeall in ~object_pool. i think it is good.
Yes. The destuctor of object_pool will call the destructor of each allocated or constructed object that has not yet been deallocated or destroyed.
can i use object_pool to replace 'new delete'?
object_pool provides object-based allocation and deallocation, very similar to new and delete.
the destroy is slow.
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
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; };
participants (2)
-
sclearyï¼ jerviswebb.com
-
wugui