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* _element_klass;            // The klass of the elements of this array type
 48   Klass* _bottom_klass;             // The one-dimensional type (InstanceKlass or TypeArrayKlass)
 49 
 50   // Constructor
 51   ObjArrayKlass(int n, Klass* element_klass, Symbol* name);
 52   static ObjArrayKlass* allocate_klass(ClassLoaderData* loader_data, int n, Klass* k, Symbol* name, TRAPS);
 53 
 54   objArrayOop allocate_instance(int length, TRAPS);
 55 
 56  protected:
 57   // Create array_name for element klass
 58   static Symbol* create_element_klass_array_name(JavaThread* current, Klass* element_klass);
 59 
 60  public:
 61   // For dummy objects
 62   ObjArrayKlass() {}
 63 
 64   // Instance variables
 65   Klass* element_klass() const      { return _element_klass; }
 66   void set_element_klass(Klass* k)  { _element_klass = k; }
 67   Klass** element_klass_addr()      { return &_element_klass; }
 68 
 69   Klass* bottom_klass() const       { return _bottom_klass; }
 70   void set_bottom_klass(Klass* k)   { _bottom_klass = k; }
 71   Klass** bottom_klass_addr()       { return &_bottom_klass; }
 72 
 73   ModuleEntry* module() const;
 74   PackageEntry* package() const;
 75 
 76   // Compiler/Interpreter offset
 77   static ByteSize element_klass_offset() { return byte_offset_of(ObjArrayKlass, _element_klass); }
 78 
 79   // Dispatched operation
 80   bool can_be_primary_super_slow() const;
 81   GrowableArray<Klass*>* compute_secondary_supers(int num_extra_slots,
 82                                                   Array<InstanceKlass*>* transitive_interfaces);
 83   DEBUG_ONLY(bool is_objArray_klass_slow()  const  { return true; })
 84   size_t oop_size(oop obj) const;
 85 
 86   // Allocation
 87   static ObjArrayKlass* allocate_objArray_klass(ClassLoaderData* loader_data,
 88                                                 int n, Klass* element_klass, TRAPS);
 89 
 90   oop multi_allocate(int rank, jint* sizes, TRAPS);
 91 
 92   // Copying
 93   void  copy_array(arrayOop s, int src_pos, arrayOop d, int dst_pos, int length, TRAPS);
 94 
 95   // Compute protection domain
 96   oop protection_domain() const { return bottom_klass()->protection_domain(); }
 97 
 98   virtual void metaspace_pointers_do(MetaspaceClosure* iter);
 99 
100  private:
101   // Either oop or narrowOop depending on UseCompressedOops.
102   // must be called from within ObjArrayKlass.cpp
103   void do_copy(arrayOop s, size_t src_offset,
104                arrayOop d, size_t dst_offset,
105                int length, TRAPS);
106  public:
107   static ObjArrayKlass* cast(Klass* k) {
108     return const_cast<ObjArrayKlass*>(cast(const_cast<const Klass*>(k)));
109   }
110 
111   static const ObjArrayKlass* cast(const Klass* k) {
112     assert(k->is_objArray_klass(), "cast to ObjArrayKlass");
113     return static_cast<const ObjArrayKlass*>(k);
114   }
115 
116   // Sizing
117   static int header_size()                { return sizeof(ObjArrayKlass)/wordSize; }
118   int size() const                        { return ArrayKlass::static_size(header_size()); }
119 
120   // Initialization (virtual from Klass)
121   void initialize(TRAPS);
122 
123   // Oop fields (and metadata) iterators
124   //
125   // The ObjArrayKlass iterators also visits the Object's klass.
126 
127   // Iterate over oop elements and metadata.
128   template <typename T, typename OopClosureType>
129   inline void oop_oop_iterate(oop obj, OopClosureType* closure);
130 
131   // Iterate over oop elements and metadata.
132   template <typename T, typename OopClosureType>
133   inline void oop_oop_iterate_reverse(oop obj, OopClosureType* closure);
134 
135   // Iterate over oop elements within mr, and metadata.
136   template <typename T, typename OopClosureType>
137   inline void oop_oop_iterate_bounded(oop obj, OopClosureType* closure, MemRegion mr);
138 
139   // Iterate over oop elements within [start, end), and metadata.
140   template <typename T, class OopClosureType>
141   inline void oop_oop_iterate_range(objArrayOop a, OopClosureType* closure, int start, int end);
142 
143  public:
144   // Iterate over all oop elements.
145   template <typename T, class OopClosureType>
146   inline void oop_oop_iterate_elements(objArrayOop a, OopClosureType* closure);
147 
148  private:
149   // Iterate over all oop elements with indices within mr.
150   template <typename T, class OopClosureType>
151   inline void oop_oop_iterate_elements_bounded(objArrayOop a, OopClosureType* closure, void* low, void* high);
152 
153  public:
154   u2 compute_modifier_flags() const;
155 
156  public:
157   // Printing
158   void print_on(outputStream* st) const;
159   void print_value_on(outputStream* st) const;
160 
161   void oop_print_value_on(oop obj, outputStream* st);
162 #ifndef PRODUCT
163   void oop_print_on      (oop obj, outputStream* st);
164 #endif //PRODUCT
165 
166   const char* internal_name() const;
167 
168   // Verification
169   void verify_on(outputStream* st);
170 
171   void oop_verify_on(oop obj, outputStream* st);
172 };
173 
174 #endif // SHARE_OOPS_OBJARRAYKLASS_HPP