blob: 886065db30927122c50b4bc677e498ee4ff56082 [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
20#include "base/logging.h"
21#include "base/macros.h"
22#include "globals.h"
23
24namespace art {
25
26// Helper classes for reference counting to enforce construction/destruction order and
27// usage of the top element of a stack in debug mode with no overhead in release mode.
28
29// Reference counter. No references allowed in destructor or in explicitly called CheckNoRefs().
30template <bool kIsDebug>
31class DebugStackRefCounterImpl;
32// Reference. Allows an explicit check that it's the top reference.
33template <bool kIsDebug>
34class DebugStackReferenceImpl;
35// Indirect top reference. Checks that the reference is the top reference when used.
36template <bool kIsDebug>
37class DebugStackIndirectTopRefImpl;
38
39typedef DebugStackRefCounterImpl<kIsDebugBuild> DebugStackRefCounter;
40typedef DebugStackReferenceImpl<kIsDebugBuild> DebugStackReference;
41typedef DebugStackIndirectTopRefImpl<kIsDebugBuild> DebugStackIndirectTopRef;
42
43// Non-debug mode specializations. This should be optimized away.
44
45template <>
46class DebugStackRefCounterImpl<false> {
47 public:
48 size_t IncrementRefCount() { return 0u; }
49 void DecrementRefCount() { }
50 size_t GetRefCount() const { return 0u; }
51 void CheckNoRefs() const { }
52};
53
54template <>
55class DebugStackReferenceImpl<false> {
56 public:
Roland Levillain4b8f1ec2015-08-26 18:34:03 +010057 explicit DebugStackReferenceImpl(DebugStackRefCounterImpl<false>* counter ATTRIBUTE_UNUSED) {}
Vladimir Marko83cc7ae2014-02-12 18:02:05 +000058 DebugStackReferenceImpl(const DebugStackReferenceImpl& other) = default;
59 DebugStackReferenceImpl& operator=(const DebugStackReferenceImpl& other) = default;
60 void CheckTop() { }
61};
62
63template <>
64class DebugStackIndirectTopRefImpl<false> {
65 public:
Roland Levillain4b8f1ec2015-08-26 18:34:03 +010066 explicit DebugStackIndirectTopRefImpl(DebugStackReferenceImpl<false>* ref ATTRIBUTE_UNUSED) {}
Vladimir Marko83cc7ae2014-02-12 18:02:05 +000067 DebugStackIndirectTopRefImpl(const DebugStackIndirectTopRefImpl& other) = default;
68 DebugStackIndirectTopRefImpl& operator=(const DebugStackIndirectTopRefImpl& other) = default;
69 void CheckTop() { }
70};
71
72// Debug mode versions.
73
74template <bool kIsDebug>
75class DebugStackRefCounterImpl {
76 public:
77 DebugStackRefCounterImpl() : ref_count_(0u) { }
78 ~DebugStackRefCounterImpl() { CheckNoRefs(); }
79 size_t IncrementRefCount() { return ++ref_count_; }
80 void DecrementRefCount() { --ref_count_; }
81 size_t GetRefCount() const { return ref_count_; }
82 void CheckNoRefs() const { CHECK_EQ(ref_count_, 0u); }
83
84 private:
85 size_t ref_count_;
86};
87
88template <bool kIsDebug>
89class DebugStackReferenceImpl {
90 public:
91 explicit DebugStackReferenceImpl(DebugStackRefCounterImpl<kIsDebug>* counter)
92 : counter_(counter), ref_count_(counter->IncrementRefCount()) {
93 }
94 DebugStackReferenceImpl(const DebugStackReferenceImpl& other)
95 : counter_(other.counter_), ref_count_(counter_->IncrementRefCount()) {
96 }
Vladimir Marko174b2e22017-10-12 13:34:49 +010097 DebugStackReferenceImpl(DebugStackReferenceImpl&& other)
98 : counter_(other.counter_), ref_count_(other.ref_count_) {
99 other.counter_ = nullptr;
100 }
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000101 DebugStackReferenceImpl& operator=(const DebugStackReferenceImpl& other) {
102 CHECK(counter_ == other.counter_);
103 return *this;
104 }
Vladimir Marko174b2e22017-10-12 13:34:49 +0100105 ~DebugStackReferenceImpl() {
106 if (counter_ != nullptr) {
107 counter_->DecrementRefCount();
108 }
109 }
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000110 void CheckTop() { CHECK_EQ(counter_->GetRefCount(), ref_count_); }
111
112 private:
113 DebugStackRefCounterImpl<true>* counter_;
114 size_t ref_count_;
115};
116
117template <bool kIsDebug>
118class DebugStackIndirectTopRefImpl {
119 public:
120 explicit DebugStackIndirectTopRefImpl(DebugStackReferenceImpl<kIsDebug>* ref)
121 : ref_(ref) {
122 CheckTop();
123 }
124 DebugStackIndirectTopRefImpl(const DebugStackIndirectTopRefImpl& other)
125 : ref_(other.ref_) {
126 CheckTop();
127 }
128 DebugStackIndirectTopRefImpl& operator=(const DebugStackIndirectTopRefImpl& other) {
Vladimir Marko69f08ba2014-04-11 12:28:11 +0100129 CHECK(ref_ == other.ref_);
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000130 CheckTop();
131 return *this;
132 }
133 ~DebugStackIndirectTopRefImpl() {
134 CheckTop();
135 }
136 void CheckTop() {
137 ref_->CheckTop();
138 }
139
140 private:
141 DebugStackReferenceImpl<kIsDebug>* ref_;
142};
143
144} // namespace art
145
Mathieu Chartierb666f482015-02-18 14:33:14 -0800146#endif // ART_RUNTIME_BASE_DEBUG_STACK_H_