Thu, 12 Jun 2008 13:50:55 -0700
Merge
1 /*
2 * Copyright 1997-2006 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 *
23 */
25 # include "incls/_precompiled.incl"
26 # include "incls/_space.cpp.incl"
28 void SpaceMemRegionOopsIterClosure::do_oop(oop* p) { SpaceMemRegionOopsIterClosure::do_oop_work(p); }
29 void SpaceMemRegionOopsIterClosure::do_oop(narrowOop* p) { SpaceMemRegionOopsIterClosure::do_oop_work(p); }
31 HeapWord* DirtyCardToOopClosure::get_actual_top(HeapWord* top,
32 HeapWord* top_obj) {
33 if (top_obj != NULL) {
34 if (_sp->block_is_obj(top_obj)) {
35 if (_precision == CardTableModRefBS::ObjHeadPreciseArray) {
36 if (oop(top_obj)->is_objArray() || oop(top_obj)->is_typeArray()) {
37 // An arrayOop is starting on the dirty card - since we do exact
38 // store checks for objArrays we are done.
39 } else {
40 // Otherwise, it is possible that the object starting on the dirty
41 // card spans the entire card, and that the store happened on a
42 // later card. Figure out where the object ends.
43 // Use the block_size() method of the space over which
44 // the iteration is being done. That space (e.g. CMS) may have
45 // specific requirements on object sizes which will
46 // be reflected in the block_size() method.
47 top = top_obj + oop(top_obj)->size();
48 }
49 }
50 } else {
51 top = top_obj;
52 }
53 } else {
54 assert(top == _sp->end(), "only case where top_obj == NULL");
55 }
56 return top;
57 }
59 void DirtyCardToOopClosure::walk_mem_region(MemRegion mr,
60 HeapWord* bottom,
61 HeapWord* top) {
62 // 1. Blocks may or may not be objects.
63 // 2. Even when a block_is_obj(), it may not entirely
64 // occupy the block if the block quantum is larger than
65 // the object size.
66 // We can and should try to optimize by calling the non-MemRegion
67 // version of oop_iterate() for all but the extremal objects
68 // (for which we need to call the MemRegion version of
69 // oop_iterate()) To be done post-beta XXX
70 for (; bottom < top; bottom += _sp->block_size(bottom)) {
71 // As in the case of contiguous space above, we'd like to
72 // just use the value returned by oop_iterate to increment the
73 // current pointer; unfortunately, that won't work in CMS because
74 // we'd need an interface change (it seems) to have the space
75 // "adjust the object size" (for instance pad it up to its
76 // block alignment or minimum block size restrictions. XXX
77 if (_sp->block_is_obj(bottom) &&
78 !_sp->obj_allocated_since_save_marks(oop(bottom))) {
79 oop(bottom)->oop_iterate(_cl, mr);
80 }
81 }
82 }
84 void DirtyCardToOopClosure::do_MemRegion(MemRegion mr) {
86 // Some collectors need to do special things whenever their dirty
87 // cards are processed. For instance, CMS must remember mutator updates
88 // (i.e. dirty cards) so as to re-scan mutated objects.
89 // Such work can be piggy-backed here on dirty card scanning, so as to make
90 // it slightly more efficient than doing a complete non-detructive pre-scan
91 // of the card table.
92 MemRegionClosure* pCl = _sp->preconsumptionDirtyCardClosure();
93 if (pCl != NULL) {
94 pCl->do_MemRegion(mr);
95 }
97 HeapWord* bottom = mr.start();
98 HeapWord* last = mr.last();
99 HeapWord* top = mr.end();
100 HeapWord* bottom_obj;
101 HeapWord* top_obj;
103 assert(_precision == CardTableModRefBS::ObjHeadPreciseArray ||
104 _precision == CardTableModRefBS::Precise,
105 "Only ones we deal with for now.");
107 assert(_precision != CardTableModRefBS::ObjHeadPreciseArray ||
108 _cl->idempotent() || _last_bottom == NULL ||
109 top <= _last_bottom,
110 "Not decreasing");
111 NOT_PRODUCT(_last_bottom = mr.start());
113 bottom_obj = _sp->block_start(bottom);
114 top_obj = _sp->block_start(last);
116 assert(bottom_obj <= bottom, "just checking");
117 assert(top_obj <= top, "just checking");
119 // Given what we think is the top of the memory region and
120 // the start of the object at the top, get the actual
121 // value of the top.
122 top = get_actual_top(top, top_obj);
124 // If the previous call did some part of this region, don't redo.
125 if (_precision == CardTableModRefBS::ObjHeadPreciseArray &&
126 _min_done != NULL &&
127 _min_done < top) {
128 top = _min_done;
129 }
131 // Top may have been reset, and in fact may be below bottom,
132 // e.g. the dirty card region is entirely in a now free object
133 // -- something that could happen with a concurrent sweeper.
134 bottom = MIN2(bottom, top);
135 mr = MemRegion(bottom, top);
136 assert(bottom <= top &&
137 (_precision != CardTableModRefBS::ObjHeadPreciseArray ||
138 _min_done == NULL ||
139 top <= _min_done),
140 "overlap!");
142 // Walk the region if it is not empty; otherwise there is nothing to do.
143 if (!mr.is_empty()) {
144 walk_mem_region(mr, bottom_obj, top);
145 }
147 // An idempotent closure might be applied in any order, so we don't
148 // record a _min_done for it.
149 if (!_cl->idempotent()) {
150 _min_done = bottom;
151 } else {
152 assert(_min_done == _last_explicit_min_done,
153 "Don't update _min_done for idempotent cl");
154 }
155 }
157 DirtyCardToOopClosure* Space::new_dcto_cl(OopClosure* cl,
158 CardTableModRefBS::PrecisionStyle precision,
159 HeapWord* boundary) {
160 return new DirtyCardToOopClosure(this, cl, precision, boundary);
161 }
163 HeapWord* ContiguousSpaceDCTOC::get_actual_top(HeapWord* top,
164 HeapWord* top_obj) {
165 if (top_obj != NULL && top_obj < (_sp->toContiguousSpace())->top()) {
166 if (_precision == CardTableModRefBS::ObjHeadPreciseArray) {
167 if (oop(top_obj)->is_objArray() || oop(top_obj)->is_typeArray()) {
168 // An arrayOop is starting on the dirty card - since we do exact
169 // store checks for objArrays we are done.
170 } else {
171 // Otherwise, it is possible that the object starting on the dirty
172 // card spans the entire card, and that the store happened on a
173 // later card. Figure out where the object ends.
174 assert(_sp->block_size(top_obj) == (size_t) oop(top_obj)->size(),
175 "Block size and object size mismatch");
176 top = top_obj + oop(top_obj)->size();
177 }
178 }
179 } else {
180 top = (_sp->toContiguousSpace())->top();
181 }
182 return top;
183 }
185 void Filtering_DCTOC::walk_mem_region(MemRegion mr,
186 HeapWord* bottom,
187 HeapWord* top) {
188 // Note that this assumption won't hold if we have a concurrent
189 // collector in this space, which may have freed up objects after
190 // they were dirtied and before the stop-the-world GC that is
191 // examining cards here.
192 assert(bottom < top, "ought to be at least one obj on a dirty card.");
194 if (_boundary != NULL) {
195 // We have a boundary outside of which we don't want to look
196 // at objects, so create a filtering closure around the
197 // oop closure before walking the region.
198 FilteringClosure filter(_boundary, _cl);
199 walk_mem_region_with_cl(mr, bottom, top, &filter);
200 } else {
201 // No boundary, simply walk the heap with the oop closure.
202 walk_mem_region_with_cl(mr, bottom, top, _cl);
203 }
205 }
207 // We must replicate this so that the static type of "FilteringClosure"
208 // (see above) is apparent at the oop_iterate calls.
209 #define ContiguousSpaceDCTOC__walk_mem_region_with_cl_DEFN(ClosureType) \
210 void ContiguousSpaceDCTOC::walk_mem_region_with_cl(MemRegion mr, \
211 HeapWord* bottom, \
212 HeapWord* top, \
213 ClosureType* cl) { \
214 bottom += oop(bottom)->oop_iterate(cl, mr); \
215 if (bottom < top) { \
216 HeapWord* next_obj = bottom + oop(bottom)->size(); \
217 while (next_obj < top) { \
218 /* Bottom lies entirely below top, so we can call the */ \
219 /* non-memRegion version of oop_iterate below. */ \
220 oop(bottom)->oop_iterate(cl); \
221 bottom = next_obj; \
222 next_obj = bottom + oop(bottom)->size(); \
223 } \
224 /* Last object. */ \
225 oop(bottom)->oop_iterate(cl, mr); \
226 } \
227 }
229 // (There are only two of these, rather than N, because the split is due
230 // only to the introduction of the FilteringClosure, a local part of the
231 // impl of this abstraction.)
232 ContiguousSpaceDCTOC__walk_mem_region_with_cl_DEFN(OopClosure)
233 ContiguousSpaceDCTOC__walk_mem_region_with_cl_DEFN(FilteringClosure)
235 DirtyCardToOopClosure*
236 ContiguousSpace::new_dcto_cl(OopClosure* cl,
237 CardTableModRefBS::PrecisionStyle precision,
238 HeapWord* boundary) {
239 return new ContiguousSpaceDCTOC(this, cl, precision, boundary);
240 }
242 void Space::set_bounds(MemRegion mr) {
243 HeapWord* bottom = mr.start();
244 HeapWord* end = mr.end();
245 assert(Universe::on_page_boundary(bottom) && Universe::on_page_boundary(end),
246 "invalid space boundaries");
247 set_bottom(bottom);
248 set_end(end);
249 }
251 void Space::initialize(MemRegion mr, bool clear_space) {
252 set_bounds(mr);
253 if (clear_space) clear();
254 }
256 void Space::clear() {
257 if (ZapUnusedHeapArea) mangle_unused_area();
258 }
260 void CompactibleSpace::initialize(MemRegion mr, bool clear_space) {
261 Space::initialize(mr, false); // We'll do the clearing if there's
262 // clearing to be done.
263 _compaction_top = bottom();
264 _next_compaction_space = NULL;
265 if (clear_space) clear();
266 }
268 void CompactibleSpace::clear() {
269 _compaction_top = bottom();
270 Space::clear();
271 }
273 void ContiguousSpace::initialize(MemRegion mr, bool clear_space) {
274 CompactibleSpace::initialize(mr, false); // We'll do the clearing if there's
275 // clearing to be done.
276 set_top(bottom());
277 set_saved_mark();
278 if (clear_space) clear();
279 }
281 void ContiguousSpace::clear() {
282 set_top(bottom());
283 set_saved_mark();
284 CompactibleSpace::clear();
285 }
287 bool Space::is_in(const void* p) const {
288 HeapWord* b = block_start_const(p);
289 return b != NULL && block_is_obj(b);
290 }
292 bool ContiguousSpace::is_in(const void* p) const {
293 return _bottom <= p && p < _top;
294 }
296 bool ContiguousSpace::is_free_block(const HeapWord* p) const {
297 return p >= _top;
298 }
300 void OffsetTableContigSpace::initialize(MemRegion mr, bool clear_space) {
301 // false ==> we'll do the clearing if there's clearing to be done.
302 ContiguousSpace::initialize(mr, false);
303 _offsets.zero_bottom_entry();
304 _offsets.initialize_threshold();
305 if (clear_space) clear();
306 }
308 void OffsetTableContigSpace::clear() {
309 ContiguousSpace::clear();
310 _offsets.zero_bottom_entry();
311 _offsets.initialize_threshold();
312 }
314 void OffsetTableContigSpace::set_bottom(HeapWord* new_bottom) {
315 Space::set_bottom(new_bottom);
316 _offsets.set_bottom(new_bottom);
317 }
319 void OffsetTableContigSpace::set_end(HeapWord* new_end) {
320 // Space should not advertize an increase in size
321 // until after the underlying offest table has been enlarged.
322 _offsets.resize(pointer_delta(new_end, bottom()));
323 Space::set_end(new_end);
324 }
326 void ContiguousSpace::mangle_unused_area() {
327 // to-space is used for storing marks during mark-sweep
328 mangle_region(MemRegion(top(), end()));
329 }
331 void ContiguousSpace::mangle_region(MemRegion mr) {
332 debug_only(Copy::fill_to_words(mr.start(), mr.word_size(), badHeapWord));
333 }
335 HeapWord* CompactibleSpace::forward(oop q, size_t size,
336 CompactPoint* cp, HeapWord* compact_top) {
337 // q is alive
338 // First check if we should switch compaction space
339 assert(this == cp->space, "'this' should be current compaction space.");
340 size_t compaction_max_size = pointer_delta(end(), compact_top);
341 while (size > compaction_max_size) {
342 // switch to next compaction space
343 cp->space->set_compaction_top(compact_top);
344 cp->space = cp->space->next_compaction_space();
345 if (cp->space == NULL) {
346 cp->gen = GenCollectedHeap::heap()->prev_gen(cp->gen);
347 assert(cp->gen != NULL, "compaction must succeed");
348 cp->space = cp->gen->first_compaction_space();
349 assert(cp->space != NULL, "generation must have a first compaction space");
350 }
351 compact_top = cp->space->bottom();
352 cp->space->set_compaction_top(compact_top);
353 cp->threshold = cp->space->initialize_threshold();
354 compaction_max_size = pointer_delta(cp->space->end(), compact_top);
355 }
357 // store the forwarding pointer into the mark word
358 if ((HeapWord*)q != compact_top) {
359 q->forward_to(oop(compact_top));
360 assert(q->is_gc_marked(), "encoding the pointer should preserve the mark");
361 } else {
362 // if the object isn't moving we can just set the mark to the default
363 // mark and handle it specially later on.
364 q->init_mark();
365 assert(q->forwardee() == NULL, "should be forwarded to NULL");
366 }
368 VALIDATE_MARK_SWEEP_ONLY(MarkSweep::register_live_oop(q, size));
369 compact_top += size;
371 // we need to update the offset table so that the beginnings of objects can be
372 // found during scavenge. Note that we are updating the offset table based on
373 // where the object will be once the compaction phase finishes.
374 if (compact_top > cp->threshold)
375 cp->threshold =
376 cp->space->cross_threshold(compact_top - size, compact_top);
377 return compact_top;
378 }
381 bool CompactibleSpace::insert_deadspace(size_t& allowed_deadspace_words,
382 HeapWord* q, size_t deadlength) {
383 if (allowed_deadspace_words >= deadlength) {
384 allowed_deadspace_words -= deadlength;
385 oop(q)->set_mark(markOopDesc::prototype()->set_marked());
386 const size_t min_int_array_size = typeArrayOopDesc::header_size(T_INT);
387 if (deadlength >= min_int_array_size) {
388 oop(q)->set_klass(Universe::intArrayKlassObj());
389 typeArrayOop(q)->set_length((int)((deadlength - min_int_array_size)
390 * (HeapWordSize/sizeof(jint))));
391 } else {
392 assert((int) deadlength == instanceOopDesc::header_size(),
393 "size for smallest fake dead object doesn't match");
394 oop(q)->set_klass(SystemDictionary::object_klass());
395 }
396 assert((int) deadlength == oop(q)->size(),
397 "make sure size for fake dead object match");
398 // Recall that we required "q == compaction_top".
399 return true;
400 } else {
401 allowed_deadspace_words = 0;
402 return false;
403 }
404 }
406 #define block_is_always_obj(q) true
407 #define obj_size(q) oop(q)->size()
408 #define adjust_obj_size(s) s
410 void CompactibleSpace::prepare_for_compaction(CompactPoint* cp) {
411 SCAN_AND_FORWARD(cp, end, block_is_obj, block_size);
412 }
414 // Faster object search.
415 void ContiguousSpace::prepare_for_compaction(CompactPoint* cp) {
416 SCAN_AND_FORWARD(cp, top, block_is_always_obj, obj_size);
417 }
419 void Space::adjust_pointers() {
420 // adjust all the interior pointers to point at the new locations of objects
421 // Used by MarkSweep::mark_sweep_phase3()
423 // First check to see if there is any work to be done.
424 if (used() == 0) {
425 return; // Nothing to do.
426 }
428 // Otherwise...
429 HeapWord* q = bottom();
430 HeapWord* t = end();
432 debug_only(HeapWord* prev_q = NULL);
433 while (q < t) {
434 if (oop(q)->is_gc_marked()) {
435 // q is alive
437 VALIDATE_MARK_SWEEP_ONLY(MarkSweep::track_interior_pointers(oop(q)));
438 // point all the oops to the new location
439 size_t size = oop(q)->adjust_pointers();
440 VALIDATE_MARK_SWEEP_ONLY(MarkSweep::check_interior_pointers());
442 debug_only(prev_q = q);
443 VALIDATE_MARK_SWEEP_ONLY(MarkSweep::validate_live_oop(oop(q), size));
445 q += size;
446 } else {
447 // q is not a live object. But we're not in a compactible space,
448 // So we don't have live ranges.
449 debug_only(prev_q = q);
450 q += block_size(q);
451 assert(q > prev_q, "we should be moving forward through memory");
452 }
453 }
454 assert(q == t, "just checking");
455 }
457 void CompactibleSpace::adjust_pointers() {
458 // Check first is there is any work to do.
459 if (used() == 0) {
460 return; // Nothing to do.
461 }
463 SCAN_AND_ADJUST_POINTERS(adjust_obj_size);
464 }
466 void CompactibleSpace::compact() {
467 SCAN_AND_COMPACT(obj_size);
468 }
470 void Space::print_short() const { print_short_on(tty); }
472 void Space::print_short_on(outputStream* st) const {
473 st->print(" space " SIZE_FORMAT "K, %3d%% used", capacity() / K,
474 (int) ((double) used() * 100 / capacity()));
475 }
477 void Space::print() const { print_on(tty); }
479 void Space::print_on(outputStream* st) const {
480 print_short_on(st);
481 st->print_cr(" [" INTPTR_FORMAT ", " INTPTR_FORMAT ")",
482 bottom(), end());
483 }
485 void ContiguousSpace::print_on(outputStream* st) const {
486 print_short_on(st);
487 st->print_cr(" [" INTPTR_FORMAT ", " INTPTR_FORMAT ", " INTPTR_FORMAT ")",
488 bottom(), top(), end());
489 }
491 void OffsetTableContigSpace::print_on(outputStream* st) const {
492 print_short_on(st);
493 st->print_cr(" [" INTPTR_FORMAT ", " INTPTR_FORMAT ", "
494 INTPTR_FORMAT ", " INTPTR_FORMAT ")",
495 bottom(), top(), _offsets.threshold(), end());
496 }
498 void ContiguousSpace::verify(bool allow_dirty) const {
499 HeapWord* p = bottom();
500 HeapWord* t = top();
501 HeapWord* prev_p = NULL;
502 while (p < t) {
503 oop(p)->verify();
504 prev_p = p;
505 p += oop(p)->size();
506 }
507 guarantee(p == top(), "end of last object must match end of space");
508 if (top() != end()) {
509 guarantee(top() == block_start_const(end()-1) &&
510 top() == block_start_const(top()),
511 "top should be start of unallocated block, if it exists");
512 }
513 }
515 void Space::oop_iterate(OopClosure* blk) {
516 ObjectToOopClosure blk2(blk);
517 object_iterate(&blk2);
518 }
520 HeapWord* Space::object_iterate_careful(ObjectClosureCareful* cl) {
521 guarantee(false, "NYI");
522 return bottom();
523 }
525 HeapWord* Space::object_iterate_careful_m(MemRegion mr,
526 ObjectClosureCareful* cl) {
527 guarantee(false, "NYI");
528 return bottom();
529 }
532 void Space::object_iterate_mem(MemRegion mr, UpwardsObjectClosure* cl) {
533 assert(!mr.is_empty(), "Should be non-empty");
534 // We use MemRegion(bottom(), end()) rather than used_region() below
535 // because the two are not necessarily equal for some kinds of
536 // spaces, in particular, certain kinds of free list spaces.
537 // We could use the more complicated but more precise:
538 // MemRegion(used_region().start(), round_to(used_region().end(), CardSize))
539 // but the slight imprecision seems acceptable in the assertion check.
540 assert(MemRegion(bottom(), end()).contains(mr),
541 "Should be within used space");
542 HeapWord* prev = cl->previous(); // max address from last time
543 if (prev >= mr.end()) { // nothing to do
544 return;
545 }
546 // This assert will not work when we go from cms space to perm
547 // space, and use same closure. Easy fix deferred for later. XXX YSR
548 // assert(prev == NULL || contains(prev), "Should be within space");
550 bool last_was_obj_array = false;
551 HeapWord *blk_start_addr, *region_start_addr;
552 if (prev > mr.start()) {
553 region_start_addr = prev;
554 blk_start_addr = prev;
555 assert(blk_start_addr == block_start(region_start_addr), "invariant");
556 } else {
557 region_start_addr = mr.start();
558 blk_start_addr = block_start(region_start_addr);
559 }
560 HeapWord* region_end_addr = mr.end();
561 MemRegion derived_mr(region_start_addr, region_end_addr);
562 while (blk_start_addr < region_end_addr) {
563 const size_t size = block_size(blk_start_addr);
564 if (block_is_obj(blk_start_addr)) {
565 last_was_obj_array = cl->do_object_bm(oop(blk_start_addr), derived_mr);
566 } else {
567 last_was_obj_array = false;
568 }
569 blk_start_addr += size;
570 }
571 if (!last_was_obj_array) {
572 assert((bottom() <= blk_start_addr) && (blk_start_addr <= end()),
573 "Should be within (closed) used space");
574 assert(blk_start_addr > prev, "Invariant");
575 cl->set_previous(blk_start_addr); // min address for next time
576 }
577 }
579 bool Space::obj_is_alive(const HeapWord* p) const {
580 assert (block_is_obj(p), "The address should point to an object");
581 return true;
582 }
584 void ContiguousSpace::object_iterate_mem(MemRegion mr, UpwardsObjectClosure* cl) {
585 assert(!mr.is_empty(), "Should be non-empty");
586 assert(used_region().contains(mr), "Should be within used space");
587 HeapWord* prev = cl->previous(); // max address from last time
588 if (prev >= mr.end()) { // nothing to do
589 return;
590 }
591 // See comment above (in more general method above) in case you
592 // happen to use this method.
593 assert(prev == NULL || is_in_reserved(prev), "Should be within space");
595 bool last_was_obj_array = false;
596 HeapWord *obj_start_addr, *region_start_addr;
597 if (prev > mr.start()) {
598 region_start_addr = prev;
599 obj_start_addr = prev;
600 assert(obj_start_addr == block_start(region_start_addr), "invariant");
601 } else {
602 region_start_addr = mr.start();
603 obj_start_addr = block_start(region_start_addr);
604 }
605 HeapWord* region_end_addr = mr.end();
606 MemRegion derived_mr(region_start_addr, region_end_addr);
607 while (obj_start_addr < region_end_addr) {
608 oop obj = oop(obj_start_addr);
609 const size_t size = obj->size();
610 last_was_obj_array = cl->do_object_bm(obj, derived_mr);
611 obj_start_addr += size;
612 }
613 if (!last_was_obj_array) {
614 assert((bottom() <= obj_start_addr) && (obj_start_addr <= end()),
615 "Should be within (closed) used space");
616 assert(obj_start_addr > prev, "Invariant");
617 cl->set_previous(obj_start_addr); // min address for next time
618 }
619 }
621 #ifndef SERIALGC
622 #define ContigSpace_PAR_OOP_ITERATE_DEFN(OopClosureType, nv_suffix) \
623 \
624 void ContiguousSpace::par_oop_iterate(MemRegion mr, OopClosureType* blk) {\
625 HeapWord* obj_addr = mr.start(); \
626 HeapWord* t = mr.end(); \
627 while (obj_addr < t) { \
628 assert(oop(obj_addr)->is_oop(), "Should be an oop"); \
629 obj_addr += oop(obj_addr)->oop_iterate(blk); \
630 } \
631 }
633 ALL_PAR_OOP_ITERATE_CLOSURES(ContigSpace_PAR_OOP_ITERATE_DEFN)
635 #undef ContigSpace_PAR_OOP_ITERATE_DEFN
636 #endif // SERIALGC
638 void ContiguousSpace::oop_iterate(OopClosure* blk) {
639 if (is_empty()) return;
640 HeapWord* obj_addr = bottom();
641 HeapWord* t = top();
642 // Could call objects iterate, but this is easier.
643 while (obj_addr < t) {
644 obj_addr += oop(obj_addr)->oop_iterate(blk);
645 }
646 }
648 void ContiguousSpace::oop_iterate(MemRegion mr, OopClosure* blk) {
649 if (is_empty()) {
650 return;
651 }
652 MemRegion cur = MemRegion(bottom(), top());
653 mr = mr.intersection(cur);
654 if (mr.is_empty()) {
655 return;
656 }
657 if (mr.equals(cur)) {
658 oop_iterate(blk);
659 return;
660 }
661 assert(mr.end() <= top(), "just took an intersection above");
662 HeapWord* obj_addr = block_start(mr.start());
663 HeapWord* t = mr.end();
665 // Handle first object specially.
666 oop obj = oop(obj_addr);
667 SpaceMemRegionOopsIterClosure smr_blk(blk, mr);
668 obj_addr += obj->oop_iterate(&smr_blk);
669 while (obj_addr < t) {
670 oop obj = oop(obj_addr);
671 assert(obj->is_oop(), "expected an oop");
672 obj_addr += obj->size();
673 // If "obj_addr" is not greater than top, then the
674 // entire object "obj" is within the region.
675 if (obj_addr <= t) {
676 obj->oop_iterate(blk);
677 } else {
678 // "obj" extends beyond end of region
679 obj->oop_iterate(&smr_blk);
680 break;
681 }
682 };
683 }
685 void ContiguousSpace::object_iterate(ObjectClosure* blk) {
686 if (is_empty()) return;
687 WaterMark bm = bottom_mark();
688 object_iterate_from(bm, blk);
689 }
691 void ContiguousSpace::object_iterate_from(WaterMark mark, ObjectClosure* blk) {
692 assert(mark.space() == this, "Mark does not match space");
693 HeapWord* p = mark.point();
694 while (p < top()) {
695 blk->do_object(oop(p));
696 p += oop(p)->size();
697 }
698 }
700 HeapWord*
701 ContiguousSpace::object_iterate_careful(ObjectClosureCareful* blk) {
702 HeapWord * limit = concurrent_iteration_safe_limit();
703 assert(limit <= top(), "sanity check");
704 for (HeapWord* p = bottom(); p < limit;) {
705 size_t size = blk->do_object_careful(oop(p));
706 if (size == 0) {
707 return p; // failed at p
708 } else {
709 p += size;
710 }
711 }
712 return NULL; // all done
713 }
715 #define ContigSpace_OOP_SINCE_SAVE_MARKS_DEFN(OopClosureType, nv_suffix) \
716 \
717 void ContiguousSpace:: \
718 oop_since_save_marks_iterate##nv_suffix(OopClosureType* blk) { \
719 HeapWord* t; \
720 HeapWord* p = saved_mark_word(); \
721 assert(p != NULL, "expected saved mark"); \
722 \
723 const intx interval = PrefetchScanIntervalInBytes; \
724 do { \
725 t = top(); \
726 while (p < t) { \
727 Prefetch::write(p, interval); \
728 debug_only(HeapWord* prev = p); \
729 oop m = oop(p); \
730 p += m->oop_iterate(blk); \
731 } \
732 } while (t < top()); \
733 \
734 set_saved_mark_word(p); \
735 }
737 ALL_SINCE_SAVE_MARKS_CLOSURES(ContigSpace_OOP_SINCE_SAVE_MARKS_DEFN)
739 #undef ContigSpace_OOP_SINCE_SAVE_MARKS_DEFN
741 // Very general, slow implementation.
742 HeapWord* ContiguousSpace::block_start_const(const void* p) const {
743 assert(MemRegion(bottom(), end()).contains(p), "p not in space");
744 if (p >= top()) {
745 return top();
746 } else {
747 HeapWord* last = bottom();
748 HeapWord* cur = last;
749 while (cur <= p) {
750 last = cur;
751 cur += oop(cur)->size();
752 }
753 assert(oop(last)->is_oop(), "Should be an object start");
754 return last;
755 }
756 }
758 size_t ContiguousSpace::block_size(const HeapWord* p) const {
759 assert(MemRegion(bottom(), end()).contains(p), "p not in space");
760 HeapWord* current_top = top();
761 assert(p <= current_top, "p is not a block start");
762 assert(p == current_top || oop(p)->is_oop(), "p is not a block start");
763 if (p < current_top)
764 return oop(p)->size();
765 else {
766 assert(p == current_top, "just checking");
767 return pointer_delta(end(), (HeapWord*) p);
768 }
769 }
771 // This version requires locking.
772 inline HeapWord* ContiguousSpace::allocate_impl(size_t size,
773 HeapWord* const end_value) {
774 assert(Heap_lock->owned_by_self() ||
775 (SafepointSynchronize::is_at_safepoint() &&
776 Thread::current()->is_VM_thread()),
777 "not locked");
778 HeapWord* obj = top();
779 if (pointer_delta(end_value, obj) >= size) {
780 HeapWord* new_top = obj + size;
781 set_top(new_top);
782 assert(is_aligned(obj) && is_aligned(new_top), "checking alignment");
783 return obj;
784 } else {
785 return NULL;
786 }
787 }
789 // This version is lock-free.
790 inline HeapWord* ContiguousSpace::par_allocate_impl(size_t size,
791 HeapWord* const end_value) {
792 do {
793 HeapWord* obj = top();
794 if (pointer_delta(end_value, obj) >= size) {
795 HeapWord* new_top = obj + size;
796 HeapWord* result = (HeapWord*)Atomic::cmpxchg_ptr(new_top, top_addr(), obj);
797 // result can be one of two:
798 // the old top value: the exchange succeeded
799 // otherwise: the new value of the top is returned.
800 if (result == obj) {
801 assert(is_aligned(obj) && is_aligned(new_top), "checking alignment");
802 return obj;
803 }
804 } else {
805 return NULL;
806 }
807 } while (true);
808 }
810 // Requires locking.
811 HeapWord* ContiguousSpace::allocate(size_t size) {
812 return allocate_impl(size, end());
813 }
815 // Lock-free.
816 HeapWord* ContiguousSpace::par_allocate(size_t size) {
817 return par_allocate_impl(size, end());
818 }
820 void ContiguousSpace::allocate_temporary_filler(int factor) {
821 // allocate temporary type array decreasing free size with factor 'factor'
822 assert(factor >= 0, "just checking");
823 size_t size = pointer_delta(end(), top());
825 // if space is full, return
826 if (size == 0) return;
828 if (factor > 0) {
829 size -= size/factor;
830 }
831 size = align_object_size(size);
833 const size_t min_int_array_size = typeArrayOopDesc::header_size(T_INT);
834 if (size >= min_int_array_size) {
835 size_t length = (size - min_int_array_size) * (HeapWordSize / sizeof(jint));
836 // allocate uninitialized int array
837 typeArrayOop t = (typeArrayOop) allocate(size);
838 assert(t != NULL, "allocation should succeed");
839 t->set_mark(markOopDesc::prototype());
840 t->set_klass(Universe::intArrayKlassObj());
841 t->set_length((int)length);
842 } else {
843 assert((int) size == instanceOopDesc::header_size(),
844 "size for smallest fake object doesn't match");
845 instanceOop obj = (instanceOop) allocate(size);
846 obj->set_mark(markOopDesc::prototype());
847 obj->set_klass_gap(0);
848 obj->set_klass(SystemDictionary::object_klass());
849 }
850 }
852 void EdenSpace::clear() {
853 ContiguousSpace::clear();
854 set_soft_end(end());
855 }
857 // Requires locking.
858 HeapWord* EdenSpace::allocate(size_t size) {
859 return allocate_impl(size, soft_end());
860 }
862 // Lock-free.
863 HeapWord* EdenSpace::par_allocate(size_t size) {
864 return par_allocate_impl(size, soft_end());
865 }
867 HeapWord* ConcEdenSpace::par_allocate(size_t size)
868 {
869 do {
870 // The invariant is top() should be read before end() because
871 // top() can't be greater than end(), so if an update of _soft_end
872 // occurs between 'end_val = end();' and 'top_val = top();' top()
873 // also can grow up to the new end() and the condition
874 // 'top_val > end_val' is true. To ensure the loading order
875 // OrderAccess::loadload() is required after top() read.
876 HeapWord* obj = top();
877 OrderAccess::loadload();
878 if (pointer_delta(*soft_end_addr(), obj) >= size) {
879 HeapWord* new_top = obj + size;
880 HeapWord* result = (HeapWord*)Atomic::cmpxchg_ptr(new_top, top_addr(), obj);
881 // result can be one of two:
882 // the old top value: the exchange succeeded
883 // otherwise: the new value of the top is returned.
884 if (result == obj) {
885 assert(is_aligned(obj) && is_aligned(new_top), "checking alignment");
886 return obj;
887 }
888 } else {
889 return NULL;
890 }
891 } while (true);
892 }
895 HeapWord* OffsetTableContigSpace::initialize_threshold() {
896 return _offsets.initialize_threshold();
897 }
899 HeapWord* OffsetTableContigSpace::cross_threshold(HeapWord* start, HeapWord* end) {
900 _offsets.alloc_block(start, end);
901 return _offsets.threshold();
902 }
904 OffsetTableContigSpace::OffsetTableContigSpace(BlockOffsetSharedArray* sharedOffsetArray,
905 MemRegion mr) :
906 _offsets(sharedOffsetArray, mr),
907 _par_alloc_lock(Mutex::leaf, "OffsetTableContigSpace par alloc lock", true)
908 {
909 _offsets.set_contig_space(this);
910 initialize(mr, true);
911 }
914 class VerifyOldOopClosure : public OopClosure {
915 public:
916 oop _the_obj;
917 bool _allow_dirty;
918 void do_oop(oop* p) {
919 _the_obj->verify_old_oop(p, _allow_dirty);
920 }
921 void do_oop(narrowOop* p) {
922 _the_obj->verify_old_oop(p, _allow_dirty);
923 }
924 };
926 #define OBJ_SAMPLE_INTERVAL 0
927 #define BLOCK_SAMPLE_INTERVAL 100
929 void OffsetTableContigSpace::verify(bool allow_dirty) const {
930 HeapWord* p = bottom();
931 HeapWord* prev_p = NULL;
932 VerifyOldOopClosure blk; // Does this do anything?
933 blk._allow_dirty = allow_dirty;
934 int objs = 0;
935 int blocks = 0;
937 if (VerifyObjectStartArray) {
938 _offsets.verify();
939 }
941 while (p < top()) {
942 size_t size = oop(p)->size();
943 // For a sampling of objects in the space, find it using the
944 // block offset table.
945 if (blocks == BLOCK_SAMPLE_INTERVAL) {
946 guarantee(p == block_start_const(p + (size/2)),
947 "check offset computation");
948 blocks = 0;
949 } else {
950 blocks++;
951 }
953 if (objs == OBJ_SAMPLE_INTERVAL) {
954 oop(p)->verify();
955 blk._the_obj = oop(p);
956 oop(p)->oop_iterate(&blk);
957 objs = 0;
958 } else {
959 objs++;
960 }
961 prev_p = p;
962 p += size;
963 }
964 guarantee(p == top(), "end of last object must match end of space");
965 }
967 void OffsetTableContigSpace::serialize_block_offset_array_offsets(
968 SerializeOopClosure* soc) {
969 _offsets.serialize(soc);
970 }
973 int TenuredSpace::allowed_dead_ratio() const {
974 return MarkSweepDeadRatio;
975 }
978 int ContigPermSpace::allowed_dead_ratio() const {
979 return PermMarkSweepDeadRatio;
980 }