duke@435: /* xdono@1014: * Copyright 1997-2009 Sun Microsystems, Inc. All Rights Reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * duke@435: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, duke@435: * CA 95054 USA or visit www.sun.com if you need additional information or duke@435: * have any questions. duke@435: * duke@435: */ duke@435: duke@435: # include "incls/_precompiled.incl" duke@435: # include "incls/_space.cpp.incl" duke@435: coleenp@548: void SpaceMemRegionOopsIterClosure::do_oop(oop* p) { SpaceMemRegionOopsIterClosure::do_oop_work(p); } coleenp@548: void SpaceMemRegionOopsIterClosure::do_oop(narrowOop* p) { SpaceMemRegionOopsIterClosure::do_oop_work(p); } coleenp@548: duke@435: HeapWord* DirtyCardToOopClosure::get_actual_top(HeapWord* top, duke@435: HeapWord* top_obj) { duke@435: if (top_obj != NULL) { duke@435: if (_sp->block_is_obj(top_obj)) { duke@435: if (_precision == CardTableModRefBS::ObjHeadPreciseArray) { duke@435: if (oop(top_obj)->is_objArray() || oop(top_obj)->is_typeArray()) { duke@435: // An arrayOop is starting on the dirty card - since we do exact duke@435: // store checks for objArrays we are done. duke@435: } else { duke@435: // Otherwise, it is possible that the object starting on the dirty duke@435: // card spans the entire card, and that the store happened on a duke@435: // later card. Figure out where the object ends. duke@435: // Use the block_size() method of the space over which duke@435: // the iteration is being done. That space (e.g. CMS) may have duke@435: // specific requirements on object sizes which will duke@435: // be reflected in the block_size() method. duke@435: top = top_obj + oop(top_obj)->size(); duke@435: } duke@435: } duke@435: } else { duke@435: top = top_obj; duke@435: } duke@435: } else { duke@435: assert(top == _sp->end(), "only case where top_obj == NULL"); duke@435: } duke@435: return top; duke@435: } duke@435: duke@435: void DirtyCardToOopClosure::walk_mem_region(MemRegion mr, duke@435: HeapWord* bottom, duke@435: HeapWord* top) { duke@435: // 1. Blocks may or may not be objects. duke@435: // 2. Even when a block_is_obj(), it may not entirely duke@435: // occupy the block if the block quantum is larger than duke@435: // the object size. duke@435: // We can and should try to optimize by calling the non-MemRegion duke@435: // version of oop_iterate() for all but the extremal objects duke@435: // (for which we need to call the MemRegion version of duke@435: // oop_iterate()) To be done post-beta XXX duke@435: for (; bottom < top; bottom += _sp->block_size(bottom)) { duke@435: // As in the case of contiguous space above, we'd like to duke@435: // just use the value returned by oop_iterate to increment the duke@435: // current pointer; unfortunately, that won't work in CMS because duke@435: // we'd need an interface change (it seems) to have the space duke@435: // "adjust the object size" (for instance pad it up to its duke@435: // block alignment or minimum block size restrictions. XXX duke@435: if (_sp->block_is_obj(bottom) && duke@435: !_sp->obj_allocated_since_save_marks(oop(bottom))) { duke@435: oop(bottom)->oop_iterate(_cl, mr); duke@435: } duke@435: } duke@435: } duke@435: duke@435: void DirtyCardToOopClosure::do_MemRegion(MemRegion mr) { duke@435: duke@435: // Some collectors need to do special things whenever their dirty duke@435: // cards are processed. For instance, CMS must remember mutator updates duke@435: // (i.e. dirty cards) so as to re-scan mutated objects. duke@435: // Such work can be piggy-backed here on dirty card scanning, so as to make duke@435: // it slightly more efficient than doing a complete non-detructive pre-scan duke@435: // of the card table. duke@435: MemRegionClosure* pCl = _sp->preconsumptionDirtyCardClosure(); duke@435: if (pCl != NULL) { duke@435: pCl->do_MemRegion(mr); duke@435: } duke@435: duke@435: HeapWord* bottom = mr.start(); duke@435: HeapWord* last = mr.last(); duke@435: HeapWord* top = mr.end(); duke@435: HeapWord* bottom_obj; duke@435: HeapWord* top_obj; duke@435: duke@435: assert(_precision == CardTableModRefBS::ObjHeadPreciseArray || duke@435: _precision == CardTableModRefBS::Precise, duke@435: "Only ones we deal with for now."); duke@435: duke@435: assert(_precision != CardTableModRefBS::ObjHeadPreciseArray || ysr@777: _cl->idempotent() || _last_bottom == NULL || duke@435: top <= _last_bottom, duke@435: "Not decreasing"); duke@435: NOT_PRODUCT(_last_bottom = mr.start()); duke@435: duke@435: bottom_obj = _sp->block_start(bottom); duke@435: top_obj = _sp->block_start(last); duke@435: duke@435: assert(bottom_obj <= bottom, "just checking"); duke@435: assert(top_obj <= top, "just checking"); duke@435: duke@435: // Given what we think is the top of the memory region and duke@435: // the start of the object at the top, get the actual duke@435: // value of the top. duke@435: top = get_actual_top(top, top_obj); duke@435: duke@435: // If the previous call did some part of this region, don't redo. duke@435: if (_precision == CardTableModRefBS::ObjHeadPreciseArray && duke@435: _min_done != NULL && duke@435: _min_done < top) { duke@435: top = _min_done; duke@435: } duke@435: duke@435: // Top may have been reset, and in fact may be below bottom, duke@435: // e.g. the dirty card region is entirely in a now free object duke@435: // -- something that could happen with a concurrent sweeper. duke@435: bottom = MIN2(bottom, top); duke@435: mr = MemRegion(bottom, top); duke@435: assert(bottom <= top && duke@435: (_precision != CardTableModRefBS::ObjHeadPreciseArray || duke@435: _min_done == NULL || duke@435: top <= _min_done), duke@435: "overlap!"); duke@435: duke@435: // Walk the region if it is not empty; otherwise there is nothing to do. duke@435: if (!mr.is_empty()) { duke@435: walk_mem_region(mr, bottom_obj, top); duke@435: } duke@435: ysr@777: // An idempotent closure might be applied in any order, so we don't ysr@777: // record a _min_done for it. ysr@777: if (!_cl->idempotent()) { ysr@777: _min_done = bottom; ysr@777: } else { ysr@777: assert(_min_done == _last_explicit_min_done, ysr@777: "Don't update _min_done for idempotent cl"); ysr@777: } duke@435: } duke@435: duke@435: DirtyCardToOopClosure* Space::new_dcto_cl(OopClosure* cl, duke@435: CardTableModRefBS::PrecisionStyle precision, duke@435: HeapWord* boundary) { duke@435: return new DirtyCardToOopClosure(this, cl, precision, boundary); duke@435: } duke@435: duke@435: HeapWord* ContiguousSpaceDCTOC::get_actual_top(HeapWord* top, duke@435: HeapWord* top_obj) { duke@435: if (top_obj != NULL && top_obj < (_sp->toContiguousSpace())->top()) { duke@435: if (_precision == CardTableModRefBS::ObjHeadPreciseArray) { duke@435: if (oop(top_obj)->is_objArray() || oop(top_obj)->is_typeArray()) { duke@435: // An arrayOop is starting on the dirty card - since we do exact duke@435: // store checks for objArrays we are done. duke@435: } else { duke@435: // Otherwise, it is possible that the object starting on the dirty duke@435: // card spans the entire card, and that the store happened on a duke@435: // later card. Figure out where the object ends. duke@435: assert(_sp->block_size(top_obj) == (size_t) oop(top_obj)->size(), duke@435: "Block size and object size mismatch"); duke@435: top = top_obj + oop(top_obj)->size(); duke@435: } duke@435: } duke@435: } else { duke@435: top = (_sp->toContiguousSpace())->top(); duke@435: } duke@435: return top; duke@435: } duke@435: duke@435: void Filtering_DCTOC::walk_mem_region(MemRegion mr, duke@435: HeapWord* bottom, duke@435: HeapWord* top) { duke@435: // Note that this assumption won't hold if we have a concurrent duke@435: // collector in this space, which may have freed up objects after duke@435: // they were dirtied and before the stop-the-world GC that is duke@435: // examining cards here. duke@435: assert(bottom < top, "ought to be at least one obj on a dirty card."); duke@435: duke@435: if (_boundary != NULL) { duke@435: // We have a boundary outside of which we don't want to look duke@435: // at objects, so create a filtering closure around the duke@435: // oop closure before walking the region. duke@435: FilteringClosure filter(_boundary, _cl); duke@435: walk_mem_region_with_cl(mr, bottom, top, &filter); duke@435: } else { duke@435: // No boundary, simply walk the heap with the oop closure. duke@435: walk_mem_region_with_cl(mr, bottom, top, _cl); duke@435: } duke@435: duke@435: } duke@435: duke@435: // We must replicate this so that the static type of "FilteringClosure" duke@435: // (see above) is apparent at the oop_iterate calls. duke@435: #define ContiguousSpaceDCTOC__walk_mem_region_with_cl_DEFN(ClosureType) \ duke@435: void ContiguousSpaceDCTOC::walk_mem_region_with_cl(MemRegion mr, \ duke@435: HeapWord* bottom, \ duke@435: HeapWord* top, \ duke@435: ClosureType* cl) { \ duke@435: bottom += oop(bottom)->oop_iterate(cl, mr); \ duke@435: if (bottom < top) { \ duke@435: HeapWord* next_obj = bottom + oop(bottom)->size(); \ duke@435: while (next_obj < top) { \ duke@435: /* Bottom lies entirely below top, so we can call the */ \ duke@435: /* non-memRegion version of oop_iterate below. */ \ duke@435: oop(bottom)->oop_iterate(cl); \ duke@435: bottom = next_obj; \ duke@435: next_obj = bottom + oop(bottom)->size(); \ duke@435: } \ duke@435: /* Last object. */ \ duke@435: oop(bottom)->oop_iterate(cl, mr); \ duke@435: } \ duke@435: } duke@435: duke@435: // (There are only two of these, rather than N, because the split is due duke@435: // only to the introduction of the FilteringClosure, a local part of the duke@435: // impl of this abstraction.) duke@435: ContiguousSpaceDCTOC__walk_mem_region_with_cl_DEFN(OopClosure) duke@435: ContiguousSpaceDCTOC__walk_mem_region_with_cl_DEFN(FilteringClosure) duke@435: duke@435: DirtyCardToOopClosure* duke@435: ContiguousSpace::new_dcto_cl(OopClosure* cl, duke@435: CardTableModRefBS::PrecisionStyle precision, duke@435: HeapWord* boundary) { duke@435: return new ContiguousSpaceDCTOC(this, cl, precision, boundary); duke@435: } duke@435: jmasa@698: void Space::initialize(MemRegion mr, jmasa@698: bool clear_space, jmasa@698: bool mangle_space) { duke@435: HeapWord* bottom = mr.start(); duke@435: HeapWord* end = mr.end(); duke@435: assert(Universe::on_page_boundary(bottom) && Universe::on_page_boundary(end), duke@435: "invalid space boundaries"); duke@435: set_bottom(bottom); duke@435: set_end(end); jmasa@698: if (clear_space) clear(mangle_space); duke@435: } duke@435: jmasa@698: void Space::clear(bool mangle_space) { jmasa@698: if (ZapUnusedHeapArea && mangle_space) { jmasa@698: mangle_unused_area(); jmasa@698: } duke@435: } duke@435: tonyp@791: ContiguousSpace::ContiguousSpace(): CompactibleSpace(), _top(NULL), tonyp@791: _concurrent_iteration_safe_limit(NULL) { jmasa@698: _mangler = new GenSpaceMangler(this); jmasa@698: } jmasa@698: jmasa@698: ContiguousSpace::~ContiguousSpace() { jmasa@698: delete _mangler; jmasa@698: } jmasa@698: jmasa@698: void ContiguousSpace::initialize(MemRegion mr, jmasa@698: bool clear_space, jmasa@698: bool mangle_space) duke@435: { jmasa@698: CompactibleSpace::initialize(mr, clear_space, mangle_space); ysr@782: set_concurrent_iteration_safe_limit(top()); duke@435: } duke@435: jmasa@698: void ContiguousSpace::clear(bool mangle_space) { duke@435: set_top(bottom()); duke@435: set_saved_mark(); tonyp@791: CompactibleSpace::clear(mangle_space); duke@435: } duke@435: duke@435: bool Space::is_in(const void* p) const { ysr@777: HeapWord* b = block_start_const(p); duke@435: return b != NULL && block_is_obj(b); duke@435: } duke@435: duke@435: bool ContiguousSpace::is_in(const void* p) const { duke@435: return _bottom <= p && p < _top; duke@435: } duke@435: duke@435: bool ContiguousSpace::is_free_block(const HeapWord* p) const { duke@435: return p >= _top; duke@435: } duke@435: jmasa@698: void OffsetTableContigSpace::clear(bool mangle_space) { jmasa@698: ContiguousSpace::clear(mangle_space); duke@435: _offsets.initialize_threshold(); duke@435: } duke@435: duke@435: void OffsetTableContigSpace::set_bottom(HeapWord* new_bottom) { duke@435: Space::set_bottom(new_bottom); duke@435: _offsets.set_bottom(new_bottom); duke@435: } duke@435: duke@435: void OffsetTableContigSpace::set_end(HeapWord* new_end) { duke@435: // Space should not advertize an increase in size duke@435: // until after the underlying offest table has been enlarged. duke@435: _offsets.resize(pointer_delta(new_end, bottom())); duke@435: Space::set_end(new_end); duke@435: } duke@435: jmasa@698: #ifndef PRODUCT jmasa@698: jmasa@698: void ContiguousSpace::set_top_for_allocations(HeapWord* v) { jmasa@698: mangler()->set_top_for_allocations(v); jmasa@698: } jmasa@698: void ContiguousSpace::set_top_for_allocations() { jmasa@698: mangler()->set_top_for_allocations(top()); jmasa@698: } jmasa@698: void ContiguousSpace::check_mangled_unused_area(HeapWord* limit) { jmasa@698: mangler()->check_mangled_unused_area(limit); duke@435: } duke@435: jmasa@698: void ContiguousSpace::check_mangled_unused_area_complete() { jmasa@698: mangler()->check_mangled_unused_area_complete(); duke@435: } duke@435: jmasa@698: // Mangled only the unused space that has not previously jmasa@698: // been mangled and that has not been allocated since being jmasa@698: // mangled. jmasa@698: void ContiguousSpace::mangle_unused_area() { jmasa@698: mangler()->mangle_unused_area(); jmasa@698: } jmasa@698: void ContiguousSpace::mangle_unused_area_complete() { jmasa@698: mangler()->mangle_unused_area_complete(); jmasa@698: } jmasa@698: void ContiguousSpace::mangle_region(MemRegion mr) { jmasa@698: // Although this method uses SpaceMangler::mangle_region() which jmasa@698: // is not specific to a space, the when the ContiguousSpace version jmasa@698: // is called, it is always with regard to a space and this jmasa@698: // bounds checking is appropriate. jmasa@698: MemRegion space_mr(bottom(), end()); jmasa@698: assert(space_mr.contains(mr), "Mangling outside space"); jmasa@698: SpaceMangler::mangle_region(mr); jmasa@698: } jmasa@698: #endif // NOT_PRODUCT jmasa@698: jmasa@698: void CompactibleSpace::initialize(MemRegion mr, jmasa@698: bool clear_space, jmasa@698: bool mangle_space) { jmasa@698: Space::initialize(mr, clear_space, mangle_space); tonyp@791: set_compaction_top(bottom()); tonyp@791: _next_compaction_space = NULL; tonyp@791: } tonyp@791: tonyp@791: void CompactibleSpace::clear(bool mangle_space) { tonyp@791: Space::clear(mangle_space); duke@435: _compaction_top = bottom(); duke@435: } duke@435: duke@435: HeapWord* CompactibleSpace::forward(oop q, size_t size, duke@435: CompactPoint* cp, HeapWord* compact_top) { duke@435: // q is alive duke@435: // First check if we should switch compaction space duke@435: assert(this == cp->space, "'this' should be current compaction space."); duke@435: size_t compaction_max_size = pointer_delta(end(), compact_top); duke@435: while (size > compaction_max_size) { duke@435: // switch to next compaction space duke@435: cp->space->set_compaction_top(compact_top); duke@435: cp->space = cp->space->next_compaction_space(); duke@435: if (cp->space == NULL) { duke@435: cp->gen = GenCollectedHeap::heap()->prev_gen(cp->gen); duke@435: assert(cp->gen != NULL, "compaction must succeed"); duke@435: cp->space = cp->gen->first_compaction_space(); duke@435: assert(cp->space != NULL, "generation must have a first compaction space"); duke@435: } duke@435: compact_top = cp->space->bottom(); duke@435: cp->space->set_compaction_top(compact_top); duke@435: cp->threshold = cp->space->initialize_threshold(); duke@435: compaction_max_size = pointer_delta(cp->space->end(), compact_top); duke@435: } duke@435: duke@435: // store the forwarding pointer into the mark word duke@435: if ((HeapWord*)q != compact_top) { duke@435: q->forward_to(oop(compact_top)); duke@435: assert(q->is_gc_marked(), "encoding the pointer should preserve the mark"); duke@435: } else { duke@435: // if the object isn't moving we can just set the mark to the default duke@435: // mark and handle it specially later on. duke@435: q->init_mark(); duke@435: assert(q->forwardee() == NULL, "should be forwarded to NULL"); duke@435: } duke@435: coleenp@548: VALIDATE_MARK_SWEEP_ONLY(MarkSweep::register_live_oop(q, size)); duke@435: compact_top += size; duke@435: duke@435: // we need to update the offset table so that the beginnings of objects can be duke@435: // found during scavenge. Note that we are updating the offset table based on duke@435: // where the object will be once the compaction phase finishes. duke@435: if (compact_top > cp->threshold) duke@435: cp->threshold = duke@435: cp->space->cross_threshold(compact_top - size, compact_top); duke@435: return compact_top; duke@435: } duke@435: duke@435: duke@435: bool CompactibleSpace::insert_deadspace(size_t& allowed_deadspace_words, duke@435: HeapWord* q, size_t deadlength) { duke@435: if (allowed_deadspace_words >= deadlength) { duke@435: allowed_deadspace_words -= deadlength; jcoomes@916: CollectedHeap::fill_with_object(q, deadlength); jcoomes@916: oop(q)->set_mark(oop(q)->mark()->set_marked()); jcoomes@916: assert((int) deadlength == oop(q)->size(), "bad filler object size"); duke@435: // Recall that we required "q == compaction_top". duke@435: return true; duke@435: } else { duke@435: allowed_deadspace_words = 0; duke@435: return false; duke@435: } duke@435: } duke@435: duke@435: #define block_is_always_obj(q) true duke@435: #define obj_size(q) oop(q)->size() duke@435: #define adjust_obj_size(s) s duke@435: duke@435: void CompactibleSpace::prepare_for_compaction(CompactPoint* cp) { duke@435: SCAN_AND_FORWARD(cp, end, block_is_obj, block_size); duke@435: } duke@435: duke@435: // Faster object search. duke@435: void ContiguousSpace::prepare_for_compaction(CompactPoint* cp) { duke@435: SCAN_AND_FORWARD(cp, top, block_is_always_obj, obj_size); duke@435: } duke@435: duke@435: void Space::adjust_pointers() { duke@435: // adjust all the interior pointers to point at the new locations of objects duke@435: // Used by MarkSweep::mark_sweep_phase3() duke@435: duke@435: // First check to see if there is any work to be done. duke@435: if (used() == 0) { duke@435: return; // Nothing to do. duke@435: } duke@435: duke@435: // Otherwise... duke@435: HeapWord* q = bottom(); duke@435: HeapWord* t = end(); duke@435: duke@435: debug_only(HeapWord* prev_q = NULL); duke@435: while (q < t) { duke@435: if (oop(q)->is_gc_marked()) { duke@435: // q is alive duke@435: coleenp@548: VALIDATE_MARK_SWEEP_ONLY(MarkSweep::track_interior_pointers(oop(q))); duke@435: // point all the oops to the new location duke@435: size_t size = oop(q)->adjust_pointers(); coleenp@548: VALIDATE_MARK_SWEEP_ONLY(MarkSweep::check_interior_pointers()); duke@435: duke@435: debug_only(prev_q = q); coleenp@548: VALIDATE_MARK_SWEEP_ONLY(MarkSweep::validate_live_oop(oop(q), size)); duke@435: duke@435: q += size; duke@435: } else { duke@435: // q is not a live object. But we're not in a compactible space, duke@435: // So we don't have live ranges. duke@435: debug_only(prev_q = q); duke@435: q += block_size(q); duke@435: assert(q > prev_q, "we should be moving forward through memory"); duke@435: } duke@435: } duke@435: assert(q == t, "just checking"); duke@435: } duke@435: duke@435: void CompactibleSpace::adjust_pointers() { duke@435: // Check first is there is any work to do. duke@435: if (used() == 0) { duke@435: return; // Nothing to do. duke@435: } duke@435: duke@435: SCAN_AND_ADJUST_POINTERS(adjust_obj_size); duke@435: } duke@435: duke@435: void CompactibleSpace::compact() { duke@435: SCAN_AND_COMPACT(obj_size); duke@435: } duke@435: duke@435: void Space::print_short() const { print_short_on(tty); } duke@435: duke@435: void Space::print_short_on(outputStream* st) const { duke@435: st->print(" space " SIZE_FORMAT "K, %3d%% used", capacity() / K, duke@435: (int) ((double) used() * 100 / capacity())); duke@435: } duke@435: duke@435: void Space::print() const { print_on(tty); } duke@435: duke@435: void Space::print_on(outputStream* st) const { duke@435: print_short_on(st); duke@435: st->print_cr(" [" INTPTR_FORMAT ", " INTPTR_FORMAT ")", duke@435: bottom(), end()); duke@435: } duke@435: duke@435: void ContiguousSpace::print_on(outputStream* st) const { duke@435: print_short_on(st); duke@435: st->print_cr(" [" INTPTR_FORMAT ", " INTPTR_FORMAT ", " INTPTR_FORMAT ")", duke@435: bottom(), top(), end()); duke@435: } duke@435: duke@435: void OffsetTableContigSpace::print_on(outputStream* st) const { duke@435: print_short_on(st); duke@435: st->print_cr(" [" INTPTR_FORMAT ", " INTPTR_FORMAT ", " duke@435: INTPTR_FORMAT ", " INTPTR_FORMAT ")", duke@435: bottom(), top(), _offsets.threshold(), end()); duke@435: } duke@435: duke@435: void ContiguousSpace::verify(bool allow_dirty) const { duke@435: HeapWord* p = bottom(); duke@435: HeapWord* t = top(); duke@435: HeapWord* prev_p = NULL; duke@435: while (p < t) { duke@435: oop(p)->verify(); duke@435: prev_p = p; duke@435: p += oop(p)->size(); duke@435: } duke@435: guarantee(p == top(), "end of last object must match end of space"); duke@435: if (top() != end()) { ysr@777: guarantee(top() == block_start_const(end()-1) && ysr@777: top() == block_start_const(top()), duke@435: "top should be start of unallocated block, if it exists"); duke@435: } duke@435: } duke@435: duke@435: void Space::oop_iterate(OopClosure* blk) { duke@435: ObjectToOopClosure blk2(blk); duke@435: object_iterate(&blk2); duke@435: } duke@435: duke@435: HeapWord* Space::object_iterate_careful(ObjectClosureCareful* cl) { duke@435: guarantee(false, "NYI"); duke@435: return bottom(); duke@435: } duke@435: duke@435: HeapWord* Space::object_iterate_careful_m(MemRegion mr, duke@435: ObjectClosureCareful* cl) { duke@435: guarantee(false, "NYI"); duke@435: return bottom(); duke@435: } duke@435: duke@435: duke@435: void Space::object_iterate_mem(MemRegion mr, UpwardsObjectClosure* cl) { duke@435: assert(!mr.is_empty(), "Should be non-empty"); duke@435: // We use MemRegion(bottom(), end()) rather than used_region() below duke@435: // because the two are not necessarily equal for some kinds of duke@435: // spaces, in particular, certain kinds of free list spaces. duke@435: // We could use the more complicated but more precise: duke@435: // MemRegion(used_region().start(), round_to(used_region().end(), CardSize)) duke@435: // but the slight imprecision seems acceptable in the assertion check. duke@435: assert(MemRegion(bottom(), end()).contains(mr), duke@435: "Should be within used space"); duke@435: HeapWord* prev = cl->previous(); // max address from last time duke@435: if (prev >= mr.end()) { // nothing to do duke@435: return; duke@435: } duke@435: // This assert will not work when we go from cms space to perm duke@435: // space, and use same closure. Easy fix deferred for later. XXX YSR duke@435: // assert(prev == NULL || contains(prev), "Should be within space"); duke@435: duke@435: bool last_was_obj_array = false; duke@435: HeapWord *blk_start_addr, *region_start_addr; duke@435: if (prev > mr.start()) { duke@435: region_start_addr = prev; duke@435: blk_start_addr = prev; jmasa@953: // The previous invocation may have pushed "prev" beyond the jmasa@953: // last allocated block yet there may be still be blocks jmasa@953: // in this region due to a particular coalescing policy. jmasa@953: // Relax the assertion so that the case where the unallocated jmasa@953: // block is maintained and "prev" is beyond the unallocated jmasa@953: // block does not cause the assertion to fire. jmasa@953: assert((BlockOffsetArrayUseUnallocatedBlock && jmasa@953: (!is_in(prev))) || jmasa@953: (blk_start_addr == block_start(region_start_addr)), "invariant"); duke@435: } else { duke@435: region_start_addr = mr.start(); duke@435: blk_start_addr = block_start(region_start_addr); duke@435: } duke@435: HeapWord* region_end_addr = mr.end(); duke@435: MemRegion derived_mr(region_start_addr, region_end_addr); duke@435: while (blk_start_addr < region_end_addr) { duke@435: const size_t size = block_size(blk_start_addr); duke@435: if (block_is_obj(blk_start_addr)) { duke@435: last_was_obj_array = cl->do_object_bm(oop(blk_start_addr), derived_mr); duke@435: } else { duke@435: last_was_obj_array = false; duke@435: } duke@435: blk_start_addr += size; duke@435: } duke@435: if (!last_was_obj_array) { duke@435: assert((bottom() <= blk_start_addr) && (blk_start_addr <= end()), duke@435: "Should be within (closed) used space"); duke@435: assert(blk_start_addr > prev, "Invariant"); duke@435: cl->set_previous(blk_start_addr); // min address for next time duke@435: } duke@435: } duke@435: duke@435: bool Space::obj_is_alive(const HeapWord* p) const { duke@435: assert (block_is_obj(p), "The address should point to an object"); duke@435: return true; duke@435: } duke@435: duke@435: void ContiguousSpace::object_iterate_mem(MemRegion mr, UpwardsObjectClosure* cl) { duke@435: assert(!mr.is_empty(), "Should be non-empty"); duke@435: assert(used_region().contains(mr), "Should be within used space"); duke@435: HeapWord* prev = cl->previous(); // max address from last time duke@435: if (prev >= mr.end()) { // nothing to do duke@435: return; duke@435: } duke@435: // See comment above (in more general method above) in case you duke@435: // happen to use this method. duke@435: assert(prev == NULL || is_in_reserved(prev), "Should be within space"); duke@435: duke@435: bool last_was_obj_array = false; duke@435: HeapWord *obj_start_addr, *region_start_addr; duke@435: if (prev > mr.start()) { duke@435: region_start_addr = prev; duke@435: obj_start_addr = prev; duke@435: assert(obj_start_addr == block_start(region_start_addr), "invariant"); duke@435: } else { duke@435: region_start_addr = mr.start(); duke@435: obj_start_addr = block_start(region_start_addr); duke@435: } duke@435: HeapWord* region_end_addr = mr.end(); duke@435: MemRegion derived_mr(region_start_addr, region_end_addr); duke@435: while (obj_start_addr < region_end_addr) { duke@435: oop obj = oop(obj_start_addr); duke@435: const size_t size = obj->size(); duke@435: last_was_obj_array = cl->do_object_bm(obj, derived_mr); duke@435: obj_start_addr += size; duke@435: } duke@435: if (!last_was_obj_array) { duke@435: assert((bottom() <= obj_start_addr) && (obj_start_addr <= end()), duke@435: "Should be within (closed) used space"); duke@435: assert(obj_start_addr > prev, "Invariant"); duke@435: cl->set_previous(obj_start_addr); // min address for next time duke@435: } duke@435: } duke@435: duke@435: #ifndef SERIALGC duke@435: #define ContigSpace_PAR_OOP_ITERATE_DEFN(OopClosureType, nv_suffix) \ duke@435: \ duke@435: void ContiguousSpace::par_oop_iterate(MemRegion mr, OopClosureType* blk) {\ duke@435: HeapWord* obj_addr = mr.start(); \ duke@435: HeapWord* t = mr.end(); \ duke@435: while (obj_addr < t) { \ duke@435: assert(oop(obj_addr)->is_oop(), "Should be an oop"); \ duke@435: obj_addr += oop(obj_addr)->oop_iterate(blk); \ duke@435: } \ duke@435: } duke@435: duke@435: ALL_PAR_OOP_ITERATE_CLOSURES(ContigSpace_PAR_OOP_ITERATE_DEFN) duke@435: duke@435: #undef ContigSpace_PAR_OOP_ITERATE_DEFN duke@435: #endif // SERIALGC duke@435: duke@435: void ContiguousSpace::oop_iterate(OopClosure* blk) { duke@435: if (is_empty()) return; duke@435: HeapWord* obj_addr = bottom(); duke@435: HeapWord* t = top(); duke@435: // Could call objects iterate, but this is easier. duke@435: while (obj_addr < t) { duke@435: obj_addr += oop(obj_addr)->oop_iterate(blk); duke@435: } duke@435: } duke@435: duke@435: void ContiguousSpace::oop_iterate(MemRegion mr, OopClosure* blk) { duke@435: if (is_empty()) { duke@435: return; duke@435: } duke@435: MemRegion cur = MemRegion(bottom(), top()); duke@435: mr = mr.intersection(cur); duke@435: if (mr.is_empty()) { duke@435: return; duke@435: } duke@435: if (mr.equals(cur)) { duke@435: oop_iterate(blk); duke@435: return; duke@435: } duke@435: assert(mr.end() <= top(), "just took an intersection above"); duke@435: HeapWord* obj_addr = block_start(mr.start()); duke@435: HeapWord* t = mr.end(); duke@435: duke@435: // Handle first object specially. duke@435: oop obj = oop(obj_addr); duke@435: SpaceMemRegionOopsIterClosure smr_blk(blk, mr); duke@435: obj_addr += obj->oop_iterate(&smr_blk); duke@435: while (obj_addr < t) { duke@435: oop obj = oop(obj_addr); duke@435: assert(obj->is_oop(), "expected an oop"); duke@435: obj_addr += obj->size(); duke@435: // If "obj_addr" is not greater than top, then the duke@435: // entire object "obj" is within the region. duke@435: if (obj_addr <= t) { duke@435: obj->oop_iterate(blk); duke@435: } else { duke@435: // "obj" extends beyond end of region duke@435: obj->oop_iterate(&smr_blk); duke@435: break; duke@435: } duke@435: }; duke@435: } duke@435: duke@435: void ContiguousSpace::object_iterate(ObjectClosure* blk) { duke@435: if (is_empty()) return; duke@435: WaterMark bm = bottom_mark(); duke@435: object_iterate_from(bm, blk); duke@435: } duke@435: jmasa@952: // For a continguous space object_iterate() and safe_object_iterate() jmasa@952: // are the same. jmasa@952: void ContiguousSpace::safe_object_iterate(ObjectClosure* blk) { jmasa@952: object_iterate(blk); jmasa@952: } jmasa@952: duke@435: void ContiguousSpace::object_iterate_from(WaterMark mark, ObjectClosure* blk) { duke@435: assert(mark.space() == this, "Mark does not match space"); duke@435: HeapWord* p = mark.point(); duke@435: while (p < top()) { duke@435: blk->do_object(oop(p)); duke@435: p += oop(p)->size(); duke@435: } duke@435: } duke@435: duke@435: HeapWord* duke@435: ContiguousSpace::object_iterate_careful(ObjectClosureCareful* blk) { duke@435: HeapWord * limit = concurrent_iteration_safe_limit(); duke@435: assert(limit <= top(), "sanity check"); duke@435: for (HeapWord* p = bottom(); p < limit;) { duke@435: size_t size = blk->do_object_careful(oop(p)); duke@435: if (size == 0) { duke@435: return p; // failed at p duke@435: } else { duke@435: p += size; duke@435: } duke@435: } duke@435: return NULL; // all done duke@435: } duke@435: duke@435: #define ContigSpace_OOP_SINCE_SAVE_MARKS_DEFN(OopClosureType, nv_suffix) \ duke@435: \ duke@435: void ContiguousSpace:: \ duke@435: oop_since_save_marks_iterate##nv_suffix(OopClosureType* blk) { \ duke@435: HeapWord* t; \ duke@435: HeapWord* p = saved_mark_word(); \ duke@435: assert(p != NULL, "expected saved mark"); \ duke@435: \ duke@435: const intx interval = PrefetchScanIntervalInBytes; \ duke@435: do { \ duke@435: t = top(); \ duke@435: while (p < t) { \ duke@435: Prefetch::write(p, interval); \ duke@435: debug_only(HeapWord* prev = p); \ duke@435: oop m = oop(p); \ duke@435: p += m->oop_iterate(blk); \ duke@435: } \ duke@435: } while (t < top()); \ duke@435: \ duke@435: set_saved_mark_word(p); \ duke@435: } duke@435: duke@435: ALL_SINCE_SAVE_MARKS_CLOSURES(ContigSpace_OOP_SINCE_SAVE_MARKS_DEFN) duke@435: duke@435: #undef ContigSpace_OOP_SINCE_SAVE_MARKS_DEFN duke@435: duke@435: // Very general, slow implementation. ysr@777: HeapWord* ContiguousSpace::block_start_const(const void* p) const { duke@435: assert(MemRegion(bottom(), end()).contains(p), "p not in space"); duke@435: if (p >= top()) { duke@435: return top(); duke@435: } else { duke@435: HeapWord* last = bottom(); duke@435: HeapWord* cur = last; duke@435: while (cur <= p) { duke@435: last = cur; duke@435: cur += oop(cur)->size(); duke@435: } duke@435: assert(oop(last)->is_oop(), "Should be an object start"); duke@435: return last; duke@435: } duke@435: } duke@435: duke@435: size_t ContiguousSpace::block_size(const HeapWord* p) const { duke@435: assert(MemRegion(bottom(), end()).contains(p), "p not in space"); duke@435: HeapWord* current_top = top(); duke@435: assert(p <= current_top, "p is not a block start"); duke@435: assert(p == current_top || oop(p)->is_oop(), "p is not a block start"); duke@435: if (p < current_top) duke@435: return oop(p)->size(); duke@435: else { duke@435: assert(p == current_top, "just checking"); duke@435: return pointer_delta(end(), (HeapWord*) p); duke@435: } duke@435: } duke@435: duke@435: // This version requires locking. duke@435: inline HeapWord* ContiguousSpace::allocate_impl(size_t size, duke@435: HeapWord* const end_value) { duke@435: assert(Heap_lock->owned_by_self() || duke@435: (SafepointSynchronize::is_at_safepoint() && duke@435: Thread::current()->is_VM_thread()), duke@435: "not locked"); duke@435: HeapWord* obj = top(); duke@435: if (pointer_delta(end_value, obj) >= size) { duke@435: HeapWord* new_top = obj + size; duke@435: set_top(new_top); duke@435: assert(is_aligned(obj) && is_aligned(new_top), "checking alignment"); duke@435: return obj; duke@435: } else { duke@435: return NULL; duke@435: } duke@435: } duke@435: duke@435: // This version is lock-free. duke@435: inline HeapWord* ContiguousSpace::par_allocate_impl(size_t size, duke@435: HeapWord* const end_value) { duke@435: do { duke@435: HeapWord* obj = top(); duke@435: if (pointer_delta(end_value, obj) >= size) { duke@435: HeapWord* new_top = obj + size; duke@435: HeapWord* result = (HeapWord*)Atomic::cmpxchg_ptr(new_top, top_addr(), obj); duke@435: // result can be one of two: duke@435: // the old top value: the exchange succeeded duke@435: // otherwise: the new value of the top is returned. duke@435: if (result == obj) { duke@435: assert(is_aligned(obj) && is_aligned(new_top), "checking alignment"); duke@435: return obj; duke@435: } duke@435: } else { duke@435: return NULL; duke@435: } duke@435: } while (true); duke@435: } duke@435: duke@435: // Requires locking. duke@435: HeapWord* ContiguousSpace::allocate(size_t size) { duke@435: return allocate_impl(size, end()); duke@435: } duke@435: duke@435: // Lock-free. duke@435: HeapWord* ContiguousSpace::par_allocate(size_t size) { duke@435: return par_allocate_impl(size, end()); duke@435: } duke@435: duke@435: void ContiguousSpace::allocate_temporary_filler(int factor) { duke@435: // allocate temporary type array decreasing free size with factor 'factor' duke@435: assert(factor >= 0, "just checking"); duke@435: size_t size = pointer_delta(end(), top()); duke@435: duke@435: // if space is full, return duke@435: if (size == 0) return; duke@435: duke@435: if (factor > 0) { duke@435: size -= size/factor; duke@435: } duke@435: size = align_object_size(size); duke@435: duke@435: const size_t min_int_array_size = typeArrayOopDesc::header_size(T_INT); duke@435: if (size >= min_int_array_size) { duke@435: size_t length = (size - min_int_array_size) * (HeapWordSize / sizeof(jint)); duke@435: // allocate uninitialized int array duke@435: typeArrayOop t = (typeArrayOop) allocate(size); duke@435: assert(t != NULL, "allocation should succeed"); duke@435: t->set_mark(markOopDesc::prototype()); duke@435: t->set_klass(Universe::intArrayKlassObj()); duke@435: t->set_length((int)length); duke@435: } else { duke@435: assert((int) size == instanceOopDesc::header_size(), duke@435: "size for smallest fake object doesn't match"); duke@435: instanceOop obj = (instanceOop) allocate(size); duke@435: obj->set_mark(markOopDesc::prototype()); coleenp@602: obj->set_klass_gap(0); duke@435: obj->set_klass(SystemDictionary::object_klass()); duke@435: } duke@435: } duke@435: jmasa@698: void EdenSpace::clear(bool mangle_space) { jmasa@698: ContiguousSpace::clear(mangle_space); duke@435: set_soft_end(end()); duke@435: } duke@435: duke@435: // Requires locking. duke@435: HeapWord* EdenSpace::allocate(size_t size) { duke@435: return allocate_impl(size, soft_end()); duke@435: } duke@435: duke@435: // Lock-free. duke@435: HeapWord* EdenSpace::par_allocate(size_t size) { duke@435: return par_allocate_impl(size, soft_end()); duke@435: } duke@435: duke@435: HeapWord* ConcEdenSpace::par_allocate(size_t size) duke@435: { duke@435: do { duke@435: // The invariant is top() should be read before end() because duke@435: // top() can't be greater than end(), so if an update of _soft_end duke@435: // occurs between 'end_val = end();' and 'top_val = top();' top() duke@435: // also can grow up to the new end() and the condition duke@435: // 'top_val > end_val' is true. To ensure the loading order duke@435: // OrderAccess::loadload() is required after top() read. duke@435: HeapWord* obj = top(); duke@435: OrderAccess::loadload(); duke@435: if (pointer_delta(*soft_end_addr(), obj) >= size) { duke@435: HeapWord* new_top = obj + size; duke@435: HeapWord* result = (HeapWord*)Atomic::cmpxchg_ptr(new_top, top_addr(), obj); duke@435: // result can be one of two: duke@435: // the old top value: the exchange succeeded duke@435: // otherwise: the new value of the top is returned. duke@435: if (result == obj) { duke@435: assert(is_aligned(obj) && is_aligned(new_top), "checking alignment"); duke@435: return obj; duke@435: } duke@435: } else { duke@435: return NULL; duke@435: } duke@435: } while (true); duke@435: } duke@435: duke@435: duke@435: HeapWord* OffsetTableContigSpace::initialize_threshold() { duke@435: return _offsets.initialize_threshold(); duke@435: } duke@435: duke@435: HeapWord* OffsetTableContigSpace::cross_threshold(HeapWord* start, HeapWord* end) { duke@435: _offsets.alloc_block(start, end); duke@435: return _offsets.threshold(); duke@435: } duke@435: duke@435: OffsetTableContigSpace::OffsetTableContigSpace(BlockOffsetSharedArray* sharedOffsetArray, duke@435: MemRegion mr) : duke@435: _offsets(sharedOffsetArray, mr), duke@435: _par_alloc_lock(Mutex::leaf, "OffsetTableContigSpace par alloc lock", true) duke@435: { duke@435: _offsets.set_contig_space(this); jmasa@698: initialize(mr, SpaceDecorator::Clear, SpaceDecorator::Mangle); duke@435: } duke@435: duke@435: duke@435: class VerifyOldOopClosure : public OopClosure { duke@435: public: coleenp@548: oop _the_obj; coleenp@548: bool _allow_dirty; duke@435: void do_oop(oop* p) { coleenp@548: _the_obj->verify_old_oop(p, _allow_dirty); coleenp@548: } coleenp@548: void do_oop(narrowOop* p) { coleenp@548: _the_obj->verify_old_oop(p, _allow_dirty); duke@435: } duke@435: }; duke@435: duke@435: #define OBJ_SAMPLE_INTERVAL 0 duke@435: #define BLOCK_SAMPLE_INTERVAL 100 duke@435: duke@435: void OffsetTableContigSpace::verify(bool allow_dirty) const { duke@435: HeapWord* p = bottom(); duke@435: HeapWord* prev_p = NULL; duke@435: VerifyOldOopClosure blk; // Does this do anything? coleenp@548: blk._allow_dirty = allow_dirty; duke@435: int objs = 0; duke@435: int blocks = 0; duke@435: duke@435: if (VerifyObjectStartArray) { duke@435: _offsets.verify(); duke@435: } duke@435: duke@435: while (p < top()) { duke@435: size_t size = oop(p)->size(); duke@435: // For a sampling of objects in the space, find it using the duke@435: // block offset table. duke@435: if (blocks == BLOCK_SAMPLE_INTERVAL) { ysr@777: guarantee(p == block_start_const(p + (size/2)), ysr@777: "check offset computation"); duke@435: blocks = 0; duke@435: } else { duke@435: blocks++; duke@435: } duke@435: duke@435: if (objs == OBJ_SAMPLE_INTERVAL) { duke@435: oop(p)->verify(); coleenp@548: blk._the_obj = oop(p); duke@435: oop(p)->oop_iterate(&blk); duke@435: objs = 0; duke@435: } else { duke@435: objs++; duke@435: } duke@435: prev_p = p; duke@435: p += size; duke@435: } duke@435: guarantee(p == top(), "end of last object must match end of space"); duke@435: } duke@435: duke@435: void OffsetTableContigSpace::serialize_block_offset_array_offsets( duke@435: SerializeOopClosure* soc) { duke@435: _offsets.serialize(soc); duke@435: } duke@435: duke@435: jcoomes@873: size_t TenuredSpace::allowed_dead_ratio() const { duke@435: return MarkSweepDeadRatio; duke@435: } duke@435: duke@435: jcoomes@873: size_t ContigPermSpace::allowed_dead_ratio() const { duke@435: return PermMarkSweepDeadRatio; duke@435: }