14 #ifndef STXXL_COMMON_COUNTING_PTR_HEADER
15 #define STXXL_COMMON_COUNTING_PTR_HEADER
63 { inc_reference(m_ptr); }
67 {
if (o) o->inc_reference(); }
71 {
if (m_ptr && m_ptr->dec_reference())
delete m_ptr; }
88 {
return operator = (other_ptr.
m_ptr); }
104 Type& operator * ()
const
111 Type* operator -> ()
const
118 operator Type* ()
const
127 {
return m_ptr == other_ptr.
m_ptr; }
131 {
return m_ptr != other_ptr.
m_ptr; }
134 operator bool ()
const
139 {
return (m_ptr != NULL); }
143 {
return (m_ptr == NULL); }
147 {
return m_ptr && m_ptr->unique(); }
152 if (m_ptr && ! m_ptr->unique())
153 operator = (
new Type(*m_ptr));
159 std::swap(m_ptr, b.
m_ptr);
190 template <
class Type>
204 { inc_reference(m_ptr); }
208 {
if (o) o->inc_reference(); }
212 {
if (m_ptr && m_ptr->dec_reference())
delete m_ptr; }
233 {
return operator = (other_ptr.
m_ptr); }
237 {
return operator = (other_ptr.
get()); }
253 const Type& operator * ()
const
260 const Type* operator -> ()
const
267 operator const Type* ()
const
271 const Type *
get()
const
276 {
return m_ptr == other_ptr.
m_ptr; }
280 {
return m_ptr != other_ptr.
m_ptr; }
284 {
return m_ptr == other_ptr.
get(); }
288 {
return m_ptr != other_ptr.
get(); }
291 operator bool ()
const
304 {
return m_ptr && m_ptr->unique(); }
309 std::swap(m_ptr, b.
m_ptr);
339 : m_reference_count(0) { }
343 : m_reference_count(0) { }
350 { assert(m_reference_count == 0); }
355 { ++m_reference_count; }
361 {
return (! --m_reference_count); }
365 {
return (m_reference_count == 1); }
369 {
return m_reference_count; }
372 #if STXXL_HAVE_SYNC_ADD_AND_FETCH || STXXL_MSVC
385 class atomic_counted_object
391 mutable long m_reference_count;
398 atomic_counted_object()
399 : m_reference_count(0) { }
402 atomic_counted_object(
const atomic_counted_object&)
403 : m_reference_count(0) { }
406 atomic_counted_object& operator = (
const atomic_counted_object&)
409 ~atomic_counted_object()
410 { assert(m_reference_count == 0); }
414 void inc_reference()
const
417 _InterlockedIncrement(&m_reference_count);
419 __sync_add_and_fetch(&m_reference_count, +1);
426 bool dec_reference()
const
429 return (_InterlockedDecrement(&m_reference_count) == 0);
431 return (__sync_add_and_fetch(&m_reference_count, -1) == 0);
438 return (m_reference_count == 1);
444 return m_reference_count;
448 #else // no atomic intrinsics found, use mutexes (slow)
474 : m_reference_count(0) { }
478 : m_reference_count(0) { }
485 { assert(m_reference_count == 0); }
501 return (--m_reference_count == 0);
508 return (m_reference_count == 1);
515 return m_reference_count;
525 #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)