1 /*
  2  * Copyright (c) 2025, 2026, 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_REFARRAYKLASS_HPP
 26 #define SHARE_OOPS_REFARRAYKLASS_HPP
 27 
 28 #include "oops/arrayKlass.hpp"
 29 #include "oops/objArrayKlass.hpp"
 30 #include "utilities/macros.hpp"
 31 
 32 class ClassLoaderData;
 33 
 34 // RefArrayKlass is the klass for arrays of references
 35 
 36 class RefArrayKlass : public ObjArrayKlass {
 37   friend class Deoptimization;
 38   friend class oopFactory;
 39   friend class VMStructs;
 40 
 41  public:
 42   static const KlassKind Kind = RefArrayKlassKind;
 43 
 44  private:
 45   // Constructor
 46   RefArrayKlass(int n, Klass* element_klass, Symbol* name, ArrayProperties props);
 47   static RefArrayKlass* allocate_klass(ClassLoaderData* loader_data, int n, Klass* k, Symbol* name,
 48                                        ArrayProperties props, TRAPS);
 49 
 50  public:
 51   // For dummy objects
 52   RefArrayKlass() {}
 53 
 54   // Dispatched operation
 55   DEBUG_ONLY(bool is_refArray_klass_slow() const override { return true; })
 56   size_t oop_size(oop obj) const override;
 57 
 58   // Allocation
 59   static RefArrayKlass* allocate_refArray_klass(ClassLoaderData* loader_data,
 60                                                 int n, Klass* element_klass,
 61                                                 ArrayProperties props, TRAPS);
 62 
 63   refArrayOop allocate_instance(int length, TRAPS);
 64 
 65   // Copying
 66   void copy_array(arrayOop s, int src_pos, arrayOop d, int dst_pos, int length, TRAPS) override;
 67 
 68   void metaspace_pointers_do(MetaspaceClosure* iter) override;
 69 
 70  private:
 71   // Either oop or narrowOop depending on UseCompressedOops.
 72   // must be called from within ObjArrayKlass.cpp
 73   void do_copy(arrayOop s, size_t src_offset,
 74                arrayOop d, size_t dst_offset,
 75                int length, TRAPS);
 76 
 77  public:
 78   static RefArrayKlass *cast(Klass* k) {
 79     assert(k->is_refArray_klass(), "cast to RefArrayKlass");
 80     return const_cast<RefArrayKlass *>(cast(const_cast<const Klass *>(k)));
 81   }
 82 
 83   static const RefArrayKlass *cast(const Klass* k) {
 84     assert(k->is_refArray_klass(), "cast to RefArrayKlass");
 85     return static_cast<const RefArrayKlass *>(k);
 86   }
 87 
 88   // Sizing
 89   static int header_size() { return sizeof(RefArrayKlass) / wordSize; }
 90   int size() const override { return ArrayKlass::static_size(header_size()); }
 91 
 92   // Initialization (virtual from Klass)
 93   void initialize(TRAPS) override;
 94 
 95   // Oop fields (and metadata) iterators
 96   //
 97   // The RefArrayKlass iterators also visits the Object's klass.
 98 
 99   // Iterate over oop elements and metadata.
100   template <typename T, typename OopClosureType>
101   inline void oop_oop_iterate(oop obj, OopClosureType* closure);
102 
103   // Iterate over oop elements and metadata.
104   template <typename T, typename OopClosureType>
105   inline void oop_oop_iterate_reverse(oop obj, OopClosureType* closure);
106 
107   // Iterate over oop elements within mr, and metadata.
108   template <typename T, typename OopClosureType>
109   inline void oop_oop_iterate_bounded(oop obj, OopClosureType* closure, MemRegion mr);
110 
111   // Iterate over all oop elements, and no metadata.
112   template <typename T, class OopClosureType>
113   inline void oop_oop_iterate_elements(refArrayOop a, OopClosureType* closure);
114 
115   // Iterate over oop elements within index range [start, end), and no metadata.
116   template <typename T, class OopClosureType>
117   inline void oop_oop_iterate_elements_range(refArrayOop a, OopClosureType* closure, int start, int end);
118 
119  private:
120   // Iterate over all oop elements bounded by addresses [low, high), and no metadata.
121   template <typename T, class OopClosureType>
122   inline void oop_oop_iterate_elements_bounded(refArrayOop a, OopClosureType* closure, void* low, void* high);
123 
124  public:
125   // Printing
126   void print_on(outputStream* st) const override;
127   void print_value_on(outputStream* st) const override;
128 
129   void oop_print_value_on(oop obj, outputStream* st) override;
130 #ifndef PRODUCT
131   void oop_print_on(oop obj, outputStream* st) override;
132 #endif // PRODUCT
133 
134   // Verification
135   void verify_on(outputStream* st) override;
136 
137   void oop_verify_on(oop obj, outputStream* st) override;
138 };
139 
140 #endif // SHARE_OOPS_REFARRAYKLASS_HPP