2
* Copyright (c) 1997, 2023, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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.
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
25
#include "precompiled.hpp"
26
#include "memory/allocation.inline.hpp"
27
#include "oops/constantPool.hpp"
28
#include "oops/method.hpp"
29
#include "oops/oop.inline.hpp"
30
#include "runtime/handles.inline.hpp"
31
#include "runtime/javaThread.hpp"
34
#define assert_handle_mark_nesting() \
35
assert(_handle_mark_nesting > 1, "memory leak: allocating handle outside HandleMark"); \
36
assert(_no_handle_mark_nesting == 0, "allocating handle inside NoHandleMark"); \
39
oop* HandleArea::allocate_handle(oop obj) {
40
assert_handle_mark_nesting();
41
assert(oopDesc::is_oop(obj), "not an oop: " INTPTR_FORMAT, p2i(obj));
42
return real_allocate_handle(obj);
45
oop* HandleArea::allocate_null_handle() {
46
assert_handle_mark_nesting();
47
return real_allocate_handle(nullptr);
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) { \
56
if (_value != nullptr) { \
57
assert(_value->is_valid(), "obj is valid"); \
58
if (h._thread != nullptr) { \
59
assert(h._thread == Thread::current(), "thread must be current");\
60
_thread = h._thread; \
62
_thread = Thread::current(); \
64
assert(_thread->is_in_live_stack((address)this), "not on stack?"); \
65
_thread->metadata_handles()->push((Metadata*)_value); \
70
name##Handle& name##Handle::operator=(const name##Handle &s) { \
73
if (_value != nullptr) { \
74
assert(_value->is_valid(), "obj is valid"); \
75
if (s._thread != nullptr) { \
76
assert(s._thread == Thread::current(), "thread must be current");\
77
_thread = s._thread; \
79
_thread = Thread::current(); \
81
assert(_thread->is_in_live_stack((address)this), "not on stack?"); \
82
_thread->metadata_handles()->push((Metadata*)_value); \
88
inline void name##Handle::remove() { \
89
if (_value != nullptr) { \
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); \
95
name##Handle::~name##Handle () { remove(); } \
97
DEF_METADATA_HANDLE_FN_NOINLINE(method, Method)
98
DEF_METADATA_HANDLE_FN_NOINLINE(constantPool, ConstantPool)
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) {
111
return handles_visited;
114
void HandleArea::oops_do(OopClosure* f) {
115
uintx handles_visited = 0;
116
// First handle the current chunk. It is filled to the high water mark.
117
handles_visited += chunk_oops_do(f, _chunk, _hwm);
118
// Then handle all previous chunks. They are completely filled.
121
handles_visited += chunk_oops_do(f, k, k->top());
125
if (_prev != nullptr) _prev->oops_do(f);
128
void HandleMark::initialize(Thread* thread) {
129
_thread = thread; // Not the current thread during thread creation.
131
_area = thread->handle_area();
133
_chunk = _area->_chunk;
136
_size_in_bytes = _area->_size_in_bytes;
137
debug_only(_area->_handle_mark_nesting++);
138
assert(_area->_handle_mark_nesting > 0, "must stack allocate HandleMarks");
140
// Link this in the thread
141
set_previous_handle_mark(thread->last_handle_mark());
142
thread->set_last_handle_mark(this);
145
HandleMark::~HandleMark() {
146
assert(_area == _thread->handle_area(), "sanity check");
147
assert(_area->_handle_mark_nesting > 0, "must stack allocate HandleMarks" );
151
// clear out first chunk (to detect allocation bugs)
152
if (ZapVMHandleArea) {
153
memset(_hwm, badHandleValue, _max - _hwm);
157
// Unlink this from the thread
158
_thread->set_last_handle_mark(previous_handle_mark());
161
void HandleMark::chop_later_chunks() {
162
// reset arena size before delete chunks. Otherwise, the total
163
// arena size could exceed total chunk size
164
_area->set_size_in_bytes(size_in_bytes());
165
Chunk::next_chop(_chunk);
168
void* HandleMark::operator new(size_t size) throw() {
169
return AllocateHeap(size, mtThread);
172
void* HandleMark::operator new [] (size_t size) throw() {
173
return AllocateHeap(size, mtThread);
176
void HandleMark::operator delete(void* p) {
180
void HandleMark::operator delete[](void* p) {
186
NoHandleMark::NoHandleMark() {
187
HandleArea* area = Thread::current()->handle_area();
188
area->_no_handle_mark_nesting++;
189
assert(area->_no_handle_mark_nesting > 0, "must stack allocate NoHandleMark" );
193
NoHandleMark::~NoHandleMark() {
194
HandleArea* area = Thread::current()->handle_area();
195
assert(area->_no_handle_mark_nesting > 0, "must stack allocate NoHandleMark" );
196
area->_no_handle_mark_nesting--;
200
ResetNoHandleMark::ResetNoHandleMark() {
201
HandleArea* area = Thread::current()->handle_area();
202
_no_handle_mark_nesting = area->_no_handle_mark_nesting;
203
area->_no_handle_mark_nesting = 0;
207
ResetNoHandleMark::~ResetNoHandleMark() {
208
HandleArea* area = Thread::current()->handle_area();
209
area->_no_handle_mark_nesting = _no_handle_mark_nesting;