1 /*
  2  * Copyright (c) 2017, 2020, 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 /*
 42  * Warning incomplete: requires embedded oops, not yet enabled, so consider this a "sketch-up" of oop iterators
 43  */
 44 
 45 template <typename T, class OopClosureType>
 46 void FlatArrayKlass::oop_oop_iterate_elements_specialized(flatArrayOop a,
 47                                                           OopClosureType* closure,
 48                                                           int start, int end) {
 49   precond(contains_oops());
 50   precond(start >= 0);
 51   assert(start <= end, "Invalid range [%d - %d)", start, end);
 52   assert(end <= a->length(), "Invalid range [%d - %d) for a.length: %d", start, end, a->length());
 53 
 54   const int shift = Klass::layout_helper_log2_element_size(layout_helper());
 55   const uintptr_t base = (uintptr_t) a->base();
 56   const uintptr_t start_addr = base + ((size_t)start << shift);
 57   const uintptr_t stop_addr = base + ((size_t)end << shift);
 58 
 59   oop_oop_iterate_elements_specialized_bounded<T>(a, closure, start_addr, stop_addr);
 60 }
 61 
 62 template <typename T, class OopClosureType>
 63 void FlatArrayKlass::oop_oop_iterate_elements_specialized_bounded(flatArrayOop a,
 64                                                                   OopClosureType* closure,
 65                                                                   uintptr_t lo, uintptr_t hi) {
 66   assert(contains_oops(), "Nothing to iterate");
 67 
 68   const int shift = Klass::layout_helper_log2_element_size(layout_helper());
 69   const int addr_incr = 1 << shift;
 70   uintptr_t elem_addr = (uintptr_t)a->base();
 71   uintptr_t stop_addr = elem_addr + ((uintptr_t)a->length() << shift);
 72   const int oop_offset = element_klass()->payload_offset();
 73 
 74   if (elem_addr < lo) {
 75     uintptr_t diff = lo - elem_addr;
 76     elem_addr += (diff >> shift) << shift;
 77   }
 78   if (stop_addr > hi) {
 79     uintptr_t diff = stop_addr - hi;
 80     stop_addr -= (diff >> shift) << shift;
 81   }
 82 
 83   const uintptr_t end = stop_addr;
 84   while (elem_addr < end) {
 85     element_klass()->oop_iterate_specialized_bounded<T>((address)(elem_addr - oop_offset), closure, lo, hi);
 86     elem_addr += addr_incr;
 87   }
 88 }
 89 
 90 template <typename T, class OopClosureType>
 91 void FlatArrayKlass::oop_oop_iterate_elements(flatArrayOop a, OopClosureType* closure) {
 92   if (contains_oops()) {
 93     oop_oop_iterate_elements_specialized<T>(a, closure, 0, a->length());
 94   }
 95 }
 96 
 97 template <typename T, typename OopClosureType>
 98 void FlatArrayKlass::oop_oop_iterate(oop obj, OopClosureType* closure) {
 99   assert(obj->klass()->is_flatArray_klass(),"must be a flat array");
100   flatArrayOop a = flatArrayOop(obj);
101 
102   if (Devirtualizer::do_metadata(closure)) {
103     Devirtualizer::do_klass(closure, obj->klass());
104     Devirtualizer::do_klass(closure, FlatArrayKlass::cast(obj->klass())->element_klass());
105   }
106 
107   oop_oop_iterate_elements<T>(a, closure);
108 }
109 
110 template <typename T, typename OopClosureType>
111 void FlatArrayKlass::oop_oop_iterate_reverse(oop obj, OopClosureType* closure) {
112   // TODO
113   oop_oop_iterate<T>(obj, closure);
114 }
115 
116 template <typename T, class OopClosureType>
117 void FlatArrayKlass::oop_oop_iterate_elements_bounded(flatArrayOop a, OopClosureType* closure, MemRegion mr) {
118   if (contains_oops()) {
119     oop_oop_iterate_elements_specialized_bounded<T>(a, closure, (uintptr_t)mr.start(), (uintptr_t)mr.end());
120   }
121 }
122 
123 
124 template <typename T, typename OopClosureType>
125 void FlatArrayKlass::oop_oop_iterate_bounded(oop obj, OopClosureType* closure, MemRegion mr) {
126   flatArrayOop a = flatArrayOop(obj);
127   if (Devirtualizer::do_metadata(closure)) {
128     Devirtualizer::do_klass(closure, a->klass());
129     Devirtualizer::do_klass(closure, FlatArrayKlass::cast(obj->klass())->element_klass());
130   }
131   oop_oop_iterate_elements_bounded<T>(a, closure, mr);
132 }
133 
134 // Like oop_oop_iterate but only iterates over the specified range [start, end)
135 template <typename T, class OopClosureType>
136 void FlatArrayKlass::oop_oop_iterate_elements_range(flatArrayOop a, OopClosureType *closure, int start, int end) {
137   if (contains_oops()) {
138     oop_oop_iterate_elements_specialized<T>(a, closure, start, end);
139   }
140 }
141 
142 #endif // SHARE_VM_OOPS_FLATARRAYKLASS_INLINE_HPP