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   ArrayKlass* volatile* adr_value_array_klasses() const {
 84     assert(_adr_inlineklass_fixed_block != nullptr, "Should have been initialized");
 85     return (ArrayKlass* volatile*) ((address)_adr_inlineklass_fixed_block) + in_bytes(byte_offset_of(InlineKlassFixedBlock, _null_free_inline_array_klasses));
 86   }
 87 
 88   ArrayKlass* value_array_klasses() const {
 89     return *adr_value_array_klasses();
 90   }
 91 
 92   address adr_alignment() const {
 93     assert(_adr_inlineklass_fixed_block != nullptr, "Should have been initialized");
 94     return ((address)_adr_inlineklass_fixed_block) + in_bytes(byte_offset_of(InlineKlassFixedBlock, _alignment));
 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_internal_null_marker_offset() 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, _internal_null_marker_offset));
110   }
111 
112  public:
113   int get_alignment() const {
114     return *(int*)adr_alignment();
115   }
116 
117   void set_alignment(int alignment) {
118     *(int*)adr_alignment() = alignment;
119   }
120 
121   int first_field_offset() const {
122     int offset = *(int*)adr_first_field_offset();
123     assert(offset != 0, "Must be initialized before use");
124     return *(int*)adr_first_field_offset();
125   }
126 
127   void set_first_field_offset(int offset) {
128     *(int*)adr_first_field_offset() = offset;
129   }
130 
131   int get_payload_size_in_bytes() const {
132     return *(int*)adr_payload_size_in_bytes();
133   }
134 
135   void set_payload_size_in_bytes(int payload_size) {
136     *(int*)adr_payload_size_in_bytes() = payload_size;
137   }
138 
139   void set_internal_null_marker_offset(int offset) {
140     *(int*)adr_internal_null_marker_offset() = offset;
141   }
142 
143   bool has_internal_null_marker_offset() const {
144     return *(int*)adr_internal_null_marker_offset() != -1;
145   }
146 
147   int get_internal_null_marker_offset() const {
148     assert(has_internal_null_marker_offset(), "Must not be call if value class has no internal null marker");
149     return *(int*)adr_internal_null_marker_offset();
150   }
151 
152   int first_field_offset_old();
153 
154   virtual void remove_unshareable_info();
155   virtual void remove_java_mirror();
156   virtual void restore_unshareable_info(ClassLoaderData* loader_data, Handle protection_domain, PackageEntry* pkg_entry, TRAPS);
157   virtual void metaspace_pointers_do(MetaspaceClosure* it);
158 
159  private:
160   int collect_fields(GrowableArray<SigEntry>* sig, int base_off = 0);
161 
162   void cleanup_blobs();
163 
164  public:
165   // Type testing
166   bool is_inline_klass_slow() const        { return true; }
167 
168   // Casting from Klass*
169   static InlineKlass* cast(Klass* k);
170 
171   // Use this to return the size of an instance in heap words.
172   // Note that this size only applies to heap allocated stand-alone instances.
173   virtual int size_helper() const {
174     return layout_helper_to_size_helper(layout_helper());
175   }
176 
177   // allocate_instance() allocates a stand alone value in the Java heap
178   // initialized to default value (cleared memory)
179   instanceOop allocate_instance(TRAPS);
180   // allocates a stand alone inline buffer in the Java heap
181   // DOES NOT have memory cleared, user MUST initialize payload before
182   // returning to Java (i.e.: inline_copy)
183   instanceOop allocate_instance_buffer(TRAPS);
184 
185   address data_for_oop(oop o) const;
186 
187   // Query if this class promises atomicity one way or another
188   bool is_atomic() { return is_naturally_atomic() || must_be_atomic(); }
189 
190   bool flat_array();
191 
192   bool contains_oops() const { return nonstatic_oop_map_count() > 0; }
193   int nonstatic_oop_count();
194 
195   // null free inline arrays...
196   //
197 
198   // null free inline array klass, akin to InstanceKlass::array_klass()
199   // Returns the array class for the n'th dimension
200   Klass* value_array_klass(int n, TRAPS);
201   Klass* value_array_klass_or_null(int n);
202 
203   // Returns the array class with this class as element type
204   Klass* value_array_klass(TRAPS);
205   Klass* value_array_klass_or_null();
206 
207 
208   // General store methods
209   //
210   // Normally loads and store methods would be found in *Oops classes, but since values can be
211   // "in-lined" (flat layout) into containing oops, these methods reside here in InlineKlass.
212   //
213   // "inline_copy_*_to_new_*" assume new memory (i.e. IS_DEST_UNINITIALIZED for write barriers)
214 
215   void inline_copy_payload_to_new_oop(void* src, oop dst);
216   void inline_copy_oop_to_new_oop(oop src, oop dst);
217   void inline_copy_oop_to_new_payload(oop src, void* dst);
218   void inline_copy_oop_to_payload(oop src, void* dst);
219 
220   oop read_flat_field(oop obj, int offset, TRAPS);
221   void write_flat_field(oop obj, int offset, oop value, TRAPS);
222   void write_non_null_flat_field(oop obj, int offset, oop value);
223 
224   // oop iterate raw inline type data pointer (where oop_addr may not be an oop, but backing/array-element)
225   template <typename T, class OopClosureType>
226   inline void oop_iterate_specialized(const address oop_addr, OopClosureType* closure);
227 
228   template <typename T, class OopClosureType>
229   inline void oop_iterate_specialized_bounded(const address oop_addr, OopClosureType* closure, void* lo, void* hi);
230 
231   // calling convention support
232   void initialize_calling_convention(TRAPS);
233   Array<SigEntry>* extended_sig() const {
234     return *((Array<SigEntry>**)adr_extended_sig());
235   }
236   inline Array<VMRegPair>* return_regs() const;
237   bool can_be_passed_as_fields() const;
238   bool can_be_returned_as_fields(bool init = false) const;
239   void save_oop_fields(const RegisterMap& map, GrowableArray<Handle>& handles) const;
240   void restore_oop_results(RegisterMap& map, GrowableArray<Handle>& handles) const;
241   oop realloc_result(const RegisterMap& reg_map, const GrowableArray<Handle>& handles, TRAPS);
242   static InlineKlass* returned_inline_klass(const RegisterMap& reg_map);
243 
244   address pack_handler() const {
245     return *(address*)adr_pack_handler();
246   }
247 
248   address unpack_handler() const {
249     return *(address*)adr_unpack_handler();
250   }
251 
252   // pack and unpack handlers. Need to be loadable from generated code
253   // so at a fixed offset from the base of the klass pointer.
254   static ByteSize pack_handler_offset() {
255     return byte_offset_of(InlineKlassFixedBlock, _pack_handler);
256   }
257 
258   static ByteSize pack_handler_jobject_offset() {
259     return byte_offset_of(InlineKlassFixedBlock, _pack_handler_jobject);
260   }
261 
262   static ByteSize unpack_handler_offset() {
263     return byte_offset_of(InlineKlassFixedBlock, _unpack_handler);
264   }
265 
266   static ByteSize default_value_offset_offset() {
267     return byte_offset_of(InlineKlassFixedBlock, _default_value_offset);
268   }
269 
270   static ByteSize first_field_offset_offset() {
271     return byte_offset_of(InlineKlassFixedBlock, _first_field_offset);
272   }
273 
274   static ByteSize internal_null_marker_offset_offset() {
275     return byte_offset_of(InlineKlassFixedBlock, _internal_null_marker_offset);
276   }
277 
278   void set_default_value_offset(int offset) {
279     *((int*)adr_default_value_offset()) = offset;
280   }
281 
282   int default_value_offset() {
283     int offset = *((int*)adr_default_value_offset());
284     assert(offset != 0, "must not be called if not initialized");
285     return offset;
286   }
287 
288   void set_default_value(oop val) {
289     java_mirror()->obj_field_put(default_value_offset(), val);
290   }
291 
292   oop default_value();
293   void deallocate_contents(ClassLoaderData* loader_data);
294   static void cleanup(InlineKlass* ik) ;
295 
296   // Verification
297   void verify_on(outputStream* st);
298   void oop_verify_on(oop obj, outputStream* st);
299 
300 };
301 
302 #endif /* SHARE_VM_OOPS_INLINEKLASS_HPP */