duke@435: /* coleenp@5614: * Copyright (c) 1997, 2013, Oracle and/or its affiliates. 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: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. duke@435: * duke@435: */ duke@435: stefank@2314: #include "precompiled.hpp" stefank@2314: #include "memory/allocation.inline.hpp" coleenp@4037: #include "oops/constantPool.hpp" stefank@2314: #include "oops/oop.inline.hpp" stefank@2314: #include "runtime/handles.inline.hpp" stefank@4299: #include "runtime/thread.inline.hpp" stefank@2314: #ifdef TARGET_OS_FAMILY_linux stefank@2314: # include "os_linux.inline.hpp" stefank@2314: #endif stefank@2314: #ifdef TARGET_OS_FAMILY_solaris stefank@2314: # include "os_solaris.inline.hpp" stefank@2314: #endif stefank@2314: #ifdef TARGET_OS_FAMILY_windows stefank@2314: # include "os_windows.inline.hpp" stefank@2314: #endif never@3156: #ifdef TARGET_OS_FAMILY_bsd never@3156: # include "os_bsd.inline.hpp" never@3156: #endif duke@435: duke@435: #ifdef ASSERT duke@435: oop* HandleArea::allocate_handle(oop obj) { duke@435: assert(_handle_mark_nesting > 1, "memory leak: allocating handle outside HandleMark"); duke@435: assert(_no_handle_mark_nesting == 0, "allocating handle inside NoHandleMark"); twisti@6039: assert(obj->is_oop(), err_msg("not an oop: " INTPTR_FORMAT, (intptr_t*) obj)); duke@435: return real_allocate_handle(obj); duke@435: } duke@435: duke@435: Handle::Handle(Thread* thread, oop obj) { duke@435: assert(thread == Thread::current(), "sanity check"); duke@435: if (obj == NULL) { duke@435: _handle = NULL; duke@435: } else { duke@435: _handle = thread->handle_area()->allocate_handle(obj); duke@435: } duke@435: } duke@435: duke@435: #endif duke@435: duke@435: static uintx chunk_oops_do(OopClosure* f, Chunk* chunk, char* chunk_top) { duke@435: oop* bottom = (oop*) chunk->bottom(); duke@435: oop* top = (oop*) chunk_top; duke@435: uintx handles_visited = top - bottom; duke@435: assert(top >= bottom && top <= (oop*) chunk->top(), "just checking"); duke@435: // during GC phase 3, a handle may be a forward pointer that duke@435: // is not yet valid, so loosen the assertion duke@435: while (bottom < top) { coleenp@4037: // This test can be moved up but for now check every oop. coleenp@4037: coleenp@4037: assert((*bottom)->is_oop(), "handle should point to oop"); coleenp@4037: duke@435: f->do_oop(bottom++); duke@435: } duke@435: return handles_visited; duke@435: } duke@435: duke@435: // Used for debugging handle allocation. duke@435: NOT_PRODUCT(jint _nof_handlemarks = 0;) duke@435: duke@435: void HandleArea::oops_do(OopClosure* f) { duke@435: uintx handles_visited = 0; duke@435: // First handle the current chunk. It is filled to the high water mark. duke@435: handles_visited += chunk_oops_do(f, _chunk, _hwm); duke@435: // Then handle all previous chunks. They are completely filled. duke@435: Chunk* k = _first; duke@435: while(k != _chunk) { duke@435: handles_visited += chunk_oops_do(f, k, k->top()); duke@435: k = k->next(); duke@435: } duke@435: duke@435: // The thread local handle areas should not get very large duke@435: if (TraceHandleAllocation && handles_visited > TotalHandleAllocationLimit) { duke@435: #ifdef ASSERT duke@435: warning("%d: Visited in HandleMark : %d", duke@435: _nof_handlemarks, handles_visited); duke@435: #else duke@435: warning("Visited in HandleMark : %d", handles_visited); duke@435: #endif duke@435: } duke@435: if (_prev != NULL) _prev->oops_do(f); duke@435: } duke@435: duke@435: void HandleMark::initialize(Thread* thread) { duke@435: _thread = thread; duke@435: // Save area duke@435: _area = thread->handle_area(); duke@435: // Save current top duke@435: _chunk = _area->_chunk; duke@435: _hwm = _area->_hwm; duke@435: _max = _area->_max; zgu@3900: _size_in_bytes = _area->_size_in_bytes; duke@435: debug_only(_area->_handle_mark_nesting++); duke@435: assert(_area->_handle_mark_nesting > 0, "must stack allocate HandleMarks"); duke@435: debug_only(Atomic::inc(&_nof_handlemarks);) duke@435: duke@435: // Link this in the thread duke@435: set_previous_handle_mark(thread->last_handle_mark()); duke@435: thread->set_last_handle_mark(this); duke@435: } duke@435: duke@435: duke@435: HandleMark::~HandleMark() { duke@435: HandleArea* area = _area; // help compilers with poor alias analysis duke@435: assert(area == _thread->handle_area(), "sanity check"); duke@435: assert(area->_handle_mark_nesting > 0, "must stack allocate HandleMarks" ); duke@435: debug_only(area->_handle_mark_nesting--); duke@435: duke@435: // Debug code to trace the number of handles allocated per mark/ duke@435: #ifdef ASSERT duke@435: if (TraceHandleAllocation) { duke@435: size_t handles = 0; duke@435: Chunk *c = _chunk->next(); duke@435: if (c == NULL) { duke@435: handles = area->_hwm - _hwm; // no new chunk allocated duke@435: } else { duke@435: handles = _max - _hwm; // add rest in first chunk duke@435: while(c != NULL) { duke@435: handles += c->length(); duke@435: c = c->next(); duke@435: } duke@435: handles -= area->_max - area->_hwm; // adjust for last trunk not full duke@435: } duke@435: handles /= sizeof(void *); // Adjust for size of a handle duke@435: if (handles > HandleAllocationLimit) { duke@435: // Note: _nof_handlemarks is only set in debug mode duke@435: warning("%d: Allocated in HandleMark : %d", _nof_handlemarks, handles); duke@435: } coleenp@4037: coleenp@4037: tty->print_cr("Handles %d", handles); duke@435: } duke@435: #endif duke@435: duke@435: // Delete later chunks duke@435: if( _chunk->next() ) { zgu@4193: // reset arena size before delete chunks. Otherwise, the total zgu@4193: // arena size could exceed total chunk size zgu@4193: assert(area->size_in_bytes() > size_in_bytes(), "Sanity check"); zgu@4193: area->set_size_in_bytes(size_in_bytes()); duke@435: _chunk->next_chop(); zgu@4193: } else { zgu@4193: assert(area->size_in_bytes() == size_in_bytes(), "Sanity check"); duke@435: } duke@435: // Roll back arena to saved top markers duke@435: area->_chunk = _chunk; duke@435: area->_hwm = _hwm; duke@435: area->_max = _max; duke@435: #ifdef ASSERT duke@435: // clear out first chunk (to detect allocation bugs) duke@435: if (ZapVMHandleArea) { duke@435: memset(_hwm, badHandleValue, _max - _hwm); duke@435: } duke@435: Atomic::dec(&_nof_handlemarks); duke@435: #endif duke@435: duke@435: // Unlink this from the thread duke@435: _thread->set_last_handle_mark(previous_handle_mark()); duke@435: } duke@435: coleenp@5614: void* HandleMark::operator new(size_t size) throw() { minqi@5103: return AllocateHeap(size, mtThread); minqi@5103: } minqi@5103: coleenp@5614: void* HandleMark::operator new [] (size_t size) throw() { minqi@5103: return AllocateHeap(size, mtThread); minqi@5103: } minqi@5103: minqi@5103: void HandleMark::operator delete(void* p) { minqi@5103: FreeHeap(p, mtThread); minqi@5103: } minqi@5103: minqi@5103: void HandleMark::operator delete[](void* p) { minqi@5103: FreeHeap(p, mtThread); minqi@5103: } minqi@5103: duke@435: #ifdef ASSERT duke@435: duke@435: NoHandleMark::NoHandleMark() { duke@435: HandleArea* area = Thread::current()->handle_area(); duke@435: area->_no_handle_mark_nesting++; duke@435: assert(area->_no_handle_mark_nesting > 0, "must stack allocate NoHandleMark" ); duke@435: } duke@435: duke@435: duke@435: NoHandleMark::~NoHandleMark() { duke@435: HandleArea* area = Thread::current()->handle_area(); duke@435: assert(area->_no_handle_mark_nesting > 0, "must stack allocate NoHandleMark" ); duke@435: area->_no_handle_mark_nesting--; duke@435: } duke@435: duke@435: duke@435: ResetNoHandleMark::ResetNoHandleMark() { duke@435: HandleArea* area = Thread::current()->handle_area(); duke@435: _no_handle_mark_nesting = area->_no_handle_mark_nesting; duke@435: area->_no_handle_mark_nesting = 0; duke@435: } duke@435: duke@435: duke@435: ResetNoHandleMark::~ResetNoHandleMark() { duke@435: HandleArea* area = Thread::current()->handle_area(); duke@435: area->_no_handle_mark_nesting = _no_handle_mark_nesting; duke@435: } duke@435: duke@435: #endif