1 /* 2 * Copyright (c) 1997, 2015, 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.inline.hpp" 30 #include "runtime/handles.inline.hpp" 31 #include "runtime/thread.inline.hpp" 32 33 #ifdef ASSERT 34 oop* 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 41 Handle::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) \ 54 name##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 } \ 70 name##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 } \ 88 inline 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 } \ 95 name##Handle::~name##Handle () { remove(); } \ 96 97 DEF_METADATA_HANDLE_FN_NOINLINE(method, Method) 98 DEF_METADATA_HANDLE_FN_NOINLINE(constantPool, ConstantPool) 99 100 101 static 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. 119 NOT_PRODUCT(jint _nof_handlemarks = 0;) 120 121 void 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 // The thread local handle areas should not get very large 133 if (TraceHandleAllocation && (size_t)handles_visited > TotalHandleAllocationLimit) { 134 #ifdef ASSERT 135 warning("%d: Visited in HandleMark : " SIZE_FORMAT, _nof_handlemarks, handles_visited); 136 #else 137 warning("Visited in HandleMark : " SIZE_FORMAT, handles_visited); 138 #endif 139 } 140 if (_prev != NULL) _prev->oops_do(f); 141 } 142 143 void HandleMark::initialize(Thread* thread) { 144 _thread = thread; 145 // Save area 146 _area = thread->handle_area(); 147 // Save current top 148 _chunk = _area->_chunk; 149 _hwm = _area->_hwm; 150 _max = _area->_max; 151 _size_in_bytes = _area->_size_in_bytes; 152 debug_only(_area->_handle_mark_nesting++); 153 assert(_area->_handle_mark_nesting > 0, "must stack allocate HandleMarks"); 154 debug_only(Atomic::inc(&_nof_handlemarks);) 155 156 // Link this in the thread 157 set_previous_handle_mark(thread->last_handle_mark()); 158 thread->set_last_handle_mark(this); 159 } 160 161 162 HandleMark::~HandleMark() { 163 HandleArea* area = _area; // help compilers with poor alias analysis 164 assert(area == _thread->handle_area(), "sanity check"); 165 assert(area->_handle_mark_nesting > 0, "must stack allocate HandleMarks" ); 166 debug_only(area->_handle_mark_nesting--); 167 168 // Debug code to trace the number of handles allocated per mark/ 169 #ifdef ASSERT 170 if (TraceHandleAllocation) { 171 size_t handles = 0; 172 Chunk *c = _chunk->next(); 173 if (c == NULL) { 174 handles = area->_hwm - _hwm; // no new chunk allocated 175 } else { 176 handles = _max - _hwm; // add rest in first chunk 177 while(c != NULL) { 178 handles += c->length(); 179 c = c->next(); 180 } 181 handles -= area->_max - area->_hwm; // adjust for last trunk not full 182 } 183 handles /= sizeof(void *); // Adjust for size of a handle 184 if (handles > HandleAllocationLimit) { 185 // Note: _nof_handlemarks is only set in debug mode 186 warning("%d: Allocated in HandleMark : " SIZE_FORMAT, _nof_handlemarks, handles); 187 } 188 189 tty->print_cr("Handles " SIZE_FORMAT, handles); 190 } 191 #endif 192 193 // Delete later chunks 194 if( _chunk->next() ) { 195 // reset arena size before delete chunks. Otherwise, the total 196 // arena size could exceed total chunk size 197 assert(area->size_in_bytes() > size_in_bytes(), "Sanity check"); 198 area->set_size_in_bytes(size_in_bytes()); 199 _chunk->next_chop(); 200 } else { 201 assert(area->size_in_bytes() == size_in_bytes(), "Sanity check"); 202 } 203 // Roll back arena to saved top markers 204 area->_chunk = _chunk; 205 area->_hwm = _hwm; 206 area->_max = _max; 207 #ifdef ASSERT 208 // clear out first chunk (to detect allocation bugs) 209 if (ZapVMHandleArea) { 210 memset(_hwm, badHandleValue, _max - _hwm); 211 } 212 Atomic::dec(&_nof_handlemarks); 213 #endif 214 215 // Unlink this from the thread 216 _thread->set_last_handle_mark(previous_handle_mark()); 217 } 218 219 void* HandleMark::operator new(size_t size) throw() { 220 return AllocateHeap(size, mtThread); 221 } 222 223 void* HandleMark::operator new [] (size_t size) throw() { 224 return AllocateHeap(size, mtThread); 225 } 226 227 void HandleMark::operator delete(void* p) { 228 FreeHeap(p); 229 } 230 231 void HandleMark::operator delete[](void* p) { 232 FreeHeap(p); 233 } 234 235 #ifdef ASSERT 236 237 NoHandleMark::NoHandleMark() { 238 HandleArea* area = Thread::current()->handle_area(); 239 area->_no_handle_mark_nesting++; 240 assert(area->_no_handle_mark_nesting > 0, "must stack allocate NoHandleMark" ); 241 } 242 243 244 NoHandleMark::~NoHandleMark() { 245 HandleArea* area = Thread::current()->handle_area(); 246 assert(area->_no_handle_mark_nesting > 0, "must stack allocate NoHandleMark" ); 247 area->_no_handle_mark_nesting--; 248 } 249 250 251 ResetNoHandleMark::ResetNoHandleMark() { 252 HandleArea* area = Thread::current()->handle_area(); 253 _no_handle_mark_nesting = area->_no_handle_mark_nesting; 254 area->_no_handle_mark_nesting = 0; 255 } 256 257 258 ResetNoHandleMark::~ResetNoHandleMark() { 259 HandleArea* area = Thread::current()->handle_area(); 260 area->_no_handle_mark_nesting = _no_handle_mark_nesting; 261 } 262 263 bool instanceKlassHandle::is_instanceKlass(const Klass* k) { 264 // Need this to avoid circular include dependency 265 return k->is_instance_klass(); 266 } 267 268 #endif