1 /*
  2  * Copyright (c) 2017, 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_VM_OOPS_INLINEKLASS_HPP
 26 #define SHARE_VM_OOPS_INLINEKLASS_HPP
 27 
 28 #include "oops/inlineOop.hpp"
 29 #include "oops/instanceKlass.hpp"
 30 #include "oops/layoutKind.hpp"
 31 #include "oops/oopsHierarchy.hpp"
 32 #include "oops/valuePayload.hpp"
 33 #include "runtime/handles.hpp"
 34 #include "utilities/exceptions.hpp"
 35 #include "utilities/globalDefinitions.hpp"
 36 
 37 template <typename T>
 38 class Array;
 39 class ClassFileParser;
 40 template <typename T>
 41 class GrowableArray;
 42 class Method;
 43 class RegisterMap;
 44 class SigEntry;
 45 
 46 // An InlineKlass is a specialized InstanceKlass for concrete value classes
 47 // (abstract value classes are represented by InstanceKlass)
 48 
 49 class InlineKlass: public InstanceKlass {
 50   friend class VMStructs;
 51   friend class InstanceKlass;
 52 
 53  public:
 54   static const KlassKind Kind = InlineKlassKind;
 55 
 56   // The member fields of the InlineKlass.
 57   //
 58   // All Klass objects have vtables starting at offset `sizeof(InstanceKlass)`.
 59   //
 60   // This has the effect that sub-klasses of InstanceKlass can't have their own
 61   // C++ fields, because those would overlap with the vtables (or some of the
 62   // other dynamically-sized sections).
 63   //
 64   // To work around this we stamp out the block members *after* all
 65   // dynamically-sized sections belonging to the InstanceKlass part of the
 66   // object.
 67   //
 68   // InlineKlass object layout:
 69   //   +-----------------------+
 70   //   | sizeof(InstanceKlass) |
 71   //   +-----------------------+ <= InstanceKlass:header_size()
 72   //   | vtable                |
 73   //   +-----------------------+
 74   //   | other sections        |
 75   //   +-----------------------+ <= end_of_instance_klass()
 76   //   | InlineKlass::Members  |
 77   //   +-----------------------+
 78   //
 79   class Members {
 80     friend class InlineKlass;
 81 
 82     // Addresses used for inline type calling convention
 83     Array<SigEntry>* _extended_sig;
 84     Array<VMRegPair>* _return_regs;
 85 
 86     address _pack_handler;
 87     address _pack_handler_jobject;
 88     address _unpack_handler;
 89 
 90     int _null_reset_value_offset;
 91     int _payload_offset;           // offset of the beginning of the payload in a heap buffered instance
 92     int _payload_size_in_bytes;    // size of payload layout
 93     int _payload_alignment;        // alignment required for payload
 94     int _null_free_non_atomic_size_in_bytes; // size of null-free non-atomic flat layout
 95     int _null_free_non_atomic_alignment;     // alignment requirement for null-free non-atomic layout
 96     int _null_free_atomic_size_in_bytes;     // size and alignment requirement for a null-free atomic layout, -1 if no atomic flat layout is possible
 97     int _nullable_atomic_size_in_bytes;      // size and alignment requirement for a nullable layout (always atomic), -1 if no nullable flat layout is possible
 98     int _nullable_non_atomic_size_in_bytes;  // size and alignment requirement for a nullable non-atomic layout, -1 if not available
 99     int _null_marker_offset;       // expressed as an offset from the beginning of the object for a heap buffered value
100                                    // payload_offset must be subtracted to get the offset from the beginning of the payload
101 
102     Members();
103   };
104 
105   InlineKlass();
106 
107  private:
108 
109   // Constructor
110   InlineKlass(const ClassFileParser& parser);
111 
112   // Calculates where the members are supposed to be placed
113   address calculate_members_address() const;
114 
115   Members& members() {
116     assert(_adr_inline_klass_members != nullptr, "Should have been initialized");
117     return *reinterpret_cast<Members*>(_adr_inline_klass_members);
118   }
119 
120   inline const Members& members() const {
121     InlineKlass* ik = const_cast<InlineKlass*>(this);
122     return const_cast<const Members&>(ik->members());
123   }
124 
125  public:
126 
127   bool is_empty_inline_type() const   { return _misc_flags.is_empty_inline_type(); }
128   void set_is_empty_inline_type()     { _misc_flags.set_is_empty_inline_type(true); }
129 
130   // Members access functions
131 
132   const Array<SigEntry>* extended_sig() const                 {return members()._extended_sig; }
133   void set_extended_sig(Array<SigEntry>* extended_sig)        { members()._extended_sig = extended_sig; }
134 
135   const Array<VMRegPair>* return_regs() const                 { return members()._return_regs; }
136   void set_return_regs(Array<VMRegPair>* return_regs)         { members()._return_regs = return_regs; }
137 
138   // pack and unpack handlers for inline types return
139 
140   address pack_handler() const                                { return members()._pack_handler; }
141   void set_pack_handler(address pack_handler)                 { members()._pack_handler = pack_handler; }
142 
143   address pack_handler_jobject() const                        { return members()._pack_handler_jobject; }
144   void set_pack_handler_jobject(address pack_handler_jobject) { members()._pack_handler_jobject = pack_handler_jobject; }
145 
146   address unpack_handler() const                              { return members()._unpack_handler; }
147   void set_unpack_handler(address unpack_handler)             { members()._unpack_handler = unpack_handler; }
148 
149   int null_reset_value_offset() const {
150     int offset = members()._null_reset_value_offset;
151     assert(offset != 0, "must not be called if not initialized");
152     return offset;
153   }
154   void set_null_reset_value_offset(int offset)                { members()._null_reset_value_offset = offset; }
155 
156   int payload_offset() const {
157     int offset = members()._payload_offset;
158     assert(offset != 0, "Must be initialized before use");
159     return offset;
160   }
161   void set_payload_offset(int offset)                         { members()._payload_offset = offset; }
162 
163   int payload_size_in_bytes() const                           { return members()._payload_size_in_bytes; }
164   void set_payload_size_in_bytes(int payload_size)            { members()._payload_size_in_bytes = payload_size; }
165 
166   int payload_alignment() const                               { return members()._payload_alignment; }
167   void set_payload_alignment(int alignment)                   { members()._payload_alignment = alignment; }
168 
169   int null_free_non_atomic_size_in_bytes() const              { return members()._null_free_non_atomic_size_in_bytes; }
170   void set_null_free_non_atomic_size_in_bytes(int size)       { members()._null_free_non_atomic_size_in_bytes = size; }
171   bool has_null_free_non_atomic_layout() const                { return null_free_non_atomic_size_in_bytes() != -1; }
172 
173   int null_free_non_atomic_alignment() const                  { return members()._null_free_non_atomic_alignment; }
174   void set_null_free_non_atomic_alignment(int alignment)      { members()._null_free_non_atomic_alignment = alignment; }
175 
176   int null_free_atomic_size_in_bytes() const                  { return members()._null_free_atomic_size_in_bytes; }
177   void set_null_free_atomic_size_in_bytes(int size)           { members()._null_free_atomic_size_in_bytes = size; }
178   bool has_null_free_atomic_layout() const                    { return null_free_atomic_size_in_bytes() != -1; }
179 
180   int nullable_atomic_size_in_bytes() const                   { return members()._nullable_atomic_size_in_bytes; }
181   void set_nullable_atomic_size_in_bytes(int size)            { members()._nullable_atomic_size_in_bytes = size; }
182   bool has_nullable_atomic_layout() const                     { return nullable_atomic_size_in_bytes() != -1; }
183 
184   int nullable_non_atomic_size_in_bytes() const               { return members()._nullable_non_atomic_size_in_bytes; }
185   void set_nullable_non_atomic_size_in_bytes(int size)        { members()._nullable_non_atomic_size_in_bytes = size; }
186   bool has_nullable_non_atomic_layout() const                 { return nullable_non_atomic_size_in_bytes() != -1; }
187 
188   int null_marker_offset() const                              { return members()._null_marker_offset; }
189   void set_null_marker_offset(int offset)                     { members()._null_marker_offset = offset; }
190   int null_marker_offset_in_payload() const                   { return null_marker_offset() - payload_offset(); }
191 
192   bool supports_nullable_layouts() const {
193     return has_nullable_non_atomic_layout() || has_nullable_atomic_layout();
194   }
195 
196   jbyte* null_marker_address(address payload) {
197     assert(supports_nullable_layouts(), " Must do");
198     return (jbyte*)payload + null_marker_offset_in_payload();
199   }
200 
201   bool is_payload_marked_as_null(address payload) {
202     assert(supports_nullable_layouts(), " Must do");
203     return *null_marker_address(payload) == 0;
204   }
205 
206   void mark_payload_as_non_null(address payload) {
207     assert(supports_nullable_layouts(), " Must do");
208     *null_marker_address(payload) = 1;
209   }
210 
211   void mark_payload_as_null(address payload) {
212     assert(supports_nullable_layouts(), " Must do");
213     *null_marker_address(payload) = 0;
214   }
215 
216   inline bool layout_has_null_marker(LayoutKind lk) const;
217 
218   inline bool is_layout_supported(LayoutKind lk) const;
219 
220   inline int layout_alignment(LayoutKind kind) const;
221   inline int layout_size_in_bytes(LayoutKind kind) const;
222 
223 #if INCLUDE_CDS
224   void remove_unshareable_info() override;
225 #endif
226 
227  private:
228   int collect_fields(GrowableArray<SigEntry>* sig, int base_off = 0, int null_marker_offset = -1);
229 
230   void cleanup_blobs();
231 
232  public:
233   // Type testing
234   bool is_inline_klass_slow() const override { return true; }
235 
236   // Casting from Klass*
237 
238   static InlineKlass* cast(Klass* k) {
239     return const_cast<InlineKlass*>(cast(const_cast<const Klass*>(k)));
240   }
241 
242   static const InlineKlass* cast(const Klass* k) {
243     assert(k != nullptr, "k should not be null");
244     assert(k->is_inline_klass(), "cast to InlineKlass");
245     return static_cast<const InlineKlass*>(k);
246   }
247 
248   // Allocates a stand alone value in the Java heap
249   // initialized to default value (cleared memory)
250   inlineOop allocate_instance(TRAPS);
251 
252   address payload_addr(oop o) const;
253 
254   bool maybe_flat_in_array();
255   bool is_always_flat_in_array();
256 
257   bool contains_oops() const { return nonstatic_oop_map_count() > 0; }
258   int nonstatic_oop_count();
259 
260   // oop iterate raw inline type data pointer (where oop_addr may not be an oop, but backing/array-element)
261   template <typename T, class OopClosureType>
262   inline void oop_iterate_specialized(const address oop_addr, OopClosureType* closure);
263 
264   template <typename T, class OopClosureType>
265   inline void oop_iterate_specialized_bounded(const address oop_addr, OopClosureType* closure, uintptr_t lo, uintptr_t hi);
266 
267   // calling convention support
268   void initialize_calling_convention(TRAPS);
269 
270   bool can_be_passed_as_fields() const;
271   bool can_be_returned_as_fields(bool init = false) const;
272   void save_oop_fields(const RegisterMap& map, GrowableArray<Handle>& handles) const;
273   void restore_oop_results(RegisterMap& map, GrowableArray<Handle>& handles) const;
274   oop realloc_result(const RegisterMap& reg_map, const GrowableArray<Handle>& handles, TRAPS);
275   static InlineKlass* returned_inline_klass(const RegisterMap& reg_map, bool* return_oop = nullptr, Method* method = nullptr);
276 
277   static ByteSize adr_members_offset() {
278     return InstanceKlass::adr_inline_klass_members_offset();
279   }
280 
281   // pack and unpack handlers. Need to be loadable from generated code
282   // so at a fixed offset from the base of the klass pointer.
283   static ByteSize pack_handler_offset() {
284     return byte_offset_of(Members, _pack_handler);
285   }
286 
287   static ByteSize pack_handler_jobject_offset() {
288     return byte_offset_of(Members, _pack_handler_jobject);
289   }
290 
291   static ByteSize unpack_handler_offset() {
292     return byte_offset_of(Members, _unpack_handler);
293   }
294 
295   static ByteSize null_reset_value_offset_offset() {
296     return byte_offset_of(Members, _null_reset_value_offset);
297   }
298 
299   static ByteSize payload_offset_offset() {
300     return byte_offset_of(Members, _payload_offset);
301   }
302 
303   static ByteSize null_marker_offset_offset() {
304     return byte_offset_of(Members, _null_marker_offset);
305   }
306 
307   oop null_reset_value() const;
308   void set_null_reset_value(oop val);
309 
310   void deallocate_contents(ClassLoaderData* loader_data);
311   static void cleanup(InlineKlass* ik) ;
312 
313   void print_on(outputStream* st) const override;
314 
315   // Verification
316   void verify_on(outputStream* st) override;
317   void oop_verify_on(oop obj, outputStream* st) override;
318 };
319 
320 #endif // SHARE_VM_OOPS_INLINEKLASS_HPP