Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/hotspot/share/gc/g1/g1BiasedArray.hpp
40957 views
1
/*
2
* Copyright (c) 2013, 2019, 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_GC_G1_G1BIASEDARRAY_HPP
26
#define SHARE_GC_G1_G1BIASEDARRAY_HPP
27
28
#include "memory/allocation.hpp"
29
#include "memory/memRegion.hpp"
30
#include "utilities/debug.hpp"
31
#include "utilities/powerOfTwo.hpp"
32
33
// Implements the common base functionality for arrays that contain provisions
34
// for accessing its elements using a biased index.
35
// The element type is defined by the instantiating the template.
36
class G1BiasedMappedArrayBase : public CHeapObj<mtGC> {
37
friend class VMStructs;
38
39
void* _alloc_base; // the address the unpadded array has been allocated to
40
41
public:
42
typedef size_t idx_t;
43
44
protected:
45
address _base; // the real base address
46
size_t _length; // the length of the array
47
address _biased_base; // base address biased by "bias" elements
48
size_t _bias; // the bias, i.e. the offset biased_base is located to the right in elements
49
uint _shift_by; // the amount of bits to shift right when mapping to an index of the array.
50
51
protected:
52
G1BiasedMappedArrayBase();
53
54
// Allocate a new array, generic version.
55
address create_new_base_array(size_t length, size_t elem_size);
56
57
// Initialize the members of this class. The biased start address of this array
58
// is the bias (in elements) multiplied by the element size.
59
void initialize_base(address base, size_t length, size_t bias, size_t elem_size, uint shift_by) {
60
assert(base != NULL, "just checking");
61
assert(length > 0, "just checking");
62
assert(shift_by < sizeof(uintptr_t) * 8, "Shifting by %u, larger than word size?", shift_by);
63
_base = base;
64
_length = length;
65
_biased_base = base - (bias * elem_size);
66
_bias = bias;
67
_shift_by = shift_by;
68
}
69
70
// Allocate and initialize this array to cover the heap addresses in the range
71
// of [bottom, end).
72
void initialize(HeapWord* bottom, HeapWord* end, size_t target_elem_size_in_bytes, size_t mapping_granularity_in_bytes) {
73
assert(mapping_granularity_in_bytes > 0, "just checking");
74
assert(is_power_of_2(mapping_granularity_in_bytes),
75
"mapping granularity must be power of 2, is " SIZE_FORMAT, mapping_granularity_in_bytes);
76
assert((uintptr_t)bottom % mapping_granularity_in_bytes == 0,
77
"bottom mapping area address must be a multiple of mapping granularity " SIZE_FORMAT ", is " PTR_FORMAT,
78
mapping_granularity_in_bytes, p2i(bottom));
79
assert((uintptr_t)end % mapping_granularity_in_bytes == 0,
80
"end mapping area address must be a multiple of mapping granularity " SIZE_FORMAT ", is " PTR_FORMAT,
81
mapping_granularity_in_bytes, p2i(end));
82
size_t num_target_elems = pointer_delta(end, bottom, mapping_granularity_in_bytes);
83
idx_t bias = (uintptr_t)bottom / mapping_granularity_in_bytes;
84
address base = create_new_base_array(num_target_elems, target_elem_size_in_bytes);
85
initialize_base(base, num_target_elems, bias, target_elem_size_in_bytes, log2i_exact(mapping_granularity_in_bytes));
86
}
87
88
size_t bias() const { return _bias; }
89
uint shift_by() const { return _shift_by; }
90
91
void verify_index(idx_t index) const PRODUCT_RETURN;
92
void verify_biased_index(idx_t biased_index) const PRODUCT_RETURN;
93
void verify_biased_index_inclusive_end(idx_t biased_index) const PRODUCT_RETURN;
94
95
public:
96
virtual ~G1BiasedMappedArrayBase();
97
98
// Return the length of the array in elements.
99
size_t length() const { return _length; }
100
};
101
102
// Array that provides biased access and mapping from (valid) addresses in the
103
// heap into this array.
104
template<class T>
105
class G1BiasedMappedArray : public G1BiasedMappedArrayBase {
106
public:
107
typedef G1BiasedMappedArrayBase::idx_t idx_t;
108
109
T* base() const { return (T*)G1BiasedMappedArrayBase::_base; }
110
// Return the element of the given array at the given index. Assume
111
// the index is valid. This is a convenience method that does sanity
112
// checking on the index.
113
T get_by_index(idx_t index) const {
114
verify_index(index);
115
return this->base()[index];
116
}
117
118
// Set the element of the given array at the given index to the
119
// given value. Assume the index is valid. This is a convenience
120
// method that does sanity checking on the index.
121
void set_by_index(idx_t index, T value) {
122
verify_index(index);
123
this->base()[index] = value;
124
}
125
126
// The raw biased base pointer.
127
T* biased_base() const { return (T*)G1BiasedMappedArrayBase::_biased_base; }
128
129
// Return the element of the given array that covers the given word in the
130
// heap. Assumes the index is valid.
131
T get_by_address(HeapWord* value) const {
132
idx_t biased_index = ((uintptr_t)value) >> this->shift_by();
133
this->verify_biased_index(biased_index);
134
return biased_base()[biased_index];
135
}
136
137
T* get_ref_by_index(uintptr_t index) const {
138
verify_index(index);
139
return &this->base()[index];
140
}
141
142
// Return the index of the element of the given array that covers the given
143
// word in the heap.
144
idx_t get_index_by_address(HeapWord* value) const {
145
idx_t biased_index = ((uintptr_t)value) >> this->shift_by();
146
this->verify_biased_index(biased_index);
147
return biased_index - _bias;
148
}
149
150
// Set the value of the array entry that corresponds to the given array.
151
void set_by_address(HeapWord * address, T value) {
152
idx_t biased_index = ((uintptr_t)address) >> this->shift_by();
153
this->verify_biased_index(biased_index);
154
biased_base()[biased_index] = value;
155
}
156
157
// Set the value of all array entries that correspond to addresses
158
// in the specified MemRegion.
159
void set_by_address(MemRegion range, T value) {
160
idx_t biased_start = ((uintptr_t)range.start()) >> this->shift_by();
161
idx_t biased_last = ((uintptr_t)range.last()) >> this->shift_by();
162
this->verify_biased_index(biased_start);
163
this->verify_biased_index(biased_last);
164
for (idx_t i = biased_start; i <= biased_last; i++) {
165
biased_base()[i] = value;
166
}
167
}
168
169
protected:
170
// Returns the address of the element the given address maps to
171
T* address_mapped_to(HeapWord* address) {
172
idx_t biased_index = ((uintptr_t)address) >> this->shift_by();
173
this->verify_biased_index_inclusive_end(biased_index);
174
return biased_base() + biased_index;
175
}
176
177
public:
178
// Return the smallest address (inclusive) in the heap that this array covers.
179
HeapWord* bottom_address_mapped() const {
180
return (HeapWord*) ((uintptr_t)this->bias() << this->shift_by());
181
}
182
183
// Return the highest address (exclusive) in the heap that this array covers.
184
HeapWord* end_address_mapped() const {
185
return (HeapWord*) ((uintptr_t)(this->bias() + this->length()) << this->shift_by());
186
}
187
188
protected:
189
virtual T default_value() const = 0;
190
// Set all elements of the given array to the given value.
191
void clear() {
192
T value = default_value();
193
for (idx_t i = 0; i < length(); i++) {
194
set_by_index(i, value);
195
}
196
}
197
public:
198
G1BiasedMappedArray() {}
199
200
// Allocate and initialize this array to cover the heap addresses in the given MemRegion.
201
void initialize(MemRegion region, size_t mapping_granularity) {
202
G1BiasedMappedArrayBase::initialize(region.start(), region.end(), sizeof(T), mapping_granularity);
203
this->clear();
204
}
205
};
206
207
#endif // SHARE_GC_G1_G1BIASEDARRAY_HPP
208
209