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 void chunk_oops_do(OopClosure* f, Chunk* chunk, char* chunk_top) {
101 oop* bottom = (oop*) chunk->bottom();
102 oop* top = (oop*) chunk_top;
103 assert(top >= bottom && top <= (oop*) chunk->top(), "just checking");
104
105 while (bottom < top) {
106 f->do_oop(bottom++);
107 }
108 }
109
110 void HandleArea::oops_do(OopClosure* f) {
111 // First handle the current chunk. It is filled to the high water mark.
112 chunk_oops_do(f, _chunk, _hwm);
113
114 // Then handle all previous chunks. They are completely filled.
115 Chunk* k = _first;
116 while(k != _chunk) {
117 chunk_oops_do(f, k, k->top());
118 k = k->next();
119 }
120 }
121
122 void HandleMark::initialize(Thread* thread) {
123 _thread = thread; // Not the current thread during thread creation.
124 // Save area
125 _area = thread->handle_area();
126 // Save current top
127 _chunk = _area->_chunk;
128 _hwm = _area->_hwm;
129 _max = _area->_max;
130 _size_in_bytes = _area->_size_in_bytes;
131 DEBUG_ONLY(_area->_handle_mark_nesting++);
132 assert(_area->_handle_mark_nesting > 0, "must stack allocate HandleMarks");
133
134 // Link this in the thread
135 set_previous_handle_mark(thread->last_handle_mark());
136 thread->set_last_handle_mark(this);
137 }
138
139 HandleMark::~HandleMark() {
140 assert(_area == _thread->handle_area(), "sanity check");
141 assert(_area->_handle_mark_nesting > 0, "must stack allocate HandleMarks" );
142
143 pop_and_restore();
144 #ifdef ASSERT
145 // clear out first chunk (to detect allocation bugs)
146 if (ZapVMHandleArea) {
147 memset(_hwm, badHandleValue, _max - _hwm);
148 }
149 #endif
150
151 // Unlink this from the thread
152 _thread->set_last_handle_mark(previous_handle_mark());
153 }
154
155 void HandleMark::chop_later_chunks() {
156 // reset arena size before delete chunks. Otherwise, the total
157 // arena size could exceed total chunk size
158 _area->set_size_in_bytes(size_in_bytes());
159 Chunk::next_chop(_chunk);
160 }
161
162 void* HandleMark::operator new(size_t size) throw() {
163 return AllocateHeap(size, mtThread);
164 }
165
166 void* HandleMark::operator new [] (size_t size) throw() {
167 return AllocateHeap(size, mtThread);
168 }
169
170 void HandleMark::operator delete(void* p) {
171 FreeHeap(p);
172 }
173
174 void HandleMark::operator delete[](void* p) {
175 FreeHeap(p);
176 }
177
178 #ifdef ASSERT
179
180 NoHandleMark::NoHandleMark() {
181 HandleArea* area = Thread::current()->handle_area();
182 area->_no_handle_mark_nesting++;
183 assert(area->_no_handle_mark_nesting > 0, "must stack allocate NoHandleMark" );
184 }
185
186
187 NoHandleMark::~NoHandleMark() {
188 HandleArea* area = Thread::current()->handle_area();
189 assert(area->_no_handle_mark_nesting > 0, "must stack allocate NoHandleMark" );
190 area->_no_handle_mark_nesting--;
191 }
192
193
194 ResetNoHandleMark::ResetNoHandleMark() {
195 HandleArea* area = Thread::current()->handle_area();
196 _no_handle_mark_nesting = area->_no_handle_mark_nesting;
197 area->_no_handle_mark_nesting = 0;
198 }
199
200
201 ResetNoHandleMark::~ResetNoHandleMark() {
202 HandleArea* area = Thread::current()->handle_area();
203 area->_no_handle_mark_nesting = _no_handle_mark_nesting;
204 }
205
206 #endif // ASSERT