Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/hotspot/share/jfr/utilities/jfrHashtable.hpp
41149 views
1
/*
2
* Copyright (c) 2016, 2020, 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_JFRHASHTABLE_HPP
26
#define SHARE_JFR_UTILITIES_JFRHASHTABLE_HPP
27
28
#include "jfr/utilities/jfrAllocation.hpp"
29
#include "runtime/atomic.hpp"
30
#include "services/memTracker.hpp"
31
#include "utilities/debug.hpp"
32
#include "utilities/macros.hpp"
33
34
template <typename T>
35
class JfrBasicHashtableEntry : public JfrCHeapObj {
36
private:
37
typedef JfrBasicHashtableEntry<T> Entry;
38
Entry* _next;
39
T _literal; // ref to item in table.
40
uintptr_t _hash;
41
42
public:
43
JfrBasicHashtableEntry(uintptr_t hash, const T& data) : _next(NULL), _literal(data), _hash(hash) {}
44
uintptr_t hash() const { return _hash; }
45
T literal() const { return _literal; }
46
T* literal_addr() { return &_literal; }
47
void set_literal(T s) { _literal = s; }
48
void set_next(Entry* next) { _next = next; }
49
Entry* next() const { return _next; }
50
Entry** next_addr() { return &_next; }
51
};
52
53
template <typename T>
54
class JfrHashtableBucket : public CHeapObj<mtTracing> {
55
template <typename>
56
friend class JfrBasicHashtable;
57
private:
58
typedef JfrBasicHashtableEntry<T> TableEntry;
59
TableEntry* _entry;
60
61
TableEntry* get_entry() const {
62
return (TableEntry*)Atomic::load_acquire(&_entry);
63
}
64
void set_entry(TableEntry* entry) { Atomic::release_store(&_entry, entry);}
65
TableEntry** entry_addr() { return &_entry; }
66
};
67
68
template <typename T>
69
class JfrBasicHashtable : public CHeapObj<mtTracing> {
70
private:
71
typedef JfrHashtableBucket<T> Bucket;
72
typedef JfrBasicHashtableEntry<T> TableEntry;
73
Bucket* _buckets;
74
uintptr_t _table_size;
75
const size_t _entry_size;
76
size_t _number_of_entries;
77
78
protected:
79
JfrBasicHashtable(uintptr_t table_size, size_t entry_size) :
80
_buckets(NULL), _table_size(table_size), _entry_size(entry_size), _number_of_entries(0) {
81
_buckets = NEW_C_HEAP_ARRAY2(Bucket, table_size, mtTracing, CURRENT_PC);
82
memset((void*)_buckets, 0, table_size * sizeof(Bucket));
83
}
84
85
size_t hash_to_index(uintptr_t full_hash) const {
86
const uintptr_t h = full_hash % _table_size;
87
assert(h < _table_size, "Illegal hash value");
88
return (size_t)h;
89
}
90
size_t entry_size() const { return _entry_size; }
91
void unlink_entry(TableEntry* entry) {
92
entry->set_next(NULL);
93
--_number_of_entries;
94
}
95
void free_buckets() {
96
FREE_C_HEAP_ARRAY(Bucket, _buckets);
97
}
98
TableEntry* bucket(size_t i) { return _buckets[i].get_entry();}
99
TableEntry** bucket_addr(size_t i) { return _buckets[i].entry_addr(); }
100
uintptr_t table_size() const { return _table_size; }
101
size_t number_of_entries() const { return _number_of_entries; }
102
void add_entry(size_t index, TableEntry* entry) {
103
assert(entry != NULL, "invariant");
104
entry->set_next(bucket(index));
105
_buckets[index].set_entry(entry);
106
++_number_of_entries;
107
}
108
};
109
110
template <typename IdType, typename Entry, typename T>
111
class AscendingId : public JfrCHeapObj {
112
private:
113
IdType _id;
114
public:
115
AscendingId() : _id(0) {}
116
// callbacks
117
void on_link(Entry* entry) {
118
assert(entry != NULL, "invariant");
119
assert(entry->id() == 0, "invariant");
120
entry->set_id(++_id);
121
}
122
bool on_equals(uintptr_t hash, const Entry* entry) {
123
assert(entry->hash() == hash, "invariant");
124
return true;
125
}
126
};
127
128
// IdType must be scalar
129
template <typename T, typename IdType>
130
class JfrHashtableEntry : public JfrBasicHashtableEntry<T> {
131
public:
132
JfrHashtableEntry(uintptr_t hash, const T& data) : JfrBasicHashtableEntry<T>(hash, data), _id(0) {}
133
typedef IdType ID;
134
ID id() const { return _id; }
135
void set_id(ID id) const { _id = id; }
136
T& value() const { return *const_cast<JfrHashtableEntry*>(this)->literal_addr();}
137
const T* value_addr() const { return const_cast<JfrHashtableEntry*>(this)->literal_addr(); }
138
private:
139
mutable ID _id;
140
};
141
142
template <typename T, typename IdType, template <typename, typename> class Entry,
143
typename Callback = AscendingId<IdType, Entry<T, IdType>, T> ,
144
size_t TABLE_SIZE = 1009>
145
class HashTableHost : public JfrBasicHashtable<T> {
146
public:
147
typedef Entry<T, IdType> HashEntry;
148
HashTableHost(size_t size = 0) : JfrBasicHashtable<T>(size == 0 ? TABLE_SIZE : size, sizeof(HashEntry)), _callback(new Callback()) {}
149
HashTableHost(Callback* cb, size_t size = 0) : JfrBasicHashtable<T>(size == 0 ? TABLE_SIZE : size, sizeof(HashEntry)), _callback(cb) {}
150
~HashTableHost() {
151
this->clear_entries();
152
this->free_buckets();
153
}
154
155
// direct insert assumes non-existing entry
156
HashEntry& put(uintptr_t hash, const T& data);
157
158
// lookup entry, will put if not found
159
HashEntry& lookup_put(uintptr_t hash, const T& data) {
160
HashEntry* entry = lookup_only(hash);
161
return entry == NULL ? put(hash, data) : *entry;
162
}
163
164
HashEntry* lookup_only(uintptr_t hash);
165
166
// id retrieval
167
IdType id(uintptr_t hash, const T& data) {
168
assert(data != NULL, "invariant");
169
const HashEntry& entry = lookup_put(hash, data);
170
assert(entry.id() > 0, "invariant");
171
return entry.id();
172
}
173
174
template <typename Functor>
175
void iterate_value(Functor& f);
176
177
template <typename Functor>
178
void iterate_entry(Functor& f);
179
180
size_t cardinality() const { return this->number_of_entries(); }
181
bool has_entries() const { return this->cardinality() > 0; }
182
void clear_entries();
183
184
// removal and deallocation
185
void free_entry(HashEntry* entry) {
186
assert(entry != NULL, "invariant");
187
JfrBasicHashtable<T>::unlink_entry(entry);
188
_callback->on_unlink(entry);
189
delete entry;
190
}
191
192
private:
193
Callback* _callback;
194
size_t index_for(uintptr_t hash) { return this->hash_to_index(hash); }
195
HashEntry* new_entry(uintptr_t hash, const T& data);
196
void add_entry(size_t index, HashEntry* new_entry) {
197
assert(new_entry != NULL, "invariant");
198
_callback->on_link(new_entry);
199
assert(new_entry->id() > 0, "invariant");
200
JfrBasicHashtable<T>::add_entry(index, new_entry);
201
}
202
};
203
204
template <typename T, typename IdType, template <typename, typename> class Entry, typename Callback, size_t TABLE_SIZE>
205
Entry<T, IdType>& HashTableHost<T, IdType, Entry, Callback, TABLE_SIZE>::put(uintptr_t hash, const T& data) {
206
assert(lookup_only(hash) == NULL, "use lookup_put()");
207
HashEntry* const entry = new_entry(hash, data);
208
add_entry(index_for(hash), entry);
209
return *entry;
210
}
211
212
template <typename T, typename IdType, template <typename, typename> class Entry, typename Callback, size_t TABLE_SIZE>
213
Entry<T, IdType>* HashTableHost<T, IdType, Entry, Callback, TABLE_SIZE>::lookup_only(uintptr_t hash) {
214
HashEntry* entry = (HashEntry*)this->bucket(index_for(hash));
215
while (entry != NULL) {
216
if (entry->hash() == hash && _callback->on_equals(hash, entry)) {
217
return entry;
218
}
219
entry = (HashEntry*)entry->next();
220
}
221
return NULL;
222
}
223
224
template <typename T, typename IdType, template <typename, typename> class Entry, typename Callback, size_t TABLE_SIZE>
225
template <typename Functor>
226
void HashTableHost<T, IdType, Entry, Callback, TABLE_SIZE>::iterate_value(Functor& f) {
227
for (size_t i = 0; i < this->table_size(); ++i) {
228
const HashEntry* entry = (const HashEntry*)this->bucket(i);
229
while (entry != NULL) {
230
if (!f(entry->value())) {
231
break;
232
}
233
entry = (HashEntry*)entry->next();
234
}
235
}
236
}
237
238
template <typename T, typename IdType, template <typename, typename> class Entry, typename Callback, size_t TABLE_SIZE>
239
template <typename Functor>
240
void HashTableHost<T, IdType, Entry, Callback, TABLE_SIZE>::iterate_entry(Functor& f) {
241
for (size_t i = 0; i < this->table_size(); ++i) {
242
const HashEntry* entry = (const HashEntry*)this->bucket(i);
243
while (entry != NULL) {
244
if (!f(entry)) {
245
break;
246
}
247
entry = (const HashEntry*)entry->next();
248
}
249
}
250
}
251
252
template <typename T, typename IdType, template <typename, typename> class Entry, typename Callback, size_t TABLE_SIZE>
253
void HashTableHost<T, IdType, Entry, Callback, TABLE_SIZE>::clear_entries() {
254
for (size_t i = 0; i < this->table_size(); ++i) {
255
HashEntry** bucket = (HashEntry**)this->bucket_addr(i);
256
HashEntry* entry = *bucket;
257
while (entry != NULL) {
258
HashEntry* entry_to_remove = entry;
259
entry = (HashEntry*)entry->next();
260
this->free_entry(entry_to_remove);
261
}
262
*bucket = NULL;
263
}
264
assert(this->number_of_entries() == 0, "should have removed all entries");
265
}
266
267
template <typename T, typename IdType, template <typename, typename> class Entry, typename Callback, size_t TABLE_SIZE>
268
Entry<T, IdType>* HashTableHost<T, IdType, Entry, Callback, TABLE_SIZE>::new_entry(uintptr_t hash, const T& data) {
269
assert(sizeof(HashEntry) == this->entry_size(), "invariant");
270
HashEntry* const entry = new HashEntry(hash, data);
271
assert(entry != NULL, "invariant");
272
assert(0 == entry->id(), "invariant");
273
return entry;
274
}
275
276
#endif // SHARE_JFR_UTILITIES_JFRHASHTABLE_HPP
277
278