1 /*
  2  * Copyright (c) 2014, 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_MEMORY_ITERATOR_INLINE_HPP
 26 #define SHARE_MEMORY_ITERATOR_INLINE_HPP
 27 
 28 #include "memory/iterator.hpp"
 29 
 30 #include "classfile/classLoaderData.hpp"
 31 #include "code/nmethod.hpp"
 32 #include "oops/access.inline.hpp"
 33 #include "oops/compressedOops.inline.hpp"
 34 #include "oops/flatArrayKlass.inline.hpp"
 35 #include "oops/instanceClassLoaderKlass.inline.hpp"
 36 #include "oops/instanceKlass.inline.hpp"
 37 #include "oops/instanceMirrorKlass.inline.hpp"
 38 #include "oops/instanceRefKlass.inline.hpp"
 39 #include "oops/instanceStackChunkKlass.inline.hpp"
 40 #include "oops/klass.hpp"
 41 #include "oops/objArrayKlass.inline.hpp"
 42 #include "oops/refArrayKlass.inline.hpp"
 43 #include "oops/typeArrayKlass.inline.hpp"
 44 #include "utilities/debug.hpp"
 45 
 46 // Defaults to strong claiming.
 47 inline MetadataVisitingOopIterateClosure::MetadataVisitingOopIterateClosure(ReferenceDiscoverer* rd) :
 48     ClaimMetadataVisitingOopIterateClosure(ClassLoaderData::_claim_strong, rd) {}
 49 
 50 inline void ClaimMetadataVisitingOopIterateClosure::do_cld(ClassLoaderData* cld) {
 51   cld->oops_do(this, _claim);
 52 }
 53 
 54 inline void ClaimMetadataVisitingOopIterateClosure::do_klass(Klass* k) {
 55   ClaimMetadataVisitingOopIterateClosure::do_cld(k->class_loader_data());
 56 }
 57 
 58 inline void ClaimMetadataVisitingOopIterateClosure::do_nmethod(nmethod* nm) {
 59   nm->follow_nmethod(this);
 60 }
 61 
 62 inline void ClaimMetadataVisitingOopIterateClosure::do_method(Method* m) {
 63   // Mark interpreted frames for class redefinition
 64   m->record_gc_epoch();
 65 }
 66 
 67 // Dispatch table implementation for *Klass::oop_oop_iterate
 68 //
 69 // It allows for a single call to do a multi-dispatch to an optimized version
 70 //   of oop_oop_iterate that statically know all these types:
 71 //   - OopClosureType    : static type give at call site
 72 //   - Klass*            : dynamic to static type through Klass::kind() -> table index
 73 //   - UseCompressedOops : dynamic to static value determined once
 74 //
 75 // when users call obj->oop_iterate(&cl).
 76 //
 77 // oopDesc::oop_iterate() calls OopOopIterateDispatch::function(klass)(cl, obj, klass),
 78 //   which dispatches to an optimized version of
 79 //   [Instance, ObjArry, etc]Klass::oop_oop_iterate(oop, OopClosureType)
 80 //
 81 // OopClosureType :
 82 //   If OopClosureType has an implementation of do_oop (and do_metadata et.al.),
 83 //   then the static type of OopClosureType will be used to allow inlining of
 84 //   do_oop (even though do_oop is virtual). Otherwise, a virtual call will be
 85 //   used when calling do_oop.
 86 //
 87 // Klass* :
 88 //   A table mapping from *Klass::Kind to function is setup. This happens once
 89 //   when the program starts, when the static _table instance is initialized for
 90 //   the OopOopIterateDispatch specialized with the OopClosureType.
 91 //
 92 // UseCompressedOops :
 93 //   Initially the table is populated with an init function, and not the actual
 94 //   oop_oop_iterate function. This is done, so that the first time we dispatch
 95 //   through the init function we check what the value of UseCompressedOops
 96 //   became, and use that to determine if we should install an optimized
 97 //   narrowOop version or optimized oop version of oop_oop_iterate. The appropriate
 98 //   oop_oop_iterate function replaces the init function in the table, and
 99 //   succeeding calls will jump directly to oop_oop_iterate.
100 
101 
102 template <typename OopClosureType>
103 class OopOopIterateDispatch : public AllStatic {
104 private:
105   typedef void (*FunctionType)(OopClosureType*, oop, Klass*);
106 
107   class Table {
108   private:
109     template <typename KlassType, typename T>
110     static void oop_oop_iterate(OopClosureType* cl, oop obj, Klass* k) {
111       ((KlassType*)k)->KlassType::template oop_oop_iterate<T>(obj, cl);
112     }
113 
114     template <typename KlassType>
115     static void init(OopClosureType* cl, oop obj, Klass* k) {
116       OopOopIterateDispatch<OopClosureType>::_table.set_resolve_function_and_execute<KlassType>(cl, obj, k);
117     }
118 
119     template <typename KlassType>
120     void set_init_function() {
121       _function[KlassType::Kind] = &init<KlassType>;
122     }
123 
124     template <typename KlassType>
125     void set_resolve_function() {
126       // Size requirement to prevent word tearing
127       // when functions pointers are updated.
128       STATIC_ASSERT(sizeof(_function[0]) == sizeof(void*));
129       if (UseCompressedOops) {
130         _function[KlassType::Kind] = &oop_oop_iterate<KlassType, narrowOop>;
131       } else {
132         _function[KlassType::Kind] = &oop_oop_iterate<KlassType, oop>;
133       }
134     }
135 
136     template <typename KlassType>
137     void set_resolve_function_and_execute(OopClosureType* cl, oop obj, Klass* k) {
138       set_resolve_function<KlassType>();
139       _function[KlassType::Kind](cl, obj, k);
140     }
141 
142   public:
143     FunctionType _function[Klass::KLASS_KIND_COUNT];
144 
145     Table(){
146       set_init_function<InstanceKlass>();
147       set_init_function<InlineKlass>();
148       set_init_function<InstanceRefKlass>();
149       set_init_function<InstanceMirrorKlass>();
150       set_init_function<InstanceClassLoaderKlass>();
151       set_init_function<InstanceStackChunkKlass>();
152       set_init_function<ObjArrayKlass>();
153       set_init_function<TypeArrayKlass>();
154       set_init_function<FlatArrayKlass>();
155       set_init_function<RefArrayKlass>();
156     }
157   };
158 
159   static Table _table;
160 public:
161 
162   static FunctionType function(Klass* klass) {
163     return _table._function[klass->kind()];
164   }
165 };
166 
167 template <typename OopClosureType>
168 typename OopOopIterateDispatch<OopClosureType>::Table OopOopIterateDispatch<OopClosureType>::_table;
169 
170 
171 template <typename OopClosureType>
172 class OopOopIterateBoundedDispatch {
173 private:
174   typedef void (*FunctionType)(OopClosureType*, oop, Klass*, MemRegion);
175 
176   class Table {
177   private:
178     template <typename KlassType, typename T>
179     static void oop_oop_iterate_bounded(OopClosureType* cl, oop obj, Klass* k, MemRegion mr) {
180       ((KlassType*)k)->KlassType::template oop_oop_iterate_bounded<T>(obj, cl, mr);
181     }
182 
183     template <typename KlassType>
184     static void init(OopClosureType* cl, oop obj, Klass* k, MemRegion mr) {
185       OopOopIterateBoundedDispatch<OopClosureType>::_table.set_resolve_function_and_execute<KlassType>(cl, obj, k, mr);
186     }
187 
188     template <typename KlassType>
189     void set_init_function() {
190       _function[KlassType::Kind] = &init<KlassType>;
191     }
192 
193     template <typename KlassType>
194     void set_resolve_function() {
195       if (UseCompressedOops) {
196         _function[KlassType::Kind] = &oop_oop_iterate_bounded<KlassType, narrowOop>;
197       } else {
198         _function[KlassType::Kind] = &oop_oop_iterate_bounded<KlassType, oop>;
199       }
200     }
201 
202     template <typename KlassType>
203     void set_resolve_function_and_execute(OopClosureType* cl, oop obj, Klass* k, MemRegion mr) {
204       set_resolve_function<KlassType>();
205       _function[KlassType::Kind](cl, obj, k, mr);
206     }
207 
208   public:
209     FunctionType _function[Klass::KLASS_KIND_COUNT];
210 
211     Table(){
212       set_init_function<InstanceKlass>();
213       set_init_function<InlineKlass>();
214       set_init_function<InstanceRefKlass>();
215       set_init_function<InstanceMirrorKlass>();
216       set_init_function<InstanceClassLoaderKlass>();
217       set_init_function<InstanceStackChunkKlass>();
218       set_init_function<ObjArrayKlass>();
219       set_init_function<TypeArrayKlass>();
220       set_init_function<FlatArrayKlass>();
221       set_init_function<RefArrayKlass>();
222     }
223   };
224 
225   static Table _table;
226 public:
227 
228   static FunctionType function(Klass* klass) {
229     return _table._function[klass->kind()];
230   }
231 };
232 
233 template <typename OopClosureType>
234 typename OopOopIterateBoundedDispatch<OopClosureType>::Table OopOopIterateBoundedDispatch<OopClosureType>::_table;
235 
236 
237 template <typename OopClosureType>
238 class OopOopIterateBackwardsDispatch {
239 private:
240   typedef void (*FunctionType)(OopClosureType*, oop, Klass*);
241 
242   class Table {
243   private:
244     template <typename KlassType, typename T>
245     static void oop_oop_iterate_backwards(OopClosureType* cl, oop obj, Klass* k) {
246       ((KlassType*)k)->KlassType::template oop_oop_iterate_reverse<T>(obj, cl);
247     }
248 
249     template <typename KlassType>
250     static void init(OopClosureType* cl, oop obj, Klass* k) {
251       OopOopIterateBackwardsDispatch<OopClosureType>::_table.set_resolve_function_and_execute<KlassType>(cl, obj, k);
252     }
253 
254     template <typename KlassType>
255     void set_init_function() {
256       _function[KlassType::Kind] = &init<KlassType>;
257     }
258 
259     template <typename KlassType>
260     void set_resolve_function() {
261       if (UseCompressedOops) {
262         _function[KlassType::Kind] = &oop_oop_iterate_backwards<KlassType, narrowOop>;
263       } else {
264         _function[KlassType::Kind] = &oop_oop_iterate_backwards<KlassType, oop>;
265       }
266     }
267 
268     template <typename KlassType>
269     void set_resolve_function_and_execute(OopClosureType* cl, oop obj, Klass* k) {
270       set_resolve_function<KlassType>();
271       _function[KlassType::Kind](cl, obj, k);
272     }
273 
274   public:
275     FunctionType _function[Klass::KLASS_KIND_COUNT];
276 
277     Table(){
278       set_init_function<InstanceKlass>();
279       set_init_function<InlineKlass>();
280       set_init_function<InstanceRefKlass>();
281       set_init_function<InstanceMirrorKlass>();
282       set_init_function<InstanceClassLoaderKlass>();
283       set_init_function<InstanceStackChunkKlass>();
284       set_init_function<ObjArrayKlass>();
285       set_init_function<TypeArrayKlass>();
286       set_init_function<FlatArrayKlass>();
287       set_init_function<RefArrayKlass>();
288     }
289   };
290 
291   static Table _table;
292 public:
293 
294   static FunctionType function(Klass* klass) {
295     return _table._function[klass->kind()];
296   }
297 };
298 
299 template <typename OopClosureType>
300 typename OopOopIterateBackwardsDispatch<OopClosureType>::Table OopOopIterateBackwardsDispatch<OopClosureType>::_table;
301 
302 
303 template <typename OopClosureType>
304 void OopIteratorClosureDispatch::oop_oop_iterate(OopClosureType* cl, oop obj, Klass* klass) {
305   OopOopIterateDispatch<OopClosureType>::function(klass)(cl, obj, klass);
306 }
307 
308 template <typename OopClosureType>
309 void OopIteratorClosureDispatch::oop_oop_iterate(OopClosureType* cl, oop obj, Klass* klass, MemRegion mr) {
310   OopOopIterateBoundedDispatch<OopClosureType>::function(klass)(cl, obj, klass, mr);
311 }
312 
313 template <typename OopClosureType>
314 void OopIteratorClosureDispatch::oop_oop_iterate_backwards(OopClosureType* cl, oop obj, Klass* klass) {
315   OopOopIterateBackwardsDispatch<OopClosureType>::function(klass)(cl, obj, klass);
316 }
317 
318 #endif // SHARE_MEMORY_ITERATOR_INLINE_HPP