From 80686d4e567bae579ea39e009826a2de92cd4ace Mon Sep 17 00:00:00 2001 From: redpony Date: Wed, 11 Aug 2010 02:37:10 +0000 Subject: major refactor, break bad circular deps git-svn-id: https://ws10smt.googlecode.com/svn/trunk@509 ec762483-ff6d-05da-a07a-a48fb63a330f --- utils/intrusive_refcount.hpp | 84 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100755 utils/intrusive_refcount.hpp (limited to 'utils/intrusive_refcount.hpp') diff --git a/utils/intrusive_refcount.hpp b/utils/intrusive_refcount.hpp new file mode 100755 index 00000000..4a4b0187 --- /dev/null +++ b/utils/intrusive_refcount.hpp @@ -0,0 +1,84 @@ +#ifndef GRAEHL__SHARED__INTRUSIVE_REFCOUNT_HPP +#define GRAEHL__SHARED__INTRUSIVE_REFCOUNT_HPP + +#include +#include +#include +#include + +/** usage: + struct mine : public boost::instrusive_refcount {}; + + boost::intrusive_ptr p(new mine()); +*/ + +namespace boost { +// note: the free functions need to be in boost namespace, OR namespace of involved type. this is the only way to do it. + +template +class intrusive_refcount; + +template +class atomic_intrusive_refcount; + +template +void intrusive_ptr_add_ref(intrusive_refcount* ptr) +{ + ++(ptr->refs); +} + +template +void intrusive_ptr_release(intrusive_refcount* ptr) +{ + if (!--(ptr->refs)) delete static_cast(ptr); +} + + +//WARNING: only 2^32 (unsigned) refs allowed. hope that's ok :) +template +class intrusive_refcount : boost::noncopyable +{ + protected: +// typedef intrusive_refcount pointed_type; + friend void intrusive_ptr_add_ref(intrusive_refcount* ptr); + friend void intrusive_ptr_release(intrusive_refcount* ptr); +// friend class intrusive_ptr; + + intrusive_refcount(): refs(0) {} + ~intrusive_refcount() { assert(refs==0); } + +private: + unsigned refs; +}; + + +template +void intrusive_ptr_add_ref(atomic_intrusive_refcount* ptr) +{ + ++(ptr->refs); +} + +template +void intrusive_ptr_release(atomic_intrusive_refcount* ptr) +{ + if(!--(ptr->refs)) delete static_cast(ptr); +} + +template +class atomic_intrusive_refcount : boost::noncopyable +{ + protected: + friend void intrusive_ptr_add_ref(atomic_intrusive_refcount* ptr); + friend void intrusive_ptr_release(atomic_intrusive_refcount* ptr); + + atomic_intrusive_refcount(): refs(0) {} + ~atomic_intrusive_refcount() { assert(refs==0); } + +private: + boost::detail::atomic_count refs; +}; + +} + + +#endif -- cgit v1.2.3