Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/memory/blockOffsetTable.cpp
32285 views
/*1* Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "gc_interface/collectedHeap.inline.hpp"26#include "memory/blockOffsetTable.inline.hpp"27#include "memory/iterator.hpp"28#include "memory/space.inline.hpp"29#include "memory/universe.hpp"30#include "oops/oop.inline.hpp"31#include "runtime/java.hpp"32#include "services/memTracker.hpp"3334//////////////////////////////////////////////////////////////////////35// BlockOffsetSharedArray36//////////////////////////////////////////////////////////////////////3738BlockOffsetSharedArray::BlockOffsetSharedArray(MemRegion reserved,39size_t init_word_size):40_reserved(reserved), _end(NULL)41{42size_t size = compute_size(reserved.word_size());43ReservedSpace rs(size);44if (!rs.is_reserved()) {45vm_exit_during_initialization("Could not reserve enough space for heap offset array");46}4748MemTracker::record_virtual_memory_type((address)rs.base(), mtGC);4950if (!_vs.initialize(rs, 0)) {51vm_exit_during_initialization("Could not reserve enough space for heap offset array");52}53_offset_array = (u_char*)_vs.low_boundary();54resize(init_word_size);55if (TraceBlockOffsetTable) {56gclog_or_tty->print_cr("BlockOffsetSharedArray::BlockOffsetSharedArray: ");57gclog_or_tty->print_cr(" "58" rs.base(): " INTPTR_FORMAT59" rs.size(): " INTPTR_FORMAT60" rs end(): " INTPTR_FORMAT,61p2i(rs.base()), rs.size(), p2i(rs.base() + rs.size()));62gclog_or_tty->print_cr(" "63" _vs.low_boundary(): " INTPTR_FORMAT64" _vs.high_boundary(): " INTPTR_FORMAT,65p2i(_vs.low_boundary()),66p2i(_vs.high_boundary()));67}68}6970void BlockOffsetSharedArray::resize(size_t new_word_size) {71assert(new_word_size <= _reserved.word_size(), "Resize larger than reserved");72size_t new_size = compute_size(new_word_size);73size_t old_size = _vs.committed_size();74size_t delta;75char* high = _vs.high();76_end = _reserved.start() + new_word_size;77if (new_size > old_size) {78delta = ReservedSpace::page_align_size_up(new_size - old_size);79assert(delta > 0, "just checking");80if (!_vs.expand_by(delta)) {81// Do better than this for Merlin82vm_exit_out_of_memory(delta, OOM_MMAP_ERROR, "offset table expansion");83}84assert(_vs.high() == high + delta, "invalid expansion");85} else {86delta = ReservedSpace::page_align_size_down(old_size - new_size);87if (delta == 0) return;88_vs.shrink_by(delta);89assert(_vs.high() == high - delta, "invalid expansion");90}91}9293bool BlockOffsetSharedArray::is_card_boundary(HeapWord* p) const {94assert(p >= _reserved.start(), "just checking");95size_t delta = pointer_delta(p, _reserved.start());96return (delta & right_n_bits(LogN_words)) == (size_t)NoBits;97}9899100//////////////////////////////////////////////////////////////////////101// BlockOffsetArray102//////////////////////////////////////////////////////////////////////103104BlockOffsetArray::BlockOffsetArray(BlockOffsetSharedArray* array,105MemRegion mr, bool init_to_zero_) :106BlockOffsetTable(mr.start(), mr.end()),107_array(array)108{109assert(_bottom <= _end, "arguments out of order");110set_init_to_zero(init_to_zero_);111if (!init_to_zero_) {112// initialize cards to point back to mr.start()113set_remainder_to_point_to_start(mr.start() + N_words, mr.end());114_array->set_offset_array(0, 0); // set first card to 0115}116}117118119// The arguments follow the normal convention of denoting120// a right-open interval: [start, end)121void122BlockOffsetArray::123set_remainder_to_point_to_start(HeapWord* start, HeapWord* end, bool reducing) {124125check_reducing_assertion(reducing);126if (start >= end) {127// The start address is equal to the end address (or to128// the right of the end address) so there are not cards129// that need to be updated..130return;131}132133// Write the backskip value for each region.134//135// offset136// card 2nd 3rd137// | +- 1st | |138// v v v v139// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+-+-+-140// |x|0|0|0|0|0|0|0|1|1|1|1|1|1| ... |1|1|1|1|2|2|2|2|2|2| ...141// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+-+-+-142// 11 19 75143// 12144//145// offset card is the card that points to the start of an object146// x - offset value of offset card147// 1st - start of first logarithmic region148// 0 corresponds to logarithmic value N_words + 0 and 2**(3 * 0) = 1149// 2nd - start of second logarithmic region150// 1 corresponds to logarithmic value N_words + 1 and 2**(3 * 1) = 8151// 3rd - start of third logarithmic region152// 2 corresponds to logarithmic value N_words + 2 and 2**(3 * 2) = 64153//154// integer below the block offset entry is an example of155// the index of the entry156//157// Given an address,158// Find the index for the address159// Find the block offset table entry160// Convert the entry to a back slide161// (e.g., with today's, offset = 0x81 =>162// back slip = 2**(3*(0x81 - N_words)) = 2**3) = 8163// Move back N (e.g., 8) entries and repeat with the164// value of the new entry165//166size_t start_card = _array->index_for(start);167size_t end_card = _array->index_for(end-1);168assert(start ==_array->address_for_index(start_card), "Precondition");169assert(end ==_array->address_for_index(end_card)+N_words, "Precondition");170set_remainder_to_point_to_start_incl(start_card, end_card, reducing); // closed interval171}172173174// Unlike the normal convention in this code, the argument here denotes175// a closed, inclusive interval: [start_card, end_card], cf set_remainder_to_point_to_start()176// above.177void178BlockOffsetArray::set_remainder_to_point_to_start_incl(size_t start_card, size_t end_card, bool reducing) {179180check_reducing_assertion(reducing);181if (start_card > end_card) {182return;183}184assert(start_card > _array->index_for(_bottom), "Cannot be first card");185assert(_array->offset_array(start_card-1) <= N_words,186"Offset card has an unexpected value");187size_t start_card_for_region = start_card;188u_char offset = max_jubyte;189for (int i = 0; i < N_powers; i++) {190// -1 so that the the card with the actual offset is counted. Another -1191// so that the reach ends in this region and not at the start192// of the next.193size_t reach = start_card - 1 + (power_to_cards_back(i+1) - 1);194offset = N_words + i;195if (reach >= end_card) {196_array->set_offset_array(start_card_for_region, end_card, offset, reducing);197start_card_for_region = reach + 1;198break;199}200_array->set_offset_array(start_card_for_region, reach, offset, reducing);201start_card_for_region = reach + 1;202}203assert(start_card_for_region > end_card, "Sanity check");204DEBUG_ONLY(check_all_cards(start_card, end_card);)205}206207// The card-interval [start_card, end_card] is a closed interval; this208// is an expensive check -- use with care and only under protection of209// suitable flag.210void BlockOffsetArray::check_all_cards(size_t start_card, size_t end_card) const {211212if (end_card < start_card) {213return;214}215guarantee(_array->offset_array(start_card) == N_words, "Wrong value in second card");216u_char last_entry = N_words;217for (size_t c = start_card + 1; c <= end_card; c++ /* yeah! */) {218u_char entry = _array->offset_array(c);219guarantee(entry >= last_entry, "Monotonicity");220if (c - start_card > power_to_cards_back(1)) {221guarantee(entry > N_words, "Should be in logarithmic region");222}223size_t backskip = entry_to_cards_back(entry);224size_t landing_card = c - backskip;225guarantee(landing_card >= (start_card - 1), "Inv");226if (landing_card >= start_card) {227guarantee(_array->offset_array(landing_card) <= entry, "Monotonicity");228} else {229guarantee(landing_card == (start_card - 1), "Tautology");230// Note that N_words is the maximum offset value231guarantee(_array->offset_array(landing_card) <= N_words, "Offset value");232}233last_entry = entry; // remember for monotonicity test234}235}236237238void239BlockOffsetArray::alloc_block(HeapWord* blk_start, HeapWord* blk_end) {240assert(blk_start != NULL && blk_end > blk_start,241"phantom block");242single_block(blk_start, blk_end);243}244245// Action_mark - update the BOT for the block [blk_start, blk_end).246// Current typical use is for splitting a block.247// Action_single - udpate the BOT for an allocation.248// Action_verify - BOT verification.249void250BlockOffsetArray::do_block_internal(HeapWord* blk_start,251HeapWord* blk_end,252Action action, bool reducing) {253assert(Universe::heap()->is_in_reserved(blk_start),254"reference must be into the heap");255assert(Universe::heap()->is_in_reserved(blk_end-1),256"limit must be within the heap");257// This is optimized to make the test fast, assuming we only rarely258// cross boundaries.259uintptr_t end_ui = (uintptr_t)(blk_end - 1);260uintptr_t start_ui = (uintptr_t)blk_start;261// Calculate the last card boundary preceding end of blk262intptr_t boundary_before_end = (intptr_t)end_ui;263clear_bits(boundary_before_end, right_n_bits(LogN));264if (start_ui <= (uintptr_t)boundary_before_end) {265// blk starts at or crosses a boundary266// Calculate index of card on which blk begins267size_t start_index = _array->index_for(blk_start);268// Index of card on which blk ends269size_t end_index = _array->index_for(blk_end - 1);270// Start address of card on which blk begins271HeapWord* boundary = _array->address_for_index(start_index);272assert(boundary <= blk_start, "blk should start at or after boundary");273if (blk_start != boundary) {274// blk starts strictly after boundary275// adjust card boundary and start_index forward to next card276boundary += N_words;277start_index++;278}279assert(start_index <= end_index, "monotonicity of index_for()");280assert(boundary <= (HeapWord*)boundary_before_end, "tautology");281switch (action) {282case Action_mark: {283if (init_to_zero()) {284_array->set_offset_array(start_index, boundary, blk_start, reducing);285break;286} // Else fall through to the next case287}288case Action_single: {289_array->set_offset_array(start_index, boundary, blk_start, reducing);290// We have finished marking the "offset card". We need to now291// mark the subsequent cards that this blk spans.292if (start_index < end_index) {293HeapWord* rem_st = _array->address_for_index(start_index) + N_words;294HeapWord* rem_end = _array->address_for_index(end_index) + N_words;295set_remainder_to_point_to_start(rem_st, rem_end, reducing);296}297break;298}299case Action_check: {300_array->check_offset_array(start_index, boundary, blk_start);301// We have finished checking the "offset card". We need to now302// check the subsequent cards that this blk spans.303check_all_cards(start_index + 1, end_index);304break;305}306default:307ShouldNotReachHere();308}309}310}311312// The range [blk_start, blk_end) represents a single contiguous block313// of storage; modify the block offset table to represent this314// information; Right-open interval: [blk_start, blk_end)315// NOTE: this method does _not_ adjust _unallocated_block.316void317BlockOffsetArray::single_block(HeapWord* blk_start,318HeapWord* blk_end) {319do_block_internal(blk_start, blk_end, Action_single);320}321322void BlockOffsetArray::verify() const {323// For each entry in the block offset table, verify that324// the entry correctly finds the start of an object at the325// first address covered by the block or to the left of that326// first address.327328size_t next_index = 1;329size_t last_index = last_active_index();330331// Use for debugging. Initialize to NULL to distinguish the332// first iteration through the while loop.333HeapWord* last_p = NULL;334HeapWord* last_start = NULL;335oop last_o = NULL;336337while (next_index <= last_index) {338// Use an address past the start of the address for339// the entry.340HeapWord* p = _array->address_for_index(next_index) + 1;341if (p >= _end) {342// That's all of the allocated block table.343return;344}345// block_start() asserts that start <= p.346HeapWord* start = block_start(p);347// First check if the start is an allocated block and only348// then if it is a valid object.349oop o = oop(start);350assert(!Universe::is_fully_initialized() ||351_sp->is_free_block(start) ||352o->is_oop_or_null(), "Bad object was found");353next_index++;354last_p = p;355last_start = start;356last_o = o;357}358}359360//////////////////////////////////////////////////////////////////////361// BlockOffsetArrayNonContigSpace362//////////////////////////////////////////////////////////////////////363364// The block [blk_start, blk_end) has been allocated;365// adjust the block offset table to represent this information;366// NOTE: Clients of BlockOffsetArrayNonContigSpace: consider using367// the somewhat more lightweight split_block() or368// (when init_to_zero()) mark_block() wherever possible.369// right-open interval: [blk_start, blk_end)370void371BlockOffsetArrayNonContigSpace::alloc_block(HeapWord* blk_start,372HeapWord* blk_end) {373assert(blk_start != NULL && blk_end > blk_start,374"phantom block");375single_block(blk_start, blk_end);376allocated(blk_start, blk_end);377}378379// Adjust BOT to show that a previously whole block has been split380// into two. We verify the BOT for the first part (prefix) and381// update the BOT for the second part (suffix).382// blk is the start of the block383// blk_size is the size of the original block384// left_blk_size is the size of the first part of the split385void BlockOffsetArrayNonContigSpace::split_block(HeapWord* blk,386size_t blk_size,387size_t left_blk_size) {388// Verify that the BOT shows [blk, blk + blk_size) to be one block.389verify_single_block(blk, blk_size);390// Update the BOT to indicate that [blk + left_blk_size, blk + blk_size)391// is one single block.392assert(blk_size > 0, "Should be positive");393assert(left_blk_size > 0, "Should be positive");394assert(left_blk_size < blk_size, "Not a split");395396// Start addresses of prefix block and suffix block.397HeapWord* pref_addr = blk;398HeapWord* suff_addr = blk + left_blk_size;399HeapWord* end_addr = blk + blk_size;400401// Indices for starts of prefix block and suffix block.402size_t pref_index = _array->index_for(pref_addr);403if (_array->address_for_index(pref_index) != pref_addr) {404// pref_addr does not begin pref_index405pref_index++;406}407408size_t suff_index = _array->index_for(suff_addr);409if (_array->address_for_index(suff_index) != suff_addr) {410// suff_addr does not begin suff_index411suff_index++;412}413414// Definition: A block B, denoted [B_start, B_end) __starts__415// a card C, denoted [C_start, C_end), where C_start and C_end416// are the heap addresses that card C covers, iff417// B_start <= C_start < B_end.418//419// We say that a card C "is started by" a block B, iff420// B "starts" C.421//422// Note that the cardinality of the set of cards {C}423// started by a block B can be 0, 1, or more.424//425// Below, pref_index and suff_index are, respectively, the426// first (least) card indices that the prefix and suffix of427// the split start; end_index is one more than the index of428// the last (greatest) card that blk starts.429size_t end_index = _array->index_for(end_addr - 1) + 1;430431// Calculate the # cards that the prefix and suffix affect.432size_t num_pref_cards = suff_index - pref_index;433434size_t num_suff_cards = end_index - suff_index;435// Change the cards that need changing436if (num_suff_cards > 0) {437HeapWord* boundary = _array->address_for_index(suff_index);438// Set the offset card for suffix block439_array->set_offset_array(suff_index, boundary, suff_addr, true /* reducing */);440// Change any further cards that need changing in the suffix441if (num_pref_cards > 0) {442if (num_pref_cards >= num_suff_cards) {443// Unilaterally fix all of the suffix cards: closed card444// index interval in args below.445set_remainder_to_point_to_start_incl(suff_index + 1, end_index - 1, true /* reducing */);446} else {447// Unilaterally fix the first (num_pref_cards - 1) following448// the "offset card" in the suffix block.449set_remainder_to_point_to_start_incl(suff_index + 1,450suff_index + num_pref_cards - 1, true /* reducing */);451// Fix the appropriate cards in the remainder of the452// suffix block -- these are the last num_pref_cards453// cards in each power block of the "new" range plumbed454// from suff_addr.455bool more = true;456uint i = 1;457while (more && (i < N_powers)) {458size_t back_by = power_to_cards_back(i);459size_t right_index = suff_index + back_by - 1;460size_t left_index = right_index - num_pref_cards + 1;461if (right_index >= end_index - 1) { // last iteration462right_index = end_index - 1;463more = false;464}465if (back_by > num_pref_cards) {466// Fill in the remainder of this "power block", if it467// is non-null.468if (left_index <= right_index) {469_array->set_offset_array(left_index, right_index,470N_words + i - 1, true /* reducing */);471} else {472more = false; // we are done473}474i++;475break;476}477i++;478}479while (more && (i < N_powers)) {480size_t back_by = power_to_cards_back(i);481size_t right_index = suff_index + back_by - 1;482size_t left_index = right_index - num_pref_cards + 1;483if (right_index >= end_index - 1) { // last iteration484right_index = end_index - 1;485if (left_index > right_index) {486break;487}488more = false;489}490assert(left_index <= right_index, "Error");491_array->set_offset_array(left_index, right_index, N_words + i - 1, true /* reducing */);492i++;493}494}495} // else no more cards to fix in suffix496} // else nothing needs to be done497// Verify that we did the right thing498verify_single_block(pref_addr, left_blk_size);499verify_single_block(suff_addr, blk_size - left_blk_size);500}501502503// Mark the BOT such that if [blk_start, blk_end) straddles a card504// boundary, the card following the first such boundary is marked505// with the appropriate offset.506// NOTE: this method does _not_ adjust _unallocated_block or507// any cards subsequent to the first one.508void509BlockOffsetArrayNonContigSpace::mark_block(HeapWord* blk_start,510HeapWord* blk_end, bool reducing) {511do_block_internal(blk_start, blk_end, Action_mark, reducing);512}513514HeapWord* BlockOffsetArrayNonContigSpace::block_start_unsafe(515const void* addr) const {516assert(_array->offset_array(0) == 0, "objects can't cross covered areas");517assert(_bottom <= addr && addr < _end,518"addr must be covered by this Array");519// Must read this exactly once because it can be modified by parallel520// allocation.521HeapWord* ub = _unallocated_block;522if (BlockOffsetArrayUseUnallocatedBlock && addr >= ub) {523assert(ub < _end, "tautology (see above)");524return ub;525}526527// Otherwise, find the block start using the table.528size_t index = _array->index_for(addr);529HeapWord* q = _array->address_for_index(index);530531uint offset = _array->offset_array(index); // Extend u_char to uint.532while (offset >= N_words) {533// The excess of the offset from N_words indicates a power of Base534// to go back by.535size_t n_cards_back = entry_to_cards_back(offset);536q -= (N_words * n_cards_back);537assert(q >= _sp->bottom(),538err_msg("q = " PTR_FORMAT " crossed below bottom = " PTR_FORMAT,539p2i(q), p2i(_sp->bottom())));540assert(q < _sp->end(),541err_msg("q = " PTR_FORMAT " crossed above end = " PTR_FORMAT,542p2i(q), p2i(_sp->end())));543index -= n_cards_back;544offset = _array->offset_array(index);545}546assert(offset < N_words, "offset too large");547index--;548q -= offset;549assert(q >= _sp->bottom(),550err_msg("q = " PTR_FORMAT " crossed below bottom = " PTR_FORMAT,551p2i(q), p2i(_sp->bottom())));552assert(q < _sp->end(),553err_msg("q = " PTR_FORMAT " crossed above end = " PTR_FORMAT,554p2i(q), p2i(_sp->end())));555HeapWord* n = q;556557while (n <= addr) {558debug_only(HeapWord* last = q); // for debugging559q = n;560n += _sp->block_size(n);561assert(n > q,562err_msg("Looping at n = " PTR_FORMAT " with last = " PTR_FORMAT ","563" while querying blk_start(" PTR_FORMAT ")"564" on _sp = [" PTR_FORMAT "," PTR_FORMAT ")",565p2i(n), p2i(last), p2i(addr), p2i(_sp->bottom()), p2i(_sp->end())));566}567assert(q <= addr,568err_msg("wrong order for current (" INTPTR_FORMAT ")" " <= arg (" INTPTR_FORMAT ")",569p2i(q), p2i(addr)));570assert(addr <= n,571err_msg("wrong order for arg (" INTPTR_FORMAT ") <= next (" INTPTR_FORMAT ")",572p2i(addr), p2i(n)));573return q;574}575576HeapWord* BlockOffsetArrayNonContigSpace::block_start_careful(577const void* addr) const {578assert(_array->offset_array(0) == 0, "objects can't cross covered areas");579580assert(_bottom <= addr && addr < _end,581"addr must be covered by this Array");582// Must read this exactly once because it can be modified by parallel583// allocation.584HeapWord* ub = _unallocated_block;585if (BlockOffsetArrayUseUnallocatedBlock && addr >= ub) {586assert(ub < _end, "tautology (see above)");587return ub;588}589590// Otherwise, find the block start using the table, but taking591// care (cf block_start_unsafe() above) not to parse any objects/blocks592// on the cards themsleves.593size_t index = _array->index_for(addr);594assert(_array->address_for_index(index) == addr,595"arg should be start of card");596597HeapWord* q = (HeapWord*)addr;598uint offset;599do {600offset = _array->offset_array(index);601if (offset < N_words) {602q -= offset;603} else {604size_t n_cards_back = entry_to_cards_back(offset);605q -= (n_cards_back * N_words);606index -= n_cards_back;607}608} while (offset >= N_words);609assert(q <= addr, "block start should be to left of arg");610return q;611}612613#ifndef PRODUCT614// Verification & debugging - ensure that the offset table reflects the fact615// that the block [blk_start, blk_end) or [blk, blk + size) is a616// single block of storage. NOTE: can't const this because of617// call to non-const do_block_internal() below.618void BlockOffsetArrayNonContigSpace::verify_single_block(619HeapWord* blk_start, HeapWord* blk_end) {620if (VerifyBlockOffsetArray) {621do_block_internal(blk_start, blk_end, Action_check);622}623}624625void BlockOffsetArrayNonContigSpace::verify_single_block(626HeapWord* blk, size_t size) {627verify_single_block(blk, blk + size);628}629630// Verify that the given block is before _unallocated_block631void BlockOffsetArrayNonContigSpace::verify_not_unallocated(632HeapWord* blk_start, HeapWord* blk_end) const {633if (BlockOffsetArrayUseUnallocatedBlock) {634assert(blk_start < blk_end, "Block inconsistency?");635assert(blk_end <= _unallocated_block, "_unallocated_block problem");636}637}638639void BlockOffsetArrayNonContigSpace::verify_not_unallocated(640HeapWord* blk, size_t size) const {641verify_not_unallocated(blk, blk + size);642}643#endif // PRODUCT644645size_t BlockOffsetArrayNonContigSpace::last_active_index() const {646if (_unallocated_block == _bottom) {647return 0;648} else {649return _array->index_for(_unallocated_block - 1);650}651}652653//////////////////////////////////////////////////////////////////////654// BlockOffsetArrayContigSpace655//////////////////////////////////////////////////////////////////////656657HeapWord* BlockOffsetArrayContigSpace::block_start_unsafe(const void* addr) const {658assert(_array->offset_array(0) == 0, "objects can't cross covered areas");659660// Otherwise, find the block start using the table.661assert(_bottom <= addr && addr < _end,662"addr must be covered by this Array");663size_t index = _array->index_for(addr);664// We must make sure that the offset table entry we use is valid. If665// "addr" is past the end, start at the last known one and go forward.666index = MIN2(index, _next_offset_index-1);667HeapWord* q = _array->address_for_index(index);668669uint offset = _array->offset_array(index); // Extend u_char to uint.670while (offset > N_words) {671// The excess of the offset from N_words indicates a power of Base672// to go back by.673size_t n_cards_back = entry_to_cards_back(offset);674q -= (N_words * n_cards_back);675assert(q >= _sp->bottom(), "Went below bottom!");676index -= n_cards_back;677offset = _array->offset_array(index);678}679while (offset == N_words) {680assert(q >= _sp->bottom(), "Went below bottom!");681q -= N_words;682index--;683offset = _array->offset_array(index);684}685assert(offset < N_words, "offset too large");686q -= offset;687HeapWord* n = q;688689while (n <= addr) {690debug_only(HeapWord* last = q); // for debugging691q = n;692n += _sp->block_size(n);693}694assert(q <= addr, "wrong order for current and arg");695assert(addr <= n, "wrong order for arg and next");696return q;697}698699//700// _next_offset_threshold701// | _next_offset_index702// v v703// +-------+-------+-------+-------+-------+704// | i-1 | i | i+1 | i+2 | i+3 |705// +-------+-------+-------+-------+-------+706// ( ^ ]707// block-start708//709710void BlockOffsetArrayContigSpace::alloc_block_work(HeapWord* blk_start,711HeapWord* blk_end) {712assert(blk_start != NULL && blk_end > blk_start,713"phantom block");714assert(blk_end > _next_offset_threshold,715"should be past threshold");716assert(blk_start <= _next_offset_threshold,717"blk_start should be at or before threshold");718assert(pointer_delta(_next_offset_threshold, blk_start) <= N_words,719"offset should be <= BlockOffsetSharedArray::N");720assert(Universe::heap()->is_in_reserved(blk_start),721"reference must be into the heap");722assert(Universe::heap()->is_in_reserved(blk_end-1),723"limit must be within the heap");724assert(_next_offset_threshold ==725_array->_reserved.start() + _next_offset_index*N_words,726"index must agree with threshold");727728debug_only(size_t orig_next_offset_index = _next_offset_index;)729730// Mark the card that holds the offset into the block. Note731// that _next_offset_index and _next_offset_threshold are not732// updated until the end of this method.733_array->set_offset_array(_next_offset_index,734_next_offset_threshold,735blk_start);736737// We need to now mark the subsequent cards that this blk spans.738739// Index of card on which blk ends.740size_t end_index = _array->index_for(blk_end - 1);741742// Are there more cards left to be updated?743if (_next_offset_index + 1 <= end_index) {744HeapWord* rem_st = _array->address_for_index(_next_offset_index + 1);745// Calculate rem_end this way because end_index746// may be the last valid index in the covered region.747HeapWord* rem_end = _array->address_for_index(end_index) + N_words;748set_remainder_to_point_to_start(rem_st, rem_end);749}750751// _next_offset_index and _next_offset_threshold updated here.752_next_offset_index = end_index + 1;753// Calculate _next_offset_threshold this way because end_index754// may be the last valid index in the covered region.755_next_offset_threshold = _array->address_for_index(end_index) + N_words;756assert(_next_offset_threshold >= blk_end, "Incorrect offset threshold");757758#ifdef ASSERT759// The offset can be 0 if the block starts on a boundary. That760// is checked by an assertion above.761size_t start_index = _array->index_for(blk_start);762HeapWord* boundary = _array->address_for_index(start_index);763assert((_array->offset_array(orig_next_offset_index) == 0 &&764blk_start == boundary) ||765(_array->offset_array(orig_next_offset_index) > 0 &&766_array->offset_array(orig_next_offset_index) <= N_words),767"offset array should have been set");768for (size_t j = orig_next_offset_index + 1; j <= end_index; j++) {769assert(_array->offset_array(j) > 0 &&770_array->offset_array(j) <= (u_char) (N_words+N_powers-1),771"offset array should have been set");772}773#endif774}775776HeapWord* BlockOffsetArrayContigSpace::initialize_threshold() {777assert(!Universe::heap()->is_in_reserved(_array->_offset_array),778"just checking");779_next_offset_index = _array->index_for(_bottom);780_next_offset_index++;781_next_offset_threshold =782_array->address_for_index(_next_offset_index);783return _next_offset_threshold;784}785786void BlockOffsetArrayContigSpace::zero_bottom_entry() {787assert(!Universe::heap()->is_in_reserved(_array->_offset_array),788"just checking");789size_t bottom_index = _array->index_for(_bottom);790_array->set_offset_array(bottom_index, 0);791}792793size_t BlockOffsetArrayContigSpace::last_active_index() const {794size_t result = _next_offset_index - 1;795return result >= 0 ? result : 0;796}797798799