blob: dfb3698339ec7fe5700e9e576927484db3a5c12f [file] [log] [blame]
license.botf003cfe2008-08-24 09:55:55 +09001// 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.com65792b72008-08-15 21:27:03 +09004
5#include "base/ref_counted.h"
6
7#include "base/logging.h"
8
9namespace base {
10
11namespace subtle {
12
13RefCountedBase::RefCountedBase() : ref_count_(0) {
14#ifndef NDEBUG
15 in_dtor_ = false;
16#endif
17}
18
19RefCountedBase::~RefCountedBase() {
20#ifndef NDEBUG
21 DCHECK(in_dtor_) << "RefCounted object deleted without calling Release()";
22#endif
23}
24
25void RefCountedBase::AddRef() {
26#ifndef NDEBUG
27 DCHECK(!in_dtor_);
28#endif
29 ++ref_count_;
30}
31
32bool 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
45RefCountedThreadSafeBase::RefCountedThreadSafeBase() : ref_count_(0) {
46#ifndef NDEBUG
47 in_dtor_ = false;
48#endif
49}
50
51RefCountedThreadSafeBase::~RefCountedThreadSafeBase() {
52#ifndef NDEBUG
53 DCHECK(in_dtor_) << "RefCountedThreadSafe object deleted without "
54 "calling Release()";
55#endif
56}
57
58void RefCountedThreadSafeBase::AddRef() {
59#ifndef NDEBUG
60 DCHECK(!in_dtor_);
61#endif
62 AtomicRefCountInc(&ref_count_);
63}
64
65bool 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.comd0841462008-08-15 22:35:59 +090081} // namespace base
license.botf003cfe2008-08-24 09:55:55 +090082