license.bot | f003cfe | 2008-08-24 09:55:55 +0900 | [diff] [blame^] | 1 | // Copyright (c) 2006-2008 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. |
maruel@google.com | 65792b7 | 2008-08-15 21:27:03 +0900 | [diff] [blame] | 4 | |
| 5 | #include "base/ref_counted.h" |
| 6 | |
| 7 | #include "base/logging.h" |
| 8 | |
| 9 | namespace base { |
| 10 | |
| 11 | namespace subtle { |
| 12 | |
| 13 | RefCountedBase::RefCountedBase() : ref_count_(0) { |
| 14 | #ifndef NDEBUG |
| 15 | in_dtor_ = false; |
| 16 | #endif |
| 17 | } |
| 18 | |
| 19 | RefCountedBase::~RefCountedBase() { |
| 20 | #ifndef NDEBUG |
| 21 | DCHECK(in_dtor_) << "RefCounted object deleted without calling Release()"; |
| 22 | #endif |
| 23 | } |
| 24 | |
| 25 | void RefCountedBase::AddRef() { |
| 26 | #ifndef NDEBUG |
| 27 | DCHECK(!in_dtor_); |
| 28 | #endif |
| 29 | ++ref_count_; |
| 30 | } |
| 31 | |
| 32 | bool RefCountedBase::Release() { |
| 33 | #ifndef NDEBUG |
| 34 | DCHECK(!in_dtor_); |
| 35 | #endif |
| 36 | if (--ref_count_ == 0) { |
| 37 | #ifndef NDEBUG |
| 38 | in_dtor_ = true; |
| 39 | #endif |
| 40 | return true; |
| 41 | } |
| 42 | return false; |
| 43 | } |
| 44 | |
| 45 | RefCountedThreadSafeBase::RefCountedThreadSafeBase() : ref_count_(0) { |
| 46 | #ifndef NDEBUG |
| 47 | in_dtor_ = false; |
| 48 | #endif |
| 49 | } |
| 50 | |
| 51 | RefCountedThreadSafeBase::~RefCountedThreadSafeBase() { |
| 52 | #ifndef NDEBUG |
| 53 | DCHECK(in_dtor_) << "RefCountedThreadSafe object deleted without " |
| 54 | "calling Release()"; |
| 55 | #endif |
| 56 | } |
| 57 | |
| 58 | void RefCountedThreadSafeBase::AddRef() { |
| 59 | #ifndef NDEBUG |
| 60 | DCHECK(!in_dtor_); |
| 61 | #endif |
| 62 | AtomicRefCountInc(&ref_count_); |
| 63 | } |
| 64 | |
| 65 | bool RefCountedThreadSafeBase::Release() { |
| 66 | #ifndef NDEBUG |
| 67 | DCHECK(!in_dtor_); |
| 68 | DCHECK(!AtomicRefCountIsZero(&ref_count_)); |
| 69 | #endif |
| 70 | if (!AtomicRefCountDec(&ref_count_)) { |
| 71 | #ifndef NDEBUG |
| 72 | in_dtor_ = true; |
| 73 | #endif |
| 74 | return true; |
| 75 | } |
| 76 | return false; |
| 77 | } |
| 78 | |
| 79 | } // namespace subtle |
| 80 | |
mmentovai@google.com | d084146 | 2008-08-15 22:35:59 +0900 | [diff] [blame] | 81 | } // namespace base |
license.bot | f003cfe | 2008-08-24 09:55:55 +0900 | [diff] [blame^] | 82 | |