Thu, 11 Dec 2008 12:05:08 -0800
6578152: fill_region_with_object has usability and safety issues
Reviewed-by: apetrusenko, ysr
1 /*
2 * Copyright 1997-2008 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::initialize(MemRegion mr,
243 bool clear_space,
244 bool mangle_space) {
245 HeapWord* bottom = mr.start();
246 HeapWord* end = mr.end();
247 assert(Universe::on_page_boundary(bottom) && Universe::on_page_boundary(end),
248 "invalid space boundaries");
249 set_bottom(bottom);
250 set_end(end);
251 if (clear_space) clear(mangle_space);
252 }
254 void Space::clear(bool mangle_space) {
255 if (ZapUnusedHeapArea && mangle_space) {
256 mangle_unused_area();
257 }
258 }
260 ContiguousSpace::ContiguousSpace(): CompactibleSpace(), _top(NULL),
261 _concurrent_iteration_safe_limit(NULL) {
262 _mangler = new GenSpaceMangler(this);
263 }
265 ContiguousSpace::~ContiguousSpace() {
266 delete _mangler;
267 }
269 void ContiguousSpace::initialize(MemRegion mr,
270 bool clear_space,
271 bool mangle_space)
272 {
273 CompactibleSpace::initialize(mr, clear_space, mangle_space);
274 set_concurrent_iteration_safe_limit(top());
275 }
277 void ContiguousSpace::clear(bool mangle_space) {
278 set_top(bottom());
279 set_saved_mark();
280 CompactibleSpace::clear(mangle_space);
281 }
283 bool Space::is_in(const void* p) const {
284 HeapWord* b = block_start_const(p);
285 return b != NULL && block_is_obj(b);
286 }
288 bool ContiguousSpace::is_in(const void* p) const {
289 return _bottom <= p && p < _top;
290 }
292 bool ContiguousSpace::is_free_block(const HeapWord* p) const {
293 return p >= _top;
294 }
296 void OffsetTableContigSpace::clear(bool mangle_space) {
297 ContiguousSpace::clear(mangle_space);
298 _offsets.initialize_threshold();
299 }
301 void OffsetTableContigSpace::set_bottom(HeapWord* new_bottom) {
302 Space::set_bottom(new_bottom);
303 _offsets.set_bottom(new_bottom);
304 }
306 void OffsetTableContigSpace::set_end(HeapWord* new_end) {
307 // Space should not advertize an increase in size
308 // until after the underlying offest table has been enlarged.
309 _offsets.resize(pointer_delta(new_end, bottom()));
310 Space::set_end(new_end);
311 }
313 #ifndef PRODUCT
315 void ContiguousSpace::set_top_for_allocations(HeapWord* v) {
316 mangler()->set_top_for_allocations(v);
317 }
318 void ContiguousSpace::set_top_for_allocations() {
319 mangler()->set_top_for_allocations(top());
320 }
321 void ContiguousSpace::check_mangled_unused_area(HeapWord* limit) {
322 mangler()->check_mangled_unused_area(limit);
323 }
325 void ContiguousSpace::check_mangled_unused_area_complete() {
326 mangler()->check_mangled_unused_area_complete();
327 }
329 // Mangled only the unused space that has not previously
330 // been mangled and that has not been allocated since being
331 // mangled.
332 void ContiguousSpace::mangle_unused_area() {
333 mangler()->mangle_unused_area();
334 }
335 void ContiguousSpace::mangle_unused_area_complete() {
336 mangler()->mangle_unused_area_complete();
337 }
338 void ContiguousSpace::mangle_region(MemRegion mr) {
339 // Although this method uses SpaceMangler::mangle_region() which
340 // is not specific to a space, the when the ContiguousSpace version
341 // is called, it is always with regard to a space and this
342 // bounds checking is appropriate.
343 MemRegion space_mr(bottom(), end());
344 assert(space_mr.contains(mr), "Mangling outside space");
345 SpaceMangler::mangle_region(mr);
346 }
347 #endif // NOT_PRODUCT
349 void CompactibleSpace::initialize(MemRegion mr,
350 bool clear_space,
351 bool mangle_space) {
352 Space::initialize(mr, clear_space, mangle_space);
353 set_compaction_top(bottom());
354 _next_compaction_space = NULL;
355 }
357 void CompactibleSpace::clear(bool mangle_space) {
358 Space::clear(mangle_space);
359 _compaction_top = bottom();
360 }
362 HeapWord* CompactibleSpace::forward(oop q, size_t size,
363 CompactPoint* cp, HeapWord* compact_top) {
364 // q is alive
365 // First check if we should switch compaction space
366 assert(this == cp->space, "'this' should be current compaction space.");
367 size_t compaction_max_size = pointer_delta(end(), compact_top);
368 while (size > compaction_max_size) {
369 // switch to next compaction space
370 cp->space->set_compaction_top(compact_top);
371 cp->space = cp->space->next_compaction_space();
372 if (cp->space == NULL) {
373 cp->gen = GenCollectedHeap::heap()->prev_gen(cp->gen);
374 assert(cp->gen != NULL, "compaction must succeed");
375 cp->space = cp->gen->first_compaction_space();
376 assert(cp->space != NULL, "generation must have a first compaction space");
377 }
378 compact_top = cp->space->bottom();
379 cp->space->set_compaction_top(compact_top);
380 cp->threshold = cp->space->initialize_threshold();
381 compaction_max_size = pointer_delta(cp->space->end(), compact_top);
382 }
384 // store the forwarding pointer into the mark word
385 if ((HeapWord*)q != compact_top) {
386 q->forward_to(oop(compact_top));
387 assert(q->is_gc_marked(), "encoding the pointer should preserve the mark");
388 } else {
389 // if the object isn't moving we can just set the mark to the default
390 // mark and handle it specially later on.
391 q->init_mark();
392 assert(q->forwardee() == NULL, "should be forwarded to NULL");
393 }
395 VALIDATE_MARK_SWEEP_ONLY(MarkSweep::register_live_oop(q, size));
396 compact_top += size;
398 // we need to update the offset table so that the beginnings of objects can be
399 // found during scavenge. Note that we are updating the offset table based on
400 // where the object will be once the compaction phase finishes.
401 if (compact_top > cp->threshold)
402 cp->threshold =
403 cp->space->cross_threshold(compact_top - size, compact_top);
404 return compact_top;
405 }
408 bool CompactibleSpace::insert_deadspace(size_t& allowed_deadspace_words,
409 HeapWord* q, size_t deadlength) {
410 if (allowed_deadspace_words >= deadlength) {
411 allowed_deadspace_words -= deadlength;
412 CollectedHeap::fill_with_object(q, deadlength);
413 oop(q)->set_mark(oop(q)->mark()->set_marked());
414 assert((int) deadlength == oop(q)->size(), "bad filler object size");
415 // Recall that we required "q == compaction_top".
416 return true;
417 } else {
418 allowed_deadspace_words = 0;
419 return false;
420 }
421 }
423 #define block_is_always_obj(q) true
424 #define obj_size(q) oop(q)->size()
425 #define adjust_obj_size(s) s
427 void CompactibleSpace::prepare_for_compaction(CompactPoint* cp) {
428 SCAN_AND_FORWARD(cp, end, block_is_obj, block_size);
429 }
431 // Faster object search.
432 void ContiguousSpace::prepare_for_compaction(CompactPoint* cp) {
433 SCAN_AND_FORWARD(cp, top, block_is_always_obj, obj_size);
434 }
436 void Space::adjust_pointers() {
437 // adjust all the interior pointers to point at the new locations of objects
438 // Used by MarkSweep::mark_sweep_phase3()
440 // First check to see if there is any work to be done.
441 if (used() == 0) {
442 return; // Nothing to do.
443 }
445 // Otherwise...
446 HeapWord* q = bottom();
447 HeapWord* t = end();
449 debug_only(HeapWord* prev_q = NULL);
450 while (q < t) {
451 if (oop(q)->is_gc_marked()) {
452 // q is alive
454 VALIDATE_MARK_SWEEP_ONLY(MarkSweep::track_interior_pointers(oop(q)));
455 // point all the oops to the new location
456 size_t size = oop(q)->adjust_pointers();
457 VALIDATE_MARK_SWEEP_ONLY(MarkSweep::check_interior_pointers());
459 debug_only(prev_q = q);
460 VALIDATE_MARK_SWEEP_ONLY(MarkSweep::validate_live_oop(oop(q), size));
462 q += size;
463 } else {
464 // q is not a live object. But we're not in a compactible space,
465 // So we don't have live ranges.
466 debug_only(prev_q = q);
467 q += block_size(q);
468 assert(q > prev_q, "we should be moving forward through memory");
469 }
470 }
471 assert(q == t, "just checking");
472 }
474 void CompactibleSpace::adjust_pointers() {
475 // Check first is there is any work to do.
476 if (used() == 0) {
477 return; // Nothing to do.
478 }
480 SCAN_AND_ADJUST_POINTERS(adjust_obj_size);
481 }
483 void CompactibleSpace::compact() {
484 SCAN_AND_COMPACT(obj_size);
485 }
487 void Space::print_short() const { print_short_on(tty); }
489 void Space::print_short_on(outputStream* st) const {
490 st->print(" space " SIZE_FORMAT "K, %3d%% used", capacity() / K,
491 (int) ((double) used() * 100 / capacity()));
492 }
494 void Space::print() const { print_on(tty); }
496 void Space::print_on(outputStream* st) const {
497 print_short_on(st);
498 st->print_cr(" [" INTPTR_FORMAT ", " INTPTR_FORMAT ")",
499 bottom(), end());
500 }
502 void ContiguousSpace::print_on(outputStream* st) const {
503 print_short_on(st);
504 st->print_cr(" [" INTPTR_FORMAT ", " INTPTR_FORMAT ", " INTPTR_FORMAT ")",
505 bottom(), top(), end());
506 }
508 void OffsetTableContigSpace::print_on(outputStream* st) const {
509 print_short_on(st);
510 st->print_cr(" [" INTPTR_FORMAT ", " INTPTR_FORMAT ", "
511 INTPTR_FORMAT ", " INTPTR_FORMAT ")",
512 bottom(), top(), _offsets.threshold(), end());
513 }
515 void ContiguousSpace::verify(bool allow_dirty) const {
516 HeapWord* p = bottom();
517 HeapWord* t = top();
518 HeapWord* prev_p = NULL;
519 while (p < t) {
520 oop(p)->verify();
521 prev_p = p;
522 p += oop(p)->size();
523 }
524 guarantee(p == top(), "end of last object must match end of space");
525 if (top() != end()) {
526 guarantee(top() == block_start_const(end()-1) &&
527 top() == block_start_const(top()),
528 "top should be start of unallocated block, if it exists");
529 }
530 }
532 void Space::oop_iterate(OopClosure* blk) {
533 ObjectToOopClosure blk2(blk);
534 object_iterate(&blk2);
535 }
537 HeapWord* Space::object_iterate_careful(ObjectClosureCareful* cl) {
538 guarantee(false, "NYI");
539 return bottom();
540 }
542 HeapWord* Space::object_iterate_careful_m(MemRegion mr,
543 ObjectClosureCareful* cl) {
544 guarantee(false, "NYI");
545 return bottom();
546 }
549 void Space::object_iterate_mem(MemRegion mr, UpwardsObjectClosure* cl) {
550 assert(!mr.is_empty(), "Should be non-empty");
551 // We use MemRegion(bottom(), end()) rather than used_region() below
552 // because the two are not necessarily equal for some kinds of
553 // spaces, in particular, certain kinds of free list spaces.
554 // We could use the more complicated but more precise:
555 // MemRegion(used_region().start(), round_to(used_region().end(), CardSize))
556 // but the slight imprecision seems acceptable in the assertion check.
557 assert(MemRegion(bottom(), end()).contains(mr),
558 "Should be within used space");
559 HeapWord* prev = cl->previous(); // max address from last time
560 if (prev >= mr.end()) { // nothing to do
561 return;
562 }
563 // This assert will not work when we go from cms space to perm
564 // space, and use same closure. Easy fix deferred for later. XXX YSR
565 // assert(prev == NULL || contains(prev), "Should be within space");
567 bool last_was_obj_array = false;
568 HeapWord *blk_start_addr, *region_start_addr;
569 if (prev > mr.start()) {
570 region_start_addr = prev;
571 blk_start_addr = prev;
572 assert(blk_start_addr == block_start(region_start_addr), "invariant");
573 } else {
574 region_start_addr = mr.start();
575 blk_start_addr = block_start(region_start_addr);
576 }
577 HeapWord* region_end_addr = mr.end();
578 MemRegion derived_mr(region_start_addr, region_end_addr);
579 while (blk_start_addr < region_end_addr) {
580 const size_t size = block_size(blk_start_addr);
581 if (block_is_obj(blk_start_addr)) {
582 last_was_obj_array = cl->do_object_bm(oop(blk_start_addr), derived_mr);
583 } else {
584 last_was_obj_array = false;
585 }
586 blk_start_addr += size;
587 }
588 if (!last_was_obj_array) {
589 assert((bottom() <= blk_start_addr) && (blk_start_addr <= end()),
590 "Should be within (closed) used space");
591 assert(blk_start_addr > prev, "Invariant");
592 cl->set_previous(blk_start_addr); // min address for next time
593 }
594 }
596 bool Space::obj_is_alive(const HeapWord* p) const {
597 assert (block_is_obj(p), "The address should point to an object");
598 return true;
599 }
601 void ContiguousSpace::object_iterate_mem(MemRegion mr, UpwardsObjectClosure* cl) {
602 assert(!mr.is_empty(), "Should be non-empty");
603 assert(used_region().contains(mr), "Should be within used space");
604 HeapWord* prev = cl->previous(); // max address from last time
605 if (prev >= mr.end()) { // nothing to do
606 return;
607 }
608 // See comment above (in more general method above) in case you
609 // happen to use this method.
610 assert(prev == NULL || is_in_reserved(prev), "Should be within space");
612 bool last_was_obj_array = false;
613 HeapWord *obj_start_addr, *region_start_addr;
614 if (prev > mr.start()) {
615 region_start_addr = prev;
616 obj_start_addr = prev;
617 assert(obj_start_addr == block_start(region_start_addr), "invariant");
618 } else {
619 region_start_addr = mr.start();
620 obj_start_addr = block_start(region_start_addr);
621 }
622 HeapWord* region_end_addr = mr.end();
623 MemRegion derived_mr(region_start_addr, region_end_addr);
624 while (obj_start_addr < region_end_addr) {
625 oop obj = oop(obj_start_addr);
626 const size_t size = obj->size();
627 last_was_obj_array = cl->do_object_bm(obj, derived_mr);
628 obj_start_addr += size;
629 }
630 if (!last_was_obj_array) {
631 assert((bottom() <= obj_start_addr) && (obj_start_addr <= end()),
632 "Should be within (closed) used space");
633 assert(obj_start_addr > prev, "Invariant");
634 cl->set_previous(obj_start_addr); // min address for next time
635 }
636 }
638 #ifndef SERIALGC
639 #define ContigSpace_PAR_OOP_ITERATE_DEFN(OopClosureType, nv_suffix) \
640 \
641 void ContiguousSpace::par_oop_iterate(MemRegion mr, OopClosureType* blk) {\
642 HeapWord* obj_addr = mr.start(); \
643 HeapWord* t = mr.end(); \
644 while (obj_addr < t) { \
645 assert(oop(obj_addr)->is_oop(), "Should be an oop"); \
646 obj_addr += oop(obj_addr)->oop_iterate(blk); \
647 } \
648 }
650 ALL_PAR_OOP_ITERATE_CLOSURES(ContigSpace_PAR_OOP_ITERATE_DEFN)
652 #undef ContigSpace_PAR_OOP_ITERATE_DEFN
653 #endif // SERIALGC
655 void ContiguousSpace::oop_iterate(OopClosure* blk) {
656 if (is_empty()) return;
657 HeapWord* obj_addr = bottom();
658 HeapWord* t = top();
659 // Could call objects iterate, but this is easier.
660 while (obj_addr < t) {
661 obj_addr += oop(obj_addr)->oop_iterate(blk);
662 }
663 }
665 void ContiguousSpace::oop_iterate(MemRegion mr, OopClosure* blk) {
666 if (is_empty()) {
667 return;
668 }
669 MemRegion cur = MemRegion(bottom(), top());
670 mr = mr.intersection(cur);
671 if (mr.is_empty()) {
672 return;
673 }
674 if (mr.equals(cur)) {
675 oop_iterate(blk);
676 return;
677 }
678 assert(mr.end() <= top(), "just took an intersection above");
679 HeapWord* obj_addr = block_start(mr.start());
680 HeapWord* t = mr.end();
682 // Handle first object specially.
683 oop obj = oop(obj_addr);
684 SpaceMemRegionOopsIterClosure smr_blk(blk, mr);
685 obj_addr += obj->oop_iterate(&smr_blk);
686 while (obj_addr < t) {
687 oop obj = oop(obj_addr);
688 assert(obj->is_oop(), "expected an oop");
689 obj_addr += obj->size();
690 // If "obj_addr" is not greater than top, then the
691 // entire object "obj" is within the region.
692 if (obj_addr <= t) {
693 obj->oop_iterate(blk);
694 } else {
695 // "obj" extends beyond end of region
696 obj->oop_iterate(&smr_blk);
697 break;
698 }
699 };
700 }
702 void ContiguousSpace::object_iterate(ObjectClosure* blk) {
703 if (is_empty()) return;
704 WaterMark bm = bottom_mark();
705 object_iterate_from(bm, blk);
706 }
708 void ContiguousSpace::object_iterate_from(WaterMark mark, ObjectClosure* blk) {
709 assert(mark.space() == this, "Mark does not match space");
710 HeapWord* p = mark.point();
711 while (p < top()) {
712 blk->do_object(oop(p));
713 p += oop(p)->size();
714 }
715 }
717 HeapWord*
718 ContiguousSpace::object_iterate_careful(ObjectClosureCareful* blk) {
719 HeapWord * limit = concurrent_iteration_safe_limit();
720 assert(limit <= top(), "sanity check");
721 for (HeapWord* p = bottom(); p < limit;) {
722 size_t size = blk->do_object_careful(oop(p));
723 if (size == 0) {
724 return p; // failed at p
725 } else {
726 p += size;
727 }
728 }
729 return NULL; // all done
730 }
732 #define ContigSpace_OOP_SINCE_SAVE_MARKS_DEFN(OopClosureType, nv_suffix) \
733 \
734 void ContiguousSpace:: \
735 oop_since_save_marks_iterate##nv_suffix(OopClosureType* blk) { \
736 HeapWord* t; \
737 HeapWord* p = saved_mark_word(); \
738 assert(p != NULL, "expected saved mark"); \
739 \
740 const intx interval = PrefetchScanIntervalInBytes; \
741 do { \
742 t = top(); \
743 while (p < t) { \
744 Prefetch::write(p, interval); \
745 debug_only(HeapWord* prev = p); \
746 oop m = oop(p); \
747 p += m->oop_iterate(blk); \
748 } \
749 } while (t < top()); \
750 \
751 set_saved_mark_word(p); \
752 }
754 ALL_SINCE_SAVE_MARKS_CLOSURES(ContigSpace_OOP_SINCE_SAVE_MARKS_DEFN)
756 #undef ContigSpace_OOP_SINCE_SAVE_MARKS_DEFN
758 // Very general, slow implementation.
759 HeapWord* ContiguousSpace::block_start_const(const void* p) const {
760 assert(MemRegion(bottom(), end()).contains(p), "p not in space");
761 if (p >= top()) {
762 return top();
763 } else {
764 HeapWord* last = bottom();
765 HeapWord* cur = last;
766 while (cur <= p) {
767 last = cur;
768 cur += oop(cur)->size();
769 }
770 assert(oop(last)->is_oop(), "Should be an object start");
771 return last;
772 }
773 }
775 size_t ContiguousSpace::block_size(const HeapWord* p) const {
776 assert(MemRegion(bottom(), end()).contains(p), "p not in space");
777 HeapWord* current_top = top();
778 assert(p <= current_top, "p is not a block start");
779 assert(p == current_top || oop(p)->is_oop(), "p is not a block start");
780 if (p < current_top)
781 return oop(p)->size();
782 else {
783 assert(p == current_top, "just checking");
784 return pointer_delta(end(), (HeapWord*) p);
785 }
786 }
788 // This version requires locking.
789 inline HeapWord* ContiguousSpace::allocate_impl(size_t size,
790 HeapWord* const end_value) {
791 assert(Heap_lock->owned_by_self() ||
792 (SafepointSynchronize::is_at_safepoint() &&
793 Thread::current()->is_VM_thread()),
794 "not locked");
795 HeapWord* obj = top();
796 if (pointer_delta(end_value, obj) >= size) {
797 HeapWord* new_top = obj + size;
798 set_top(new_top);
799 assert(is_aligned(obj) && is_aligned(new_top), "checking alignment");
800 return obj;
801 } else {
802 return NULL;
803 }
804 }
806 // This version is lock-free.
807 inline HeapWord* ContiguousSpace::par_allocate_impl(size_t size,
808 HeapWord* const end_value) {
809 do {
810 HeapWord* obj = top();
811 if (pointer_delta(end_value, obj) >= size) {
812 HeapWord* new_top = obj + size;
813 HeapWord* result = (HeapWord*)Atomic::cmpxchg_ptr(new_top, top_addr(), obj);
814 // result can be one of two:
815 // the old top value: the exchange succeeded
816 // otherwise: the new value of the top is returned.
817 if (result == obj) {
818 assert(is_aligned(obj) && is_aligned(new_top), "checking alignment");
819 return obj;
820 }
821 } else {
822 return NULL;
823 }
824 } while (true);
825 }
827 // Requires locking.
828 HeapWord* ContiguousSpace::allocate(size_t size) {
829 return allocate_impl(size, end());
830 }
832 // Lock-free.
833 HeapWord* ContiguousSpace::par_allocate(size_t size) {
834 return par_allocate_impl(size, end());
835 }
837 void ContiguousSpace::allocate_temporary_filler(int factor) {
838 // allocate temporary type array decreasing free size with factor 'factor'
839 assert(factor >= 0, "just checking");
840 size_t size = pointer_delta(end(), top());
842 // if space is full, return
843 if (size == 0) return;
845 if (factor > 0) {
846 size -= size/factor;
847 }
848 size = align_object_size(size);
850 const size_t min_int_array_size = typeArrayOopDesc::header_size(T_INT);
851 if (size >= min_int_array_size) {
852 size_t length = (size - min_int_array_size) * (HeapWordSize / sizeof(jint));
853 // allocate uninitialized int array
854 typeArrayOop t = (typeArrayOop) allocate(size);
855 assert(t != NULL, "allocation should succeed");
856 t->set_mark(markOopDesc::prototype());
857 t->set_klass(Universe::intArrayKlassObj());
858 t->set_length((int)length);
859 } else {
860 assert((int) size == instanceOopDesc::header_size(),
861 "size for smallest fake object doesn't match");
862 instanceOop obj = (instanceOop) allocate(size);
863 obj->set_mark(markOopDesc::prototype());
864 obj->set_klass_gap(0);
865 obj->set_klass(SystemDictionary::object_klass());
866 }
867 }
869 void EdenSpace::clear(bool mangle_space) {
870 ContiguousSpace::clear(mangle_space);
871 set_soft_end(end());
872 }
874 // Requires locking.
875 HeapWord* EdenSpace::allocate(size_t size) {
876 return allocate_impl(size, soft_end());
877 }
879 // Lock-free.
880 HeapWord* EdenSpace::par_allocate(size_t size) {
881 return par_allocate_impl(size, soft_end());
882 }
884 HeapWord* ConcEdenSpace::par_allocate(size_t size)
885 {
886 do {
887 // The invariant is top() should be read before end() because
888 // top() can't be greater than end(), so if an update of _soft_end
889 // occurs between 'end_val = end();' and 'top_val = top();' top()
890 // also can grow up to the new end() and the condition
891 // 'top_val > end_val' is true. To ensure the loading order
892 // OrderAccess::loadload() is required after top() read.
893 HeapWord* obj = top();
894 OrderAccess::loadload();
895 if (pointer_delta(*soft_end_addr(), obj) >= size) {
896 HeapWord* new_top = obj + size;
897 HeapWord* result = (HeapWord*)Atomic::cmpxchg_ptr(new_top, top_addr(), obj);
898 // result can be one of two:
899 // the old top value: the exchange succeeded
900 // otherwise: the new value of the top is returned.
901 if (result == obj) {
902 assert(is_aligned(obj) && is_aligned(new_top), "checking alignment");
903 return obj;
904 }
905 } else {
906 return NULL;
907 }
908 } while (true);
909 }
912 HeapWord* OffsetTableContigSpace::initialize_threshold() {
913 return _offsets.initialize_threshold();
914 }
916 HeapWord* OffsetTableContigSpace::cross_threshold(HeapWord* start, HeapWord* end) {
917 _offsets.alloc_block(start, end);
918 return _offsets.threshold();
919 }
921 OffsetTableContigSpace::OffsetTableContigSpace(BlockOffsetSharedArray* sharedOffsetArray,
922 MemRegion mr) :
923 _offsets(sharedOffsetArray, mr),
924 _par_alloc_lock(Mutex::leaf, "OffsetTableContigSpace par alloc lock", true)
925 {
926 _offsets.set_contig_space(this);
927 initialize(mr, SpaceDecorator::Clear, SpaceDecorator::Mangle);
928 }
931 class VerifyOldOopClosure : public OopClosure {
932 public:
933 oop _the_obj;
934 bool _allow_dirty;
935 void do_oop(oop* p) {
936 _the_obj->verify_old_oop(p, _allow_dirty);
937 }
938 void do_oop(narrowOop* p) {
939 _the_obj->verify_old_oop(p, _allow_dirty);
940 }
941 };
943 #define OBJ_SAMPLE_INTERVAL 0
944 #define BLOCK_SAMPLE_INTERVAL 100
946 void OffsetTableContigSpace::verify(bool allow_dirty) const {
947 HeapWord* p = bottom();
948 HeapWord* prev_p = NULL;
949 VerifyOldOopClosure blk; // Does this do anything?
950 blk._allow_dirty = allow_dirty;
951 int objs = 0;
952 int blocks = 0;
954 if (VerifyObjectStartArray) {
955 _offsets.verify();
956 }
958 while (p < top()) {
959 size_t size = oop(p)->size();
960 // For a sampling of objects in the space, find it using the
961 // block offset table.
962 if (blocks == BLOCK_SAMPLE_INTERVAL) {
963 guarantee(p == block_start_const(p + (size/2)),
964 "check offset computation");
965 blocks = 0;
966 } else {
967 blocks++;
968 }
970 if (objs == OBJ_SAMPLE_INTERVAL) {
971 oop(p)->verify();
972 blk._the_obj = oop(p);
973 oop(p)->oop_iterate(&blk);
974 objs = 0;
975 } else {
976 objs++;
977 }
978 prev_p = p;
979 p += size;
980 }
981 guarantee(p == top(), "end of last object must match end of space");
982 }
984 void OffsetTableContigSpace::serialize_block_offset_array_offsets(
985 SerializeOopClosure* soc) {
986 _offsets.serialize(soc);
987 }
990 size_t TenuredSpace::allowed_dead_ratio() const {
991 return MarkSweepDeadRatio;
992 }
995 size_t ContigPermSpace::allowed_dead_ratio() const {
996 return PermMarkSweepDeadRatio;
997 }