summaryrefslogtreecommitdiffstats
path: root/base
diff options
context:
space:
mode:
Diffstat (limited to 'base')
-rw-r--r--base/ref_counted.cc20
-rw-r--r--base/ref_counted.h17
2 files changed, 26 insertions, 11 deletions
diff --git a/base/ref_counted.cc b/base/ref_counted.cc
index 2d459ae..54c1071 100644
--- a/base/ref_counted.cc
+++ b/base/ref_counted.cc
@@ -12,7 +12,7 @@ namespace base {
namespace subtle {
RefCountedBase::RefCountedBase()
- : ref_count_(0)
+ : counter_holder_(new CounterHolder)
#ifndef NDEBUG
, in_dtor_(false)
#endif
@@ -20,6 +20,7 @@ RefCountedBase::RefCountedBase()
}
RefCountedBase::~RefCountedBase() {
+ delete counter_holder_;
#ifndef NDEBUG
DCHECK(in_dtor_) << "RefCounted object deleted without calling Release()";
#endif
@@ -32,7 +33,7 @@ void RefCountedBase::AddRef() const {
#ifndef NDEBUG
DCHECK(!in_dtor_);
#endif
- ++ref_count_;
+ ++(counter_holder_->ref_count);
}
bool RefCountedBase::Release() const {
@@ -42,7 +43,7 @@ bool RefCountedBase::Release() const {
#ifndef NDEBUG
DCHECK(!in_dtor_);
#endif
- if (--ref_count_ == 0) {
+ if (--(counter_holder_->ref_count) == 0) {
#ifndef NDEBUG
in_dtor_ = true;
#endif
@@ -53,16 +54,19 @@ bool RefCountedBase::Release() const {
bool RefCountedThreadSafeBase::HasOneRef() const {
return AtomicRefCountIsOne(
- &const_cast<RefCountedThreadSafeBase*>(this)->ref_count_);
+ &const_cast<RefCountedThreadSafeBase*>(this)->
+ counter_holder_->ref_count);
}
-RefCountedThreadSafeBase::RefCountedThreadSafeBase() : ref_count_(0) {
+RefCountedThreadSafeBase::RefCountedThreadSafeBase()
+ : counter_holder_(new CounterHolder) {
#ifndef NDEBUG
in_dtor_ = false;
#endif
}
RefCountedThreadSafeBase::~RefCountedThreadSafeBase() {
+ delete counter_holder_;
#ifndef NDEBUG
DCHECK(in_dtor_) << "RefCountedThreadSafe object deleted without "
"calling Release()";
@@ -73,15 +77,15 @@ void RefCountedThreadSafeBase::AddRef() const {
#ifndef NDEBUG
DCHECK(!in_dtor_);
#endif
- AtomicRefCountInc(&ref_count_);
+ AtomicRefCountInc(&counter_holder_->ref_count);
}
bool RefCountedThreadSafeBase::Release() const {
#ifndef NDEBUG
DCHECK(!in_dtor_);
- DCHECK(!AtomicRefCountIsZero(&ref_count_));
+ DCHECK(!AtomicRefCountIsZero(&counter_holder_->ref_count));
#endif
- if (!AtomicRefCountDec(&ref_count_)) {
+ if (!AtomicRefCountDec(&counter_holder_->ref_count)) {
#ifndef NDEBUG
in_dtor_ = true;
#endif
diff --git a/base/ref_counted.h b/base/ref_counted.h
index 4c3aeb8..6a2b996 100644
--- a/base/ref_counted.h
+++ b/base/ref_counted.h
@@ -17,19 +17,25 @@ class RefCountedBase {
public:
static bool ImplementsThreadSafeReferenceCounting() { return false; }
- bool HasOneRef() const { return ref_count_ == 1; }
+ bool HasOneRef() const { return counter_holder_->ref_count == 1; }
protected:
RefCountedBase();
~RefCountedBase();
+ struct CounterHolder {
+ CounterHolder() : ref_count(0), weak_count(0) {}
+ int ref_count;
+ int weak_count; // Simulates weak pointer.
+ };
+
void AddRef() const;
// Returns true if the object should self-delete.
bool Release() const;
private:
- mutable int ref_count_;
+ mutable CounterHolder* counter_holder_;
#ifndef NDEBUG
mutable bool in_dtor_;
#endif
@@ -55,7 +61,12 @@ class RefCountedThreadSafeBase {
bool Release() const;
private:
- mutable AtomicRefCount ref_count_;
+ struct CounterHolder {
+ CounterHolder() : ref_count(0), weak_count(0) {}
+ AtomicRefCount ref_count;
+ AtomicRefCount weak_count; // Simulates weak pointer.
+ };
+ mutable CounterHolder* counter_holder_;
#ifndef NDEBUG
mutable bool in_dtor_;
#endif