Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Mathieu Chartier | b666f48 | 2015-02-18 14:33:14 -0800 | [diff] [blame] | 17 | #ifndef ART_RUNTIME_BASE_DEBUG_STACK_H_ |
| 18 | #define ART_RUNTIME_BASE_DEBUG_STACK_H_ |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 19 | |
Andreas Gampe | 5794381 | 2017-12-06 21:39:13 -0800 | [diff] [blame] | 20 | #include <android-base/logging.h> |
| 21 | #include <android-base/macros.h> |
| 22 | |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 23 | #include "globals.h" |
| 24 | |
| 25 | namespace art { |
| 26 | |
| 27 | // Helper classes for reference counting to enforce construction/destruction order and |
| 28 | // usage of the top element of a stack in debug mode with no overhead in release mode. |
| 29 | |
| 30 | // Reference counter. No references allowed in destructor or in explicitly called CheckNoRefs(). |
| 31 | template <bool kIsDebug> |
| 32 | class DebugStackRefCounterImpl; |
| 33 | // Reference. Allows an explicit check that it's the top reference. |
| 34 | template <bool kIsDebug> |
| 35 | class DebugStackReferenceImpl; |
| 36 | // Indirect top reference. Checks that the reference is the top reference when used. |
| 37 | template <bool kIsDebug> |
| 38 | class DebugStackIndirectTopRefImpl; |
| 39 | |
| 40 | typedef DebugStackRefCounterImpl<kIsDebugBuild> DebugStackRefCounter; |
| 41 | typedef DebugStackReferenceImpl<kIsDebugBuild> DebugStackReference; |
| 42 | typedef DebugStackIndirectTopRefImpl<kIsDebugBuild> DebugStackIndirectTopRef; |
| 43 | |
| 44 | // Non-debug mode specializations. This should be optimized away. |
| 45 | |
| 46 | template <> |
| 47 | class DebugStackRefCounterImpl<false> { |
| 48 | public: |
| 49 | size_t IncrementRefCount() { return 0u; } |
| 50 | void DecrementRefCount() { } |
| 51 | size_t GetRefCount() const { return 0u; } |
| 52 | void CheckNoRefs() const { } |
| 53 | }; |
| 54 | |
| 55 | template <> |
| 56 | class DebugStackReferenceImpl<false> { |
| 57 | public: |
Roland Levillain | 4b8f1ec | 2015-08-26 18:34:03 +0100 | [diff] [blame] | 58 | explicit DebugStackReferenceImpl(DebugStackRefCounterImpl<false>* counter ATTRIBUTE_UNUSED) {} |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 59 | DebugStackReferenceImpl(const DebugStackReferenceImpl& other) = default; |
| 60 | DebugStackReferenceImpl& operator=(const DebugStackReferenceImpl& other) = default; |
| 61 | void CheckTop() { } |
| 62 | }; |
| 63 | |
| 64 | template <> |
| 65 | class DebugStackIndirectTopRefImpl<false> { |
| 66 | public: |
Roland Levillain | 4b8f1ec | 2015-08-26 18:34:03 +0100 | [diff] [blame] | 67 | explicit DebugStackIndirectTopRefImpl(DebugStackReferenceImpl<false>* ref ATTRIBUTE_UNUSED) {} |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 68 | DebugStackIndirectTopRefImpl(const DebugStackIndirectTopRefImpl& other) = default; |
| 69 | DebugStackIndirectTopRefImpl& operator=(const DebugStackIndirectTopRefImpl& other) = default; |
| 70 | void CheckTop() { } |
| 71 | }; |
| 72 | |
| 73 | // Debug mode versions. |
| 74 | |
| 75 | template <bool kIsDebug> |
| 76 | class DebugStackRefCounterImpl { |
| 77 | public: |
| 78 | DebugStackRefCounterImpl() : ref_count_(0u) { } |
| 79 | ~DebugStackRefCounterImpl() { CheckNoRefs(); } |
| 80 | size_t IncrementRefCount() { return ++ref_count_; } |
| 81 | void DecrementRefCount() { --ref_count_; } |
| 82 | size_t GetRefCount() const { return ref_count_; } |
| 83 | void CheckNoRefs() const { CHECK_EQ(ref_count_, 0u); } |
| 84 | |
| 85 | private: |
| 86 | size_t ref_count_; |
| 87 | }; |
| 88 | |
| 89 | template <bool kIsDebug> |
| 90 | class DebugStackReferenceImpl { |
| 91 | public: |
| 92 | explicit DebugStackReferenceImpl(DebugStackRefCounterImpl<kIsDebug>* counter) |
| 93 | : counter_(counter), ref_count_(counter->IncrementRefCount()) { |
| 94 | } |
| 95 | DebugStackReferenceImpl(const DebugStackReferenceImpl& other) |
| 96 | : counter_(other.counter_), ref_count_(counter_->IncrementRefCount()) { |
| 97 | } |
Vladimir Marko | 174b2e2 | 2017-10-12 13:34:49 +0100 | [diff] [blame] | 98 | DebugStackReferenceImpl(DebugStackReferenceImpl&& other) |
| 99 | : counter_(other.counter_), ref_count_(other.ref_count_) { |
| 100 | other.counter_ = nullptr; |
| 101 | } |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 102 | DebugStackReferenceImpl& operator=(const DebugStackReferenceImpl& other) { |
| 103 | CHECK(counter_ == other.counter_); |
| 104 | return *this; |
| 105 | } |
Vladimir Marko | 174b2e2 | 2017-10-12 13:34:49 +0100 | [diff] [blame] | 106 | ~DebugStackReferenceImpl() { |
| 107 | if (counter_ != nullptr) { |
| 108 | counter_->DecrementRefCount(); |
| 109 | } |
| 110 | } |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 111 | void CheckTop() { CHECK_EQ(counter_->GetRefCount(), ref_count_); } |
| 112 | |
| 113 | private: |
| 114 | DebugStackRefCounterImpl<true>* counter_; |
| 115 | size_t ref_count_; |
| 116 | }; |
| 117 | |
| 118 | template <bool kIsDebug> |
| 119 | class DebugStackIndirectTopRefImpl { |
| 120 | public: |
| 121 | explicit DebugStackIndirectTopRefImpl(DebugStackReferenceImpl<kIsDebug>* ref) |
| 122 | : ref_(ref) { |
| 123 | CheckTop(); |
| 124 | } |
| 125 | DebugStackIndirectTopRefImpl(const DebugStackIndirectTopRefImpl& other) |
| 126 | : ref_(other.ref_) { |
| 127 | CheckTop(); |
| 128 | } |
| 129 | DebugStackIndirectTopRefImpl& operator=(const DebugStackIndirectTopRefImpl& other) { |
Vladimir Marko | 69f08ba | 2014-04-11 12:28:11 +0100 | [diff] [blame] | 130 | CHECK(ref_ == other.ref_); |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 131 | CheckTop(); |
| 132 | return *this; |
| 133 | } |
| 134 | ~DebugStackIndirectTopRefImpl() { |
| 135 | CheckTop(); |
| 136 | } |
| 137 | void CheckTop() { |
| 138 | ref_->CheckTop(); |
| 139 | } |
| 140 | |
| 141 | private: |
| 142 | DebugStackReferenceImpl<kIsDebug>* ref_; |
| 143 | }; |
| 144 | |
| 145 | } // namespace art |
| 146 | |
Mathieu Chartier | b666f48 | 2015-02-18 14:33:14 -0800 | [diff] [blame] | 147 | #endif // ART_RUNTIME_BASE_DEBUG_STACK_H_ |