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