1 /*
  2  * Copyright (c) 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_REFARRAYKLASS_INLINE_HPP
 26 #define SHARE_OOPS_REFARRAYKLASS_INLINE_HPP
 27 
 28 #include "oops/refArrayKlass.hpp"
 29 
 30 #include "memory/memRegion.hpp"
 31 #include "oops/arrayKlass.hpp"
 32 #include "oops/arrayOop.hpp"
 33 #include "oops/klass.hpp"
 34 #include "oops/oop.inline.hpp"
 35 #include "oops/refArrayOop.inline.hpp"
 36 #include "utilities/devirtualizer.inline.hpp"
 37 #include "utilities/macros.hpp"
 38 
 39 template <typename T, class OopClosureType>
 40 void RefArrayKlass::oop_oop_iterate_elements(refArrayOop a,
 41                                              OopClosureType *closure) {
 42   oop_oop_iterate_elements_range<T>(a, closure, 0, a->length());
 43 }
 44 
 45 // Like oop_oop_iterate but only iterates over a specified range and only used
 46 // for refArrayOops.
 47 template <typename T, class OopClosureType>
 48 void RefArrayKlass::oop_oop_iterate_elements_range(refArrayOop a, OopClosureType* closure, int start, int end) {
 49   T* base        = (T*)a->base();
 50   T* p           = base + start;
 51   T* const end_p = base + end;
 52 
 53   assert(a->klass()->is_refArray_klass(), "must be refArray");
 54   for (;p < end_p; ++p) {
 55     Devirtualizer::do_oop(closure, p);
 56   }
 57 }
 58 
 59 
 60 template <typename T, class OopClosureType>
 61 void RefArrayKlass::oop_oop_iterate_elements_bounded(refArrayOop a,
 62                                                      OopClosureType *closure,
 63                                                      void *low, void *high) {
 64 
 65   T *const l = (T *)low;
 66   T *const h = (T *)high;
 67 
 68   T *p = (T *)a->base();
 69   T *end = p + a->length();
 70 
 71   if (p < l) {
 72     p = l;
 73   }
 74   if (end > h) {
 75     end = h;
 76   }
 77 
 78   for (; p < end; ++p) {
 79     Devirtualizer::do_oop(closure, p);
 80   }
 81 }
 82 
 83 template <typename T, typename OopClosureType>
 84 void RefArrayKlass::oop_oop_iterate(oop obj, OopClosureType *closure) {
 85   assert(obj->is_array(), "obj must be array");
 86   refArrayOop a = refArrayOop(obj);
 87 
 88   if (Devirtualizer::do_metadata(closure)) {
 89     Devirtualizer::do_klass(closure, obj->klass());
 90   }
 91 
 92   oop_oop_iterate_elements<T>(a, closure);
 93 }
 94 
 95 template <typename T, typename OopClosureType>
 96 void RefArrayKlass::oop_oop_iterate_reverse(oop obj, OopClosureType *closure) {
 97   // No reverse implementation ATM.
 98   oop_oop_iterate<T>(obj, closure);
 99 }
100 
101 template <typename T, typename OopClosureType>
102 void RefArrayKlass::oop_oop_iterate_bounded(oop obj, OopClosureType *closure,
103                                             MemRegion mr) {
104   assert(obj->is_array(), "obj must be array");
105   refArrayOop a = refArrayOop(obj);
106 
107   if (Devirtualizer::do_metadata(closure)) {
108     Devirtualizer::do_klass(closure, a->klass());
109   }
110 
111   oop_oop_iterate_elements_bounded<T>(a, closure, mr.start(), mr.end());
112 }
113 
114 #endif // SHARE_OOPS_REFARRAYKLASS_INLINE_HPP