1 /*
  2  * Copyright (c) 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_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 VMStructs;
 38   friend class JVMCIVMStructs;
 39 
 40  public:
 41   static const KlassKind Kind = RefArrayKlassKind;
 42 
 43  private:
 44   // Constructor
 45   RefArrayKlass(int n, Klass* element_klass, Symbol* name, ArrayKlass::ArrayProperties props);
 46   static RefArrayKlass* allocate_klass(ClassLoaderData* loader_data, int n, Klass* k, Symbol* name,
 47                                        ArrayKlass::ArrayProperties props, TRAPS);
 48 
 49  public:
 50   // For dummy objects
 51   RefArrayKlass() {}
 52 
 53   // Dispatched operation
 54   DEBUG_ONLY(bool is_refArray_klass_slow() const { return true; })
 55   size_t oop_size(oop obj) const;  // TODO FIXME make it virtual in objArrayKlass
 56 
 57   // Allocation
 58   static RefArrayKlass* allocate_refArray_klass(ClassLoaderData* loader_data,
 59                                                 int n, Klass* element_klass,
 60                                                 ArrayKlass::ArrayProperties props, TRAPS);
 61 
 62   objArrayOop allocate_instance(int length, ArrayProperties props, TRAPS);
 63 
 64   // Copying TODO FIXME make copying method in objArrayKlass virtual and default implementation invalid (ShouldNotReachHere())
 65   void copy_array(arrayOop s, int src_pos, arrayOop d, int dst_pos, int length, TRAPS);
 66 
 67   virtual void metaspace_pointers_do(MetaspaceClosure* iter);
 68 
 69  private:
 70   // Either oop or narrowOop depending on UseCompressedOops.
 71   // must be called from within ObjArrayKlass.cpp
 72   void do_copy(arrayOop s, size_t src_offset,
 73                arrayOop d, size_t dst_offset,
 74                int length, TRAPS);
 75 
 76  public:
 77   static RefArrayKlass *cast(Klass* k) {
 78     assert(k->is_refArray_klass(), "cast to RefArrayKlass");
 79     return const_cast<RefArrayKlass *>(cast(const_cast<const Klass *>(k)));
 80   }
 81 
 82   static const RefArrayKlass *cast(const Klass* k) {
 83     assert(k->is_refArray_klass(), "cast to RefArrayKlass");
 84     return static_cast<const RefArrayKlass *>(k);
 85   }
 86 
 87   // Sizing
 88   static int header_size() { return sizeof(RefArrayKlass) / wordSize; }
 89   int size() const { return ArrayKlass::static_size(header_size()); }
 90 
 91   // Initialization (virtual from Klass)
 92   void initialize(TRAPS);
 93 
 94   // Oop fields (and metadata) iterators
 95   //
 96   // The RefArrayKlass iterators also visits the Object's klass.
 97 
 98   // Iterate over oop elements and metadata.
 99   template <typename T, typename OopClosureType>
100   inline void oop_oop_iterate(oop obj, OopClosureType* closure);
101 
102   // Iterate over oop elements and metadata.
103   template <typename T, typename OopClosureType>
104   inline void oop_oop_iterate_reverse(oop obj, OopClosureType* closure);
105 
106   // Iterate over oop elements within mr, and metadata.
107   template <typename T, typename OopClosureType>
108   inline void oop_oop_iterate_bounded(oop obj, OopClosureType* closure, MemRegion mr);
109 
110   // Iterate over oop elements within [start, end), and metadata.
111   template <typename T, class OopClosureType>
112   inline void oop_oop_iterate_range(refArrayOop a, OopClosureType* closure, int start, int end);
113 
114  public:
115   // Iterate over all oop elements.
116   template <typename T, class OopClosureType>
117   inline void oop_oop_iterate_elements(refArrayOop a, OopClosureType* closure);
118 
119  private:
120   // Iterate over all oop elements with indices within mr.
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;
127   void print_value_on(outputStream* st) const;
128 
129   void oop_print_value_on(oop obj, outputStream* st);
130 #ifndef PRODUCT
131   void oop_print_on(oop obj, outputStream* st);
132 #endif // PRODUCT
133 
134   // Verification
135   void verify_on(outputStream* st);
136 
137   void oop_verify_on(oop obj, outputStream* st);
138 };
139 
140 #endif // SHARE_OOPS_REFARRAYKLASS_HPP