1 /*
  2  * Copyright (c) 1997, 2025, 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_OOP_HPP
 26 #define SHARE_OOPS_OOP_HPP
 27 
 28 #include "memory/iterator.hpp"
 29 #include "memory/memRegion.hpp"
 30 #include "oops/compressedKlass.hpp"
 31 #include "oops/accessDecorators.hpp"
 32 #include "oops/markWord.hpp"
 33 #include "oops/metadata.hpp"
 34 #include "oops/objLayout.hpp"
 35 #include "runtime/atomic.hpp"
 36 #include "utilities/globalDefinitions.hpp"
 37 #include "utilities/macros.hpp"
 38 #include <type_traits>
 39 
 40 // oopDesc is the top baseclass for objects classes. The {name}Desc classes describe
 41 // the format of Java objects so the fields can be accessed from C++.
 42 // oopDesc is abstract.
 43 // (see oopHierarchy for complete oop class hierarchy)
 44 //
 45 // no virtual functions allowed
 46 
 47 class oopDesc {
 48   friend class VMStructs;
 49   friend class JVMCIVMStructs;
 50  private:
 51   volatile markWord _mark;
 52   union _metadata {
 53     Klass*      _klass;
 54     narrowKlass _compressed_klass;
 55   } _metadata;
 56 
 57   // There may be ordering constraints on the initialization of fields that
 58   // make use of the C++ copy/assign incorrect.
 59   NONCOPYABLE(oopDesc);
 60 
 61   inline oop cas_set_forwardee(markWord new_mark, markWord old_mark, atomic_memory_order order);
 62 
 63  public:
 64   // Must be trivial; see verifying static assert after the class.
 65   oopDesc() = default;
 66 
 67   inline markWord  mark()          const;
 68   inline markWord  mark_acquire()  const;
 69   inline markWord* mark_addr() const;
 70 
 71   inline void set_mark(markWord m);
 72   static inline void set_mark(HeapWord* mem, markWord m);
 73   inline void set_mark_full(markWord m);
 74   static inline void release_set_mark(HeapWord* mem, markWord m);
 75 
 76   inline void release_set_mark(markWord m);
 77   inline markWord cas_set_mark(markWord new_mark, markWord old_mark);
 78   inline markWord cas_set_mark(markWord new_mark, markWord old_mark, atomic_memory_order order);
 79 
 80   // Returns the prototype mark that should be used for this object.
 81   inline markWord prototype_mark() const;
 82 
 83   // Used only to re-initialize the mark word (e.g., of promoted
 84   // objects during a GC) -- requires a valid klass pointer
 85   inline void init_mark();
 86 
 87   inline Klass* klass() const;
 88   inline Klass* klass_or_null() const;
 89   inline Klass* klass_or_null_acquire() const;
 90   // Get the klass without running any asserts.
 91   inline Klass* klass_without_asserts() const;
 92 
 93   void set_narrow_klass(narrowKlass nk) NOT_CDS_JAVA_HEAP_RETURN;
 94   inline void set_klass(Klass* k);
 95   static inline void release_set_klass(HeapWord* mem, Klass* k);
 96 
 97   // For klass field compression
 98   static inline void set_klass_gap(HeapWord* mem, int z);
 99 
100   // Size of object header, aligned to platform wordSize
101   static int header_size() {
102     if (UseCompactObjectHeaders) {
103       return sizeof(markWord) / HeapWordSize;
104     } else {
105       return sizeof(oopDesc)  / HeapWordSize;
106     }
107   }
108 
109   // Returns whether this is an instance of k or an instance of a subclass of k
110   inline bool is_a(Klass* k) const;
111 
112   // Returns the actual oop size of the object in machine words
113   inline size_t size();
114 
115   // Returns the size that a copy of this object requires, in machine words.
116   // It can be 1 word larger than its current size to accomodate
117   // an additional 4-byte-field for the identity hash-code.
118   //
119   // size: the current size of this object, we're passing this here for performance
120   //       reasons, because all callers compute this anyway, and we want to avoid
121   //       recomputing it.
122   // mark: the mark-word of this object. Some callers (e.g. G1ParScanThreadState::do_copy_to_survivor_space())
123   //       need to use a known markWord because of racing GC threads that can change
124   //       the markWord at any time.
125   inline size_t copy_size(size_t size, markWord mark) const;
126   // Special version to deal with scratch classes in CDS. There we allocate
127   // temporary scratch classes (which are skeleton versions of InstanceMirrorKlass,
128   // which represent java.lang.Class objects in the CDS archive). At that point, we
129   // don't know whether or not the final archived version will be hashed or expanded,
130   // and therefore we allocate them in the special state not-hashed-but-expanded.
131   // When creating the final copy of those objects, we either populate the hidden hash
132   // field and make the object 'expanded', or we turn it back to 'not-hashed'
133   // and reduce the object's size. We do this by providing a separate method for CDS
134   // so that we don't affect GC performance.
135   inline size_t copy_size_cds(size_t size, markWord mark) const;
136 
137   // Sometimes (for complicated concurrency-related reasons), it is useful
138   // to be able to figure out the size of an object knowing its klass.
139   inline size_t base_size_given_klass(markWord m, const Klass* klass);
140   inline size_t size_given_mark_and_klass(markWord mrk, const Klass* kls);
141 
142   // type test operations (inlined in oop.inline.hpp)
143   inline bool is_instance()    const;
144   inline bool is_instanceRef() const;
145   inline bool is_stackChunk()  const;
146   inline bool is_array()       const;
147   inline bool is_objArray()    const;
148   inline bool is_typeArray()   const;
149 
150   // type test operations that don't require inclusion of oop.inline.hpp.
151   bool is_instance_noinline()    const;
152   bool is_instanceRef_noinline() const;
153   bool is_stackChunk_noinline()  const;
154   bool is_array_noinline()       const;
155   bool is_objArray_noinline()    const;
156   bool is_typeArray_noinline()   const;
157 
158  protected:
159   inline oop        as_oop() const { return const_cast<oopDesc*>(this); }
160 
161  public:
162   template<typename T>
163   inline T* field_addr(int offset) const;
164 
165   template <typename T> inline size_t field_offset(T* p) const;
166 
167   // Standard compare function returns negative value if o1 < o2
168   //                                   0              if o1 == o2
169   //                                   positive value if o1 > o2
170   inline static int  compare(oop o1, oop o2) {
171     void* o1_addr = (void*)o1;
172     void* o2_addr = (void*)o2;
173     if (o1_addr < o2_addr) {
174       return -1;
175     } else if (o1_addr > o2_addr) {
176       return 1;
177     } else {
178       return 0;
179     }
180   }
181 
182   // Access to fields in a instanceOop through these methods.
183   template<DecoratorSet decorators>
184   oop obj_field_access(int offset) const;
185   oop obj_field(int offset) const;
186 
187   void obj_field_put(int offset, oop value);
188   void obj_field_put_raw(int offset, oop value);
189   void obj_field_put_volatile(int offset, oop value);
190   template<DecoratorSet decorators>
191   void obj_field_put_access(int offset, oop value);
192 
193   Metadata* metadata_field(int offset) const;
194   void metadata_field_put(int offset, Metadata* value);
195 
196   Metadata* metadata_field_acquire(int offset) const;
197   void release_metadata_field_put(int offset, Metadata* value);
198 
199   jbyte byte_field(int offset) const;
200   void byte_field_put(int offset, jbyte contents);
201 
202   jchar char_field(int offset) const;
203   void char_field_put(int offset, jchar contents);
204 
205   jboolean bool_field(int offset) const;
206   void bool_field_put(int offset, jboolean contents);
207   jboolean bool_field_volatile(int offset) const;
208   void bool_field_put_volatile(int offset, jboolean contents);
209 
210   jint int_field(int offset) const;
211   void int_field_put(int offset, jint contents);
212 
213   jshort short_field(int offset) const;
214   void short_field_put(int offset, jshort contents);
215 
216   jlong long_field(int offset) const;
217   void long_field_put(int offset, jlong contents);
218 
219   jfloat float_field(int offset) const;
220   void float_field_put(int offset, jfloat contents);
221 
222   jdouble double_field(int offset) const;
223   void double_field_put(int offset, jdouble contents);
224 
225   address address_field(int offset) const;
226   void address_field_put(int offset, address contents);
227 
228   oop obj_field_acquire(int offset) const;
229   void release_obj_field_put(int offset, oop value);
230 
231   jbyte byte_field_acquire(int offset) const;
232   void release_byte_field_put(int offset, jbyte contents);
233 
234   jchar char_field_acquire(int offset) const;
235   void release_char_field_put(int offset, jchar contents);
236 
237   jboolean bool_field_acquire(int offset) const;
238   void release_bool_field_put(int offset, jboolean contents);
239 
240   jint int_field_relaxed(int offset) const;
241   void int_field_put_relaxed(int offset, jint contents);
242   jint int_field_acquire(int offset) const;
243   void release_int_field_put(int offset, jint contents);
244 
245   jshort short_field_acquire(int offset) const;
246   void release_short_field_put(int offset, jshort contents);
247 
248   jlong long_field_acquire(int offset) const;
249   void release_long_field_put(int offset, jlong contents);
250 
251   jfloat float_field_acquire(int offset) const;
252   void release_float_field_put(int offset, jfloat contents);
253 
254   jdouble double_field_acquire(int offset) const;
255   void release_double_field_put(int offset, jdouble contents);
256 
257   address address_field_acquire(int offset) const;
258   void release_address_field_put(int offset, address contents);
259 
260   // printing functions for VM debugging
261   void print_on(outputStream* st) const;         // First level print
262   void print_value_on(outputStream* st) const;   // Second level print.
263   void print_address_on(outputStream* st) const; // Address printing
264   void print_name_on(outputStream* st) const;    // External name printing.
265 
266   // printing on default output stream
267   void print();
268   void print_value();
269   void print_address();
270 
271   // return the print strings
272   char* print_string();
273   char* print_value_string();
274 
275   // verification operations
276   static void verify_on(outputStream* st, oopDesc* oop_desc);
277   static void verify(oopDesc* oopDesc);
278 
279   // locking operations
280   inline bool is_locked()   const;
281   inline bool is_unlocked() const;
282 
283   // asserts and guarantees
284   static bool is_oop(oop obj, bool ignore_mark_word = false);
285   static bool is_oop_or_null(oop obj, bool ignore_mark_word = false);
286 
287   // garbage collection
288   inline bool is_gc_marked() const;
289 
290   // Forward pointer operations for scavenge
291   inline bool is_forwarded() const;
292   inline bool is_self_forwarded() const;
293 
294   inline void forward_to(oop p);
295   inline void forward_to_self();
296 
297   // Like "forward_to", but inserts the forwarding pointer atomically.
298   // Exactly one thread succeeds in inserting the forwarding pointer, and
299   // this call returns null for that thread; any other thread has the
300   // value of the forwarding pointer returned and does not modify "this".
301   inline oop forward_to_atomic(oop p, markWord compare, atomic_memory_order order = memory_order_conservative);
302   inline oop forward_to_self_atomic(markWord compare, atomic_memory_order order = memory_order_conservative);
303 
304   inline oop forwardee() const;
305   inline oop forwardee(markWord header) const;
306 
307   inline void unset_self_forwarded();
308   inline void reset_forwarded();
309 
310   // Age of object during scavenge
311   inline uint age() const;
312   inline void incr_age();
313 
314   template <typename OopClosureType>
315   inline void oop_iterate(OopClosureType* cl);
316 
317   template <typename OopClosureType>
318   inline void oop_iterate(OopClosureType* cl, MemRegion mr);
319 
320   template <typename OopClosureType>
321   inline size_t oop_iterate_size(OopClosureType* cl);
322 
323   template <typename OopClosureType>
324   inline size_t oop_iterate_size(OopClosureType* cl, MemRegion mr);
325 
326   template <typename OopClosureType>
327   inline void oop_iterate_backwards(OopClosureType* cl);
328 
329   template <typename OopClosureType>
330   inline void oop_iterate_backwards(OopClosureType* cl, Klass* klass);
331 
332   inline static bool is_instanceof_or_null(oop obj, Klass* klass);
333 
334   // identity hash; returns the identity hash key (computes it if necessary)
335   inline intptr_t identity_hash();
336   intptr_t slow_identity_hash();
337   inline bool fast_no_hash_check();
338 
339   // Initialize identity hash code in hash word of object copy from original object.
340   // Returns true if the object has been expanded, false otherwise.
341   inline void initialize_hash_if_necessary(oop obj);
342   // For CDS only.
343   void initialize_hash_if_necessary(oop obj, Klass* k, markWord m);
344 
345   // marks are forwarded to stack when object is locked
346   inline bool     has_displaced_mark() const;
347   inline markWord displaced_mark() const;
348   inline void     set_displaced_mark(markWord m);
349 
350   // Checks if the mark word needs to be preserved
351   inline bool mark_must_be_preserved() const;
352   inline bool mark_must_be_preserved(markWord m) const;
353 
354   inline static bool has_klass_gap() {
355     return ObjLayout::oop_has_klass_gap();
356   }
357 
358   // for code generation
359   static int mark_offset_in_bytes()      { return (int)offset_of(oopDesc, _mark); }
360   static int klass_offset_in_bytes()     {
361     assert(!UseCompactObjectHeaders, "don't use this with compact headers");
362     return (int)offset_of(oopDesc, _metadata._klass);
363   }
364   static int klass_gap_offset_in_bytes() {
365     assert(has_klass_gap(), "only applicable to compressed klass pointers");
366     if (UseCompactObjectHeaders) {
367       return base_offset_in_bytes();
368     } else {
369       return klass_offset_in_bytes() + sizeof(narrowKlass);
370     }
371   }
372 
373   static int base_offset_in_bytes() {
374     return ObjLayout::oop_base_offset_in_bytes();
375   }
376 
377   // for error reporting
378   static void* load_oop_raw(oop obj, int offset);
379 };
380 
381 // An oopDesc is not initialized via a constructor.  Space is allocated in
382 // the Java heap, and static functions provided here on HeapWord* are used
383 // to fill in certain parts of that memory.  The allocated memory is then
384 // treated as referring to an oopDesc.  For that to be valid, the oopDesc
385 // class must have a trivial default constructor (C++14 3.8/1).
386 static_assert(std::is_trivially_default_constructible<oopDesc>::value, "required");
387 
388 #endif // SHARE_OOPS_OOP_HPP