1 /*
  2  * Copyright (c) 2014, 2019, 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 "oops/access.inline.hpp"
 32 #include "oops/compressedOops.inline.hpp"
 33 #include "oops/klass.hpp"

 34 #include "oops/instanceKlass.inline.hpp"
 35 #include "oops/instanceMirrorKlass.inline.hpp"
 36 #include "oops/instanceClassLoaderKlass.inline.hpp"
 37 #include "oops/instanceRefKlass.inline.hpp"
 38 #include "oops/objArrayKlass.inline.hpp"
 39 #include "oops/typeArrayKlass.inline.hpp"
 40 #include "utilities/debug.hpp"
 41 
 42 // Defaults to strong claiming.
 43 inline MetadataVisitingOopIterateClosure::MetadataVisitingOopIterateClosure(ReferenceDiscoverer* rd) :
 44     ClaimMetadataVisitingOopIterateClosure(ClassLoaderData::_claim_strong, rd) {}
 45 
 46 inline void ClaimMetadataVisitingOopIterateClosure::do_cld(ClassLoaderData* cld) {
 47   cld->oops_do(this, _claim);
 48 }
 49 
 50 inline void ClaimMetadataVisitingOopIterateClosure::do_klass(Klass* k) {
 51   ClassLoaderData* cld = k->class_loader_data();
 52   ClaimMetadataVisitingOopIterateClosure::do_cld(cld);
 53 }
 54 
 55 // Implementation of the non-virtual do_oop dispatch.
 56 //
 57 // The same implementation is used for do_metadata, do_klass, and do_cld.
 58 //
 59 // Preconditions:
 60 //  - Base has a pure virtual do_oop
 61 //  - Only one of the classes in the inheritance chain from OopClosureType to
 62 //    Base implements do_oop.
 63 //
 64 // Given the preconditions:
 65 //  - If &OopClosureType::do_oop is resolved to &Base::do_oop, then there is no
 66 //    implementation of do_oop between Base and OopClosureType. However, there
 67 //    must be one implementation in one of the subclasses of OopClosureType.
 68 //    In this case we take the virtual call.
 69 //
 70 //  - Conversely, if &OopClosureType::do_oop is not resolved to &Base::do_oop,
 71 //    then we've found the one and only concrete implementation. In this case we
 72 //    take a non-virtual call.
 73 //
 74 // Because of this it's clear when we should call the virtual call and
 75 //   when the non-virtual call should be made.
 76 //
 77 // The way we find if &OopClosureType::do_oop is resolved to &Base::do_oop is to
 78 //   check if the resulting type of the class of a member-function pointer to
 79 //   &OopClosureType::do_oop is equal to the type of the class of a
 80 //   &Base::do_oop member-function pointer. Template parameter deduction is used
 81 //   to find these types, and then the IsSame trait is used to check if they are
 82 //   equal. Finally, SFINAE is used to select the appropriate implementation.
 83 //
 84 // Template parameters:
 85 //   T              - narrowOop or oop
 86 //   Receiver       - the resolved type of the class of the
 87 //                    &OopClosureType::do_oop member-function pointer. That is,
 88 //                    the klass with the do_oop member function.
 89 //   Base           - klass with the pure virtual do_oop member function.
 90 //   OopClosureType - The dynamic closure type
 91 //
 92 // Parameters:
 93 //   closure - The closure to call
 94 //   p       - The oop (or narrowOop) field to pass to the closure
 95 
 96 template <typename T, typename Receiver, typename Base, typename OopClosureType>
 97 static typename EnableIf<IsSame<Receiver, Base>::value, void>::type
 98 call_do_oop(void (Receiver::*)(T*), void (Base::*)(T*), OopClosureType* closure, T* p) {
 99   closure->do_oop(p);
100 }
101 
102 template <typename T, typename Receiver, typename Base, typename OopClosureType>
103 static typename EnableIf<!IsSame<Receiver, Base>::value, void>::type
104 call_do_oop(void (Receiver::*)(T*), void (Base::*)(T*), OopClosureType* closure, T* p) {
105   // Sanity check
106   STATIC_ASSERT((!IsSame<OopClosureType, OopIterateClosure>::value));
107   closure->OopClosureType::do_oop(p);
108 }
109 
110 template <typename OopClosureType, typename T>
111 inline void Devirtualizer::do_oop(OopClosureType* closure, T* p) {
112   call_do_oop<T>(&OopClosureType::do_oop, &OopClosure::do_oop, closure, p);
113 }
114 
115 // Implementation of the non-virtual do_metadata dispatch.
116 
117 template <typename Receiver, typename Base, typename OopClosureType>
118 static typename EnableIf<IsSame<Receiver, Base>::value, bool>::type
119 call_do_metadata(bool (Receiver::*)(), bool (Base::*)(), OopClosureType* closure) {
120   return closure->do_metadata();
121 }
122 
123 template <typename Receiver, typename Base, typename OopClosureType>
124 static typename EnableIf<!IsSame<Receiver, Base>::value, bool>::type
125 call_do_metadata(bool (Receiver::*)(), bool (Base::*)(), OopClosureType* closure) {
126   return closure->OopClosureType::do_metadata();
127 }
128 
129 template <typename OopClosureType>
130 inline bool Devirtualizer::do_metadata(OopClosureType* closure) {
131   return call_do_metadata(&OopClosureType::do_metadata, &OopIterateClosure::do_metadata, closure);
132 }
133 
134 // Implementation of the non-virtual do_klass dispatch.
135 
136 template <typename Receiver, typename Base, typename OopClosureType>
137 static typename EnableIf<IsSame<Receiver, Base>::value, void>::type
138 call_do_klass(void (Receiver::*)(Klass*), void (Base::*)(Klass*), OopClosureType* closure, Klass* k) {
139   closure->do_klass(k);
140 }
141 
142 template <typename Receiver, typename Base, typename OopClosureType>
143 static typename EnableIf<!IsSame<Receiver, Base>::value, void>::type
144 call_do_klass(void (Receiver::*)(Klass*), void (Base::*)(Klass*), OopClosureType* closure, Klass* k) {
145   closure->OopClosureType::do_klass(k);
146 }
147 
148 template <typename OopClosureType>
149 inline void Devirtualizer::do_klass(OopClosureType* closure, Klass* k) {
150   call_do_klass(&OopClosureType::do_klass, &OopIterateClosure::do_klass, closure, k);
151 }
152 
153 // Implementation of the non-virtual do_cld dispatch.
154 
155 template <typename Receiver, typename Base, typename OopClosureType>
156 static typename EnableIf<IsSame<Receiver, Base>::value, void>::type
157 call_do_cld(void (Receiver::*)(ClassLoaderData*), void (Base::*)(ClassLoaderData*), OopClosureType* closure, ClassLoaderData* cld) {
158   closure->do_cld(cld);
159 }
160 
161 template <typename Receiver, typename Base, typename OopClosureType>
162 static typename EnableIf<!IsSame<Receiver, Base>::value, void>::type
163 call_do_cld(void (Receiver::*)(ClassLoaderData*), void (Base::*)(ClassLoaderData*), OopClosureType* closure, ClassLoaderData* cld) {
164   closure->OopClosureType::do_cld(cld);
165 }
166 
167 template <typename OopClosureType>
168 void Devirtualizer::do_cld(OopClosureType* closure, ClassLoaderData* cld) {
169   call_do_cld(&OopClosureType::do_cld, &OopIterateClosure::do_cld, closure, cld);
170 }
171 
172 // Dispatch table implementation for *Klass::oop_oop_iterate
173 //
174 // It allows for a single call to do a multi-dispatch to an optimized version
175 //   of oop_oop_iterate that statically know all these types:
176 //   - OopClosureType    : static type give at call site
177 //   - Klass*            : dynamic to static type through Klass::id() -> table index
178 //   - UseCompressedOops : dynamic to static value determined once
179 //
180 // when users call obj->oop_iterate(&cl).
181 //
182 // oopDesc::oop_iterate() calls OopOopIterateDispatch::function(klass)(cl, obj, klass),
183 //   which dispatches to an optimized version of
184 //   [Instance, ObjArry, etc]Klass::oop_oop_iterate(oop, OopClosureType)
185 //
186 // OopClosureType :
187 //   If OopClosureType has an implementation of do_oop (and do_metadata et.al.),
188 //   then the static type of OopClosureType will be used to allow inlining of
189 //   do_oop (even though do_oop is virtual). Otherwise, a virtual call will be
190 //   used when calling do_oop.
191 //
192 // Klass* :
193 //   A table mapping from *Klass::ID to function is setup. This happens once
194 //   when the program starts, when the static _table instance is initialized for
195 //   the OopOopIterateDispatch specialized with the OopClosureType.
196 //
197 // UseCompressedOops :
198 //   Initially the table is populated with an init function, and not the actual
199 //   oop_oop_iterate function. This is done, so that the first time we dispatch
200 //   through the init function we check what the value of UseCompressedOops
201 //   became, and use that to determine if we should install an optimized
202 //   narrowOop version or optimized oop version of oop_oop_iterate. The appropriate
203 //   oop_oop_iterate function replaces the init function in the table, and
204 //   succeeding calls will jump directly to oop_oop_iterate.
205 
206 
207 template <typename OopClosureType>
208 class OopOopIterateDispatch : public AllStatic {
209 private:
210   typedef void (*FunctionType)(OopClosureType*, oop, Klass*);
211 
212   class Table {
213   private:
214     template <typename KlassType, typename T>
215     static void oop_oop_iterate(OopClosureType* cl, oop obj, Klass* k) {
216       ((KlassType*)k)->KlassType::template oop_oop_iterate<T>(obj, cl);
217     }
218 
219     template <typename KlassType>
220     static void init(OopClosureType* cl, oop obj, Klass* k) {
221       OopOopIterateDispatch<OopClosureType>::_table.set_resolve_function_and_execute<KlassType>(cl, obj, k);
222     }
223 
224     template <typename KlassType>
225     void set_init_function() {
226       _function[KlassType::ID] = &init<KlassType>;
227     }
228 
229     template <typename KlassType>
230     void set_resolve_function() {
231       // Size requirement to prevent word tearing
232       // when functions pointers are updated.
233       STATIC_ASSERT(sizeof(_function[0]) == sizeof(void*));
234       if (UseCompressedOops) {
235         _function[KlassType::ID] = &oop_oop_iterate<KlassType, narrowOop>;
236       } else {
237         _function[KlassType::ID] = &oop_oop_iterate<KlassType, oop>;
238       }
239     }
240 
241     template <typename KlassType>
242     void set_resolve_function_and_execute(OopClosureType* cl, oop obj, Klass* k) {
243       set_resolve_function<KlassType>();
244       _function[KlassType::ID](cl, obj, k);
245     }
246 
247   public:
248     FunctionType _function[KLASS_ID_COUNT];
249 
250     Table(){
251       set_init_function<InstanceKlass>();
252       set_init_function<InstanceRefKlass>();
253       set_init_function<InstanceMirrorKlass>();
254       set_init_function<InstanceClassLoaderKlass>();
255       set_init_function<ObjArrayKlass>();
256       set_init_function<TypeArrayKlass>();

257     }
258   };
259 
260   static Table _table;
261 public:
262 
263   static FunctionType function(Klass* klass) {
264     return _table._function[klass->id()];
265   }
266 };
267 
268 template <typename OopClosureType>
269 typename OopOopIterateDispatch<OopClosureType>::Table OopOopIterateDispatch<OopClosureType>::_table;
270 
271 
272 template <typename OopClosureType>
273 class OopOopIterateBoundedDispatch {
274 private:
275   typedef void (*FunctionType)(OopClosureType*, oop, Klass*, MemRegion);
276 
277   class Table {
278   private:
279     template <typename KlassType, typename T>
280     static void oop_oop_iterate_bounded(OopClosureType* cl, oop obj, Klass* k, MemRegion mr) {
281       ((KlassType*)k)->KlassType::template oop_oop_iterate_bounded<T>(obj, cl, mr);
282     }
283 
284     template <typename KlassType>
285     static void init(OopClosureType* cl, oop obj, Klass* k, MemRegion mr) {
286       OopOopIterateBoundedDispatch<OopClosureType>::_table.set_resolve_function_and_execute<KlassType>(cl, obj, k, mr);
287     }
288 
289     template <typename KlassType>
290     void set_init_function() {
291       _function[KlassType::ID] = &init<KlassType>;
292     }
293 
294     template <typename KlassType>
295     void set_resolve_function() {
296       if (UseCompressedOops) {
297         _function[KlassType::ID] = &oop_oop_iterate_bounded<KlassType, narrowOop>;
298       } else {
299         _function[KlassType::ID] = &oop_oop_iterate_bounded<KlassType, oop>;
300       }
301     }
302 
303     template <typename KlassType>
304     void set_resolve_function_and_execute(OopClosureType* cl, oop obj, Klass* k, MemRegion mr) {
305       set_resolve_function<KlassType>();
306       _function[KlassType::ID](cl, obj, k, mr);
307     }
308 
309   public:
310     FunctionType _function[KLASS_ID_COUNT];
311 
312     Table(){
313       set_init_function<InstanceKlass>();
314       set_init_function<InstanceRefKlass>();
315       set_init_function<InstanceMirrorKlass>();
316       set_init_function<InstanceClassLoaderKlass>();
317       set_init_function<ObjArrayKlass>();
318       set_init_function<TypeArrayKlass>();

319     }
320   };
321 
322   static Table _table;
323 public:
324 
325   static FunctionType function(Klass* klass) {
326     return _table._function[klass->id()];
327   }
328 };
329 
330 template <typename OopClosureType>
331 typename OopOopIterateBoundedDispatch<OopClosureType>::Table OopOopIterateBoundedDispatch<OopClosureType>::_table;
332 
333 
334 template <typename OopClosureType>
335 class OopOopIterateBackwardsDispatch {
336 private:
337   typedef void (*FunctionType)(OopClosureType*, oop, Klass*);
338 
339   class Table {
340   private:
341     template <typename KlassType, typename T>
342     static void oop_oop_iterate_backwards(OopClosureType* cl, oop obj, Klass* k) {
343       ((KlassType*)k)->KlassType::template oop_oop_iterate_reverse<T>(obj, cl);
344     }
345 
346     template <typename KlassType>
347     static void init(OopClosureType* cl, oop obj, Klass* k) {
348       OopOopIterateBackwardsDispatch<OopClosureType>::_table.set_resolve_function_and_execute<KlassType>(cl, obj, k);
349     }
350 
351     template <typename KlassType>
352     void set_init_function() {
353       _function[KlassType::ID] = &init<KlassType>;
354     }
355 
356     template <typename KlassType>
357     void set_resolve_function() {
358       if (UseCompressedOops) {
359         _function[KlassType::ID] = &oop_oop_iterate_backwards<KlassType, narrowOop>;
360       } else {
361         _function[KlassType::ID] = &oop_oop_iterate_backwards<KlassType, oop>;
362       }
363     }
364 
365     template <typename KlassType>
366     void set_resolve_function_and_execute(OopClosureType* cl, oop obj, Klass* k) {
367       set_resolve_function<KlassType>();
368       _function[KlassType::ID](cl, obj, k);
369     }
370 
371   public:
372     FunctionType _function[KLASS_ID_COUNT];
373 
374     Table(){
375       set_init_function<InstanceKlass>();
376       set_init_function<InstanceRefKlass>();
377       set_init_function<InstanceMirrorKlass>();
378       set_init_function<InstanceClassLoaderKlass>();
379       set_init_function<ObjArrayKlass>();
380       set_init_function<TypeArrayKlass>();

381     }
382   };
383 
384   static Table _table;
385 public:
386 
387   static FunctionType function(Klass* klass) {
388     return _table._function[klass->id()];
389   }
390 };
391 
392 template <typename OopClosureType>
393 typename OopOopIterateBackwardsDispatch<OopClosureType>::Table OopOopIterateBackwardsDispatch<OopClosureType>::_table;
394 
395 
396 template <typename OopClosureType>
397 void OopIteratorClosureDispatch::oop_oop_iterate(OopClosureType* cl, oop obj, Klass* klass) {
398   OopOopIterateDispatch<OopClosureType>::function(klass)(cl, obj, klass);
399 }
400 
401 template <typename OopClosureType>
402 void OopIteratorClosureDispatch::oop_oop_iterate(OopClosureType* cl, oop obj, Klass* klass, MemRegion mr) {
403   OopOopIterateBoundedDispatch<OopClosureType>::function(klass)(cl, obj, klass, mr);
404 }
405 
406 template <typename OopClosureType>
407 void OopIteratorClosureDispatch::oop_oop_iterate_backwards(OopClosureType* cl, oop obj, Klass* klass) {
408   OopOopIterateBackwardsDispatch<OopClosureType>::function(klass)(cl, obj, klass);
409 }
410 
411 #endif // SHARE_MEMORY_ITERATOR_INLINE_HPP
--- EOF ---