1 /*
  2  * Copyright (c) 1997, 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_OOPS_OBJARRAYKLASS_HPP
 26 #define SHARE_OOPS_OBJARRAYKLASS_HPP
 27 
 28 #include "oops/arrayKlass.hpp"
 29 #include "utilities/macros.hpp"
 30 
 31 class ClassLoaderData;
 32 
 33 // ObjArrayKlass is the klass for objArrays
 34 
 35 class ObjArrayKlass : public ArrayKlass {
 36   friend class Deoptimization;
 37   friend class JVMCIVMStructs;
 38   friend class oopFactory;
 39   friend class VMStructs;
 40 
 41  public:
 42   static const KlassKind Kind = ObjArrayKlassKind;
 43 
 44  private:
 45   // If you add a new field that points to any metaspace object, you
 46   // must add this field to ObjArrayKlass::metaspace_pointers_do().
 47   Klass* _bottom_klass;             // The one-dimensional type (InstanceKlass or TypeArrayKlass)
 48  protected:
 49   Klass* _element_klass;            // The klass of the elements of this array type
 50   ObjArrayKlass* _next_refined_array_klass;
 51 
 52  protected:
 53   // Constructor
 54   ObjArrayKlass(int n, Klass* element_klass, Symbol* name, KlassKind kind, ArrayKlass::ArrayProperties props, markWord mw);
 55   static ObjArrayKlass* allocate_klass(ClassLoaderData* loader_data, int n, Klass* k, Symbol* name, ArrayKlass::ArrayProperties props, TRAPS);
 56 
 57   static ArrayDescription array_layout_selection(Klass* element, ArrayProperties properties);
 58   ObjArrayKlass* allocate_klass_with_properties(ArrayKlass::ArrayProperties props, TRAPS);
 59   virtual objArrayOop allocate_instance(int length, ArrayProperties props, TRAPS);
 60 
 61    // Create array_name for element klass
 62   static Symbol* create_element_klass_array_name(JavaThread* current, Klass* element_klass);
 63 
 64  public:
 65   // For dummy objects
 66   ObjArrayKlass() {}
 67 
 68   virtual Klass* element_klass() const      { return _element_klass; }
 69   virtual void set_element_klass(Klass* k)  { _element_klass = k; }
 70 
 71 
 72   ObjArrayKlass* next_refined_array_klass() const      { return _next_refined_array_klass; }
 73   inline ObjArrayKlass* next_refined_array_klass_acquire() const;
 74   void set_next_refined_klass_klass(ObjArrayKlass* ak) { _next_refined_array_klass = ak; }
 75   inline void release_set_next_refined_klass(ObjArrayKlass* ak);
 76   ObjArrayKlass* klass_with_properties(ArrayKlass::ArrayProperties properties, TRAPS);
 77   static ByteSize next_refined_array_klass_offset() { return byte_offset_of(ObjArrayKlass, _next_refined_array_klass); }
 78 
 79   // Compiler/Interpreter offset
 80   static ByteSize element_klass_offset() { return byte_offset_of(ObjArrayKlass, _element_klass); }
 81 
 82   Klass* bottom_klass() const       { return _bottom_klass; }
 83   void set_bottom_klass(Klass* k)   { _bottom_klass = k; }
 84   Klass** bottom_klass_addr()       { return &_bottom_klass; }
 85 
 86   ModuleEntry* module() const;
 87   PackageEntry* package() const;
 88 
 89   // Dispatched operation
 90   bool can_be_primary_super_slow() const;
 91   GrowableArray<Klass*>* compute_secondary_supers(int num_extra_slots,
 92                                                   Array<InstanceKlass*>* transitive_interfaces);
 93   DEBUG_ONLY(bool is_objArray_klass_slow()  const  { return true; })
 94   size_t oop_size(oop obj) const;
 95 
 96   // Allocation
 97   static ObjArrayKlass* allocate_objArray_klass(ClassLoaderData* loader_data,
 98                                                 int n, Klass* element_klass, TRAPS);
 99 
100   oop multi_allocate(int rank, jint* sizes, TRAPS);
101 
102   // Copying
103   void  copy_array(arrayOop s, int src_pos, arrayOop d, int dst_pos, int length, TRAPS);
104 
105   // Compute protection domain
106   oop protection_domain() const { return bottom_klass()->protection_domain(); }
107 
108   virtual void metaspace_pointers_do(MetaspaceClosure* iter);
109 
110 #if INCLUDE_CDS
111   virtual void remove_unshareable_info();
112   virtual void remove_java_mirror();
113   void restore_unshareable_info(ClassLoaderData* loader_data, Handle protection_domain, TRAPS);
114 #endif
115 
116  public:
117   static ObjArrayKlass* cast(Klass* k) {
118     return const_cast<ObjArrayKlass*>(cast(const_cast<const Klass*>(k)));
119   }
120 
121   static const ObjArrayKlass* cast(const Klass* k) {
122     assert(k->is_objArray_klass(), "cast to ObjArrayKlass");
123     return static_cast<const ObjArrayKlass*>(k);
124   }
125 
126   // Sizing
127   static int header_size()                { return sizeof(ObjArrayKlass)/wordSize; }
128   int size() const                        { return ArrayKlass::static_size(header_size()); }
129 
130   // Initialization (virtual from Klass)
131   void initialize(TRAPS);
132 
133   // Oop fields (and metadata) iterators
134   //
135   // The ObjArrayKlass iterators also visits the Object's klass.
136 
137   // Iterate over oop elements and metadata.
138   template <typename T, typename OopClosureType>
139   inline void oop_oop_iterate(oop obj, OopClosureType* closure);
140 
141   // Iterate over oop elements and metadata.
142   template <typename T, typename OopClosureType>
143   inline void oop_oop_iterate_reverse(oop obj, OopClosureType* closure);
144 
145   // Iterate over oop elements within mr, and metadata.
146   template <typename T, typename OopClosureType>
147   inline void oop_oop_iterate_bounded(oop obj, OopClosureType* closure, MemRegion mr);
148 
149   // Iterate over oop elements within [start, end), and metadata.
150   template <typename T, class OopClosureType>
151   inline void oop_oop_iterate_range(objArrayOop a, OopClosureType* closure, int start, int end);
152 
153  public:
154   // Iterate over all oop elements.
155   template <typename T, class OopClosureType>
156   inline void oop_oop_iterate_elements(objArrayOop a, OopClosureType* closure);
157 
158  private:
159   // Iterate over all oop elements with indices within mr.
160   template <typename T, class OopClosureType>
161   inline void oop_oop_iterate_elements_bounded(objArrayOop a, OopClosureType* closure, void* low, void* high);
162 
163  public:
164   u2 compute_modifier_flags() const;
165 
166  public:
167   // Printing
168   void print_on(outputStream* st) const;
169   void print_value_on(outputStream* st) const;
170 
171   virtual void oop_print_value_on(oop obj, outputStream* st);
172 #ifndef PRODUCT
173   virtual void oop_print_on      (oop obj, outputStream* st);
174 #endif //PRODUCT
175 
176   const char* internal_name() const;
177 
178   // Verification
179   void verify_on(outputStream* st);
180 
181   void oop_verify_on(oop obj, outputStream* st);
182 };
183 
184 #endif // SHARE_OOPS_OBJARRAYKLASS_HPP