1 /* 2 * Copyright (c) 1997, 2025, 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 "memory/allocation.inline.hpp" 26 #include "oops/constantPool.hpp" 27 #include "oops/method.hpp" 28 #include "oops/oop.inline.hpp" 29 #include "runtime/handles.inline.hpp" 30 #include "runtime/javaThread.hpp" 31 32 #ifdef ASSERT 33 #define assert_handle_mark_nesting() \ 34 assert(_handle_mark_nesting > 1, "memory leak: allocating handle outside HandleMark"); \ 35 assert(_no_handle_mark_nesting == 0, "allocating handle inside NoHandleMark"); \ 36 37 38 oop* HandleArea::allocate_handle(oop obj) { 39 assert_handle_mark_nesting(); 40 assert(oopDesc::is_oop(obj), "not an oop: " INTPTR_FORMAT, p2i(obj)); 41 return real_allocate_handle(obj); 42 } 43 44 oop* HandleArea::allocate_null_handle() { 45 assert_handle_mark_nesting(); 46 return real_allocate_handle(nullptr); 47 } 48 #endif 49 50 // Copy constructors and destructors for metadata handles 51 // These do too much to inline. 52 #define DEF_METADATA_HANDLE_FN_NOINLINE(name, type) \ 53 name##Handle::name##Handle(const name##Handle &h) { \ 54 _value = h._value; \ 55 if (_value != nullptr) { \ 56 assert(_value->is_valid(), "obj is valid"); \ 57 if (h._thread != nullptr) { \ 58 assert(h._thread == Thread::current(), "thread must be current");\ 59 _thread = h._thread; \ 60 } else { \ 61 _thread = Thread::current(); \ 62 } \ 63 assert(_thread->is_in_live_stack((address)this), "not on stack?"); \ 64 _thread->metadata_handles()->push((Metadata*)_value); \ 65 } else { \ 66 _thread = nullptr; \ 67 } \ 68 } \ 69 name##Handle& name##Handle::operator=(const name##Handle &s) { \ 70 remove(); \ 71 _value = s._value; \ 72 if (_value != nullptr) { \ 73 assert(_value->is_valid(), "obj is valid"); \ 74 if (s._thread != nullptr) { \ 75 assert(s._thread == Thread::current(), "thread must be current");\ 76 _thread = s._thread; \ 77 } else { \ 78 _thread = Thread::current(); \ 79 } \ 80 assert(_thread->is_in_live_stack((address)this), "not on stack?"); \ 81 _thread->metadata_handles()->push((Metadata*)_value); \ 82 } else { \ 83 _thread = nullptr; \ 84 } \ 85 return *this; \ 86 } \ 87 inline void name##Handle::remove() { \ 88 if (_value != nullptr) { \ 89 int i = _thread->metadata_handles()->find_from_end((Metadata*)_value); \ 90 assert(i!=-1, "not in metadata_handles list"); \ 91 _thread->metadata_handles()->remove_at(i); \ 92 } \ 93 } \ 94 name##Handle::~name##Handle () { remove(); } \ 95 96 DEF_METADATA_HANDLE_FN_NOINLINE(method, Method) 97 DEF_METADATA_HANDLE_FN_NOINLINE(constantPool, ConstantPool) 98 99 100 static uintx chunk_oops_do(OopClosure* f, Chunk* chunk, char* chunk_top) { 101 oop* bottom = (oop*) chunk->bottom(); 102 oop* top = (oop*) chunk_top; 103 uintx handles_visited = top - bottom; 104 assert(top >= bottom && top <= (oop*) chunk->top(), "just checking"); 105 // during GC phase 3, a handle may be a forward pointer that 106 // is not yet valid, so loosen the assertion 107 while (bottom < top) { 108 f->do_oop(bottom++); 109 } 110 return handles_visited; 111 } 112 113 void HandleArea::oops_do(OopClosure* f) { 114 uintx handles_visited = 0; 115 // First handle the current chunk. It is filled to the high water mark. 116 handles_visited += chunk_oops_do(f, _chunk, _hwm); 117 // Then handle all previous chunks. They are completely filled. 118 Chunk* k = _first; 119 while(k != _chunk) { 120 handles_visited += chunk_oops_do(f, k, k->top()); 121 k = k->next(); 122 } 123 124 if (_prev != nullptr) _prev->oops_do(f); 125 } 126 127 void HandleMark::initialize(Thread* thread) { 128 _thread = thread; // Not the current thread during thread creation. 129 // Save area 130 _area = thread->handle_area(); 131 // Save current top 132 _chunk = _area->_chunk; 133 _hwm = _area->_hwm; 134 _max = _area->_max; 135 _size_in_bytes = _area->_size_in_bytes; 136 debug_only(_area->_handle_mark_nesting++); 137 assert(_area->_handle_mark_nesting > 0, "must stack allocate HandleMarks"); 138 139 // Link this in the thread 140 set_previous_handle_mark(thread->last_handle_mark()); 141 thread->set_last_handle_mark(this); 142 } 143 144 HandleMark::~HandleMark() { 145 assert(_area == _thread->handle_area(), "sanity check"); 146 assert(_area->_handle_mark_nesting > 0, "must stack allocate HandleMarks" ); 147 148 pop_and_restore(); 149 #ifdef ASSERT 150 // clear out first chunk (to detect allocation bugs) 151 if (ZapVMHandleArea) { 152 memset(_hwm, badHandleValue, _max - _hwm); 153 } 154 #endif 155 156 // Unlink this from the thread 157 _thread->set_last_handle_mark(previous_handle_mark()); 158 } 159 160 void HandleMark::chop_later_chunks() { 161 // reset arena size before delete chunks. Otherwise, the total 162 // arena size could exceed total chunk size 163 _area->set_size_in_bytes(size_in_bytes()); 164 Chunk::next_chop(_chunk); 165 } 166 167 void* HandleMark::operator new(size_t size) throw() { 168 return AllocateHeap(size, mtThread); 169 } 170 171 void* HandleMark::operator new [] (size_t size) throw() { 172 return AllocateHeap(size, mtThread); 173 } 174 175 void HandleMark::operator delete(void* p) { 176 FreeHeap(p); 177 } 178 179 void HandleMark::operator delete[](void* p) { 180 FreeHeap(p); 181 } 182 183 #ifdef ASSERT 184 185 NoHandleMark::NoHandleMark() { 186 HandleArea* area = Thread::current()->handle_area(); 187 area->_no_handle_mark_nesting++; 188 assert(area->_no_handle_mark_nesting > 0, "must stack allocate NoHandleMark" ); 189 } 190 191 192 NoHandleMark::~NoHandleMark() { 193 HandleArea* area = Thread::current()->handle_area(); 194 assert(area->_no_handle_mark_nesting > 0, "must stack allocate NoHandleMark" ); 195 area->_no_handle_mark_nesting--; 196 } 197 198 199 ResetNoHandleMark::ResetNoHandleMark() { 200 HandleArea* area = Thread::current()->handle_area(); 201 _no_handle_mark_nesting = area->_no_handle_mark_nesting; 202 area->_no_handle_mark_nesting = 0; 203 } 204 205 206 ResetNoHandleMark::~ResetNoHandleMark() { 207 HandleArea* area = Thread::current()->handle_area(); 208 area->_no_handle_mark_nesting = _no_handle_mark_nesting; 209 } 210 211 #endif // ASSERT