handles.cpp revision 11857:d0fbf661cc16
1/* 2 * Copyright (c) 1997, 2016, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25#include "precompiled.hpp" 26#include "memory/allocation.inline.hpp" 27#include "oops/constantPool.hpp" 28#include "oops/oop.inline.hpp" 29#include "runtime/atomic.hpp" 30#include "runtime/handles.inline.hpp" 31#include "runtime/thread.inline.hpp" 32 33#ifdef ASSERT 34oop* HandleArea::allocate_handle(oop obj) { 35 assert(_handle_mark_nesting > 1, "memory leak: allocating handle outside HandleMark"); 36 assert(_no_handle_mark_nesting == 0, "allocating handle inside NoHandleMark"); 37 assert(obj->is_oop(), "not an oop: " INTPTR_FORMAT, p2i(obj)); 38 return real_allocate_handle(obj); 39} 40 41Handle::Handle(Thread* thread, oop obj) { 42 assert(thread == Thread::current(), "sanity check"); 43 if (obj == NULL) { 44 _handle = NULL; 45 } else { 46 _handle = thread->handle_area()->allocate_handle(obj); 47 } 48} 49#endif 50 51// Copy constructors and destructors for metadata handles 52// These do too much to inline. 53#define DEF_METADATA_HANDLE_FN_NOINLINE(name, type) \ 54name##Handle::name##Handle(const name##Handle &h) { \ 55 _value = h._value; \ 56 if (_value != NULL) { \ 57 assert(_value->is_valid(), "obj is valid"); \ 58 if (h._thread != NULL) { \ 59 assert(h._thread == Thread::current(), "thread must be current");\ 60 _thread = h._thread; \ 61 } else { \ 62 _thread = Thread::current(); \ 63 } \ 64 assert (_thread->is_in_stack((address)this), "not on stack?"); \ 65 _thread->metadata_handles()->push((Metadata*)_value); \ 66 } else { \ 67 _thread = NULL; \ 68 } \ 69} \ 70name##Handle& name##Handle::operator=(const name##Handle &s) { \ 71 remove(); \ 72 _value = s._value; \ 73 if (_value != NULL) { \ 74 assert(_value->is_valid(), "obj is valid"); \ 75 if (s._thread != NULL) { \ 76 assert(s._thread == Thread::current(), "thread must be current");\ 77 _thread = s._thread; \ 78 } else { \ 79 _thread = Thread::current(); \ 80 } \ 81 assert (_thread->is_in_stack((address)this), "not on stack?"); \ 82 _thread->metadata_handles()->push((Metadata*)_value); \ 83 } else { \ 84 _thread = NULL; \ 85 } \ 86 return *this; \ 87} \ 88inline void name##Handle::remove() { \ 89 if (_value != NULL) { \ 90 int i = _thread->metadata_handles()->find_from_end((Metadata*)_value); \ 91 assert(i!=-1, "not in metadata_handles list"); \ 92 _thread->metadata_handles()->remove_at(i); \ 93 } \ 94} \ 95name##Handle::~name##Handle () { remove(); } \ 96 97DEF_METADATA_HANDLE_FN_NOINLINE(method, Method) 98DEF_METADATA_HANDLE_FN_NOINLINE(constantPool, ConstantPool) 99 100 101static uintx chunk_oops_do(OopClosure* f, Chunk* chunk, char* chunk_top) { 102 oop* bottom = (oop*) chunk->bottom(); 103 oop* top = (oop*) chunk_top; 104 uintx handles_visited = top - bottom; 105 assert(top >= bottom && top <= (oop*) chunk->top(), "just checking"); 106 // during GC phase 3, a handle may be a forward pointer that 107 // is not yet valid, so loosen the assertion 108 while (bottom < top) { 109 // This test can be moved up but for now check every oop. 110 111 assert((*bottom)->is_oop(), "handle should point to oop"); 112 113 f->do_oop(bottom++); 114 } 115 return handles_visited; 116} 117 118// Used for debugging handle allocation. 119NOT_PRODUCT(jint _nof_handlemarks = 0;) 120 121void HandleArea::oops_do(OopClosure* f) { 122 uintx handles_visited = 0; 123 // First handle the current chunk. It is filled to the high water mark. 124 handles_visited += chunk_oops_do(f, _chunk, _hwm); 125 // Then handle all previous chunks. They are completely filled. 126 Chunk* k = _first; 127 while(k != _chunk) { 128 handles_visited += chunk_oops_do(f, k, k->top()); 129 k = k->next(); 130 } 131 132 if (_prev != NULL) _prev->oops_do(f); 133} 134 135void HandleMark::initialize(Thread* thread) { 136 _thread = thread; 137 // Save area 138 _area = thread->handle_area(); 139 // Save current top 140 _chunk = _area->_chunk; 141 _hwm = _area->_hwm; 142 _max = _area->_max; 143 _size_in_bytes = _area->_size_in_bytes; 144 debug_only(_area->_handle_mark_nesting++); 145 assert(_area->_handle_mark_nesting > 0, "must stack allocate HandleMarks"); 146 debug_only(Atomic::inc(&_nof_handlemarks);) 147 148 // Link this in the thread 149 set_previous_handle_mark(thread->last_handle_mark()); 150 thread->set_last_handle_mark(this); 151} 152 153 154HandleMark::~HandleMark() { 155 HandleArea* area = _area; // help compilers with poor alias analysis 156 assert(area == _thread->handle_area(), "sanity check"); 157 assert(area->_handle_mark_nesting > 0, "must stack allocate HandleMarks" ); 158 debug_only(area->_handle_mark_nesting--); 159 160 // Delete later chunks 161 if( _chunk->next() ) { 162 // reset arena size before delete chunks. Otherwise, the total 163 // arena size could exceed total chunk size 164 assert(area->size_in_bytes() > size_in_bytes(), "Sanity check"); 165 area->set_size_in_bytes(size_in_bytes()); 166 _chunk->next_chop(); 167 } else { 168 assert(area->size_in_bytes() == size_in_bytes(), "Sanity check"); 169 } 170 // Roll back arena to saved top markers 171 area->_chunk = _chunk; 172 area->_hwm = _hwm; 173 area->_max = _max; 174#ifdef ASSERT 175 // clear out first chunk (to detect allocation bugs) 176 if (ZapVMHandleArea) { 177 memset(_hwm, badHandleValue, _max - _hwm); 178 } 179 Atomic::dec(&_nof_handlemarks); 180#endif 181 182 // Unlink this from the thread 183 _thread->set_last_handle_mark(previous_handle_mark()); 184} 185 186void* HandleMark::operator new(size_t size) throw() { 187 return AllocateHeap(size, mtThread); 188} 189 190void* HandleMark::operator new [] (size_t size) throw() { 191 return AllocateHeap(size, mtThread); 192} 193 194void HandleMark::operator delete(void* p) { 195 FreeHeap(p); 196} 197 198void HandleMark::operator delete[](void* p) { 199 FreeHeap(p); 200} 201 202#ifdef ASSERT 203 204NoHandleMark::NoHandleMark() { 205 HandleArea* area = Thread::current()->handle_area(); 206 area->_no_handle_mark_nesting++; 207 assert(area->_no_handle_mark_nesting > 0, "must stack allocate NoHandleMark" ); 208} 209 210 211NoHandleMark::~NoHandleMark() { 212 HandleArea* area = Thread::current()->handle_area(); 213 assert(area->_no_handle_mark_nesting > 0, "must stack allocate NoHandleMark" ); 214 area->_no_handle_mark_nesting--; 215} 216 217 218ResetNoHandleMark::ResetNoHandleMark() { 219 HandleArea* area = Thread::current()->handle_area(); 220 _no_handle_mark_nesting = area->_no_handle_mark_nesting; 221 area->_no_handle_mark_nesting = 0; 222} 223 224 225ResetNoHandleMark::~ResetNoHandleMark() { 226 HandleArea* area = Thread::current()->handle_area(); 227 area->_no_handle_mark_nesting = _no_handle_mark_nesting; 228} 229 230bool instanceKlassHandle::is_instanceKlass(const Klass* k) { 231 // Need this to avoid circular include dependency 232 return k->is_instance_klass(); 233} 234 235#endif 236