1 /*
  2  * Copyright (c) 2010, 2024, 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_OBJARRAYKLASS_INLINE_HPP
 26 #define SHARE_OOPS_OBJARRAYKLASS_INLINE_HPP
 27 
 28 #include "oops/objArrayKlass.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/objArrayOop.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 ObjArrayKlass::oop_oop_iterate_elements(objArrayOop a, OopClosureType* closure) {
 41   oop_oop_iterate_elements_range<T>(a, closure, 0, a->length());
 42 }
 43 
 44 // Like oop_oop_iterate but only iterates over a specified range and only used
 45 // for objArrayOops.
 46 template <typename T, class OopClosureType>
 47 void ObjArrayKlass::oop_oop_iterate_elements_range(objArrayOop a, OopClosureType* closure, int start, int end) {
 48   T* base        = (T*)a->base();
 49   T* p           = base + start;
 50   T* const end_p = base + end;
 51 
 52   for (;p < end_p; ++p) {
 53     Devirtualizer::do_oop(closure, p);
 54   }
 55 }
 56 
 57 template <typename T, class OopClosureType>
 58 void ObjArrayKlass::oop_oop_iterate_elements_bounded(
 59     objArrayOop a, OopClosureType* closure, void* low, void* high) {
 60 
 61   T* const l = (T*)low;
 62   T* const h = (T*)high;
 63 
 64   T* p   = (T*)a->base();
 65   T* end = p + a->length();
 66 
 67   if (p < l) {
 68     p = l;
 69   }
 70   if (end > h) {
 71     end = h;
 72   }
 73 
 74   for (;p < end; ++p) {
 75     Devirtualizer::do_oop(closure, p);
 76   }
 77 }
 78 
 79 template <typename T, typename OopClosureType>
 80 void ObjArrayKlass::oop_oop_iterate(oop obj, OopClosureType* closure) {
 81   assert(obj->is_array(), "obj must be array");
 82   objArrayOop a = objArrayOop(obj);
 83 
 84   if (Devirtualizer::do_metadata(closure)) {
 85     Devirtualizer::do_klass(closure, obj->klass());
 86   }
 87 
 88   oop_oop_iterate_elements<T>(a, closure);
 89 }
 90 
 91 template <typename T, typename OopClosureType>
 92 void ObjArrayKlass::oop_oop_iterate_reverse(oop obj, OopClosureType* closure) {
 93   // No reverse implementation ATM.
 94   oop_oop_iterate<T>(obj, closure);
 95 }
 96 
 97 template <typename T, typename OopClosureType>
 98 void ObjArrayKlass::oop_oop_iterate_bounded(oop obj, OopClosureType* closure, MemRegion mr) {
 99   assert(obj->is_array(), "obj must be array");
100   objArrayOop a  = objArrayOop(obj);
101 
102   if (Devirtualizer::do_metadata(closure)) {
103     Devirtualizer::do_klass(closure, a->klass());
104   }
105 
106   oop_oop_iterate_elements_bounded<T>(a, closure, mr.start(), mr.end());
107 }
108 
109 #endif // SHARE_OOPS_OBJARRAYKLASS_INLINE_HPP