1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "base/memory/ref_counted.h" 6 7 namespace base { 8 9 namespace subtle { 10 HasOneRef() const11bool RefCountedThreadSafeBase::HasOneRef() const { 12 return ref_count_ == 1; 13 } 14 RefCountedThreadSafeBase()15RefCountedThreadSafeBase::RefCountedThreadSafeBase() : ref_count_(0) { 16 #ifndef NDEBUG 17 in_dtor_ = false; 18 #endif 19 } 20 ~RefCountedThreadSafeBase()21RefCountedThreadSafeBase::~RefCountedThreadSafeBase() { 22 #ifndef NDEBUG 23 DCHECK(in_dtor_) << "RefCountedThreadSafe object deleted without " 24 "calling Release()"; 25 #endif 26 } 27 AddRef() const28void RefCountedThreadSafeBase::AddRef() const { 29 #ifndef NDEBUG 30 DCHECK(!in_dtor_); 31 #endif 32 ++ref_count_; 33 } 34 Release() const35bool RefCountedThreadSafeBase::Release() const { 36 #ifndef NDEBUG 37 DCHECK(!in_dtor_); 38 DCHECK(ref_count_ != 0); 39 #endif 40 if (--ref_count_ == 0) { 41 #ifndef NDEBUG 42 in_dtor_ = true; 43 #endif 44 return true; 45 } 46 return false; 47 } 48 49 } // namespace subtle 50 51 } // namespace base 52