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