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 "memory/memRegion.hpp"
 28 #include "memory/iterator.hpp"
 29 #include "oops/arrayKlass.hpp"
 30 #include "oops/flatArrayKlass.hpp"
 31 #include "oops/flatArrayOop.hpp"
 32 #include "oops/flatArrayOop.inline.hpp"
 33 #include "oops/inlineKlass.hpp"
 34 #include "oops/inlineKlass.inline.hpp"
 35 #include "oops/klass.hpp"
 36 #include "oops/oop.inline.hpp"
 37 #include "utilities/macros.hpp"
 38 
 39 /*
 40  * Warning incomplete: requires embedded oops, not yet enabled, so consider this a "sketch-up" of oop iterators
 41  */
 42 
 43 template <typename T, class OopClosureType>
 44 void FlatArrayKlass::oop_oop_iterate_elements_specialized(flatArrayOop a,
 45                                                           OopClosureType* closure) {
 46   assert(contains_oops(), "Nothing to iterate");
 47 
 48   const int shift = Klass::layout_helper_log2_element_size(layout_helper());
 49   const int addr_incr = 1 << shift;
 50   uintptr_t elem_addr = (uintptr_t) a->base();
 51   const uintptr_t stop_addr = elem_addr + ((uintptr_t)a->length() << shift);
 52   const int oop_offset = element_klass()->first_field_offset();
 53 
 54   while (elem_addr < stop_addr) {
 55     element_klass()->oop_iterate_specialized<T>((address)(elem_addr - oop_offset), closure);
 56     elem_addr += addr_incr;
 57   }
 58 }
 59 
 60 template <typename T, class OopClosureType>
 61 void FlatArrayKlass::oop_oop_iterate_elements_specialized_bounded(flatArrayOop a,
 62                                                                   OopClosureType* closure,
 63                                                                   void* lo, void* hi) {
 64   assert(contains_oops(), "Nothing to iterate");
 65 
 66   const int shift = Klass::layout_helper_log2_element_size(layout_helper());
 67   const int addr_incr = 1 << shift;
 68   uintptr_t elem_addr = (uintptr_t)a->base();
 69   uintptr_t stop_addr = elem_addr + ((uintptr_t)a->length() << shift);
 70   const int oop_offset = element_klass()->first_field_offset();
 71 
 72   if (elem_addr < (uintptr_t) lo) {
 73     uintptr_t diff = ((uintptr_t) lo) - elem_addr;
 74     elem_addr += (diff >> shift) << shift;
 75   }
 76   if (stop_addr > (uintptr_t) hi) {
 77     uintptr_t diff = stop_addr - ((uintptr_t) hi);
 78     stop_addr -= (diff >> shift) << shift;
 79   }
 80 
 81   const uintptr_t end = stop_addr;
 82   while (elem_addr < end) {
 83     element_klass()->oop_iterate_specialized_bounded<T>((address)(elem_addr - oop_offset), closure, lo, hi);
 84     elem_addr += addr_incr;
 85   }
 86 }
 87 
 88 template <typename T, class OopClosureType>
 89 void FlatArrayKlass::oop_oop_iterate_elements(flatArrayOop a, OopClosureType* closure) {
 90   if (contains_oops()) {
 91     oop_oop_iterate_elements_specialized<T>(a, closure);
 92   }
 93 }
 94 
 95 template <typename T, typename OopClosureType>
 96 void FlatArrayKlass::oop_oop_iterate(oop obj, OopClosureType* closure) {
 97   assert(obj->klass()->is_flatArray_klass(),"must be a flat array");
 98   flatArrayOop a = flatArrayOop(obj);
 99 
100   if (Devirtualizer::do_metadata(closure)) {
101     Devirtualizer::do_klass(closure, obj->klass());
102     Devirtualizer::do_klass(closure, FlatArrayKlass::cast(obj->klass())->element_klass());
103   }
104 
105   oop_oop_iterate_elements<T>(a, closure);
106 }
107 
108 template <typename T, typename OopClosureType>
109 void FlatArrayKlass::oop_oop_iterate_reverse(oop obj, OopClosureType* closure) {
110   // TODO
111   oop_oop_iterate<T>(obj, closure);
112 }
113 
114 template <typename T, class OopClosureType>
115 void FlatArrayKlass::oop_oop_iterate_elements_bounded(flatArrayOop a, OopClosureType* closure, MemRegion mr) {
116   if (contains_oops()) {
117     oop_oop_iterate_elements_specialized_bounded<T>(a, closure, mr.start(), mr.end());
118   }
119 }
120 
121 
122 template <typename T, typename OopClosureType>
123 void FlatArrayKlass::oop_oop_iterate_bounded(oop obj, OopClosureType* closure, MemRegion mr) {
124   flatArrayOop a = flatArrayOop(obj);
125   if (Devirtualizer::do_metadata(closure)) {
126     Devirtualizer::do_klass(closure, a->klass());
127     Devirtualizer::do_klass(closure, FlatArrayKlass::cast(obj->klass())->element_klass());
128   }
129   oop_oop_iterate_elements_bounded<T>(a, closure, mr);
130 }
131 
132 #endif // SHARE_VM_OOPS_FLATARRAYKLASS_INLINE_HPP