1 /*
  2  * Copyright (c) 2017, 2025, 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_VM_OOPS_FLATARRAYKLASS_HPP
 26 #define SHARE_VM_OOPS_FLATARRAYKLASS_HPP
 27 
 28 #include "classfile/classLoaderData.hpp"
 29 #include "oops/arrayKlass.hpp"
 30 #include "oops/inlineKlass.hpp"
 31 #include "oops/objArrayKlass.hpp"
 32 #include "utilities/macros.hpp"
 33 
 34 /**
 35  * Array of inline types, gives a layout of typeArrayOop, but needs oops iterators
 36  */
 37 class FlatArrayKlass : public ObjArrayKlass {
 38   friend class Deoptimization;
 39   friend class oopFactory;
 40   friend class VMStructs;
 41 
 42  public:
 43   static const KlassKind Kind = FlatArrayKlassKind;
 44 
 45  private:
 46   // Constructor
 47   FlatArrayKlass(Klass* element_klass, Symbol* name, ArrayProperties props, LayoutKind lk);
 48 
 49   LayoutKind _layout_kind;
 50 
 51  public:
 52 
 53   FlatArrayKlass() {} // used by CppVtableCloner<T>::initialize()
 54 
 55   InlineKlass* element_klass() const { return InlineKlass::cast(ObjArrayKlass::element_klass()); }
 56 
 57   LayoutKind layout_kind() const  { return _layout_kind; }
 58   static ByteSize layout_kind_offset() { return in_ByteSize(offset_of(FlatArrayKlass, _layout_kind)); }
 59 
 60   // Casting from Klass*
 61   static FlatArrayKlass* cast(Klass* k) {
 62     return const_cast<FlatArrayKlass*>(cast(const_cast<const Klass*>(k)));
 63   }
 64 
 65   static const FlatArrayKlass* cast(const Klass* k) {
 66     assert(k != nullptr, "k should not be null");
 67     assert(k->is_flatArray_klass(), "cast to FlatArrayKlass");
 68     return static_cast<const FlatArrayKlass*>(k);
 69   }
 70 
 71   // klass allocation
 72   static FlatArrayKlass* allocate_klass(Klass* element_klass, ArrayProperties props, LayoutKind lk, TRAPS);
 73 
 74   void initialize(TRAPS) override;
 75 
 76   bool can_be_primary_super_slow() const override;
 77 
 78   int element_byte_size() const { return 1 << layout_helper_log2_element_size(_layout_helper); }
 79 
 80   DEBUG_ONLY(bool is_flatArray_klass_slow() const override { return true; })
 81 
 82   bool contains_oops() {
 83     return element_klass()->contains_oops();
 84   }
 85 
 86   oop protection_domain() const override;
 87 
 88   void metaspace_pointers_do(MetaspaceClosure* iter) override;
 89 
 90   static jint array_layout_helper(InlineKlass* vklass, LayoutKind lk); // layout helper for values
 91 
 92   // sizing
 93   static int header_size()  { return sizeof(FlatArrayKlass) / wordSize; }
 94   int size() const override { return ArrayKlass::static_size(header_size()); }
 95 
 96   jint max_elements() const;
 97 
 98   size_t oop_size(oop obj) const override;
 99 
100   // Oop Allocation
101   flatArrayOop allocate_instance(int length, TRAPS);
102 
103   oop multi_allocate(int rank, jint* sizes, TRAPS) override;
104 
105   // Naming
106   const char* internal_name() const override { return external_name(); }
107 
108   // Copying
109   void copy_array(arrayOop s, int src_pos, arrayOop d, int dst_pos, int length, TRAPS) override;
110 
111   // GC specific object visitors
112   template <typename T, typename OopClosureType>
113   inline void oop_oop_iterate(oop obj, OopClosureType* closure);
114 
115   template <typename T, typename OopClosureType>
116   inline void oop_oop_iterate_reverse(oop obj, OopClosureType* closure);
117 
118   template <typename T, typename OopClosureType>
119   inline void oop_oop_iterate_bounded(oop obj, OopClosureType* closure, MemRegion mr);
120 
121   template <typename T, class OopClosureType>
122   inline void oop_oop_iterate_elements(flatArrayOop a, OopClosureType* closure);
123 
124   // Iterate over oop elements within index range [start, end), and no metadata.
125   template <typename T, class OopClosureType>
126   inline void oop_oop_iterate_elements_range(flatArrayOop a, OopClosureType* closure, int start, int end);
127 
128 private:
129   template <typename T, class OopClosureType>
130   inline void oop_oop_iterate_elements_specialized(flatArrayOop a, OopClosureType* closure, int start, int end);
131 
132   template <typename T, class OopClosureType>
133   inline void oop_oop_iterate_elements_bounded(flatArrayOop a, OopClosureType* closure, MemRegion mr);
134 
135   template <typename T, class OopClosureType>
136   inline void oop_oop_iterate_elements_specialized_bounded(flatArrayOop a, OopClosureType* closure, uintptr_t low, uintptr_t high);
137 
138  public:
139   u2 compute_modifier_flags() const override;
140 
141   // Printing
142   void print_on(outputStream* st) const override;
143   void print_value_on(outputStream* st) const override;
144 
145   void oop_print_value_on(oop obj, outputStream* st) override;
146 #ifndef PRODUCT
147   void oop_print_on(oop obj, outputStream* st) override;
148 #endif
149   void oop_print_elements_on(flatArrayOop fa, outputStream* st);
150 
151   // Verification
152   void verify_on(outputStream* st) override;
153   void oop_verify_on(oop obj, outputStream* st) override;
154 };
155 
156 #endif