blob: 1331e10a0242d9ef77814f628e5d66ae279c14ba [file] [log] [blame]
Vladimir Marko83cc7ae2014-02-12 18:02:05 +00001/*
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 Chartierb666f482015-02-18 14:33:14 -080017#ifndef ART_RUNTIME_BASE_DEBUG_STACK_H_
18#define ART_RUNTIME_BASE_DEBUG_STACK_H_
Vladimir Marko83cc7ae2014-02-12 18:02:05 +000019
Andreas Gampe57943812017-12-06 21:39:13 -080020#include <android-base/logging.h>
21#include <android-base/macros.h>
22
Vladimir Marko83cc7ae2014-02-12 18:02:05 +000023#include "globals.h"
24
25namespace 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().
31template <bool kIsDebug>
32class DebugStackRefCounterImpl;
33// Reference. Allows an explicit check that it's the top reference.
34template <bool kIsDebug>
35class DebugStackReferenceImpl;
36// Indirect top reference. Checks that the reference is the top reference when used.
37template <bool kIsDebug>
38class DebugStackIndirectTopRefImpl;
39
40typedef DebugStackRefCounterImpl<kIsDebugBuild> DebugStackRefCounter;
41typedef DebugStackReferenceImpl<kIsDebugBuild> DebugStackReference;
42typedef DebugStackIndirectTopRefImpl<kIsDebugBuild> DebugStackIndirectTopRef;
43
44// Non-debug mode specializations. This should be optimized away.
45
46template <>
47class 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
55template <>
56class DebugStackReferenceImpl<false> {
57 public:
Roland Levillain4b8f1ec2015-08-26 18:34:03 +010058 explicit DebugStackReferenceImpl(DebugStackRefCounterImpl<false>* counter ATTRIBUTE_UNUSED) {}
Vladimir Marko83cc7ae2014-02-12 18:02:05 +000059 DebugStackReferenceImpl(const DebugStackReferenceImpl& other) = default;
60 DebugStackReferenceImpl& operator=(const DebugStackReferenceImpl& other) = default;
61 void CheckTop() { }
62};
63
64template <>
65class DebugStackIndirectTopRefImpl<false> {
66 public:
Roland Levillain4b8f1ec2015-08-26 18:34:03 +010067 explicit DebugStackIndirectTopRefImpl(DebugStackReferenceImpl<false>* ref ATTRIBUTE_UNUSED) {}
Vladimir Marko83cc7ae2014-02-12 18:02:05 +000068 DebugStackIndirectTopRefImpl(const DebugStackIndirectTopRefImpl& other) = default;
69 DebugStackIndirectTopRefImpl& operator=(const DebugStackIndirectTopRefImpl& other) = default;
70 void CheckTop() { }
71};
72
73// Debug mode versions.
74
75template <bool kIsDebug>
76class 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
89template <bool kIsDebug>
90class 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 Marko174b2e22017-10-12 13:34:49 +010098 DebugStackReferenceImpl(DebugStackReferenceImpl&& other)
99 : counter_(other.counter_), ref_count_(other.ref_count_) {
100 other.counter_ = nullptr;
101 }
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000102 DebugStackReferenceImpl& operator=(const DebugStackReferenceImpl& other) {
103 CHECK(counter_ == other.counter_);
104 return *this;
105 }
Vladimir Marko174b2e22017-10-12 13:34:49 +0100106 ~DebugStackReferenceImpl() {
107 if (counter_ != nullptr) {
108 counter_->DecrementRefCount();
109 }
110 }
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000111 void CheckTop() { CHECK_EQ(counter_->GetRefCount(), ref_count_); }
112
113 private:
114 DebugStackRefCounterImpl<true>* counter_;
115 size_t ref_count_;
116};
117
118template <bool kIsDebug>
119class 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 Marko69f08ba2014-04-11 12:28:11 +0100130 CHECK(ref_ == other.ref_);
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000131 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 Chartierb666f482015-02-18 14:33:14 -0800147#endif // ART_RUNTIME_BASE_DEBUG_STACK_H_