1 /*
  2  * Copyright (c) 2017, 2024, 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 "classfile/classFileParser.hpp"
 29 #include "classfile/javaClasses.hpp"
 30 #include "oops/instanceKlass.hpp"
 31 #include "oops/method.hpp"
 32 #include "runtime/registerMap.hpp"
 33 
 34 // An InlineKlass is a specialized InstanceKlass for concrete value classes
 35 // (abstract value classes are represented by InstanceKlass)
 36 
 37 
 38 class InlineKlass: public InstanceKlass {
 39   friend class VMStructs;
 40   friend class InstanceKlass;
 41   friend class ClassFileParser;
 42 
 43  public:
 44   static const KlassKind Kind = InlineKlassKind;
 45 
 46   InlineKlass();
 47 
 48  private:
 49 
 50   // Constructor
 51   InlineKlass(const ClassFileParser& parser);
 52 
 53   void init_fixed_block();
 54   inline InlineKlassFixedBlock* inlineklass_static_block() const;
 55   inline address adr_return_regs() const;
 56 
 57   address adr_extended_sig() const {
 58     assert(_adr_inlineklass_fixed_block != nullptr, "Should have been initialized");
 59     return ((address)_adr_inlineklass_fixed_block) + in_bytes(byte_offset_of(InlineKlassFixedBlock, _extended_sig));
 60   }
 61 
 62   // pack and unpack handlers for inline types return
 63   address adr_pack_handler() const {
 64     assert(_adr_inlineklass_fixed_block != nullptr, "Should have been initialized");
 65     return ((address)_adr_inlineklass_fixed_block) + in_bytes(byte_offset_of(InlineKlassFixedBlock, _pack_handler));
 66   }
 67 
 68   address adr_pack_handler_jobject() const {
 69     assert(_adr_inlineklass_fixed_block != nullptr, "Should have been initialized");
 70     return ((address)_adr_inlineklass_fixed_block) + in_bytes(byte_offset_of(InlineKlassFixedBlock, _pack_handler_jobject));
 71   }
 72 
 73   address adr_unpack_handler() const {
 74     assert(_adr_inlineklass_fixed_block != nullptr, "Should have been initialized");
 75     return ((address)_adr_inlineklass_fixed_block) + in_bytes(byte_offset_of(InlineKlassFixedBlock, _unpack_handler));
 76   }
 77 
 78   address adr_default_value_offset() const {
 79     assert(_adr_inlineklass_fixed_block != nullptr, "Should have been initialized");
 80     return ((address)_adr_inlineklass_fixed_block) + in_bytes(default_value_offset_offset());
 81   }
 82 
 83   address adr_null_reset_value_offset() const {
 84     assert(_adr_inlineklass_fixed_block != nullptr, "Should have been initialized");
 85     return ((address)_adr_inlineklass_fixed_block) + in_bytes(null_reset_value_offset_offset());
 86   }
 87 
 88   ArrayKlass* volatile* adr_value_array_klasses() const {
 89     assert(_adr_inlineklass_fixed_block != nullptr, "Should have been initialized");
 90     return (ArrayKlass* volatile*) ((address)_adr_inlineklass_fixed_block) + in_bytes(byte_offset_of(InlineKlassFixedBlock, _null_free_inline_array_klasses));
 91   }
 92 
 93   ArrayKlass* value_array_klasses() const {
 94     return *adr_value_array_klasses();
 95   }
 96 
 97   address adr_first_field_offset() const {
 98     assert(_adr_inlineklass_fixed_block != nullptr, "Should have been initialized");
 99     return ((address)_adr_inlineklass_fixed_block) + in_bytes(byte_offset_of(InlineKlassFixedBlock, _first_field_offset));
100   }
101 
102   address adr_payload_size_in_bytes() const {
103     assert(_adr_inlineklass_fixed_block != nullptr, "Should have been initialized");
104     return ((address)_adr_inlineklass_fixed_block) + in_bytes(byte_offset_of(InlineKlassFixedBlock, _payload_size_in_bytes));
105   }
106 
107   address adr_payload_alignment() const {
108     assert(_adr_inlineklass_fixed_block != nullptr, "Should have been initialized");
109     return ((address)_adr_inlineklass_fixed_block) + in_bytes(byte_offset_of(InlineKlassFixedBlock, _payload_alignment));
110   }
111 
112   address adr_non_atomic_size_in_bytes() const {
113     assert(_adr_inlineklass_fixed_block != nullptr, "Should have been initialized");
114     return ((address)_adr_inlineklass_fixed_block) + in_bytes(byte_offset_of(InlineKlassFixedBlock, _non_atomic_size_in_bytes));
115   }
116 
117   address adr_non_atomic_alignment() const {
118     assert(_adr_inlineklass_fixed_block != nullptr, "Should have been initialized");
119     return ((address)_adr_inlineklass_fixed_block) + in_bytes(byte_offset_of(InlineKlassFixedBlock, _non_atomic_alignment));
120   }
121 
122   address adr_atomic_size_in_bytes() const {
123     assert(_adr_inlineklass_fixed_block != nullptr, "Should have been initialized");
124     return ((address)_adr_inlineklass_fixed_block) + in_bytes(byte_offset_of(InlineKlassFixedBlock, _atomic_size_in_bytes));
125   }
126 
127   address adr_nullable_size_in_bytes() const {
128     assert(_adr_inlineklass_fixed_block != nullptr, "Should have been initialized");
129     return ((address)_adr_inlineklass_fixed_block) + in_bytes(byte_offset_of(InlineKlassFixedBlock, _nullable_size_in_bytes));
130   }
131 
132   address adr_null_marker_offset() const {
133     assert(_adr_inlineklass_fixed_block != nullptr, "Should have been initialized");
134     return ((address)_adr_inlineklass_fixed_block) + in_bytes(byte_offset_of(InlineKlassFixedBlock, _null_marker_offset));
135   }
136 
137  public:
138 
139   bool is_empty_inline_type() const   { return _misc_flags.is_empty_inline_type(); }
140   void set_is_empty_inline_type()     { _misc_flags.set_is_empty_inline_type(true); }
141 
142   int first_field_offset() const {
143     int offset = *(int*)adr_first_field_offset();
144     assert(offset != 0, "Must be initialized before use");
145     return *(int*)adr_first_field_offset();
146   }
147 
148   void set_first_field_offset(int offset) { *(int*)adr_first_field_offset() = offset; }
149 
150   int payload_size_in_bytes() const { return *(int*)adr_payload_size_in_bytes(); }
151   void set_payload_size_in_bytes(int payload_size) { *(int*)adr_payload_size_in_bytes() = payload_size; }
152 
153   int payload_alignment() const { return *(int*)adr_payload_alignment(); }
154   void set_payload_alignment(int alignment) { *(int*)adr_payload_alignment() = alignment; }
155 
156   bool has_non_atomic_layout() const { return non_atomic_size_in_bytes() != -1; }
157   int non_atomic_size_in_bytes() const { return *(int*)adr_non_atomic_size_in_bytes(); }
158   void set_non_atomic_size_in_bytes(int size) { *(int*)adr_non_atomic_size_in_bytes() = size; }
159   int non_atomic_alignment() const { return *(int*)adr_non_atomic_alignment(); }
160   void set_non_atomic_alignment(int alignment) { *(int*)adr_non_atomic_alignment() = alignment; }
161 
162   bool has_atomic_layout() const { return atomic_size_in_bytes() != -1; }
163   int atomic_size_in_bytes() const { return *(int*)adr_atomic_size_in_bytes(); }
164   void set_atomic_size_in_bytes(int size) { *(int*)adr_atomic_size_in_bytes() = size; }
165 
166   bool has_nullable_layout() const { return nullable_size_in_bytes() != -1; }
167   int nullable_size_in_bytes() const { return *(int*)adr_nullable_size_in_bytes(); }
168   void set_nullable_size_in_bytes(int size) { *(int*)adr_nullable_size_in_bytes() = size; }
169   int null_marker_offset() const { return *(int*)adr_null_marker_offset(); }
170   void set_null_marker_offset(int offset) { *(int*)adr_null_marker_offset() = offset; }
171 
172   int layout_alignment(LayoutKind kind) const;
173   int layout_size_in_bytes(LayoutKind kind) const;
174 
175   virtual void remove_unshareable_info();
176   virtual void remove_java_mirror();
177   virtual void restore_unshareable_info(ClassLoaderData* loader_data, Handle protection_domain, PackageEntry* pkg_entry, TRAPS);
178   virtual void metaspace_pointers_do(MetaspaceClosure* it);
179 
180  private:
181   int collect_fields(GrowableArray<SigEntry>* sig, int base_off = 0);
182 
183   void cleanup_blobs();
184 
185  public:
186   // Type testing
187   bool is_inline_klass_slow() const        { return true; }
188 
189   // Casting from Klass*
190 
191   static InlineKlass* cast(Klass* k) {
192     return const_cast<InlineKlass*>(cast(const_cast<const Klass*>(k)));
193   }
194 
195   static const InlineKlass* cast(const Klass* k) {
196     assert(k != nullptr, "k should not be null");
197     assert(k->is_inline_klass(), "cast to InlineKlass");
198     return static_cast<const InlineKlass*>(k);
199   }
200 
201   // Use this to return the size of an instance in heap words.
202   // Note that this size only applies to heap allocated stand-alone instances.
203   virtual int size_helper() const {
204     return layout_helper_to_size_helper(layout_helper());
205   }
206 
207   // allocate_instance() allocates a stand alone value in the Java heap
208   // initialized to default value (cleared memory)
209   instanceOop allocate_instance(TRAPS);
210   // allocates a stand alone inline buffer in the Java heap
211   // DOES NOT have memory cleared, user MUST initialize payload before
212   // returning to Java (i.e.: inline_copy)
213   instanceOop allocate_instance_buffer(TRAPS);
214 
215   address data_for_oop(oop o) const;
216 
217   bool flat_array();
218 
219   bool contains_oops() const { return nonstatic_oop_map_count() > 0; }
220   int nonstatic_oop_count();
221 
222   // null free inline arrays...
223   //
224 
225   // null free inline array klass, akin to InstanceKlass::array_klass()
226   // Returns the array class for the n'th dimension
227   Klass* value_array_klass(int n, TRAPS);
228   Klass* value_array_klass_or_null(int n);
229 
230   // Returns the array class with this class as element type
231   Klass* value_array_klass(TRAPS);
232   Klass* value_array_klass_or_null();
233 
234 
235   // General store methods
236   //
237   // Normally loads and store methods would be found in *Oops classes, but since values can be
238   // "in-lined" (flat layout) into containing oops, these methods reside here in InlineKlass.
239   //
240   // "inline_copy_*_to_new_*" assume new memory (i.e. IS_DEST_UNINITIALIZED for write barriers)
241 
242   void inline_copy_payload_to_new_oop(void* src, oop dst, LayoutKind lk);
243   void inline_copy_oop_to_new_oop(oop src, oop dst, LayoutKind lk);
244   void inline_copy_oop_to_new_payload(oop src, void* dst, LayoutKind lk);
245   void inline_copy_oop_to_payload(oop src, void* dst, LayoutKind lk);
246 
247   oop read_flat_field(oop obj, int offset, LayoutKind lk, TRAPS);
248   void write_flat_field(oop obj, int offset, oop value, bool is_null_free, LayoutKind lk, TRAPS);
249 
250   // oop iterate raw inline type data pointer (where oop_addr may not be an oop, but backing/array-element)
251   template <typename T, class OopClosureType>
252   inline void oop_iterate_specialized(const address oop_addr, OopClosureType* closure);
253 
254   template <typename T, class OopClosureType>
255   inline void oop_iterate_specialized_bounded(const address oop_addr, OopClosureType* closure, void* lo, void* hi);
256 
257   // calling convention support
258   void initialize_calling_convention(TRAPS);
259   Array<SigEntry>* extended_sig() const {
260     return *((Array<SigEntry>**)adr_extended_sig());
261   }
262   inline Array<VMRegPair>* return_regs() const;
263   bool can_be_passed_as_fields() const;
264   bool can_be_returned_as_fields(bool init = false) const;
265   void save_oop_fields(const RegisterMap& map, GrowableArray<Handle>& handles) const;
266   void restore_oop_results(RegisterMap& map, GrowableArray<Handle>& handles) const;
267   oop realloc_result(const RegisterMap& reg_map, const GrowableArray<Handle>& handles, TRAPS);
268   static InlineKlass* returned_inline_klass(const RegisterMap& reg_map);
269 
270   address pack_handler() const {
271     return *(address*)adr_pack_handler();
272   }
273 
274   address unpack_handler() const {
275     return *(address*)adr_unpack_handler();
276   }
277 
278   // pack and unpack handlers. Need to be loadable from generated code
279   // so at a fixed offset from the base of the klass pointer.
280   static ByteSize pack_handler_offset() {
281     return byte_offset_of(InlineKlassFixedBlock, _pack_handler);
282   }
283 
284   static ByteSize pack_handler_jobject_offset() {
285     return byte_offset_of(InlineKlassFixedBlock, _pack_handler_jobject);
286   }
287 
288   static ByteSize unpack_handler_offset() {
289     return byte_offset_of(InlineKlassFixedBlock, _unpack_handler);
290   }
291 
292   static ByteSize default_value_offset_offset() {
293     return byte_offset_of(InlineKlassFixedBlock, _default_value_offset);
294   }
295 
296   static ByteSize null_reset_value_offset_offset() {
297     return byte_offset_of(InlineKlassFixedBlock, _null_reset_value_offset);
298   }
299 
300   static ByteSize first_field_offset_offset() {
301     return byte_offset_of(InlineKlassFixedBlock, _first_field_offset);
302   }
303 
304   static ByteSize null_marker_offset_offset() {
305     return byte_offset_of(InlineKlassFixedBlock, _null_marker_offset);
306   }
307 
308   void set_default_value_offset(int offset) {
309     *((int*)adr_default_value_offset()) = offset;
310   }
311 
312   int default_value_offset() {
313     int offset = *((int*)adr_default_value_offset());
314     assert(offset != 0, "must not be called if not initialized");
315     return offset;
316   }
317 
318   void set_default_value(oop val);
319 
320   oop default_value() {
321     assert(is_initialized() || is_being_initialized() || is_in_error_state(), "default value is set at the beginning of initialization");
322     oop val = java_mirror()->obj_field_acquire(default_value_offset());
323     assert(val != nullptr, "Sanity check");
324     return val;
325   }
326 
327   void set_null_reset_value_offset(int offset) {
328     *((int*)adr_null_reset_value_offset()) = offset;
329   }
330 
331   int null_reset_value_offset() {
332     int offset = *((int*)adr_null_reset_value_offset());
333     assert(offset != 0, "must not be called if not initialized");
334     return offset;
335   }
336 
337   void set_null_reset_value(oop val);
338 
339   oop null_reset_value() {
340     assert(is_initialized() || is_being_initialized() || is_in_error_state(), "null reset value is set at the beginning of initialization");
341     oop val = java_mirror()->obj_field_acquire(null_reset_value_offset());
342     assert(val != nullptr, "Sanity check");
343     return val;
344   }
345 
346   void deallocate_contents(ClassLoaderData* loader_data);
347   static void cleanup(InlineKlass* ik) ;
348 
349   // Verification
350   void verify_on(outputStream* st);
351   void oop_verify_on(oop obj, outputStream* st);
352 
353 };
354 
355 #endif /* SHARE_VM_OOPS_INLINEKLASS_HPP */