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