Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/hotspot/share/gc/shared/blockOffsetTable.hpp
40957 views
1
/*
2
* Copyright (c) 2000, 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_GC_SHARED_BLOCKOFFSETTABLE_HPP
26
#define SHARE_GC_SHARED_BLOCKOFFSETTABLE_HPP
27
28
#include "gc/shared/gc_globals.hpp"
29
#include "gc/shared/memset_with_concurrent_readers.hpp"
30
#include "memory/allocation.hpp"
31
#include "memory/memRegion.hpp"
32
#include "memory/virtualspace.hpp"
33
#include "runtime/globals.hpp"
34
#include "utilities/globalDefinitions.hpp"
35
#include "utilities/macros.hpp"
36
37
// The CollectedHeap type requires subtypes to implement a method
38
// "block_start". For some subtypes, notably generational
39
// systems using card-table-based write barriers, the efficiency of this
40
// operation may be important. Implementations of the "BlockOffsetArray"
41
// class may be useful in providing such efficient implementations.
42
//
43
// BlockOffsetTable (abstract)
44
// - BlockOffsetArray (abstract)
45
// - BlockOffsetArrayContigSpace
46
//
47
48
class ContiguousSpace;
49
50
class BOTConstants : public AllStatic {
51
public:
52
static const uint LogN = 9;
53
static const uint LogN_words = LogN - LogHeapWordSize;
54
static const uint N_bytes = 1 << LogN;
55
static const uint N_words = 1 << LogN_words;
56
// entries "e" of at least N_words mean "go back by Base^(e-N_words)."
57
// All entries are less than "N_words + N_powers".
58
static const uint LogBase = 4;
59
static const uint Base = (1 << LogBase);
60
static const uint N_powers = 14;
61
62
static size_t power_to_cards_back(uint i) {
63
return (size_t)1 << (LogBase * i);
64
}
65
static size_t power_to_words_back(uint i) {
66
return power_to_cards_back(i) * N_words;
67
}
68
static size_t entry_to_cards_back(u_char entry) {
69
assert(entry >= N_words, "Precondition");
70
return power_to_cards_back(entry - N_words);
71
}
72
static size_t entry_to_words_back(u_char entry) {
73
assert(entry >= N_words, "Precondition");
74
return power_to_words_back(entry - N_words);
75
}
76
};
77
78
//////////////////////////////////////////////////////////////////////////
79
// The BlockOffsetTable "interface"
80
//////////////////////////////////////////////////////////////////////////
81
class BlockOffsetTable {
82
friend class VMStructs;
83
protected:
84
// These members describe the region covered by the table.
85
86
// The space this table is covering.
87
HeapWord* _bottom; // == reserved.start
88
HeapWord* _end; // End of currently allocated region.
89
90
public:
91
// Initialize the table to cover the given space.
92
// The contents of the initial table are undefined.
93
BlockOffsetTable(HeapWord* bottom, HeapWord* end):
94
_bottom(bottom), _end(end) {
95
assert(_bottom <= _end, "arguments out of order");
96
}
97
98
// Note that the committed size of the covered space may have changed,
99
// so the table size might also wish to change.
100
virtual void resize(size_t new_word_size) = 0;
101
102
virtual void set_bottom(HeapWord* new_bottom) {
103
assert(new_bottom <= _end, "new_bottom > _end");
104
_bottom = new_bottom;
105
resize(pointer_delta(_end, _bottom));
106
}
107
108
// Requires "addr" to be contained by a block, and returns the address of
109
// the start of that block.
110
virtual HeapWord* block_start_unsafe(const void* addr) const = 0;
111
112
// Returns the address of the start of the block containing "addr", or
113
// else "null" if it is covered by no block.
114
HeapWord* block_start(const void* addr) const;
115
};
116
117
//////////////////////////////////////////////////////////////////////////
118
// One implementation of "BlockOffsetTable," the BlockOffsetArray,
119
// divides the covered region into "N"-word subregions (where
120
// "N" = 2^"LogN". An array with an entry for each such subregion
121
// indicates how far back one must go to find the start of the
122
// chunk that includes the first word of the subregion.
123
//
124
// Each BlockOffsetArray is owned by a Space. However, the actual array
125
// may be shared by several BlockOffsetArrays; this is useful
126
// when a single resizable area (such as a generation) is divided up into
127
// several spaces in which contiguous allocation takes place. (Consider,
128
// for example, the garbage-first generation.)
129
130
// Here is the shared array type.
131
//////////////////////////////////////////////////////////////////////////
132
// BlockOffsetSharedArray
133
//////////////////////////////////////////////////////////////////////////
134
class BlockOffsetSharedArray: public CHeapObj<mtGC> {
135
friend class BlockOffsetArray;
136
friend class BlockOffsetArrayNonContigSpace;
137
friend class BlockOffsetArrayContigSpace;
138
friend class VMStructs;
139
140
private:
141
bool _init_to_zero;
142
143
// The reserved region covered by the shared array.
144
MemRegion _reserved;
145
146
// End of the current committed region.
147
HeapWord* _end;
148
149
// Array for keeping offsets for retrieving object start fast given an
150
// address.
151
VirtualSpace _vs;
152
u_char* _offset_array; // byte array keeping backwards offsets
153
154
void fill_range(size_t start, size_t num_cards, u_char offset) {
155
void* start_ptr = &_offset_array[start];
156
// If collector is concurrent, special handling may be needed.
157
G1GC_ONLY(assert(!UseG1GC, "Shouldn't be here when using G1");)
158
memset(start_ptr, offset, num_cards);
159
}
160
161
protected:
162
// Bounds checking accessors:
163
// For performance these have to devolve to array accesses in product builds.
164
u_char offset_array(size_t index) const {
165
assert(index < _vs.committed_size(), "index out of range");
166
return _offset_array[index];
167
}
168
// An assertion-checking helper method for the set_offset_array() methods below.
169
void check_reducing_assertion(bool reducing);
170
171
void set_offset_array(size_t index, u_char offset, bool reducing = false) {
172
check_reducing_assertion(reducing);
173
assert(index < _vs.committed_size(), "index out of range");
174
assert(!reducing || _offset_array[index] >= offset, "Not reducing");
175
_offset_array[index] = offset;
176
}
177
178
void set_offset_array(size_t index, HeapWord* high, HeapWord* low, bool reducing = false) {
179
check_reducing_assertion(reducing);
180
assert(index < _vs.committed_size(), "index out of range");
181
assert(high >= low, "addresses out of order");
182
assert(pointer_delta(high, low) <= BOTConstants::N_words, "offset too large");
183
assert(!reducing || _offset_array[index] >= (u_char)pointer_delta(high, low),
184
"Not reducing");
185
_offset_array[index] = (u_char)pointer_delta(high, low);
186
}
187
188
void set_offset_array(HeapWord* left, HeapWord* right, u_char offset, bool reducing = false) {
189
check_reducing_assertion(reducing);
190
assert(index_for(right - 1) < _vs.committed_size(),
191
"right address out of range");
192
assert(left < right, "Heap addresses out of order");
193
size_t num_cards = pointer_delta(right, left) >> BOTConstants::LogN_words;
194
195
fill_range(index_for(left), num_cards, offset);
196
}
197
198
void set_offset_array(size_t left, size_t right, u_char offset, bool reducing = false) {
199
check_reducing_assertion(reducing);
200
assert(right < _vs.committed_size(), "right address out of range");
201
assert(left <= right, "indexes out of order");
202
size_t num_cards = right - left + 1;
203
204
fill_range(left, num_cards, offset);
205
}
206
207
void check_offset_array(size_t index, HeapWord* high, HeapWord* low) const {
208
assert(index < _vs.committed_size(), "index out of range");
209
assert(high >= low, "addresses out of order");
210
assert(pointer_delta(high, low) <= BOTConstants::N_words, "offset too large");
211
assert(_offset_array[index] == pointer_delta(high, low),
212
"Wrong offset");
213
}
214
215
bool is_card_boundary(HeapWord* p) const;
216
217
// Return the number of slots needed for an offset array
218
// that covers mem_region_words words.
219
// We always add an extra slot because if an object
220
// ends on a card boundary we put a 0 in the next
221
// offset array slot, so we want that slot always
222
// to be reserved.
223
224
size_t compute_size(size_t mem_region_words) {
225
size_t number_of_slots = (mem_region_words / BOTConstants::N_words) + 1;
226
return ReservedSpace::allocation_align_size_up(number_of_slots);
227
}
228
229
public:
230
// Initialize the table to cover from "base" to (at least)
231
// "base + init_word_size". In the future, the table may be expanded
232
// (see "resize" below) up to the size of "_reserved" (which must be at
233
// least "init_word_size".) The contents of the initial table are
234
// undefined; it is the responsibility of the constituent
235
// BlockOffsetTable(s) to initialize cards.
236
BlockOffsetSharedArray(MemRegion reserved, size_t init_word_size);
237
238
// Notes a change in the committed size of the region covered by the
239
// table. The "new_word_size" may not be larger than the size of the
240
// reserved region this table covers.
241
void resize(size_t new_word_size);
242
243
void set_bottom(HeapWord* new_bottom);
244
245
// Whether entries should be initialized to zero. Used currently only for
246
// error checking.
247
void set_init_to_zero(bool val) { _init_to_zero = val; }
248
bool init_to_zero() { return _init_to_zero; }
249
250
// Updates all the BlockOffsetArray's sharing this shared array to
251
// reflect the current "top"'s of their spaces.
252
void update_offset_arrays(); // Not yet implemented!
253
254
// Return the appropriate index into "_offset_array" for "p".
255
size_t index_for(const void* p) const;
256
257
// Return the address indicating the start of the region corresponding to
258
// "index" in "_offset_array".
259
HeapWord* address_for_index(size_t index) const;
260
};
261
262
class Space;
263
264
//////////////////////////////////////////////////////////////////////////
265
// The BlockOffsetArray whose subtypes use the BlockOffsetSharedArray.
266
//////////////////////////////////////////////////////////////////////////
267
class BlockOffsetArray: public BlockOffsetTable {
268
friend class VMStructs;
269
protected:
270
// The following enums are used by do_block_internal() below
271
enum Action {
272
Action_single, // BOT records a single block (see single_block())
273
Action_mark, // BOT marks the start of a block (see mark_block())
274
Action_check // Check that BOT records block correctly
275
// (see verify_single_block()).
276
};
277
278
// The shared array, which is shared with other BlockOffsetArray's
279
// corresponding to different spaces within a generation or span of
280
// memory.
281
BlockOffsetSharedArray* _array;
282
283
// The space that owns this subregion.
284
Space* _sp;
285
286
// If true, array entries are initialized to 0; otherwise, they are
287
// initialized to point backwards to the beginning of the covered region.
288
bool _init_to_zero;
289
290
// An assertion-checking helper method for the set_remainder*() methods below.
291
void check_reducing_assertion(bool reducing) { _array->check_reducing_assertion(reducing); }
292
293
// Sets the entries
294
// corresponding to the cards starting at "start" and ending at "end"
295
// to point back to the card before "start": the interval [start, end)
296
// is right-open. The last parameter, reducing, indicates whether the
297
// updates to individual entries always reduce the entry from a higher
298
// to a lower value. (For example this would hold true during a temporal
299
// regime during which only block splits were updating the BOT.
300
void set_remainder_to_point_to_start(HeapWord* start, HeapWord* end, bool reducing = false);
301
// Same as above, except that the args here are a card _index_ interval
302
// that is closed: [start_index, end_index]
303
void set_remainder_to_point_to_start_incl(size_t start, size_t end, bool reducing = false);
304
305
// A helper function for BOT adjustment/verification work
306
void do_block_internal(HeapWord* blk_start, HeapWord* blk_end, Action action, bool reducing = false);
307
308
public:
309
// The space may not have its bottom and top set yet, which is why the
310
// region is passed as a parameter. If "init_to_zero" is true, the
311
// elements of the array are initialized to zero. Otherwise, they are
312
// initialized to point backwards to the beginning.
313
BlockOffsetArray(BlockOffsetSharedArray* array, MemRegion mr,
314
bool init_to_zero_);
315
316
// Note: this ought to be part of the constructor, but that would require
317
// "this" to be passed as a parameter to a member constructor for
318
// the containing concrete subtype of Space.
319
// This would be legal C++, but MS VC++ doesn't allow it.
320
void set_space(Space* sp) { _sp = sp; }
321
322
// Resets the covered region to the given "mr".
323
void set_region(MemRegion mr) {
324
_bottom = mr.start();
325
_end = mr.end();
326
}
327
328
// Note that the committed size of the covered space may have changed,
329
// so the table size might also wish to change.
330
virtual void resize(size_t new_word_size) {
331
HeapWord* new_end = _bottom + new_word_size;
332
if (_end < new_end && !init_to_zero()) {
333
// verify that the old and new boundaries are also card boundaries
334
assert(_array->is_card_boundary(_end),
335
"_end not a card boundary");
336
assert(_array->is_card_boundary(new_end),
337
"new _end would not be a card boundary");
338
// set all the newly added cards
339
_array->set_offset_array(_end, new_end, BOTConstants::N_words);
340
}
341
_end = new_end; // update _end
342
}
343
344
// Adjust the BOT to show that it has a single block in the
345
// range [blk_start, blk_start + size). All necessary BOT
346
// cards are adjusted, but _unallocated_block isn't.
347
void single_block(HeapWord* blk_start, HeapWord* blk_end);
348
void single_block(HeapWord* blk, size_t size) {
349
single_block(blk, blk + size);
350
}
351
352
// When the alloc_block() call returns, the block offset table should
353
// have enough information such that any subsequent block_start() call
354
// with an argument equal to an address that is within the range
355
// [blk_start, blk_end) would return the value blk_start, provided
356
// there have been no calls in between that reset this information
357
// (e.g. see BlockOffsetArrayNonContigSpace::single_block() call
358
// for an appropriate range covering the said interval).
359
// These methods expect to be called with [blk_start, blk_end)
360
// representing a block of memory in the heap.
361
virtual void alloc_block(HeapWord* blk_start, HeapWord* blk_end);
362
void alloc_block(HeapWord* blk, size_t size) {
363
alloc_block(blk, blk + size);
364
}
365
366
// If true, initialize array slots with no allocated blocks to zero.
367
// Otherwise, make them point back to the front.
368
bool init_to_zero() { return _init_to_zero; }
369
// Corresponding setter
370
void set_init_to_zero(bool val) {
371
_init_to_zero = val;
372
assert(_array != NULL, "_array should be non-NULL");
373
_array->set_init_to_zero(val);
374
}
375
376
// Debugging
377
// Return the index of the last entry in the "active" region.
378
virtual size_t last_active_index() const = 0;
379
// Verify the block offset table
380
void verify() const;
381
void check_all_cards(size_t left_card, size_t right_card) const;
382
};
383
384
////////////////////////////////////////////////////////////////////////////
385
// A subtype of BlockOffsetArray that takes advantage of the fact
386
// that its underlying space is a ContiguousSpace, so that its "active"
387
// region can be more efficiently tracked (than for a non-contiguous space).
388
////////////////////////////////////////////////////////////////////////////
389
class BlockOffsetArrayContigSpace: public BlockOffsetArray {
390
friend class VMStructs;
391
private:
392
// allocation boundary at which offset array must be updated
393
HeapWord* _next_offset_threshold;
394
size_t _next_offset_index; // index corresponding to that boundary
395
396
// Work function when allocation start crosses threshold.
397
void alloc_block_work(HeapWord* blk_start, HeapWord* blk_end);
398
399
public:
400
BlockOffsetArrayContigSpace(BlockOffsetSharedArray* array, MemRegion mr):
401
BlockOffsetArray(array, mr, true) {
402
_next_offset_threshold = NULL;
403
_next_offset_index = 0;
404
}
405
406
void set_contig_space(ContiguousSpace* sp) { set_space((Space*)sp); }
407
408
// Initialize the threshold for an empty heap.
409
HeapWord* initialize_threshold();
410
// Zero out the entry for _bottom (offset will be zero)
411
void zero_bottom_entry();
412
413
// Return the next threshold, the point at which the table should be
414
// updated.
415
HeapWord* threshold() const { return _next_offset_threshold; }
416
417
// In general, these methods expect to be called with
418
// [blk_start, blk_end) representing a block of memory in the heap.
419
// In this implementation, however, we are OK even if blk_start and/or
420
// blk_end are NULL because NULL is represented as 0, and thus
421
// never exceeds the "_next_offset_threshold".
422
void alloc_block(HeapWord* blk_start, HeapWord* blk_end) {
423
if (blk_end > _next_offset_threshold) {
424
alloc_block_work(blk_start, blk_end);
425
}
426
}
427
void alloc_block(HeapWord* blk, size_t size) {
428
alloc_block(blk, blk + size);
429
}
430
431
HeapWord* block_start_unsafe(const void* addr) const;
432
433
// Debugging support
434
virtual size_t last_active_index() const;
435
};
436
437
#endif // SHARE_GC_SHARED_BLOCKOFFSETTABLE_HPP
438
439