1 /*
  2  * Copyright (c) 1998, 2023, 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 #ifndef SHARE_RUNTIME_HANDLES_INLINE_HPP
 26 #define SHARE_RUNTIME_HANDLES_INLINE_HPP
 27 
 28 #include "runtime/handles.hpp"
 29 
 30 #include "runtime/javaThread.hpp"
 31 #include "oops/metadata.hpp"
 32 #include "oops/oop.hpp"
 33 
 34 // these inline functions are in a separate file to break an include cycle
 35 // between Thread and Handle
 36 
 37 inline Handle::Handle(Thread* thread, oop obj) {
 38   assert(thread == Thread::current(), "sanity check");
 39   if (obj == nullptr) {
 40     _handle = nullptr;
 41   } else {
 42     _handle = thread->handle_area()->allocate_handle(obj);
 43   }
 44 }
 45 
 46 inline void Handle::replace(oop obj) {
 47   // Unlike in OopHandle::replace, we shouldn't use a barrier here.
 48   // OopHandle has its storage in OopStorage, which is walked concurrently and uses barriers.
 49   // Handle is thread private, and iterated by Thread::oops_do, which is why it shouldn't have any barriers at all.
 50   assert(_handle != nullptr, "should not use replace");
 51   *_handle = obj;
 52 }
 53 
 54 // Inline constructors for Specific Handles for different oop types
 55 #define DEF_HANDLE_CONSTR(type, is_a)                   \
 56 inline type##Handle::type##Handle (Thread* thread, type##Oop obj) : Handle(thread, (oop)obj) { \
 57   assert(is_null() || ((oop)obj)->is_a(), "illegal type");                \
 58 }
 59 
 60 DEF_HANDLE_CONSTR(instance , is_instance_noinline )
 61 DEF_HANDLE_CONSTR(array    , is_array_noinline    )
 62 DEF_HANDLE_CONSTR(objArray , is_objArray_noinline )
 63 DEF_HANDLE_CONSTR(typeArray, is_typeArray_noinline)
 64 DEF_HANDLE_CONSTR(flatArray, is_flatArray_noinline)
 65 
 66 // Constructor for metadata handles
 67 #define DEF_METADATA_HANDLE_FN(name, type) \
 68 inline name##Handle::name##Handle(Thread* thread, type* obj) : _value(obj), _thread(thread) { \
 69   if (obj != nullptr) {                                                   \
 70     assert(((Metadata*)obj)->is_valid(), "obj is valid");              \
 71     assert(_thread == Thread::current(), "thread must be current");    \
 72     assert(_thread->is_in_live_stack((address)this), "not on stack?"); \
 73     _thread->metadata_handles()->push((Metadata*)obj);                 \
 74   }                                                                    \
 75 }                                                                      \
 76 
 77 DEF_METADATA_HANDLE_FN(method, Method)
 78 DEF_METADATA_HANDLE_FN(constantPool, ConstantPool)
 79 
 80 inline void HandleMark::push() {
 81   // This is intentionally a NOP. pop_and_restore will reset
 82   // values to the HandleMark further down the stack, typically
 83   // in JavaCalls::call_helper.
 84   debug_only(_area->_handle_mark_nesting++);
 85 }
 86 
 87 inline void HandleMark::pop_and_restore() {
 88   // Delete later chunks
 89   if(_chunk->next() != nullptr) {
 90     assert(_area->size_in_bytes() > size_in_bytes(), "Sanity check");
 91     chop_later_chunks();
 92   } else {
 93     assert(_area->size_in_bytes() == size_in_bytes(), "Sanity check");
 94   }
 95   // Roll back arena to saved top markers
 96   _area->_chunk = _chunk;
 97   _area->_hwm = _hwm;
 98   _area->_max = _max;
 99   debug_only(_area->_handle_mark_nesting--);
100 }
101 
102 inline HandleMarkCleaner::HandleMarkCleaner(Thread* thread) {
103   _thread = thread;
104   _thread->last_handle_mark()->push();
105 }
106 
107 inline HandleMarkCleaner::~HandleMarkCleaner() {
108   _thread->last_handle_mark()->pop_and_restore();
109 }
110 
111 #endif // SHARE_RUNTIME_HANDLES_INLINE_HPP