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