Created
January 27, 2009 14:49
-
-
Save godfat/53367 to your computer and use it in GitHub Desktop.
object_pool.hpp.eruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<% header_guard do %> | |
#include <boost/pool/object_pool.hpp> | |
#include <boost/thread/mutex.hpp> | |
#include <boost/thread/thread.hpp> | |
#define LOKI_CLASS_LEVEL_THREADING | |
#include <loki/Singleton.h> | |
namespace psc{ namespace utils{ | |
template <typename T, typename UserAllocator = boost::default_user_allocator_new_delete> | |
class extended_object_pool: public boost::object_pool<T, UserAllocator>{ | |
public: | |
typedef typename boost::object_pool<T, UserAllocator>::element_type element_type; | |
using boost::object_pool<T, UserAllocator>::construct; | |
<% for_template_parameters_within(4..5, []){ |args_list| %> | |
template <%= template_parameters args_list %> | |
element_type * construct(<%= forward_parameters args_list %>){ | |
element_type * const ret = this->malloc_mt(); | |
if (ret == 0) | |
return ret; | |
try { new (ret) element_type(<%= arguments args_list %>); } | |
catch (...) { this->free_mt(ret); throw; } | |
return ret; | |
} | |
<% } %> | |
public: | |
boost::mutex& mutex(){ return mutex_; } | |
private: | |
element_type* malloc_mt(){ | |
boost::mutex::scoped_lock lock(mutex()); | |
return this->malloc(); | |
} | |
void free_mt(element_type* p){ | |
boost::mutex::scoped_lock lock(mutex()); | |
return this->free(p); | |
} | |
boost::mutex mutex_; | |
}; | |
template <class T> | |
class ObjectPool{ | |
public: | |
typedef typename T::pointer_type element_type; | |
public: | |
static element_type create(){ | |
return element_type(SPool::Instance().construct(), Deleter()); | |
} | |
<% for_template_parameters_within(1..5, []){ |args_list| %> | |
template <%= template_parameters args_list %> | |
static element_type create(<%= forward_parameters args_list %>){ | |
return element_type(SPool::Instance().construct(<%= arguments args_list %>), Deleter()); | |
} | |
<% } %> | |
static void destroy_all(){ | |
Loki::DeletableSingleton<pool_type>::GracefulDelete(); | |
} | |
private: | |
typedef extended_object_pool<T> pool_type; | |
typedef Loki::SingletonHolder<pool_type, Loki::CreateUsingNew, Loki::DeletableSingleton> SPool; | |
static void destroy(T* t){ | |
SPool::Instance().destroy(t); | |
} | |
static bool is_from(T* t){ | |
return SPool::Instance().is_from(t); | |
} | |
friend class Deleter; | |
class Deleter{ | |
public: | |
typedef void result_type; | |
typedef T* argument_type; | |
public: | |
// u.is_from(p) bool Returns true if p was allocated from u or may be returned as the result of a future allocation from u. Returns false if p was allocated from some other pool or may be returned as the result of a future allocation from some other pool. Otherwise, the return value is meaningless; note that this function may not be used to reliably test random pointer values. | |
void operator()(argument_type p){ | |
boost::mutex::scoped_lock lock(SPool::Instance().mutex()); | |
if(ObjectPool<T>::is_from(p)) | |
ObjectPool<T>::destroy(p); | |
} | |
}; | |
}; | |
}} // end of namespace | |
<% end %> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment