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