counted_base is now smaller

[SVN r14212]
This commit is contained in:
Peter Dimov
2002-06-20 14:56:10 +00:00
parent 23f68a5657
commit 951c2b7e83

View File

@@ -50,14 +50,14 @@ private:
public: public:
counted_base(): counted_base():
use_count_(0), weak_count_(0), self_deleter_(&self_delete) use_count_(0), weak_count_(0)
{ {
} }
// pre: initial_use_count <= initial_weak_count // pre: initial_use_count <= initial_weak_count
explicit counted_base(long initial_use_count, long initial_weak_count): explicit counted_base(long initial_use_count, long initial_weak_count):
use_count_(initial_use_count), weak_count_(initial_weak_count), self_deleter_(&self_delete) use_count_(initial_use_count), weak_count_(initial_weak_count)
{ {
} }
@@ -78,6 +78,13 @@ public:
{ {
} }
// destruct() is called when weak_count_ drops to zero.
virtual void destruct() // nothrow
{
delete this;
}
void add_ref() void add_ref()
{ {
#ifdef BOOST_HAS_THREADS #ifdef BOOST_HAS_THREADS
@@ -108,9 +115,7 @@ public:
if(new_weak_count == 0) if(new_weak_count == 0)
{ {
// not a direct 'delete this', because the inlined destruct();
// release() may use a different heap manager
self_deleter_(this);
} }
} }
@@ -135,7 +140,7 @@ public:
if(new_weak_count == 0) if(new_weak_count == 0)
{ {
self_deleter_(this); destruct();
} }
} }
@@ -152,19 +157,14 @@ private:
counted_base(counted_base const &); counted_base(counted_base const &);
counted_base & operator= (counted_base const &); counted_base & operator= (counted_base const &);
static void self_delete(counted_base * p)
{
delete p;
}
// inv: use_count_ <= weak_count_ // inv: use_count_ <= weak_count_
long use_count_; long use_count_;
long weak_count_; long weak_count_;
#ifdef BOOST_HAS_THREADS #ifdef BOOST_HAS_THREADS
mutable mutex_type mtx_; mutable mutex_type mtx_;
#endif #endif
void (*self_deleter_) (counted_base *);
}; };
inline void intrusive_ptr_add_ref(counted_base * p) inline void intrusive_ptr_add_ref(counted_base * p)