1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/security/sandbox/chromium/base/memory/ref_counted.cc Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,95 @@ 1.4 +// Copyright (c) 2011 The Chromium Authors. All rights reserved. 1.5 +// Use of this source code is governed by a BSD-style license that can be 1.6 +// found in the LICENSE file. 1.7 + 1.8 +#include "base/memory/ref_counted.h" 1.9 + 1.10 +#include "base/logging.h" 1.11 +#include "base/threading/thread_collision_warner.h" 1.12 + 1.13 +namespace base { 1.14 + 1.15 +namespace subtle { 1.16 + 1.17 +RefCountedBase::RefCountedBase() 1.18 + : ref_count_(0) 1.19 +#ifndef NDEBUG 1.20 + , in_dtor_(false) 1.21 +#endif 1.22 + { 1.23 +} 1.24 + 1.25 +RefCountedBase::~RefCountedBase() { 1.26 +#ifndef NDEBUG 1.27 + DCHECK(in_dtor_) << "RefCounted object deleted without calling Release()"; 1.28 +#endif 1.29 +} 1.30 + 1.31 +void RefCountedBase::AddRef() const { 1.32 + // TODO(maruel): Add back once it doesn't assert 500 times/sec. 1.33 + // Current thread books the critical section "AddRelease" without release it. 1.34 + // DFAKE_SCOPED_LOCK_THREAD_LOCKED(add_release_); 1.35 +#ifndef NDEBUG 1.36 + DCHECK(!in_dtor_); 1.37 +#endif 1.38 + ++ref_count_; 1.39 +} 1.40 + 1.41 +bool RefCountedBase::Release() const { 1.42 + // TODO(maruel): Add back once it doesn't assert 500 times/sec. 1.43 + // Current thread books the critical section "AddRelease" without release it. 1.44 + // DFAKE_SCOPED_LOCK_THREAD_LOCKED(add_release_); 1.45 +#ifndef NDEBUG 1.46 + DCHECK(!in_dtor_); 1.47 +#endif 1.48 + if (--ref_count_ == 0) { 1.49 +#ifndef NDEBUG 1.50 + in_dtor_ = true; 1.51 +#endif 1.52 + return true; 1.53 + } 1.54 + return false; 1.55 +} 1.56 + 1.57 +bool RefCountedThreadSafeBase::HasOneRef() const { 1.58 + return AtomicRefCountIsOne( 1.59 + &const_cast<RefCountedThreadSafeBase*>(this)->ref_count_); 1.60 +} 1.61 + 1.62 +RefCountedThreadSafeBase::RefCountedThreadSafeBase() : ref_count_(0) { 1.63 +#ifndef NDEBUG 1.64 + in_dtor_ = false; 1.65 +#endif 1.66 +} 1.67 + 1.68 +RefCountedThreadSafeBase::~RefCountedThreadSafeBase() { 1.69 +#ifndef NDEBUG 1.70 + DCHECK(in_dtor_) << "RefCountedThreadSafe object deleted without " 1.71 + "calling Release()"; 1.72 +#endif 1.73 +} 1.74 + 1.75 +void RefCountedThreadSafeBase::AddRef() const { 1.76 +#ifndef NDEBUG 1.77 + DCHECK(!in_dtor_); 1.78 +#endif 1.79 + AtomicRefCountInc(&ref_count_); 1.80 +} 1.81 + 1.82 +bool RefCountedThreadSafeBase::Release() const { 1.83 +#ifndef NDEBUG 1.84 + DCHECK(!in_dtor_); 1.85 + DCHECK(!AtomicRefCountIsZero(&ref_count_)); 1.86 +#endif 1.87 + if (!AtomicRefCountDec(&ref_count_)) { 1.88 +#ifndef NDEBUG 1.89 + in_dtor_ = true; 1.90 +#endif 1.91 + return true; 1.92 + } 1.93 + return false; 1.94 +} 1.95 + 1.96 +} // namespace subtle 1.97 + 1.98 +} // namespace base