Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/hotspot/share/jfr/utilities/jfrRefCountPointer.hpp
41149 views
1
/*
2
* Copyright (c) 2017, 2021, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*
23
*/
24
25
#ifndef SHARE_JFR_UTILITIES_JFRREFCOUNTPOINTER_HPP
26
#define SHARE_JFR_UTILITIES_JFRREFCOUNTPOINTER_HPP
27
28
#include "jfr/utilities/jfrAllocation.hpp"
29
#include "runtime/atomic.hpp"
30
31
template <typename T>
32
class RefCountHandle {
33
private:
34
const T* _ptr;
35
36
RefCountHandle(const T* ptr) : _ptr(ptr) {
37
assert(_ptr != NULL, "invariant");
38
_ptr->add_ref();
39
}
40
41
public:
42
RefCountHandle() : _ptr(NULL) {}
43
44
RefCountHandle(const RefCountHandle<T>& rhs) : _ptr(rhs._ptr) {
45
if (_ptr != NULL) {
46
_ptr->add_ref();
47
}
48
}
49
50
~RefCountHandle() {
51
if (_ptr != NULL) {
52
_ptr->remove_ref();
53
_ptr = NULL;
54
}
55
}
56
57
// The copy-and-swap idiom upholds reference counting semantics
58
void operator=(RefCountHandle<T> rhs) {
59
const T* temp = rhs._ptr;
60
rhs._ptr = _ptr;
61
_ptr = temp;
62
}
63
64
bool operator==(const RefCountHandle<T>& rhs) const {
65
return _ptr == rhs._ptr;
66
}
67
68
bool operator!=(const RefCountHandle<T>& rhs) const {
69
return !operator==(rhs);
70
}
71
72
bool valid() const {
73
return _ptr != NULL;
74
}
75
76
const T& operator->() const {
77
return *_ptr;
78
}
79
80
T& operator->() {
81
return *const_cast<T*>(_ptr);
82
}
83
84
static RefCountHandle<T> make(const T* ptr) {
85
return ptr;
86
}
87
};
88
89
class SingleThreadedRefCounter {
90
private:
91
mutable intptr_t _refs;
92
public:
93
SingleThreadedRefCounter() : _refs(0) {}
94
95
void inc() const {
96
++_refs;
97
}
98
99
bool dec() const {
100
return --_refs == 0;
101
}
102
103
intptr_t current() const {
104
return _refs;
105
}
106
};
107
108
class MultiThreadedRefCounter {
109
private:
110
mutable volatile intptr_t _refs;
111
public:
112
MultiThreadedRefCounter() : _refs(0) {}
113
114
void inc() const {
115
Atomic::add(&_refs, 1);
116
}
117
118
bool dec() const {
119
return 0 == Atomic::add(&_refs, (-1));
120
}
121
122
intptr_t current() const {
123
return _refs;
124
}
125
};
126
127
template <typename T, typename RefCountImpl = MultiThreadedRefCounter>
128
class RefCountPointer : public JfrCHeapObj {
129
template <typename>
130
friend class RefCountHandle;
131
typedef RefCountHandle<RefCountPointer<T, RefCountImpl> > RefHandle;
132
private:
133
const T* _ptr;
134
mutable RefCountImpl _refs;
135
136
// disallow multiple copies
137
RefCountPointer(const RefCountPointer<T, RefCountImpl>& rhs);
138
void operator=(const RefCountPointer<T, RefCountImpl>& rhs);
139
140
~RefCountPointer() {
141
assert(_refs.current() == 0, "invariant");
142
delete const_cast<T*>(_ptr);
143
}
144
145
void add_ref() const {
146
_refs.inc();
147
}
148
149
void remove_ref() const {
150
if (_refs.dec()) {
151
delete this;
152
}
153
}
154
155
RefCountPointer(const T* ptr) : _ptr(ptr), _refs() {
156
assert(_ptr != NULL, "invariant");
157
}
158
159
public:
160
const T* operator->() const {
161
return _ptr;
162
}
163
164
T* operator->() {
165
return const_cast<T*>(_ptr);
166
}
167
168
static RefHandle make(const T* ptr) {
169
return RefHandle::make(new RefCountPointer<T, RefCountImpl>(ptr));
170
}
171
};
172
173
#endif // SHARE_JFR_UTILITIES_JFRREFCOUNTPOINTER_HPP
174
175