1 /*
  2  * Copyright (c) 2017, 2026, 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 #ifndef SHARE_VM_OOPS_FLATARRAYKLASS_INLINE_HPP
 25 #define SHARE_VM_OOPS_FLATARRAYKLASS_INLINE_HPP
 26 
 27 #include "oops/flatArrayKlass.hpp"
 28 
 29 #include "memory/iterator.hpp"
 30 #include "memory/memRegion.hpp"
 31 #include "oops/arrayKlass.hpp"
 32 #include "oops/flatArrayOop.hpp"
 33 #include "oops/flatArrayOop.inline.hpp"
 34 #include "oops/inlineKlass.hpp"
 35 #include "oops/inlineKlass.inline.hpp"
 36 #include "oops/klass.hpp"
 37 #include "oops/oop.inline.hpp"
 38 #include "utilities/devirtualizer.inline.hpp"
 39 #include "utilities/macros.hpp"
 40 
 41 template <typename T, class OopClosureType>
 42 void FlatArrayKlass::oop_oop_iterate_elements_specialized(flatArrayOop a,
 43                                                           OopClosureType* closure,
 44                                                           int start, int end) {
 45   precond(contains_oops());
 46   precond(start >= 0);
 47   assert(start <= end, "Invalid range [%d - %d)", start, end);
 48   assert(end <= a->length(), "Invalid range [%d - %d) for a.length: %d", start, end, a->length());
 49 
 50   const int shift = Klass::layout_helper_log2_element_size(layout_helper());
 51   const uintptr_t base = (uintptr_t) a->base();
 52   const uintptr_t start_addr = base + ((size_t)start << shift);
 53   const uintptr_t stop_addr = base + ((size_t)end << shift);
 54 
 55   oop_oop_iterate_elements_specialized_bounded<T>(a, closure, start_addr, stop_addr);
 56 }
 57 
 58 template <typename T, class OopClosureType>
 59 void FlatArrayKlass::oop_oop_iterate_elements_specialized_bounded(flatArrayOop a,
 60                                                                   OopClosureType* closure,
 61                                                                   uintptr_t lo, uintptr_t hi) {
 62   assert(contains_oops(), "Nothing to iterate");
 63 
 64   const int shift = Klass::layout_helper_log2_element_size(layout_helper());
 65   const int addr_incr = 1 << shift;
 66   uintptr_t elem_addr = (uintptr_t)a->base();
 67   uintptr_t stop_addr = elem_addr + ((uintptr_t)a->length() << shift);
 68   const int oop_offset = element_klass()->payload_offset();
 69 
 70   if (elem_addr < lo) {
 71     uintptr_t diff = lo - elem_addr;
 72     elem_addr += (diff >> shift) << shift;
 73   }
 74   if (stop_addr > hi) {
 75     uintptr_t diff = stop_addr - hi;
 76     stop_addr -= (diff >> shift) << shift;
 77   }
 78 
 79   const uintptr_t end = stop_addr;
 80   while (elem_addr < end) {
 81     element_klass()->oop_iterate_specialized_bounded<T>((address)(elem_addr - oop_offset), closure, lo, hi);
 82     elem_addr += addr_incr;
 83   }
 84 }
 85 
 86 template <typename T, class OopClosureType>
 87 void FlatArrayKlass::oop_oop_iterate_elements(flatArrayOop a, OopClosureType* closure) {
 88   if (contains_oops()) {
 89     oop_oop_iterate_elements_specialized<T>(a, closure, 0, a->length());
 90   }
 91 }
 92 
 93 template <typename T, typename OopClosureType>
 94 void FlatArrayKlass::oop_oop_iterate(oop obj, OopClosureType* closure) {
 95   assert(obj->is_flatArray(), "must be a flat array");
 96   flatArrayOop a = flatArrayOop(obj);
 97 
 98   if (Devirtualizer::do_metadata(closure)) {
 99     Devirtualizer::do_klass(closure, obj->klass());
100   }
101 
102   oop_oop_iterate_elements<T>(a, closure);
103 }
104 
105 template <typename T, typename OopClosureType>
106 void FlatArrayKlass::oop_oop_iterate_reverse(oop obj, OopClosureType* closure) {
107   // TODO
108   oop_oop_iterate<T>(obj, closure);
109 }
110 
111 template <typename T, class OopClosureType>
112 void FlatArrayKlass::oop_oop_iterate_elements_bounded(flatArrayOop a, OopClosureType* closure, MemRegion mr) {
113   if (contains_oops()) {
114     oop_oop_iterate_elements_specialized_bounded<T>(a, closure, (uintptr_t)mr.start(), (uintptr_t)mr.end());
115   }
116 }
117 
118 template <typename T, typename OopClosureType>
119 void FlatArrayKlass::oop_oop_iterate_bounded(oop obj, OopClosureType* closure, MemRegion mr) {
120   flatArrayOop a = flatArrayOop(obj);
121   if (Devirtualizer::do_metadata(closure)) {
122     Devirtualizer::do_klass(closure, a->klass());
123   }
124   oop_oop_iterate_elements_bounded<T>(a, closure, mr);
125 }
126 
127 // Like oop_oop_iterate but only iterates over the specified range [start, end)
128 template <typename T, class OopClosureType>
129 void FlatArrayKlass::oop_oop_iterate_elements_range(flatArrayOop a, OopClosureType *closure, int start, int end) {
130   if (contains_oops()) {
131     oop_oop_iterate_elements_specialized<T>(a, closure, start, end);
132   }
133 }
134 
135 #endif // SHARE_VM_OOPS_FLATARRAYKLASS_INLINE_HPP