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_ARRAYKLASS_HPP
26 #define SHARE_OOPS_ARRAYKLASS_HPP
27
28 #include "oops/arrayProperties.hpp"
29 #include "oops/klass.hpp"
30 #include "oops/layoutKind.hpp"
31
32 class fieldDescriptor;
33 class klassVtable;
34 class ObjArrayKlass;
35
36 // ArrayKlass is the abstract baseclass for all array classes
37
38 class ArrayKlass: public Klass {
39 friend class VMStructs;
40
41 public:
42 static ArrayProperties array_properties_from_layout(LayoutKind lk);
43
44 private:
45 // If you add a new field that points to any metaspace object, you
46 // must add this field to ArrayKlass::metaspace_pointers_do().
47 int _dimension; // This is n'th-dimensional array.
48 ObjArrayKlass* volatile _higher_dimension; // Refers the (n+1)'th-dimensional array (if present).
49 ArrayKlass* volatile _lower_dimension; // Refers the (n-1)'th-dimensional array (if present).
50
51 const ArrayProperties _properties;
52
53 protected:
54 // Constructors
55 // The constructor with the Symbol argument does the real array
56 // initialization, the other is a dummy
57 ArrayKlass(int n, Symbol* name, KlassKind kind, ArrayProperties props);
58 ArrayKlass();
59
60 public:
61 // Testing operation
62 DEBUG_ONLY(bool is_array_klass_slow() const override { return true; })
63
64 // Returns the ObjArrayKlass for n'th dimension.
65 ArrayKlass* array_klass(int n, TRAPS) override;
66 ArrayKlass* array_klass_or_null(int n) override;
67
68 // Returns the array class with this class as element type.
69 ArrayKlass* array_klass(TRAPS) override;
70 ArrayKlass* array_klass_or_null() override;
71
72 // Instance variables
73 int dimension() const { return _dimension; }
74
75 ArrayProperties properties() const { return _properties; }
76 static ByteSize properties_offset() { return byte_offset_of(ArrayKlass, _properties); }
77
78 ObjArrayKlass* higher_dimension() const { return _higher_dimension; }
79 inline ObjArrayKlass* higher_dimension_acquire() const; // load with acquire semantics
80 void set_higher_dimension(ObjArrayKlass* k) { _higher_dimension = k; }
81 inline void release_set_higher_dimension(ObjArrayKlass* k); // store with release semantics
82
83 ArrayKlass* lower_dimension() const { return _lower_dimension; }
84 void set_lower_dimension(ArrayKlass* k) { _lower_dimension = k; }
85
86 // offset of first element, including any padding for the sake of alignment
87 int array_header_in_bytes() const { return layout_helper_header_size(layout_helper()); }
88 int log2_element_size() const { return layout_helper_log2_element_size(layout_helper()); }
89 // type of elements (T_OBJECT for both oop arrays and array-arrays)
90 BasicType element_type() const { return layout_helper_element_type(layout_helper()); }
91
92 InstanceKlass* java_super() const override;
93
94 // Allocation
95 // Sizes points to the first dimension of the array, subsequent dimensions
96 // are always in higher memory. The callers of these set that up.
97 virtual oop multi_allocate(int rank, jint* sizes, TRAPS);
98
99 // find field according to JVM spec 5.4.3.2, returns the klass in which the field is defined
100 Klass* find_field(Symbol* name, Symbol* sig, fieldDescriptor* fd) const override;
101
102 // Lookup operations
103 Method* uncached_lookup_method(const Symbol* name,
104 const Symbol* signature,
105 OverpassLookupMode overpass_mode,
106 PrivateLookupMode private_mode = PrivateLookupMode::find) const override;
107
108 static ArrayKlass* cast(Klass* k) {
109 return const_cast<ArrayKlass*>(cast(const_cast<const Klass*>(k)));
110 }
111
112 static const ArrayKlass* cast(const Klass* k) {
113 assert(k->is_array_klass(), "cast to ArrayKlass");
114 return static_cast<const ArrayKlass*>(k);
115 }
116
117 GrowableArray<Klass*>* compute_secondary_supers(int num_extra_slots,
118 Array<InstanceKlass*>* transitive_interfaces) override;
119
120 oop component_mirror() const;
121
122 // Sizing
123 static int static_size(int header_size);
124
125 void metaspace_pointers_do(MetaspaceClosure* iter) override;
126
127 // Return a handle.
128 static void complete_create_array_klass(ArrayKlass* k, Klass* super_klass, ModuleEntry* module, TRAPS);
129
130 // JVMTI support
131 jint jvmti_class_status() const override;
132
133 #if INCLUDE_CDS
134 // CDS support - remove and restore oops from metadata. Oops are not shared.
135 void remove_unshareable_info() override;
136 void remove_java_mirror() override;
137 void restore_unshareable_info(ClassLoaderData* loader_data, Handle protection_domain, TRAPS);
138 void cds_print_value_on(outputStream* st) const;
139 #endif
140
141 void log_array_class_load(Klass* k);
142 // Printing
143 void print_on(outputStream* st) const override;
144 void print_value_on(outputStream* st) const override;
145
146 void oop_print_on(oop obj, outputStream* st) override;
147
148 // Verification
149 void verify_on(outputStream* st) override;
150
151 void oop_verify_on(oop obj, outputStream* st) override;
152 };
153
154 class ArrayDescription : public StackObj {
155 public:
156 Klass::KlassKind _kind;
157 ArrayProperties _properties;
158 LayoutKind _layout_kind;
159
160 ArrayDescription(Klass::KlassKind k, ArrayProperties p, LayoutKind lk) {
161 _kind = k;
162 _layout_kind = lk;
163 assert(lk == LayoutKind::REFERENCE || k != Klass::KlassKind::RefArrayKlassKind, "Sanity check");
164 assert(lk != LayoutKind::UNKNOWN, "Sanity check");
165
166 // Atomicity depends on the layout kind, which might be different than what
167 // the given properties says
168 const bool non_atomic = lk != LayoutKind::REFERENCE && !LayoutKindHelper::is_atomic_flat(lk);
169 _properties = p.with_non_atomic(non_atomic);
170 }
171 };
172
173 #endif // SHARE_OOPS_ARRAYKLASS_HPP