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/refArrayOop.inline.hpp"
 35 #include "oops/oop.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   T *p = (T *)a->base();
 43   T *const end = p + a->length();
 44 
 45   for (; p < end; p++) {
 46     Devirtualizer::do_oop(closure, p);
 47   }
 48 }
 49 
 50 template <typename T, class OopClosureType>
 51 void RefArrayKlass::oop_oop_iterate_elements_bounded(refArrayOop a,
 52                                                      OopClosureType *closure,
 53                                                      void *low, void *high) {
 54 
 55   T *const l = (T *)low;
 56   T *const h = (T *)high;
 57 
 58   T *p = (T *)a->base();
 59   T *end = p + a->length();
 60 
 61   if (p < l) {
 62     p = l;
 63   }
 64   if (end > h) {
 65     end = h;
 66   }
 67 
 68   for (; p < end; ++p) {
 69     Devirtualizer::do_oop(closure, p);
 70   }
 71 }
 72 
 73 template <typename T, typename OopClosureType>
 74 void RefArrayKlass::oop_oop_iterate(oop obj, OopClosureType *closure) {
 75   assert(obj->is_array(), "obj must be array");
 76   refArrayOop a = refArrayOop(obj);
 77 
 78   if (Devirtualizer::do_metadata(closure)) {
 79     Devirtualizer::do_klass(closure, obj->klass());
 80   }
 81 
 82   oop_oop_iterate_elements<T>(a, closure);
 83 }
 84 
 85 template <typename T, typename OopClosureType>
 86 void RefArrayKlass::oop_oop_iterate_reverse(oop obj, OopClosureType *closure) {
 87   // No reverse implementation ATM.
 88   oop_oop_iterate<T>(obj, closure);
 89 }
 90 
 91 template <typename T, typename OopClosureType>
 92 void RefArrayKlass::oop_oop_iterate_bounded(oop obj, OopClosureType *closure,
 93                                             MemRegion mr) {
 94   assert(obj->is_array(), "obj must be array");
 95   refArrayOop a = refArrayOop(obj);
 96 
 97   if (Devirtualizer::do_metadata(closure)) {
 98     Devirtualizer::do_klass(closure, a->klass());
 99   }
100 
101   oop_oop_iterate_elements_bounded<T>(a, closure, mr.start(), mr.end());
102 }
103 
104 // Like oop_oop_iterate but only iterates over a specified range and only used
105 // for objArrayOops.
106 template <typename T, class OopClosureType>
107 void RefArrayKlass::oop_oop_iterate_range(refArrayOop a,
108                                           OopClosureType *closure, int start,
109                                           int end) {
110   T *low = (T *)a->base() + start;
111   T *high = (T *)a->base() + end;
112 
113   oop_oop_iterate_elements_bounded<T>(a, closure, low, high);
114 }
115 
116 // Placed here to resolve include cycle between objArrayKlass.inline.hpp and
117 // objArrayOop.inline.hpp
118 template <typename OopClosureType>
119 void refArrayOopDesc::oop_iterate_range(OopClosureType *blk, int start,
120                                         int end) {
121   if (UseCompressedOops) {
122     ((RefArrayKlass *)klass())
123         ->oop_oop_iterate_range<narrowOop>(this, blk, start, end);
124   } else {
125     ((RefArrayKlass *)klass())
126         ->oop_oop_iterate_range<oop>(this, blk, start, end);
127   }
128 }
129 
130 #endif // SHARE_OOPS_REFARRAYKLASS_INLINE_HPP