duke@435: /* duke@435: * Copyright 2001-2007 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/_mutableSpace.cpp.incl" duke@435: jmasa@698: MutableSpace::MutableSpace(): ImmutableSpace(), _top(NULL) { jmasa@698: _mangler = new MutableSpaceMangler(this); jmasa@698: } jmasa@698: jmasa@698: MutableSpace::~MutableSpace() { jmasa@698: delete _mangler; jmasa@698: } jmasa@698: jmasa@698: void MutableSpace::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: 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); duke@435: jmasa@698: if (clear_space) { jmasa@698: clear(mangle_space); jmasa@698: } duke@435: } duke@435: jmasa@698: void MutableSpace::clear(bool mangle_space) { duke@435: set_top(bottom()); jmasa@698: if (ZapUnusedHeapArea && mangle_space) { jmasa@698: mangle_unused_area(); jmasa@698: } duke@435: } duke@435: jmasa@698: #ifndef PRODUCT jmasa@698: void MutableSpace::check_mangled_unused_area(HeapWord* limit) { jmasa@698: mangler()->check_mangled_unused_area(limit); jmasa@698: } jmasa@698: jmasa@698: void MutableSpace::check_mangled_unused_area_complete() { jmasa@698: mangler()->check_mangled_unused_area_complete(); jmasa@698: } jmasa@698: jmasa@698: // Mangle 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 MutableSpace::mangle_unused_area() { jmasa@698: mangler()->mangle_unused_area(); jmasa@698: } jmasa@698: jmasa@698: void MutableSpace::mangle_unused_area_complete() { jmasa@698: mangler()->mangle_unused_area_complete(); jmasa@698: } jmasa@698: jmasa@698: void MutableSpace::mangle_region(MemRegion mr) { jmasa@698: SpaceMangler::mangle_region(mr); jmasa@698: } jmasa@698: jmasa@698: void MutableSpace::set_top_for_allocations(HeapWord* v) { jmasa@698: mangler()->set_top_for_allocations(v); jmasa@698: } jmasa@698: jmasa@698: void MutableSpace::set_top_for_allocations() { jmasa@698: mangler()->set_top_for_allocations(top()); jmasa@698: } jmasa@698: #endif jmasa@698: duke@435: // This version requires locking. */ duke@435: HeapWord* MutableSpace::allocate(size_t size) { 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(), obj) >= size) { duke@435: HeapWord* new_top = obj + size; duke@435: set_top(new_top); duke@435: assert(is_object_aligned((intptr_t)obj) && is_object_aligned((intptr_t)new_top), duke@435: "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: HeapWord* MutableSpace::cas_allocate(size_t size) { duke@435: do { duke@435: HeapWord* obj = top(); duke@435: if (pointer_delta(end(), 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: continue; // another thread beat us to the allocation, try again duke@435: } duke@435: assert(is_object_aligned((intptr_t)obj) && is_object_aligned((intptr_t)new_top), duke@435: "checking alignment"); duke@435: return obj; duke@435: } else { duke@435: return NULL; duke@435: } duke@435: } while (true); duke@435: } duke@435: duke@435: // Try to deallocate previous allocation. Returns true upon success. duke@435: bool MutableSpace::cas_deallocate(HeapWord *obj, size_t size) { duke@435: HeapWord* expected_top = obj + size; duke@435: return (HeapWord*)Atomic::cmpxchg_ptr(obj, top_addr(), expected_top) == expected_top; duke@435: } duke@435: duke@435: void MutableSpace::oop_iterate(OopClosure* cl) { 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(cl); duke@435: } duke@435: } duke@435: duke@435: void MutableSpace::object_iterate(ObjectClosure* cl) { duke@435: HeapWord* p = bottom(); duke@435: while (p < top()) { duke@435: cl->do_object(oop(p)); duke@435: p += oop(p)->size(); duke@435: } duke@435: } duke@435: duke@435: void MutableSpace::print_short() const { print_short_on(tty); } duke@435: void MutableSpace::print_short_on( outputStream* st) const { duke@435: st->print(" space " SIZE_FORMAT "K, %d%% used", capacity_in_bytes() / K, duke@435: (int) ((double) used_in_bytes() * 100 / capacity_in_bytes())); duke@435: } duke@435: duke@435: void MutableSpace::print() const { print_on(tty); } duke@435: void MutableSpace::print_on(outputStream* st) const { duke@435: MutableSpace::print_short_on(st); duke@435: st->print_cr(" [" INTPTR_FORMAT "," INTPTR_FORMAT "," INTPTR_FORMAT ")", duke@435: bottom(), top(), end()); duke@435: } duke@435: iveresov@625: void MutableSpace::verify(bool allow_dirty) { 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: }