14 #ifndef STXXL_COMMON_COUNTING_PTR_HEADER
15 #define STXXL_COMMON_COUNTING_PTR_HEADER
61 { inc_reference(m_ptr); }
65 {
if (o) o->inc_reference(); }
69 {
if (m_ptr && m_ptr->dec_reference())
delete m_ptr; }
86 {
return operator = (other_ptr.
m_ptr); }
102 Type& operator * ()
const
109 Type* operator -> ()
const
116 operator Type* ()
const
125 {
return m_ptr == other_ptr.
m_ptr; }
129 {
return m_ptr != other_ptr.
m_ptr; }
132 operator bool ()
const
137 {
return (m_ptr != NULL); }
141 {
return (m_ptr == NULL); }
145 {
return m_ptr && m_ptr->unique(); }
150 if (m_ptr && ! m_ptr->unique())
151 operator = (
new Type(*m_ptr));
157 std::swap(m_ptr, b.
m_ptr);
188 template <
class Type>
202 { inc_reference(m_ptr); }
206 {
if (o) o->inc_reference(); }
210 {
if (m_ptr && m_ptr->dec_reference())
delete m_ptr; }
231 {
return operator = (other_ptr.
m_ptr); }
235 {
return operator = (other_ptr.
get()); }
251 const Type& operator * ()
const
258 const Type* operator -> ()
const
265 operator const Type* ()
const
269 const Type *
get()
const
274 {
return m_ptr == other_ptr.
m_ptr; }
278 {
return m_ptr != other_ptr.
m_ptr; }
282 {
return m_ptr == other_ptr.
get(); }
286 {
return m_ptr != other_ptr.
get(); }
289 operator bool ()
const
302 {
return m_ptr && m_ptr->unique(); }
307 std::swap(m_ptr, b.
m_ptr);
337 : m_reference_count(0) { }
341 : m_reference_count(0) { }
348 { assert(m_reference_count == 0); }
353 { ++m_reference_count; }
359 {
return (! --m_reference_count); }
363 {
return (m_reference_count == 1); }
367 {
return m_reference_count; }
370 #if STXXL_HAVE_SYNC_ADD_AND_FETCH || STXXL_MSVC
383 class atomic_counted_object
389 mutable long m_reference_count;
396 atomic_counted_object()
397 : m_reference_count(0) { }
400 atomic_counted_object(
const atomic_counted_object&)
401 : m_reference_count(0) { }
404 atomic_counted_object& operator = (
const atomic_counted_object&)
407 ~atomic_counted_object()
408 { assert(m_reference_count == 0); }
412 void inc_reference()
const
415 _InterlockedIncrement(&m_reference_count);
417 __sync_add_and_fetch(&m_reference_count, +1);
424 bool dec_reference()
const
427 return (_InterlockedDecrement(&m_reference_count) == 0);
429 return (__sync_add_and_fetch(&m_reference_count, -1) == 0);
436 return (m_reference_count == 1);
442 return m_reference_count;
446 #else // no atomic intrinsics found, use mutexes (slow)
472 : m_reference_count(0) { }
476 : m_reference_count(0) { }
483 { assert(m_reference_count == 0); }
499 return (--m_reference_count == 0);
506 return (m_reference_count == 1);
513 return m_reference_count;
521 #endif // !STXXL_COMMON_COUNTING_PTR_HEADER
unsigned_type m_reference_count
the reference count is kept mutable to all const_counting_ptr() to change the reference count...
Type * m_ptr
the pointer to the currently referenced object.
void swap(const_counting_ptr &b)
swap enclosed object with another const_counting pointer (no reference counts need change) ...
bool valid() const
test for a non-NULL pointer
void inc_reference() const
Call whenever setting a pointer to the object.
Type element_type
contained type.
bool dec_reference() const
Call whenever resetting (i.e. overwriting) a pointer to the object. IMPORTANT: In case of self-assign...
~counting_ptr()
destructor: decrements reference counter in ptr.
High-performance smart pointer used as a wrapping reference counting pointer.
void inc_reference() const
Call whenever setting a pointer to the object.
Provides reference counting abilities for use with counting_ptr with mutex locking.
High-performance smart pointer used as a wrapping reference counting pointer.
Type * get() const
return the enclosed pointer.
bool empty() const
test for a NULL pointer
counted_object()
new objects have zero reference count
bool empty() const
test for a NULL pointer
const Type * m_ptr
the pointer to the currently referenced object.
const_counting_ptr()
default constructor: contains a NULL pointer.
bool valid() const
test for a non-NULL pointer
~const_counting_ptr()
destructor: decrements reference counter in ptr.
counting_ptr(const counting_ptr &other_ptr)
copy-constructor: also initializes new reference to ptr.
counting_ptr(Type *ptr)
constructor with pointer: initializes new reference to ptr.
bool unique() const
Test if the counted_object is referenced by only one counting_ptr.
void inc_reference()
increment reference counter for current object.
mutex m_reference_count_mutex
the mutex used to synchronize access to the reference counter.
void swap(counting_ptr &b)
swap enclosed object with another counting pointer (no reference counts need change) ...
bool operator!=(const uint_pair &b) const
inequality checking operator
void dec_reference()
decrement reference counter of current object and maybe delete it.
counting_ptr()
default constructor: contains a NULL pointer.
Provides reference counting abilities for use with counting_ptr.
atomic_counted_object()
new objects have zero reference count
void inc_reference(const Type *o)
increment reference counter of other object.
const_counting_ptr(const Type *ptr)
constructor with pointer: initializes new reference to ptr.
counted_object(const counted_object &)
coping still creates a new object with zero reference count
Aquire a lock that's valid until the end of scope.
#define STXXL_BEGIN_NAMESPACE
unsigned_type m_reference_count
the reference count is kept mutable to all const_counting_ptr() to change the reference count...
bool unique() const
if the object is referred by this counting_ptr only
void inc_reference(Type *o)
increment reference counter of other object.
bool unique() const
if the object is referred by this const_counting_ptr only
Type element_type
contained type.
void dec_reference()
decrement reference counter of current object and maybe delete it.
bool unique() const
Test if the counted_object is referenced by only one counting_ptr.
void inc_reference()
increment reference counter for current object.
atomic_counted_object(const atomic_counted_object &)
coping still creates a new object with zero reference count
choose_int_types< my_pointer_size >::unsigned_type unsigned_type
bool dec_reference() const
Call whenever resetting (i.e. overwriting) a pointer to the object. IMPORTANT: In case of self-assign...
const_counting_ptr(const counting_ptr< Type > &other_ptr)
constructor from non-const: also initializes new reference to ptr.
unsigned_type get_reference_count() const
Return the number of references to this object (for debugging)
void unify()
make and refer a copy if the original object was shared.
bool operator==(const uint_pair &b) const
equality checking operator
const_counting_ptr(const const_counting_ptr &other_ptr)
copy-constructor: also initializes new reference to ptr.
#define STXXL_END_NAMESPACE
unsigned_type get_reference_count() const
Return the number of references to this object (for debugging)