Wed, 15 Feb 2012 10:12:55 -0800
7145537: minor tweaks to LogEvents
Reviewed-by: kvn, twisti
1 /*
2 * Copyright (c) 2000, 2011, 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 */
25 #include "precompiled.hpp"
26 #include "gc_interface/collectedHeap.inline.hpp"
27 #include "memory/blockOffsetTable.inline.hpp"
28 #include "memory/iterator.hpp"
29 #include "memory/space.inline.hpp"
30 #include "memory/universe.hpp"
31 #include "oops/oop.inline.hpp"
32 #include "runtime/java.hpp"
34 //////////////////////////////////////////////////////////////////////
35 // BlockOffsetSharedArray
36 //////////////////////////////////////////////////////////////////////
38 BlockOffsetSharedArray::BlockOffsetSharedArray(MemRegion reserved,
39 size_t init_word_size):
40 _reserved(reserved), _end(NULL)
41 {
42 size_t size = compute_size(reserved.word_size());
43 ReservedSpace rs(size);
44 if (!rs.is_reserved()) {
45 vm_exit_during_initialization("Could not reserve enough space for heap offset array");
46 }
47 if (!_vs.initialize(rs, 0)) {
48 vm_exit_during_initialization("Could not reserve enough space for heap offset array");
49 }
50 _offset_array = (u_char*)_vs.low_boundary();
51 resize(init_word_size);
52 if (TraceBlockOffsetTable) {
53 gclog_or_tty->print_cr("BlockOffsetSharedArray::BlockOffsetSharedArray: ");
54 gclog_or_tty->print_cr(" "
55 " rs.base(): " INTPTR_FORMAT
56 " rs.size(): " INTPTR_FORMAT
57 " rs end(): " INTPTR_FORMAT,
58 rs.base(), rs.size(), rs.base() + rs.size());
59 gclog_or_tty->print_cr(" "
60 " _vs.low_boundary(): " INTPTR_FORMAT
61 " _vs.high_boundary(): " INTPTR_FORMAT,
62 _vs.low_boundary(),
63 _vs.high_boundary());
64 }
65 }
67 void BlockOffsetSharedArray::resize(size_t new_word_size) {
68 assert(new_word_size <= _reserved.word_size(), "Resize larger than reserved");
69 size_t new_size = compute_size(new_word_size);
70 size_t old_size = _vs.committed_size();
71 size_t delta;
72 char* high = _vs.high();
73 _end = _reserved.start() + new_word_size;
74 if (new_size > old_size) {
75 delta = ReservedSpace::page_align_size_up(new_size - old_size);
76 assert(delta > 0, "just checking");
77 if (!_vs.expand_by(delta)) {
78 // Do better than this for Merlin
79 vm_exit_out_of_memory(delta, "offset table expansion");
80 }
81 assert(_vs.high() == high + delta, "invalid expansion");
82 } else {
83 delta = ReservedSpace::page_align_size_down(old_size - new_size);
84 if (delta == 0) return;
85 _vs.shrink_by(delta);
86 assert(_vs.high() == high - delta, "invalid expansion");
87 }
88 }
90 bool BlockOffsetSharedArray::is_card_boundary(HeapWord* p) const {
91 assert(p >= _reserved.start(), "just checking");
92 size_t delta = pointer_delta(p, _reserved.start());
93 return (delta & right_n_bits(LogN_words)) == (size_t)NoBits;
94 }
97 void BlockOffsetSharedArray::serialize(SerializeOopClosure* soc,
98 HeapWord* start, HeapWord* end) {
99 assert(_offset_array[0] == 0, "objects can't cross covered areas");
100 assert(start <= end, "bad address range");
101 size_t start_index = index_for(start);
102 size_t end_index = index_for(end-1)+1;
103 soc->do_region(&_offset_array[start_index],
104 (end_index - start_index) * sizeof(_offset_array[0]));
105 }
107 //////////////////////////////////////////////////////////////////////
108 // BlockOffsetArray
109 //////////////////////////////////////////////////////////////////////
111 BlockOffsetArray::BlockOffsetArray(BlockOffsetSharedArray* array,
112 MemRegion mr, bool init_to_zero_) :
113 BlockOffsetTable(mr.start(), mr.end()),
114 _array(array)
115 {
116 assert(_bottom <= _end, "arguments out of order");
117 set_init_to_zero(init_to_zero_);
118 if (!init_to_zero_) {
119 // initialize cards to point back to mr.start()
120 set_remainder_to_point_to_start(mr.start() + N_words, mr.end());
121 _array->set_offset_array(0, 0); // set first card to 0
122 }
123 }
126 // The arguments follow the normal convention of denoting
127 // a right-open interval: [start, end)
128 void
129 BlockOffsetArray::
130 set_remainder_to_point_to_start(HeapWord* start, HeapWord* end, bool reducing) {
132 check_reducing_assertion(reducing);
133 if (start >= end) {
134 // The start address is equal to the end address (or to
135 // the right of the end address) so there are not cards
136 // that need to be updated..
137 return;
138 }
140 // Write the backskip value for each region.
141 //
142 // offset
143 // card 2nd 3rd
144 // | +- 1st | |
145 // v v v v
146 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+-+-+-
147 // |x|0|0|0|0|0|0|0|1|1|1|1|1|1| ... |1|1|1|1|2|2|2|2|2|2| ...
148 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+-+-+-
149 // 11 19 75
150 // 12
151 //
152 // offset card is the card that points to the start of an object
153 // x - offset value of offset card
154 // 1st - start of first logarithmic region
155 // 0 corresponds to logarithmic value N_words + 0 and 2**(3 * 0) = 1
156 // 2nd - start of second logarithmic region
157 // 1 corresponds to logarithmic value N_words + 1 and 2**(3 * 1) = 8
158 // 3rd - start of third logarithmic region
159 // 2 corresponds to logarithmic value N_words + 2 and 2**(3 * 2) = 64
160 //
161 // integer below the block offset entry is an example of
162 // the index of the entry
163 //
164 // Given an address,
165 // Find the index for the address
166 // Find the block offset table entry
167 // Convert the entry to a back slide
168 // (e.g., with today's, offset = 0x81 =>
169 // back slip = 2**(3*(0x81 - N_words)) = 2**3) = 8
170 // Move back N (e.g., 8) entries and repeat with the
171 // value of the new entry
172 //
173 size_t start_card = _array->index_for(start);
174 size_t end_card = _array->index_for(end-1);
175 assert(start ==_array->address_for_index(start_card), "Precondition");
176 assert(end ==_array->address_for_index(end_card)+N_words, "Precondition");
177 set_remainder_to_point_to_start_incl(start_card, end_card, reducing); // closed interval
178 }
181 // Unlike the normal convention in this code, the argument here denotes
182 // a closed, inclusive interval: [start_card, end_card], cf set_remainder_to_point_to_start()
183 // above.
184 void
185 BlockOffsetArray::set_remainder_to_point_to_start_incl(size_t start_card, size_t end_card, bool reducing) {
187 check_reducing_assertion(reducing);
188 if (start_card > end_card) {
189 return;
190 }
191 assert(start_card > _array->index_for(_bottom), "Cannot be first card");
192 assert(_array->offset_array(start_card-1) <= N_words,
193 "Offset card has an unexpected value");
194 size_t start_card_for_region = start_card;
195 u_char offset = max_jubyte;
196 for (int i = 0; i < N_powers; i++) {
197 // -1 so that the the card with the actual offset is counted. Another -1
198 // so that the reach ends in this region and not at the start
199 // of the next.
200 size_t reach = start_card - 1 + (power_to_cards_back(i+1) - 1);
201 offset = N_words + i;
202 if (reach >= end_card) {
203 _array->set_offset_array(start_card_for_region, end_card, offset, reducing);
204 start_card_for_region = reach + 1;
205 break;
206 }
207 _array->set_offset_array(start_card_for_region, reach, offset, reducing);
208 start_card_for_region = reach + 1;
209 }
210 assert(start_card_for_region > end_card, "Sanity check");
211 DEBUG_ONLY(check_all_cards(start_card, end_card);)
212 }
214 // The card-interval [start_card, end_card] is a closed interval; this
215 // is an expensive check -- use with care and only under protection of
216 // suitable flag.
217 void BlockOffsetArray::check_all_cards(size_t start_card, size_t end_card) const {
219 if (end_card < start_card) {
220 return;
221 }
222 guarantee(_array->offset_array(start_card) == N_words, "Wrong value in second card");
223 u_char last_entry = N_words;
224 for (size_t c = start_card + 1; c <= end_card; c++ /* yeah! */) {
225 u_char entry = _array->offset_array(c);
226 guarantee(entry >= last_entry, "Monotonicity");
227 if (c - start_card > power_to_cards_back(1)) {
228 guarantee(entry > N_words, "Should be in logarithmic region");
229 }
230 size_t backskip = entry_to_cards_back(entry);
231 size_t landing_card = c - backskip;
232 guarantee(landing_card >= (start_card - 1), "Inv");
233 if (landing_card >= start_card) {
234 guarantee(_array->offset_array(landing_card) <= entry, "Monotonicity");
235 } else {
236 guarantee(landing_card == (start_card - 1), "Tautology");
237 // Note that N_words is the maximum offset value
238 guarantee(_array->offset_array(landing_card) <= N_words, "Offset value");
239 }
240 last_entry = entry; // remember for monotonicity test
241 }
242 }
245 void
246 BlockOffsetArray::alloc_block(HeapWord* blk_start, HeapWord* blk_end) {
247 assert(blk_start != NULL && blk_end > blk_start,
248 "phantom block");
249 single_block(blk_start, blk_end);
250 }
252 // Action_mark - update the BOT for the block [blk_start, blk_end).
253 // Current typical use is for splitting a block.
254 // Action_single - udpate the BOT for an allocation.
255 // Action_verify - BOT verification.
256 void
257 BlockOffsetArray::do_block_internal(HeapWord* blk_start,
258 HeapWord* blk_end,
259 Action action, bool reducing) {
260 assert(Universe::heap()->is_in_reserved(blk_start),
261 "reference must be into the heap");
262 assert(Universe::heap()->is_in_reserved(blk_end-1),
263 "limit must be within the heap");
264 // This is optimized to make the test fast, assuming we only rarely
265 // cross boundaries.
266 uintptr_t end_ui = (uintptr_t)(blk_end - 1);
267 uintptr_t start_ui = (uintptr_t)blk_start;
268 // Calculate the last card boundary preceding end of blk
269 intptr_t boundary_before_end = (intptr_t)end_ui;
270 clear_bits(boundary_before_end, right_n_bits(LogN));
271 if (start_ui <= (uintptr_t)boundary_before_end) {
272 // blk starts at or crosses a boundary
273 // Calculate index of card on which blk begins
274 size_t start_index = _array->index_for(blk_start);
275 // Index of card on which blk ends
276 size_t end_index = _array->index_for(blk_end - 1);
277 // Start address of card on which blk begins
278 HeapWord* boundary = _array->address_for_index(start_index);
279 assert(boundary <= blk_start, "blk should start at or after boundary");
280 if (blk_start != boundary) {
281 // blk starts strictly after boundary
282 // adjust card boundary and start_index forward to next card
283 boundary += N_words;
284 start_index++;
285 }
286 assert(start_index <= end_index, "monotonicity of index_for()");
287 assert(boundary <= (HeapWord*)boundary_before_end, "tautology");
288 switch (action) {
289 case Action_mark: {
290 if (init_to_zero()) {
291 _array->set_offset_array(start_index, boundary, blk_start, reducing);
292 break;
293 } // Else fall through to the next case
294 }
295 case Action_single: {
296 _array->set_offset_array(start_index, boundary, blk_start, reducing);
297 // We have finished marking the "offset card". We need to now
298 // mark the subsequent cards that this blk spans.
299 if (start_index < end_index) {
300 HeapWord* rem_st = _array->address_for_index(start_index) + N_words;
301 HeapWord* rem_end = _array->address_for_index(end_index) + N_words;
302 set_remainder_to_point_to_start(rem_st, rem_end, reducing);
303 }
304 break;
305 }
306 case Action_check: {
307 _array->check_offset_array(start_index, boundary, blk_start);
308 // We have finished checking the "offset card". We need to now
309 // check the subsequent cards that this blk spans.
310 check_all_cards(start_index + 1, end_index);
311 break;
312 }
313 default:
314 ShouldNotReachHere();
315 }
316 }
317 }
319 // The range [blk_start, blk_end) represents a single contiguous block
320 // of storage; modify the block offset table to represent this
321 // information; Right-open interval: [blk_start, blk_end)
322 // NOTE: this method does _not_ adjust _unallocated_block.
323 void
324 BlockOffsetArray::single_block(HeapWord* blk_start,
325 HeapWord* blk_end) {
326 do_block_internal(blk_start, blk_end, Action_single);
327 }
329 void BlockOffsetArray::verify() const {
330 // For each entry in the block offset table, verify that
331 // the entry correctly finds the start of an object at the
332 // first address covered by the block or to the left of that
333 // first address.
335 size_t next_index = 1;
336 size_t last_index = last_active_index();
338 // Use for debugging. Initialize to NULL to distinguish the
339 // first iteration through the while loop.
340 HeapWord* last_p = NULL;
341 HeapWord* last_start = NULL;
342 oop last_o = NULL;
344 while (next_index <= last_index) {
345 // Use an address past the start of the address for
346 // the entry.
347 HeapWord* p = _array->address_for_index(next_index) + 1;
348 if (p >= _end) {
349 // That's all of the allocated block table.
350 return;
351 }
352 // block_start() asserts that start <= p.
353 HeapWord* start = block_start(p);
354 // First check if the start is an allocated block and only
355 // then if it is a valid object.
356 oop o = oop(start);
357 assert(!Universe::is_fully_initialized() ||
358 _sp->is_free_block(start) ||
359 o->is_oop_or_null(), "Bad object was found");
360 next_index++;
361 last_p = p;
362 last_start = start;
363 last_o = o;
364 }
365 }
367 //////////////////////////////////////////////////////////////////////
368 // BlockOffsetArrayNonContigSpace
369 //////////////////////////////////////////////////////////////////////
371 // The block [blk_start, blk_end) has been allocated;
372 // adjust the block offset table to represent this information;
373 // NOTE: Clients of BlockOffsetArrayNonContigSpace: consider using
374 // the somewhat more lightweight split_block() or
375 // (when init_to_zero()) mark_block() wherever possible.
376 // right-open interval: [blk_start, blk_end)
377 void
378 BlockOffsetArrayNonContigSpace::alloc_block(HeapWord* blk_start,
379 HeapWord* blk_end) {
380 assert(blk_start != NULL && blk_end > blk_start,
381 "phantom block");
382 single_block(blk_start, blk_end);
383 allocated(blk_start, blk_end);
384 }
386 // Adjust BOT to show that a previously whole block has been split
387 // into two. We verify the BOT for the first part (prefix) and
388 // update the BOT for the second part (suffix).
389 // blk is the start of the block
390 // blk_size is the size of the original block
391 // left_blk_size is the size of the first part of the split
392 void BlockOffsetArrayNonContigSpace::split_block(HeapWord* blk,
393 size_t blk_size,
394 size_t left_blk_size) {
395 // Verify that the BOT shows [blk, blk + blk_size) to be one block.
396 verify_single_block(blk, blk_size);
397 // Update the BOT to indicate that [blk + left_blk_size, blk + blk_size)
398 // is one single block.
399 assert(blk_size > 0, "Should be positive");
400 assert(left_blk_size > 0, "Should be positive");
401 assert(left_blk_size < blk_size, "Not a split");
403 // Start addresses of prefix block and suffix block.
404 HeapWord* pref_addr = blk;
405 HeapWord* suff_addr = blk + left_blk_size;
406 HeapWord* end_addr = blk + blk_size;
408 // Indices for starts of prefix block and suffix block.
409 size_t pref_index = _array->index_for(pref_addr);
410 if (_array->address_for_index(pref_index) != pref_addr) {
411 // pref_addr does not begin pref_index
412 pref_index++;
413 }
415 size_t suff_index = _array->index_for(suff_addr);
416 if (_array->address_for_index(suff_index) != suff_addr) {
417 // suff_addr does not begin suff_index
418 suff_index++;
419 }
421 // Definition: A block B, denoted [B_start, B_end) __starts__
422 // a card C, denoted [C_start, C_end), where C_start and C_end
423 // are the heap addresses that card C covers, iff
424 // B_start <= C_start < B_end.
425 //
426 // We say that a card C "is started by" a block B, iff
427 // B "starts" C.
428 //
429 // Note that the cardinality of the set of cards {C}
430 // started by a block B can be 0, 1, or more.
431 //
432 // Below, pref_index and suff_index are, respectively, the
433 // first (least) card indices that the prefix and suffix of
434 // the split start; end_index is one more than the index of
435 // the last (greatest) card that blk starts.
436 size_t end_index = _array->index_for(end_addr - 1) + 1;
438 // Calculate the # cards that the prefix and suffix affect.
439 size_t num_pref_cards = suff_index - pref_index;
441 size_t num_suff_cards = end_index - suff_index;
442 // Change the cards that need changing
443 if (num_suff_cards > 0) {
444 HeapWord* boundary = _array->address_for_index(suff_index);
445 // Set the offset card for suffix block
446 _array->set_offset_array(suff_index, boundary, suff_addr, true /* reducing */);
447 // Change any further cards that need changing in the suffix
448 if (num_pref_cards > 0) {
449 if (num_pref_cards >= num_suff_cards) {
450 // Unilaterally fix all of the suffix cards: closed card
451 // index interval in args below.
452 set_remainder_to_point_to_start_incl(suff_index + 1, end_index - 1, true /* reducing */);
453 } else {
454 // Unilaterally fix the first (num_pref_cards - 1) following
455 // the "offset card" in the suffix block.
456 set_remainder_to_point_to_start_incl(suff_index + 1,
457 suff_index + num_pref_cards - 1, true /* reducing */);
458 // Fix the appropriate cards in the remainder of the
459 // suffix block -- these are the last num_pref_cards
460 // cards in each power block of the "new" range plumbed
461 // from suff_addr.
462 bool more = true;
463 uint i = 1;
464 while (more && (i < N_powers)) {
465 size_t back_by = power_to_cards_back(i);
466 size_t right_index = suff_index + back_by - 1;
467 size_t left_index = right_index - num_pref_cards + 1;
468 if (right_index >= end_index - 1) { // last iteration
469 right_index = end_index - 1;
470 more = false;
471 }
472 if (back_by > num_pref_cards) {
473 // Fill in the remainder of this "power block", if it
474 // is non-null.
475 if (left_index <= right_index) {
476 _array->set_offset_array(left_index, right_index,
477 N_words + i - 1, true /* reducing */);
478 } else {
479 more = false; // we are done
480 }
481 i++;
482 break;
483 }
484 i++;
485 }
486 while (more && (i < N_powers)) {
487 size_t back_by = power_to_cards_back(i);
488 size_t right_index = suff_index + back_by - 1;
489 size_t left_index = right_index - num_pref_cards + 1;
490 if (right_index >= end_index - 1) { // last iteration
491 right_index = end_index - 1;
492 if (left_index > right_index) {
493 break;
494 }
495 more = false;
496 }
497 assert(left_index <= right_index, "Error");
498 _array->set_offset_array(left_index, right_index, N_words + i - 1, true /* reducing */);
499 i++;
500 }
501 }
502 } // else no more cards to fix in suffix
503 } // else nothing needs to be done
504 // Verify that we did the right thing
505 verify_single_block(pref_addr, left_blk_size);
506 verify_single_block(suff_addr, blk_size - left_blk_size);
507 }
510 // Mark the BOT such that if [blk_start, blk_end) straddles a card
511 // boundary, the card following the first such boundary is marked
512 // with the appropriate offset.
513 // NOTE: this method does _not_ adjust _unallocated_block or
514 // any cards subsequent to the first one.
515 void
516 BlockOffsetArrayNonContigSpace::mark_block(HeapWord* blk_start,
517 HeapWord* blk_end, bool reducing) {
518 do_block_internal(blk_start, blk_end, Action_mark, reducing);
519 }
521 HeapWord* BlockOffsetArrayNonContigSpace::block_start_unsafe(
522 const void* addr) const {
523 assert(_array->offset_array(0) == 0, "objects can't cross covered areas");
524 assert(_bottom <= addr && addr < _end,
525 "addr must be covered by this Array");
526 // Must read this exactly once because it can be modified by parallel
527 // allocation.
528 HeapWord* ub = _unallocated_block;
529 if (BlockOffsetArrayUseUnallocatedBlock && addr >= ub) {
530 assert(ub < _end, "tautology (see above)");
531 return ub;
532 }
534 // Otherwise, find the block start using the table.
535 size_t index = _array->index_for(addr);
536 HeapWord* q = _array->address_for_index(index);
538 uint offset = _array->offset_array(index); // Extend u_char to uint.
539 while (offset >= N_words) {
540 // The excess of the offset from N_words indicates a power of Base
541 // to go back by.
542 size_t n_cards_back = entry_to_cards_back(offset);
543 q -= (N_words * n_cards_back);
544 assert(q >= _sp->bottom(),
545 err_msg("q = " PTR_FORMAT " crossed below bottom = " PTR_FORMAT,
546 q, _sp->bottom()));
547 assert(q < _sp->end(),
548 err_msg("q = " PTR_FORMAT " crossed above end = " PTR_FORMAT,
549 q, _sp->end()));
550 index -= n_cards_back;
551 offset = _array->offset_array(index);
552 }
553 assert(offset < N_words, "offset too large");
554 index--;
555 q -= offset;
556 assert(q >= _sp->bottom(),
557 err_msg("q = " PTR_FORMAT " crossed below bottom = " PTR_FORMAT,
558 q, _sp->bottom()));
559 assert(q < _sp->end(),
560 err_msg("q = " PTR_FORMAT " crossed above end = " PTR_FORMAT,
561 q, _sp->end()));
562 HeapWord* n = q;
564 while (n <= addr) {
565 debug_only(HeapWord* last = q); // for debugging
566 q = n;
567 n += _sp->block_size(n);
568 assert(n > q,
569 err_msg("Looping at n = " PTR_FORMAT " with last = " PTR_FORMAT","
570 " while querying blk_start(" PTR_FORMAT ")"
571 " on _sp = [" PTR_FORMAT "," PTR_FORMAT ")",
572 n, last, addr, _sp->bottom(), _sp->end()));
573 }
574 assert(q <= addr,
575 err_msg("wrong order for current (" INTPTR_FORMAT ")" " <= arg (" INTPTR_FORMAT ")",
576 q, addr));
577 assert(addr <= n,
578 err_msg("wrong order for arg (" INTPTR_FORMAT ") <= next (" INTPTR_FORMAT ")",
579 addr, n));
580 return q;
581 }
583 HeapWord* BlockOffsetArrayNonContigSpace::block_start_careful(
584 const void* addr) const {
585 assert(_array->offset_array(0) == 0, "objects can't cross covered areas");
587 assert(_bottom <= addr && addr < _end,
588 "addr must be covered by this Array");
589 // Must read this exactly once because it can be modified by parallel
590 // allocation.
591 HeapWord* ub = _unallocated_block;
592 if (BlockOffsetArrayUseUnallocatedBlock && addr >= ub) {
593 assert(ub < _end, "tautology (see above)");
594 return ub;
595 }
597 // Otherwise, find the block start using the table, but taking
598 // care (cf block_start_unsafe() above) not to parse any objects/blocks
599 // on the cards themsleves.
600 size_t index = _array->index_for(addr);
601 assert(_array->address_for_index(index) == addr,
602 "arg should be start of card");
604 HeapWord* q = (HeapWord*)addr;
605 uint offset;
606 do {
607 offset = _array->offset_array(index);
608 if (offset < N_words) {
609 q -= offset;
610 } else {
611 size_t n_cards_back = entry_to_cards_back(offset);
612 q -= (n_cards_back * N_words);
613 index -= n_cards_back;
614 }
615 } while (offset >= N_words);
616 assert(q <= addr, "block start should be to left of arg");
617 return q;
618 }
620 #ifndef PRODUCT
621 // Verification & debugging - ensure that the offset table reflects the fact
622 // that the block [blk_start, blk_end) or [blk, blk + size) is a
623 // single block of storage. NOTE: can't const this because of
624 // call to non-const do_block_internal() below.
625 void BlockOffsetArrayNonContigSpace::verify_single_block(
626 HeapWord* blk_start, HeapWord* blk_end) {
627 if (VerifyBlockOffsetArray) {
628 do_block_internal(blk_start, blk_end, Action_check);
629 }
630 }
632 void BlockOffsetArrayNonContigSpace::verify_single_block(
633 HeapWord* blk, size_t size) {
634 verify_single_block(blk, blk + size);
635 }
637 // Verify that the given block is before _unallocated_block
638 void BlockOffsetArrayNonContigSpace::verify_not_unallocated(
639 HeapWord* blk_start, HeapWord* blk_end) const {
640 if (BlockOffsetArrayUseUnallocatedBlock) {
641 assert(blk_start < blk_end, "Block inconsistency?");
642 assert(blk_end <= _unallocated_block, "_unallocated_block problem");
643 }
644 }
646 void BlockOffsetArrayNonContigSpace::verify_not_unallocated(
647 HeapWord* blk, size_t size) const {
648 verify_not_unallocated(blk, blk + size);
649 }
650 #endif // PRODUCT
652 size_t BlockOffsetArrayNonContigSpace::last_active_index() const {
653 if (_unallocated_block == _bottom) {
654 return 0;
655 } else {
656 return _array->index_for(_unallocated_block - 1);
657 }
658 }
660 //////////////////////////////////////////////////////////////////////
661 // BlockOffsetArrayContigSpace
662 //////////////////////////////////////////////////////////////////////
664 HeapWord* BlockOffsetArrayContigSpace::block_start_unsafe(const void* addr) const {
665 assert(_array->offset_array(0) == 0, "objects can't cross covered areas");
667 // Otherwise, find the block start using the table.
668 assert(_bottom <= addr && addr < _end,
669 "addr must be covered by this Array");
670 size_t index = _array->index_for(addr);
671 // We must make sure that the offset table entry we use is valid. If
672 // "addr" is past the end, start at the last known one and go forward.
673 index = MIN2(index, _next_offset_index-1);
674 HeapWord* q = _array->address_for_index(index);
676 uint offset = _array->offset_array(index); // Extend u_char to uint.
677 while (offset > N_words) {
678 // The excess of the offset from N_words indicates a power of Base
679 // to go back by.
680 size_t n_cards_back = entry_to_cards_back(offset);
681 q -= (N_words * n_cards_back);
682 assert(q >= _sp->bottom(), "Went below bottom!");
683 index -= n_cards_back;
684 offset = _array->offset_array(index);
685 }
686 while (offset == N_words) {
687 assert(q >= _sp->bottom(), "Went below bottom!");
688 q -= N_words;
689 index--;
690 offset = _array->offset_array(index);
691 }
692 assert(offset < N_words, "offset too large");
693 q -= offset;
694 HeapWord* n = q;
696 while (n <= addr) {
697 debug_only(HeapWord* last = q); // for debugging
698 q = n;
699 n += _sp->block_size(n);
700 }
701 assert(q <= addr, "wrong order for current and arg");
702 assert(addr <= n, "wrong order for arg and next");
703 return q;
704 }
706 //
707 // _next_offset_threshold
708 // | _next_offset_index
709 // v v
710 // +-------+-------+-------+-------+-------+
711 // | i-1 | i | i+1 | i+2 | i+3 |
712 // +-------+-------+-------+-------+-------+
713 // ( ^ ]
714 // block-start
715 //
717 void BlockOffsetArrayContigSpace::alloc_block_work(HeapWord* blk_start,
718 HeapWord* blk_end) {
719 assert(blk_start != NULL && blk_end > blk_start,
720 "phantom block");
721 assert(blk_end > _next_offset_threshold,
722 "should be past threshold");
723 assert(blk_start <= _next_offset_threshold,
724 "blk_start should be at or before threshold");
725 assert(pointer_delta(_next_offset_threshold, blk_start) <= N_words,
726 "offset should be <= BlockOffsetSharedArray::N");
727 assert(Universe::heap()->is_in_reserved(blk_start),
728 "reference must be into the heap");
729 assert(Universe::heap()->is_in_reserved(blk_end-1),
730 "limit must be within the heap");
731 assert(_next_offset_threshold ==
732 _array->_reserved.start() + _next_offset_index*N_words,
733 "index must agree with threshold");
735 debug_only(size_t orig_next_offset_index = _next_offset_index;)
737 // Mark the card that holds the offset into the block. Note
738 // that _next_offset_index and _next_offset_threshold are not
739 // updated until the end of this method.
740 _array->set_offset_array(_next_offset_index,
741 _next_offset_threshold,
742 blk_start);
744 // We need to now mark the subsequent cards that this blk spans.
746 // Index of card on which blk ends.
747 size_t end_index = _array->index_for(blk_end - 1);
749 // Are there more cards left to be updated?
750 if (_next_offset_index + 1 <= end_index) {
751 HeapWord* rem_st = _array->address_for_index(_next_offset_index + 1);
752 // Calculate rem_end this way because end_index
753 // may be the last valid index in the covered region.
754 HeapWord* rem_end = _array->address_for_index(end_index) + N_words;
755 set_remainder_to_point_to_start(rem_st, rem_end);
756 }
758 // _next_offset_index and _next_offset_threshold updated here.
759 _next_offset_index = end_index + 1;
760 // Calculate _next_offset_threshold this way because end_index
761 // may be the last valid index in the covered region.
762 _next_offset_threshold = _array->address_for_index(end_index) + N_words;
763 assert(_next_offset_threshold >= blk_end, "Incorrect offset threshold");
765 #ifdef ASSERT
766 // The offset can be 0 if the block starts on a boundary. That
767 // is checked by an assertion above.
768 size_t start_index = _array->index_for(blk_start);
769 HeapWord* boundary = _array->address_for_index(start_index);
770 assert((_array->offset_array(orig_next_offset_index) == 0 &&
771 blk_start == boundary) ||
772 (_array->offset_array(orig_next_offset_index) > 0 &&
773 _array->offset_array(orig_next_offset_index) <= N_words),
774 "offset array should have been set");
775 for (size_t j = orig_next_offset_index + 1; j <= end_index; j++) {
776 assert(_array->offset_array(j) > 0 &&
777 _array->offset_array(j) <= (u_char) (N_words+N_powers-1),
778 "offset array should have been set");
779 }
780 #endif
781 }
783 HeapWord* BlockOffsetArrayContigSpace::initialize_threshold() {
784 assert(!Universe::heap()->is_in_reserved(_array->_offset_array),
785 "just checking");
786 _next_offset_index = _array->index_for(_bottom);
787 _next_offset_index++;
788 _next_offset_threshold =
789 _array->address_for_index(_next_offset_index);
790 return _next_offset_threshold;
791 }
793 void BlockOffsetArrayContigSpace::zero_bottom_entry() {
794 assert(!Universe::heap()->is_in_reserved(_array->_offset_array),
795 "just checking");
796 size_t bottom_index = _array->index_for(_bottom);
797 _array->set_offset_array(bottom_index, 0);
798 }
801 void BlockOffsetArrayContigSpace::serialize(SerializeOopClosure* soc) {
802 if (soc->reading()) {
803 // Null these values so that the serializer won't object to updating them.
804 _next_offset_threshold = NULL;
805 _next_offset_index = 0;
806 }
807 soc->do_ptr(&_next_offset_threshold);
808 soc->do_size_t(&_next_offset_index);
809 }
811 size_t BlockOffsetArrayContigSpace::last_active_index() const {
812 size_t result = _next_offset_index - 1;
813 return result >= 0 ? result : 0;
814 }