annotate src/share/vm/gc_implementation/g1/g1BlockOffsetTable.cpp @ 342:37f87013dfd8

6711316: Open source the Garbage-First garbage collector Summary: First mercurial integration of the code for the Garbage-First garbage collector. Reviewed-by: apetrusenko, iveresov, jmasa, sgoldman, tonyp, ysr
author ysr
date Thu, 05 Jun 2008 15:57:56 -0700
parents
children e0c09f7ec5c4
rev   line source
ysr@342 1 /*
ysr@342 2 * Copyright 2001-2007 Sun Microsystems, Inc. All Rights Reserved.
ysr@342 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ysr@342 4 *
ysr@342 5 * This code is free software; you can redistribute it and/or modify it
ysr@342 6 * under the terms of the GNU General Public License version 2 only, as
ysr@342 7 * published by the Free Software Foundation.
ysr@342 8 *
ysr@342 9 * This code is distributed in the hope that it will be useful, but WITHOUT
ysr@342 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ysr@342 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
ysr@342 12 * version 2 for more details (a copy is included in the LICENSE file that
ysr@342 13 * accompanied this code).
ysr@342 14 *
ysr@342 15 * You should have received a copy of the GNU General Public License version
ysr@342 16 * 2 along with this work; if not, write to the Free Software Foundation,
ysr@342 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ysr@342 18 *
ysr@342 19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
ysr@342 20 * CA 95054 USA or visit www.sun.com if you need additional information or
ysr@342 21 * have any questions.
ysr@342 22 *
ysr@342 23 */
ysr@342 24
ysr@342 25 #include "incls/_precompiled.incl"
ysr@342 26 #include "incls/_g1BlockOffsetTable.cpp.incl"
ysr@342 27
ysr@342 28 //////////////////////////////////////////////////////////////////////
ysr@342 29 // G1BlockOffsetSharedArray
ysr@342 30 //////////////////////////////////////////////////////////////////////
ysr@342 31
ysr@342 32 G1BlockOffsetSharedArray::G1BlockOffsetSharedArray(MemRegion reserved,
ysr@342 33 size_t init_word_size) :
ysr@342 34 _reserved(reserved), _end(NULL)
ysr@342 35 {
ysr@342 36 size_t size = compute_size(reserved.word_size());
ysr@342 37 ReservedSpace rs(ReservedSpace::allocation_align_size_up(size));
ysr@342 38 if (!rs.is_reserved()) {
ysr@342 39 vm_exit_during_initialization("Could not reserve enough space for heap offset array");
ysr@342 40 }
ysr@342 41 if (!_vs.initialize(rs, 0)) {
ysr@342 42 vm_exit_during_initialization("Could not reserve enough space for heap offset array");
ysr@342 43 }
ysr@342 44 _offset_array = (u_char*)_vs.low_boundary();
ysr@342 45 resize(init_word_size);
ysr@342 46 if (TraceBlockOffsetTable) {
ysr@342 47 gclog_or_tty->print_cr("G1BlockOffsetSharedArray::G1BlockOffsetSharedArray: ");
ysr@342 48 gclog_or_tty->print_cr(" "
ysr@342 49 " rs.base(): " INTPTR_FORMAT
ysr@342 50 " rs.size(): " INTPTR_FORMAT
ysr@342 51 " rs end(): " INTPTR_FORMAT,
ysr@342 52 rs.base(), rs.size(), rs.base() + rs.size());
ysr@342 53 gclog_or_tty->print_cr(" "
ysr@342 54 " _vs.low_boundary(): " INTPTR_FORMAT
ysr@342 55 " _vs.high_boundary(): " INTPTR_FORMAT,
ysr@342 56 _vs.low_boundary(),
ysr@342 57 _vs.high_boundary());
ysr@342 58 }
ysr@342 59 }
ysr@342 60
ysr@342 61 void G1BlockOffsetSharedArray::resize(size_t new_word_size) {
ysr@342 62 assert(new_word_size <= _reserved.word_size(), "Resize larger than reserved");
ysr@342 63 size_t new_size = compute_size(new_word_size);
ysr@342 64 size_t old_size = _vs.committed_size();
ysr@342 65 size_t delta;
ysr@342 66 char* high = _vs.high();
ysr@342 67 _end = _reserved.start() + new_word_size;
ysr@342 68 if (new_size > old_size) {
ysr@342 69 delta = ReservedSpace::page_align_size_up(new_size - old_size);
ysr@342 70 assert(delta > 0, "just checking");
ysr@342 71 if (!_vs.expand_by(delta)) {
ysr@342 72 // Do better than this for Merlin
ysr@342 73 vm_exit_out_of_memory(delta, "offset table expansion");
ysr@342 74 }
ysr@342 75 assert(_vs.high() == high + delta, "invalid expansion");
ysr@342 76 // Initialization of the contents is left to the
ysr@342 77 // G1BlockOffsetArray that uses it.
ysr@342 78 } else {
ysr@342 79 delta = ReservedSpace::page_align_size_down(old_size - new_size);
ysr@342 80 if (delta == 0) return;
ysr@342 81 _vs.shrink_by(delta);
ysr@342 82 assert(_vs.high() == high - delta, "invalid expansion");
ysr@342 83 }
ysr@342 84 }
ysr@342 85
ysr@342 86 bool G1BlockOffsetSharedArray::is_card_boundary(HeapWord* p) const {
ysr@342 87 assert(p >= _reserved.start(), "just checking");
ysr@342 88 size_t delta = pointer_delta(p, _reserved.start());
ysr@342 89 return (delta & right_n_bits(LogN_words)) == (size_t)NoBits;
ysr@342 90 }
ysr@342 91
ysr@342 92
ysr@342 93 //////////////////////////////////////////////////////////////////////
ysr@342 94 // G1BlockOffsetArray
ysr@342 95 //////////////////////////////////////////////////////////////////////
ysr@342 96
ysr@342 97 G1BlockOffsetArray::G1BlockOffsetArray(G1BlockOffsetSharedArray* array,
ysr@342 98 MemRegion mr, bool init_to_zero) :
ysr@342 99 G1BlockOffsetTable(mr.start(), mr.end()),
ysr@342 100 _unallocated_block(_bottom),
ysr@342 101 _array(array), _csp(NULL),
ysr@342 102 _init_to_zero(init_to_zero) {
ysr@342 103 assert(_bottom <= _end, "arguments out of order");
ysr@342 104 if (!_init_to_zero) {
ysr@342 105 // initialize cards to point back to mr.start()
ysr@342 106 set_remainder_to_point_to_start(mr.start() + N_words, mr.end());
ysr@342 107 _array->set_offset_array(0, 0); // set first card to 0
ysr@342 108 }
ysr@342 109 }
ysr@342 110
ysr@342 111 void G1BlockOffsetArray::set_space(Space* sp) {
ysr@342 112 _sp = sp;
ysr@342 113 _csp = sp->toContiguousSpace();
ysr@342 114 }
ysr@342 115
ysr@342 116 // The arguments follow the normal convention of denoting
ysr@342 117 // a right-open interval: [start, end)
ysr@342 118 void
ysr@342 119 G1BlockOffsetArray:: set_remainder_to_point_to_start(HeapWord* start, HeapWord* end) {
ysr@342 120
ysr@342 121 if (start >= end) {
ysr@342 122 // The start address is equal to the end address (or to
ysr@342 123 // the right of the end address) so there are not cards
ysr@342 124 // that need to be updated..
ysr@342 125 return;
ysr@342 126 }
ysr@342 127
ysr@342 128 // Write the backskip value for each region.
ysr@342 129 //
ysr@342 130 // offset
ysr@342 131 // card 2nd 3rd
ysr@342 132 // | +- 1st | |
ysr@342 133 // v v v v
ysr@342 134 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+-+-+-
ysr@342 135 // |x|0|0|0|0|0|0|0|1|1|1|1|1|1| ... |1|1|1|1|2|2|2|2|2|2| ...
ysr@342 136 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+-+-+-
ysr@342 137 // 11 19 75
ysr@342 138 // 12
ysr@342 139 //
ysr@342 140 // offset card is the card that points to the start of an object
ysr@342 141 // x - offset value of offset card
ysr@342 142 // 1st - start of first logarithmic region
ysr@342 143 // 0 corresponds to logarithmic value N_words + 0 and 2**(3 * 0) = 1
ysr@342 144 // 2nd - start of second logarithmic region
ysr@342 145 // 1 corresponds to logarithmic value N_words + 1 and 2**(3 * 1) = 8
ysr@342 146 // 3rd - start of third logarithmic region
ysr@342 147 // 2 corresponds to logarithmic value N_words + 2 and 2**(3 * 2) = 64
ysr@342 148 //
ysr@342 149 // integer below the block offset entry is an example of
ysr@342 150 // the index of the entry
ysr@342 151 //
ysr@342 152 // Given an address,
ysr@342 153 // Find the index for the address
ysr@342 154 // Find the block offset table entry
ysr@342 155 // Convert the entry to a back slide
ysr@342 156 // (e.g., with today's, offset = 0x81 =>
ysr@342 157 // back slip = 2**(3*(0x81 - N_words)) = 2**3) = 8
ysr@342 158 // Move back N (e.g., 8) entries and repeat with the
ysr@342 159 // value of the new entry
ysr@342 160 //
ysr@342 161 size_t start_card = _array->index_for(start);
ysr@342 162 size_t end_card = _array->index_for(end-1);
ysr@342 163 assert(start ==_array->address_for_index(start_card), "Precondition");
ysr@342 164 assert(end ==_array->address_for_index(end_card)+N_words, "Precondition");
ysr@342 165 set_remainder_to_point_to_start_incl(start_card, end_card); // closed interval
ysr@342 166 }
ysr@342 167
ysr@342 168 // Unlike the normal convention in this code, the argument here denotes
ysr@342 169 // a closed, inclusive interval: [start_card, end_card], cf set_remainder_to_point_to_start()
ysr@342 170 // above.
ysr@342 171 void
ysr@342 172 G1BlockOffsetArray::set_remainder_to_point_to_start_incl(size_t start_card, size_t end_card) {
ysr@342 173 if (start_card > end_card) {
ysr@342 174 return;
ysr@342 175 }
ysr@342 176 assert(start_card > _array->index_for(_bottom), "Cannot be first card");
ysr@342 177 assert(_array->offset_array(start_card-1) <= N_words,
ysr@342 178 "Offset card has an unexpected value");
ysr@342 179 size_t start_card_for_region = start_card;
ysr@342 180 u_char offset = max_jubyte;
ysr@342 181 for (int i = 0; i < BlockOffsetArray::N_powers; i++) {
ysr@342 182 // -1 so that the the card with the actual offset is counted. Another -1
ysr@342 183 // so that the reach ends in this region and not at the start
ysr@342 184 // of the next.
ysr@342 185 size_t reach = start_card - 1 + (BlockOffsetArray::power_to_cards_back(i+1) - 1);
ysr@342 186 offset = N_words + i;
ysr@342 187 if (reach >= end_card) {
ysr@342 188 _array->set_offset_array(start_card_for_region, end_card, offset);
ysr@342 189 start_card_for_region = reach + 1;
ysr@342 190 break;
ysr@342 191 }
ysr@342 192 _array->set_offset_array(start_card_for_region, reach, offset);
ysr@342 193 start_card_for_region = reach + 1;
ysr@342 194 }
ysr@342 195 assert(start_card_for_region > end_card, "Sanity check");
ysr@342 196 DEBUG_ONLY(check_all_cards(start_card, end_card);)
ysr@342 197 }
ysr@342 198
ysr@342 199 // The block [blk_start, blk_end) has been allocated;
ysr@342 200 // adjust the block offset table to represent this information;
ysr@342 201 // right-open interval: [blk_start, blk_end)
ysr@342 202 void
ysr@342 203 G1BlockOffsetArray::alloc_block(HeapWord* blk_start, HeapWord* blk_end) {
ysr@342 204 mark_block(blk_start, blk_end);
ysr@342 205 allocated(blk_start, blk_end);
ysr@342 206 }
ysr@342 207
ysr@342 208 // Adjust BOT to show that a previously whole block has been split
ysr@342 209 // into two.
ysr@342 210 void G1BlockOffsetArray::split_block(HeapWord* blk, size_t blk_size,
ysr@342 211 size_t left_blk_size) {
ysr@342 212 // Verify that the BOT shows [blk, blk + blk_size) to be one block.
ysr@342 213 verify_single_block(blk, blk_size);
ysr@342 214 // Update the BOT to indicate that [blk + left_blk_size, blk + blk_size)
ysr@342 215 // is one single block.
ysr@342 216 mark_block(blk + left_blk_size, blk + blk_size);
ysr@342 217 }
ysr@342 218
ysr@342 219
ysr@342 220 // Action_mark - update the BOT for the block [blk_start, blk_end).
ysr@342 221 // Current typical use is for splitting a block.
ysr@342 222 // Action_single - udpate the BOT for an allocation.
ysr@342 223 // Action_verify - BOT verification.
ysr@342 224 void G1BlockOffsetArray::do_block_internal(HeapWord* blk_start,
ysr@342 225 HeapWord* blk_end,
ysr@342 226 Action action) {
ysr@342 227 assert(Universe::heap()->is_in_reserved(blk_start),
ysr@342 228 "reference must be into the heap");
ysr@342 229 assert(Universe::heap()->is_in_reserved(blk_end-1),
ysr@342 230 "limit must be within the heap");
ysr@342 231 // This is optimized to make the test fast, assuming we only rarely
ysr@342 232 // cross boundaries.
ysr@342 233 uintptr_t end_ui = (uintptr_t)(blk_end - 1);
ysr@342 234 uintptr_t start_ui = (uintptr_t)blk_start;
ysr@342 235 // Calculate the last card boundary preceding end of blk
ysr@342 236 intptr_t boundary_before_end = (intptr_t)end_ui;
ysr@342 237 clear_bits(boundary_before_end, right_n_bits(LogN));
ysr@342 238 if (start_ui <= (uintptr_t)boundary_before_end) {
ysr@342 239 // blk starts at or crosses a boundary
ysr@342 240 // Calculate index of card on which blk begins
ysr@342 241 size_t start_index = _array->index_for(blk_start);
ysr@342 242 // Index of card on which blk ends
ysr@342 243 size_t end_index = _array->index_for(blk_end - 1);
ysr@342 244 // Start address of card on which blk begins
ysr@342 245 HeapWord* boundary = _array->address_for_index(start_index);
ysr@342 246 assert(boundary <= blk_start, "blk should start at or after boundary");
ysr@342 247 if (blk_start != boundary) {
ysr@342 248 // blk starts strictly after boundary
ysr@342 249 // adjust card boundary and start_index forward to next card
ysr@342 250 boundary += N_words;
ysr@342 251 start_index++;
ysr@342 252 }
ysr@342 253 assert(start_index <= end_index, "monotonicity of index_for()");
ysr@342 254 assert(boundary <= (HeapWord*)boundary_before_end, "tautology");
ysr@342 255 switch (action) {
ysr@342 256 case Action_mark: {
ysr@342 257 if (init_to_zero()) {
ysr@342 258 _array->set_offset_array(start_index, boundary, blk_start);
ysr@342 259 break;
ysr@342 260 } // Else fall through to the next case
ysr@342 261 }
ysr@342 262 case Action_single: {
ysr@342 263 _array->set_offset_array(start_index, boundary, blk_start);
ysr@342 264 // We have finished marking the "offset card". We need to now
ysr@342 265 // mark the subsequent cards that this blk spans.
ysr@342 266 if (start_index < end_index) {
ysr@342 267 HeapWord* rem_st = _array->address_for_index(start_index) + N_words;
ysr@342 268 HeapWord* rem_end = _array->address_for_index(end_index) + N_words;
ysr@342 269 set_remainder_to_point_to_start(rem_st, rem_end);
ysr@342 270 }
ysr@342 271 break;
ysr@342 272 }
ysr@342 273 case Action_check: {
ysr@342 274 _array->check_offset_array(start_index, boundary, blk_start);
ysr@342 275 // We have finished checking the "offset card". We need to now
ysr@342 276 // check the subsequent cards that this blk spans.
ysr@342 277 check_all_cards(start_index + 1, end_index);
ysr@342 278 break;
ysr@342 279 }
ysr@342 280 default:
ysr@342 281 ShouldNotReachHere();
ysr@342 282 }
ysr@342 283 }
ysr@342 284 }
ysr@342 285
ysr@342 286 // The card-interval [start_card, end_card] is a closed interval; this
ysr@342 287 // is an expensive check -- use with care and only under protection of
ysr@342 288 // suitable flag.
ysr@342 289 void G1BlockOffsetArray::check_all_cards(size_t start_card, size_t end_card) const {
ysr@342 290
ysr@342 291 if (end_card < start_card) {
ysr@342 292 return;
ysr@342 293 }
ysr@342 294 guarantee(_array->offset_array(start_card) == N_words, "Wrong value in second card");
ysr@342 295 for (size_t c = start_card + 1; c <= end_card; c++ /* yeah! */) {
ysr@342 296 u_char entry = _array->offset_array(c);
ysr@342 297 if (c - start_card > BlockOffsetArray::power_to_cards_back(1)) {
ysr@342 298 guarantee(entry > N_words, "Should be in logarithmic region");
ysr@342 299 }
ysr@342 300 size_t backskip = BlockOffsetArray::entry_to_cards_back(entry);
ysr@342 301 size_t landing_card = c - backskip;
ysr@342 302 guarantee(landing_card >= (start_card - 1), "Inv");
ysr@342 303 if (landing_card >= start_card) {
ysr@342 304 guarantee(_array->offset_array(landing_card) <= entry, "monotonicity");
ysr@342 305 } else {
ysr@342 306 guarantee(landing_card == start_card - 1, "Tautology");
ysr@342 307 guarantee(_array->offset_array(landing_card) <= N_words, "Offset value");
ysr@342 308 }
ysr@342 309 }
ysr@342 310 }
ysr@342 311
ysr@342 312 // The range [blk_start, blk_end) represents a single contiguous block
ysr@342 313 // of storage; modify the block offset table to represent this
ysr@342 314 // information; Right-open interval: [blk_start, blk_end)
ysr@342 315 // NOTE: this method does _not_ adjust _unallocated_block.
ysr@342 316 void
ysr@342 317 G1BlockOffsetArray::single_block(HeapWord* blk_start, HeapWord* blk_end) {
ysr@342 318 do_block_internal(blk_start, blk_end, Action_single);
ysr@342 319 }
ysr@342 320
ysr@342 321 // Mark the BOT such that if [blk_start, blk_end) straddles a card
ysr@342 322 // boundary, the card following the first such boundary is marked
ysr@342 323 // with the appropriate offset.
ysr@342 324 // NOTE: this method does _not_ adjust _unallocated_block or
ysr@342 325 // any cards subsequent to the first one.
ysr@342 326 void
ysr@342 327 G1BlockOffsetArray::mark_block(HeapWord* blk_start, HeapWord* blk_end) {
ysr@342 328 do_block_internal(blk_start, blk_end, Action_mark);
ysr@342 329 }
ysr@342 330
ysr@342 331 void G1BlockOffsetArray::join_blocks(HeapWord* blk1, HeapWord* blk2) {
ysr@342 332 HeapWord* blk1_start = Universe::heap()->block_start(blk1);
ysr@342 333 HeapWord* blk2_start = Universe::heap()->block_start(blk2);
ysr@342 334 assert(blk1 == blk1_start && blk2 == blk2_start,
ysr@342 335 "Must be block starts.");
ysr@342 336 assert(blk1 + _sp->block_size(blk1) == blk2, "Must be contiguous.");
ysr@342 337 size_t blk1_start_index = _array->index_for(blk1);
ysr@342 338 size_t blk2_start_index = _array->index_for(blk2);
ysr@342 339 assert(blk1_start_index <= blk2_start_index, "sanity");
ysr@342 340 HeapWord* blk2_card_start = _array->address_for_index(blk2_start_index);
ysr@342 341 if (blk2 == blk2_card_start) {
ysr@342 342 // blk2 starts a card. Does blk1 start on the prevous card, or futher
ysr@342 343 // back?
ysr@342 344 assert(blk1_start_index < blk2_start_index, "must be lower card.");
ysr@342 345 if (blk1_start_index + 1 == blk2_start_index) {
ysr@342 346 // previous card; new value for blk2 card is size of blk1.
ysr@342 347 _array->set_offset_array(blk2_start_index, (u_char) _sp->block_size(blk1));
ysr@342 348 } else {
ysr@342 349 // Earlier card; go back a card.
ysr@342 350 _array->set_offset_array(blk2_start_index, N_words);
ysr@342 351 }
ysr@342 352 } else {
ysr@342 353 // blk2 does not start a card. Does it cross a card? If not, nothing
ysr@342 354 // to do.
ysr@342 355 size_t blk2_end_index =
ysr@342 356 _array->index_for(blk2 + _sp->block_size(blk2) - 1);
ysr@342 357 assert(blk2_end_index >= blk2_start_index, "sanity");
ysr@342 358 if (blk2_end_index > blk2_start_index) {
ysr@342 359 // Yes, it crosses a card. The value for the next card must change.
ysr@342 360 if (blk1_start_index + 1 == blk2_start_index) {
ysr@342 361 // previous card; new value for second blk2 card is size of blk1.
ysr@342 362 _array->set_offset_array(blk2_start_index + 1,
ysr@342 363 (u_char) _sp->block_size(blk1));
ysr@342 364 } else {
ysr@342 365 // Earlier card; go back a card.
ysr@342 366 _array->set_offset_array(blk2_start_index + 1, N_words);
ysr@342 367 }
ysr@342 368 }
ysr@342 369 }
ysr@342 370 }
ysr@342 371
ysr@342 372 HeapWord* G1BlockOffsetArray::block_start_unsafe(const void* addr) {
ysr@342 373 assert(_bottom <= addr && addr < _end,
ysr@342 374 "addr must be covered by this Array");
ysr@342 375 // Must read this exactly once because it can be modified by parallel
ysr@342 376 // allocation.
ysr@342 377 HeapWord* ub = _unallocated_block;
ysr@342 378 if (BlockOffsetArrayUseUnallocatedBlock && addr >= ub) {
ysr@342 379 assert(ub < _end, "tautology (see above)");
ysr@342 380 return ub;
ysr@342 381 }
ysr@342 382 // Otherwise, find the block start using the table.
ysr@342 383 HeapWord* q = block_at_or_preceding(addr, false, 0);
ysr@342 384 return forward_to_block_containing_addr(q, addr);
ysr@342 385 }
ysr@342 386
ysr@342 387 // This duplicates a little code from the above: unavoidable.
ysr@342 388 HeapWord*
ysr@342 389 G1BlockOffsetArray::block_start_unsafe_const(const void* addr) const {
ysr@342 390 assert(_bottom <= addr && addr < _end,
ysr@342 391 "addr must be covered by this Array");
ysr@342 392 // Must read this exactly once because it can be modified by parallel
ysr@342 393 // allocation.
ysr@342 394 HeapWord* ub = _unallocated_block;
ysr@342 395 if (BlockOffsetArrayUseUnallocatedBlock && addr >= ub) {
ysr@342 396 assert(ub < _end, "tautology (see above)");
ysr@342 397 return ub;
ysr@342 398 }
ysr@342 399 // Otherwise, find the block start using the table.
ysr@342 400 HeapWord* q = block_at_or_preceding(addr, false, 0);
ysr@342 401 HeapWord* n = q + _sp->block_size(q);
ysr@342 402 return forward_to_block_containing_addr_const(q, n, addr);
ysr@342 403 }
ysr@342 404
ysr@342 405
ysr@342 406 HeapWord*
ysr@342 407 G1BlockOffsetArray::forward_to_block_containing_addr_slow(HeapWord* q,
ysr@342 408 HeapWord* n,
ysr@342 409 const void* addr) {
ysr@342 410 // We're not in the normal case. We need to handle an important subcase
ysr@342 411 // here: LAB allocation. An allocation previously recorded in the
ysr@342 412 // offset table was actually a lab allocation, and was divided into
ysr@342 413 // several objects subsequently. Fix this situation as we answer the
ysr@342 414 // query, by updating entries as we cross them.
ysr@342 415 size_t next_index = _array->index_for(n) + 1;
ysr@342 416 HeapWord* next_boundary = _array->address_for_index(next_index);
ysr@342 417 if (csp() != NULL) {
ysr@342 418 if (addr >= csp()->top()) return csp()->top();
ysr@342 419 while (next_boundary < addr) {
ysr@342 420 while (n <= next_boundary) {
ysr@342 421 q = n;
ysr@342 422 oop obj = oop(q);
ysr@342 423 if (obj->klass() == NULL) return q;
ysr@342 424 n += obj->size();
ysr@342 425 }
ysr@342 426 assert(q <= next_boundary && n > next_boundary, "Consequence of loop");
ysr@342 427 // [q, n) is the block that crosses the boundary.
ysr@342 428 alloc_block_work2(&next_boundary, &next_index, q, n);
ysr@342 429 }
ysr@342 430 } else {
ysr@342 431 while (next_boundary < addr) {
ysr@342 432 while (n <= next_boundary) {
ysr@342 433 q = n;
ysr@342 434 oop obj = oop(q);
ysr@342 435 if (obj->klass() == NULL) return q;
ysr@342 436 n += _sp->block_size(q);
ysr@342 437 }
ysr@342 438 assert(q <= next_boundary && n > next_boundary, "Consequence of loop");
ysr@342 439 // [q, n) is the block that crosses the boundary.
ysr@342 440 alloc_block_work2(&next_boundary, &next_index, q, n);
ysr@342 441 }
ysr@342 442 }
ysr@342 443 return forward_to_block_containing_addr_const(q, n, addr);
ysr@342 444 }
ysr@342 445
ysr@342 446 HeapWord* G1BlockOffsetArray::block_start_careful(const void* addr) const {
ysr@342 447 assert(_array->offset_array(0) == 0, "objects can't cross covered areas");
ysr@342 448
ysr@342 449 assert(_bottom <= addr && addr < _end,
ysr@342 450 "addr must be covered by this Array");
ysr@342 451 // Must read this exactly once because it can be modified by parallel
ysr@342 452 // allocation.
ysr@342 453 HeapWord* ub = _unallocated_block;
ysr@342 454 if (BlockOffsetArrayUseUnallocatedBlock && addr >= ub) {
ysr@342 455 assert(ub < _end, "tautology (see above)");
ysr@342 456 return ub;
ysr@342 457 }
ysr@342 458
ysr@342 459 // Otherwise, find the block start using the table, but taking
ysr@342 460 // care (cf block_start_unsafe() above) not to parse any objects/blocks
ysr@342 461 // on the cards themsleves.
ysr@342 462 size_t index = _array->index_for(addr);
ysr@342 463 assert(_array->address_for_index(index) == addr,
ysr@342 464 "arg should be start of card");
ysr@342 465
ysr@342 466 HeapWord* q = (HeapWord*)addr;
ysr@342 467 uint offset;
ysr@342 468 do {
ysr@342 469 offset = _array->offset_array(index--);
ysr@342 470 q -= offset;
ysr@342 471 } while (offset == N_words);
ysr@342 472 assert(q <= addr, "block start should be to left of arg");
ysr@342 473 return q;
ysr@342 474 }
ysr@342 475
ysr@342 476 // Note that the committed size of the covered space may have changed,
ysr@342 477 // so the table size might also wish to change.
ysr@342 478 void G1BlockOffsetArray::resize(size_t new_word_size) {
ysr@342 479 HeapWord* new_end = _bottom + new_word_size;
ysr@342 480 if (_end < new_end && !init_to_zero()) {
ysr@342 481 // verify that the old and new boundaries are also card boundaries
ysr@342 482 assert(_array->is_card_boundary(_end),
ysr@342 483 "_end not a card boundary");
ysr@342 484 assert(_array->is_card_boundary(new_end),
ysr@342 485 "new _end would not be a card boundary");
ysr@342 486 // set all the newly added cards
ysr@342 487 _array->set_offset_array(_end, new_end, N_words);
ysr@342 488 }
ysr@342 489 _end = new_end; // update _end
ysr@342 490 }
ysr@342 491
ysr@342 492 void G1BlockOffsetArray::set_region(MemRegion mr) {
ysr@342 493 _bottom = mr.start();
ysr@342 494 _end = mr.end();
ysr@342 495 }
ysr@342 496
ysr@342 497 //
ysr@342 498 // threshold_
ysr@342 499 // | _index_
ysr@342 500 // v v
ysr@342 501 // +-------+-------+-------+-------+-------+
ysr@342 502 // | i-1 | i | i+1 | i+2 | i+3 |
ysr@342 503 // +-------+-------+-------+-------+-------+
ysr@342 504 // ( ^ ]
ysr@342 505 // block-start
ysr@342 506 //
ysr@342 507 void G1BlockOffsetArray::alloc_block_work2(HeapWord** threshold_, size_t* index_,
ysr@342 508 HeapWord* blk_start, HeapWord* blk_end) {
ysr@342 509 // For efficiency, do copy-in/copy-out.
ysr@342 510 HeapWord* threshold = *threshold_;
ysr@342 511 size_t index = *index_;
ysr@342 512
ysr@342 513 assert(blk_start != NULL && blk_end > blk_start,
ysr@342 514 "phantom block");
ysr@342 515 assert(blk_end > threshold, "should be past threshold");
ysr@342 516 assert(blk_start <= threshold, "blk_start should be at or before threshold")
ysr@342 517 assert(pointer_delta(threshold, blk_start) <= N_words,
ysr@342 518 "offset should be <= BlockOffsetSharedArray::N");
ysr@342 519 assert(Universe::heap()->is_in_reserved(blk_start),
ysr@342 520 "reference must be into the heap");
ysr@342 521 assert(Universe::heap()->is_in_reserved(blk_end-1),
ysr@342 522 "limit must be within the heap");
ysr@342 523 assert(threshold == _array->_reserved.start() + index*N_words,
ysr@342 524 "index must agree with threshold");
ysr@342 525
ysr@342 526 DEBUG_ONLY(size_t orig_index = index;)
ysr@342 527
ysr@342 528 // Mark the card that holds the offset into the block. Note
ysr@342 529 // that _next_offset_index and _next_offset_threshold are not
ysr@342 530 // updated until the end of this method.
ysr@342 531 _array->set_offset_array(index, threshold, blk_start);
ysr@342 532
ysr@342 533 // We need to now mark the subsequent cards that this blk spans.
ysr@342 534
ysr@342 535 // Index of card on which blk ends.
ysr@342 536 size_t end_index = _array->index_for(blk_end - 1);
ysr@342 537
ysr@342 538 // Are there more cards left to be updated?
ysr@342 539 if (index + 1 <= end_index) {
ysr@342 540 HeapWord* rem_st = _array->address_for_index(index + 1);
ysr@342 541 // Calculate rem_end this way because end_index
ysr@342 542 // may be the last valid index in the covered region.
ysr@342 543 HeapWord* rem_end = _array->address_for_index(end_index) + N_words;
ysr@342 544 set_remainder_to_point_to_start(rem_st, rem_end);
ysr@342 545 }
ysr@342 546
ysr@342 547 index = end_index + 1;
ysr@342 548 // Calculate threshold_ this way because end_index
ysr@342 549 // may be the last valid index in the covered region.
ysr@342 550 threshold = _array->address_for_index(end_index) + N_words;
ysr@342 551 assert(threshold >= blk_end, "Incorrect offset threshold");
ysr@342 552
ysr@342 553 // index_ and threshold_ updated here.
ysr@342 554 *threshold_ = threshold;
ysr@342 555 *index_ = index;
ysr@342 556
ysr@342 557 #ifdef ASSERT
ysr@342 558 // The offset can be 0 if the block starts on a boundary. That
ysr@342 559 // is checked by an assertion above.
ysr@342 560 size_t start_index = _array->index_for(blk_start);
ysr@342 561 HeapWord* boundary = _array->address_for_index(start_index);
ysr@342 562 assert((_array->offset_array(orig_index) == 0 &&
ysr@342 563 blk_start == boundary) ||
ysr@342 564 (_array->offset_array(orig_index) > 0 &&
ysr@342 565 _array->offset_array(orig_index) <= N_words),
ysr@342 566 "offset array should have been set");
ysr@342 567 for (size_t j = orig_index + 1; j <= end_index; j++) {
ysr@342 568 assert(_array->offset_array(j) > 0 &&
ysr@342 569 _array->offset_array(j) <=
ysr@342 570 (u_char) (N_words+BlockOffsetArray::N_powers-1),
ysr@342 571 "offset array should have been set");
ysr@342 572 }
ysr@342 573 #endif
ysr@342 574 }
ysr@342 575
ysr@342 576 //////////////////////////////////////////////////////////////////////
ysr@342 577 // G1BlockOffsetArrayContigSpace
ysr@342 578 //////////////////////////////////////////////////////////////////////
ysr@342 579
ysr@342 580 HeapWord*
ysr@342 581 G1BlockOffsetArrayContigSpace::block_start_unsafe(const void* addr) {
ysr@342 582 assert(_bottom <= addr && addr < _end,
ysr@342 583 "addr must be covered by this Array");
ysr@342 584 HeapWord* q = block_at_or_preceding(addr, true, _next_offset_index-1);
ysr@342 585 return forward_to_block_containing_addr(q, addr);
ysr@342 586 }
ysr@342 587
ysr@342 588 HeapWord*
ysr@342 589 G1BlockOffsetArrayContigSpace::
ysr@342 590 block_start_unsafe_const(const void* addr) const {
ysr@342 591 assert(_bottom <= addr && addr < _end,
ysr@342 592 "addr must be covered by this Array");
ysr@342 593 HeapWord* q = block_at_or_preceding(addr, true, _next_offset_index-1);
ysr@342 594 HeapWord* n = q + _sp->block_size(q);
ysr@342 595 return forward_to_block_containing_addr_const(q, n, addr);
ysr@342 596 }
ysr@342 597
ysr@342 598 G1BlockOffsetArrayContigSpace::
ysr@342 599 G1BlockOffsetArrayContigSpace(G1BlockOffsetSharedArray* array,
ysr@342 600 MemRegion mr) :
ysr@342 601 G1BlockOffsetArray(array, mr, true)
ysr@342 602 {
ysr@342 603 _next_offset_threshold = NULL;
ysr@342 604 _next_offset_index = 0;
ysr@342 605 }
ysr@342 606
ysr@342 607 HeapWord* G1BlockOffsetArrayContigSpace::initialize_threshold() {
ysr@342 608 assert(!Universe::heap()->is_in_reserved(_array->_offset_array),
ysr@342 609 "just checking");
ysr@342 610 _next_offset_index = _array->index_for(_bottom);
ysr@342 611 _next_offset_index++;
ysr@342 612 _next_offset_threshold =
ysr@342 613 _array->address_for_index(_next_offset_index);
ysr@342 614 return _next_offset_threshold;
ysr@342 615 }
ysr@342 616
ysr@342 617 void G1BlockOffsetArrayContigSpace::zero_bottom_entry() {
ysr@342 618 assert(!Universe::heap()->is_in_reserved(_array->_offset_array),
ysr@342 619 "just checking");
ysr@342 620 size_t bottom_index = _array->index_for(_bottom);
ysr@342 621 assert(_array->address_for_index(bottom_index) == _bottom,
ysr@342 622 "Precondition of call");
ysr@342 623 _array->set_offset_array(bottom_index, 0);
ysr@342 624 }