1 /*
  2  * Copyright (c) 2015, 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 #ifndef SHARE_OOPS_INSTANCEKLASS_INLINE_HPP
 26 #define SHARE_OOPS_INSTANCEKLASS_INLINE_HPP
 27 
 28 #include "oops/instanceKlass.hpp"
 29 
 30 #include "memory/memRegion.hpp"
 31 #include "oops/fieldInfo.inline.hpp"
 32 #include "oops/klass.inline.hpp"
 33 #include "oops/oop.inline.hpp"
 34 #include "runtime/atomicAccess.hpp"
 35 #include "utilities/devirtualizer.inline.hpp"
 36 #include "utilities/globalDefinitions.hpp"
 37 
 38 inline intptr_t* InstanceKlass::start_of_itable()   const { return (intptr_t*)start_of_vtable() + vtable_length(); }
 39 inline intptr_t* InstanceKlass::end_of_itable()     const { return start_of_itable() + itable_length(); }
 40 
 41 inline oop InstanceKlass::static_field_base_raw() { return java_mirror(); }
 42 
 43 inline Symbol* InstanceKlass::field_name(int index) const { return field(index).name(constants()); }
 44 inline Symbol* InstanceKlass::field_signature(int index) const { return field(index).signature(constants()); }
 45 
 46 inline int InstanceKlass::java_fields_count() const { return FieldInfoStream::num_java_fields(fieldinfo_stream()); }
 47 inline int InstanceKlass::total_fields_count() const { return FieldInfoStream::num_total_fields(fieldinfo_stream()); }
 48 
 49 inline OopMapBlock* InstanceKlass::start_of_nonstatic_oop_maps() const {
 50   return (OopMapBlock*)(start_of_itable() + itable_length());
 51 }
 52 
 53 inline Klass** InstanceKlass::end_of_nonstatic_oop_maps() const {
 54   return (Klass**)(start_of_nonstatic_oop_maps() +
 55                    nonstatic_oop_map_count());
 56 }
 57 
 58 inline InstanceKlass* volatile* InstanceKlass::adr_implementor() const {
 59   if (is_interface()) {
 60     return (InstanceKlass* volatile*)end_of_nonstatic_oop_maps();
 61   } else {
 62     return nullptr;
 63   }
 64 }
 65 
 66 inline address InstanceKlass::end_of_instance_klass() const {
 67   return (address)end_of_nonstatic_oop_maps() +
 68       (is_interface() ? sizeof(InstanceKlass*) : 0);
 69 }
 70 
 71 inline InlineKlass* InstanceKlass::get_inline_type_field_klass(int idx) const {
 72   assert(has_inline_type_fields(), "Sanity checking");
 73   assert(idx < java_fields_count(), "IOOB");
 74   InlineKlass* k = inline_layout_info(idx).klass();
 75   assert(k != nullptr, "Should always be set before being read");
 76   return k;
 77 }
 78 
 79 inline InlineKlass* InstanceKlass::get_inline_type_field_klass_or_null(int idx) const {
 80   assert(has_inline_type_fields(), "Sanity checking");
 81   assert(idx < java_fields_count(), "IOOB");
 82   InlineKlass* k = inline_layout_info(idx).klass();
 83   return k;
 84 }
 85 
 86 inline ObjArrayKlass* InstanceKlass::array_klasses_acquire() const {
 87   return AtomicAccess::load_acquire(&_array_klasses);
 88 }
 89 
 90 inline void InstanceKlass::release_set_array_klasses(ObjArrayKlass* k) {
 91   AtomicAccess::release_store(&_array_klasses, k);
 92 }
 93 
 94 // The iteration over the oops in objects is a hot path in the GC code.
 95 // By force inlining the following functions, we get similar GC performance
 96 // as the previous macro based implementation.
 97 
 98 template <typename T, class OopClosureType>
 99 ALWAYSINLINE void InstanceKlass::oop_oop_iterate_oop_map(OopMapBlock* map, oop obj, OopClosureType* closure) {
100   T* p         = obj->field_addr<T>(map->offset());
101   T* const end = p + map->count();
102 
103   for (; p < end; ++p) {
104     Devirtualizer::do_oop(closure, p);
105   }
106 }
107 
108 template <typename T, class OopClosureType>
109 ALWAYSINLINE void InstanceKlass::oop_oop_iterate_oop_map_reverse(OopMapBlock* map, oop obj, OopClosureType* closure) {
110   T* const start = obj->field_addr<T>(map->offset());
111   T*       p     = start + map->count();
112 
113   while (start < p) {
114     --p;
115     Devirtualizer::do_oop(closure, p);
116   }
117 }
118 
119 template <typename T, class OopClosureType>
120 ALWAYSINLINE void InstanceKlass::oop_oop_iterate_oop_map_bounded(OopMapBlock* map, oop obj, OopClosureType* closure, MemRegion mr) {
121   T* p   = obj->field_addr<T>(map->offset());
122   T* end = p + map->count();
123 
124   T* const l   = (T*)mr.start();
125   T* const h   = (T*)mr.end();
126   assert(mask_bits((intptr_t)l, sizeof(T)-1) == 0 &&
127          mask_bits((intptr_t)h, sizeof(T)-1) == 0,
128          "bounded region must be properly aligned");
129 
130   if (p < l) {
131     p = l;
132   }
133   if (end > h) {
134     end = h;
135   }
136 
137   for (;p < end; ++p) {
138     Devirtualizer::do_oop(closure, p);
139   }
140 }
141 
142 template <typename T, class OopClosureType>
143 ALWAYSINLINE void InstanceKlass::oop_oop_iterate_oop_maps(oop obj, OopClosureType* closure) {
144   OopMapBlock* map           = start_of_nonstatic_oop_maps();
145   OopMapBlock* const end_map = map + nonstatic_oop_map_count();
146 
147   for (; map < end_map; ++map) {
148     oop_oop_iterate_oop_map<T>(map, obj, closure);
149   }
150 }
151 
152 template <typename T, class OopClosureType>
153 ALWAYSINLINE void InstanceKlass::oop_oop_iterate_oop_maps_reverse(oop obj, OopClosureType* closure) {
154   OopMapBlock* const start_map = start_of_nonstatic_oop_maps();
155   OopMapBlock* map             = start_map + nonstatic_oop_map_count();
156 
157   while (start_map < map) {
158     --map;
159     oop_oop_iterate_oop_map_reverse<T>(map, obj, closure);
160   }
161 }
162 
163 template <typename T, class OopClosureType>
164 ALWAYSINLINE void InstanceKlass::oop_oop_iterate_oop_maps_bounded(oop obj, OopClosureType* closure, MemRegion mr) {
165   OopMapBlock* map           = start_of_nonstatic_oop_maps();
166   OopMapBlock* const end_map = map + nonstatic_oop_map_count();
167 
168   for (;map < end_map; ++map) {
169     oop_oop_iterate_oop_map_bounded<T>(map, obj, closure, mr);
170   }
171 }
172 
173 template <typename T, class OopClosureType>
174 ALWAYSINLINE void InstanceKlass::oop_oop_iterate(oop obj, OopClosureType* closure) {
175   if (Devirtualizer::do_metadata(closure)) {
176     Devirtualizer::do_klass(closure, this);
177   }
178 
179   oop_oop_iterate_oop_maps<T>(obj, closure);
180 }
181 
182 template <typename T, class OopClosureType>
183 ALWAYSINLINE void InstanceKlass::oop_oop_iterate_reverse(oop obj, OopClosureType* closure) {
184   assert(!Devirtualizer::do_metadata(closure),
185       "Code to handle metadata is not implemented");
186 
187   oop_oop_iterate_oop_maps_reverse<T>(obj, closure);
188 }
189 
190 template <typename T, class OopClosureType>
191 ALWAYSINLINE void InstanceKlass::oop_oop_iterate_bounded(oop obj, OopClosureType* closure, MemRegion mr) {
192   if (Devirtualizer::do_metadata(closure)) {
193     if (mr.contains(obj)) {
194       Devirtualizer::do_klass(closure, this);
195     }
196   }
197 
198   oop_oop_iterate_oop_maps_bounded<T>(obj, closure, mr);
199 }
200 
201 #endif // SHARE_OOPS_INSTANCEKLASS_INLINE_HPP