1 /* 2 * Copyright (c) 1997, 2021, 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/accessDecorators.hpp" 31 #include "oops/markWord.hpp" 32 #include "oops/metadata.hpp" 33 #include "runtime/atomic.hpp" 34 #include "utilities/macros.hpp" 35 36 // oopDesc is the top baseclass for objects classes. The {name}Desc classes describe 37 // the format of Java objects so the fields can be accessed from C++. 38 // oopDesc is abstract. 39 // (see oopHierarchy for complete oop class hierarchy) 40 // 41 // no virtual functions allowed 42 43 // Forward declarations. 44 class OopClosure; 45 class FilteringClosure; 46 47 class PSPromotionManager; 48 class ParCompactionManager; 49 50 class oopDesc { 51 friend class VMStructs; 52 friend class JVMCIVMStructs; 53 private: 54 volatile markWord _mark; 55 union _metadata { 56 Klass* _klass; 57 narrowKlass _compressed_klass; 58 } _metadata; 59 60 public: 61 inline markWord mark() const; 62 inline markWord mark_acquire() const; 63 inline markWord* mark_addr() const; 64 65 inline void set_mark(markWord m); 66 static inline void set_mark(HeapWord* mem, markWord m); 67 68 inline void release_set_mark(markWord m); 69 static inline void release_set_mark(HeapWord* mem, markWord m); 70 inline markWord cas_set_mark(markWord new_mark, markWord old_mark); 71 inline markWord cas_set_mark(markWord new_mark, markWord old_mark, atomic_memory_order order); 72 73 inline markWord resolve_mark() const; 74 75 // Returns the prototype mark that should be used for this object. 76 inline markWord prototype_mark() const; 77 78 // Used only to re-initialize the mark word (e.g., of promoted 79 // objects during a GC) -- requires a valid klass pointer 80 inline void init_mark(); 81 82 inline Klass* klass() const; 83 inline Klass* klass_or_null() const; 84 inline Klass* klass_or_null_acquire() const; 85 86 void set_narrow_klass(narrowKlass nk) NOT_CDS_JAVA_HEAP_RETURN; 87 inline void set_klass(Klass* k); 88 static inline void release_set_klass(HeapWord* mem, Klass* k); 89 90 // For klass field compression 91 inline int klass_gap() const; 92 inline void set_klass_gap(int z); 93 static inline void set_klass_gap(HeapWord* mem, int z); 94 95 // size of object header, aligned to platform wordSize 96 static int header_size() { 97 #ifdef _LP64 98 if (UseCompactObjectHeaders) { 99 return sizeof(markWord) / HeapWordSize; 100 } else 101 #endif 102 return sizeof(oopDesc)/HeapWordSize; 103 } 104 105 // Returns whether this is an instance of k or an instance of a subclass of k 106 inline bool is_a(Klass* k) const; 107 108 // Returns the actual oop size of the object 109 inline int size(); 110 111 // Sometimes (for complicated concurrency-related reasons), it is useful 112 // to be able to figure out the size of an object knowing its klass. 113 inline int size_given_klass(Klass* klass); 114 115 // The following set of methods is used to access the mark-word and related 116 // properties when the object may be forwarded. Be careful where and when 117 // using this method. It assumes that the forwardee is installed in 118 // the header as a plain pointer (or self-forwarded). In particular, 119 // those methods can not deal with the sliding-forwarding that is used 120 // in Serial, G1 and Shenandoah full-GCs. 121 private: 122 inline Klass* forward_safe_klass_impl(markWord m) const; 123 public: 124 inline Klass* forward_safe_klass() const; 125 inline size_t forward_safe_size(); 126 inline Klass* forward_safe_klass(markWord m) const; 127 inline void forward_safe_init_mark(); 128 129 // type test operations (inlined in oop.inline.hpp) 130 inline bool is_instance() const; 131 inline bool is_array() const; 132 inline bool is_objArray() const; 133 inline bool is_typeArray() const; 134 135 // type test operations that don't require inclusion of oop.inline.hpp. 136 bool is_instance_noinline() const; 137 bool is_array_noinline() const; 138 bool is_objArray_noinline() const; 139 bool is_typeArray_noinline() const; 140 141 protected: 142 inline oop as_oop() const { return const_cast<oopDesc*>(this); } 143 144 public: 145 // field addresses in oop 146 inline void* field_addr(int offset) const; 147 148 // Need this as public for garbage collection. 149 template <class T> inline T* obj_field_addr(int offset) const; 150 151 template <typename T> inline size_t field_offset(T* p) const; 152 153 // Standard compare function returns negative value if o1 < o2 154 // 0 if o1 == o2 155 // positive value if o1 > o2 156 inline static int compare(oop o1, oop o2) { 157 void* o1_addr = (void*)o1; 158 void* o2_addr = (void*)o2; 159 if (o1_addr < o2_addr) { 160 return -1; 161 } else if (o1_addr > o2_addr) { 162 return 1; 163 } else { 164 return 0; 165 } 166 } 167 168 // Access to fields in a instanceOop through these methods. 169 template <DecoratorSet decorator> 170 oop obj_field_access(int offset) const; 171 oop obj_field(int offset) const; 172 void obj_field_put(int offset, oop value); 173 void obj_field_put_raw(int offset, oop value); 174 void obj_field_put_volatile(int offset, oop value); 175 176 Metadata* metadata_field(int offset) const; 177 Metadata* metadata_field_raw(int offset) const; 178 void metadata_field_put(int offset, Metadata* value); 179 180 Metadata* metadata_field_acquire(int offset) const; 181 void release_metadata_field_put(int offset, Metadata* value); 182 183 jbyte byte_field(int offset) const; 184 void byte_field_put(int offset, jbyte contents); 185 186 jchar char_field(int offset) const; 187 void char_field_put(int offset, jchar contents); 188 189 jboolean bool_field(int offset) const; 190 void bool_field_put(int offset, jboolean contents); 191 jboolean bool_field_volatile(int offset) const; 192 void bool_field_put_volatile(int offset, jboolean contents); 193 194 jint int_field(int offset) const; 195 jint int_field_raw(int offset) const; 196 void int_field_put(int offset, jint contents); 197 198 jshort short_field(int offset) const; 199 void short_field_put(int offset, jshort contents); 200 201 jlong long_field(int offset) const; 202 void long_field_put(int offset, jlong contents); 203 204 jfloat float_field(int offset) const; 205 void float_field_put(int offset, jfloat contents); 206 207 jdouble double_field(int offset) const; 208 void double_field_put(int offset, jdouble contents); 209 210 address address_field(int offset) const; 211 void address_field_put(int offset, address contents); 212 213 oop obj_field_acquire(int offset) const; 214 void release_obj_field_put(int offset, oop value); 215 216 jbyte byte_field_acquire(int offset) const; 217 void release_byte_field_put(int offset, jbyte contents); 218 219 jchar char_field_acquire(int offset) const; 220 void release_char_field_put(int offset, jchar contents); 221 222 jboolean bool_field_acquire(int offset) const; 223 void release_bool_field_put(int offset, jboolean contents); 224 225 jint int_field_acquire(int offset) const; 226 void release_int_field_put(int offset, jint contents); 227 228 jshort short_field_acquire(int offset) const; 229 void release_short_field_put(int offset, jshort contents); 230 231 jlong long_field_acquire(int offset) const; 232 void release_long_field_put(int offset, jlong contents); 233 234 jfloat float_field_acquire(int offset) const; 235 void release_float_field_put(int offset, jfloat contents); 236 237 jdouble double_field_acquire(int offset) const; 238 void release_double_field_put(int offset, jdouble contents); 239 240 address address_field_acquire(int offset) const; 241 void release_address_field_put(int offset, address contents); 242 243 // printing functions for VM debugging 244 void print_on(outputStream* st) const; // First level print 245 void print_value_on(outputStream* st) const; // Second level print. 246 void print_address_on(outputStream* st) const; // Address printing 247 248 // printing on default output stream 249 void print(); 250 void print_value(); 251 void print_address(); 252 253 // return the print strings 254 char* print_string(); 255 char* print_value_string(); 256 257 // verification operations 258 static void verify_on(outputStream* st, oopDesc* oop_desc); 259 static void verify(oopDesc* oopDesc); 260 261 // locking operations 262 inline bool is_locked() const; 263 inline bool is_unlocked() const; 264 inline bool has_bias_pattern() const; 265 266 // asserts and guarantees 267 static bool is_oop(oop obj, bool ignore_mark_word = false); 268 static bool is_oop_or_null(oop obj, bool ignore_mark_word = false); 269 270 // garbage collection 271 inline bool is_gc_marked() const; 272 273 // Forward pointer operations for scavenge 274 inline bool is_forwarded() const; 275 276 void verify_forwardee(oop forwardee) NOT_DEBUG_RETURN; 277 278 inline void forward_to(oop p); 279 inline void forward_to_self(); 280 281 // Like "forward_to", but inserts the forwarding pointer atomically. 282 // Exactly one thread succeeds in inserting the forwarding pointer, and 283 // this call returns "NULL" for that thread; any other thread has the 284 // value of the forwarding pointer returned and does not modify "this". 285 inline oop forward_to_atomic(oop p, markWord compare, atomic_memory_order order = memory_order_conservative); 286 inline oop forward_to_self_atomic(markWord compare, atomic_memory_order order = memory_order_conservative); 287 288 inline oop forwardee() const; 289 inline oop forwardee(markWord header) const; 290 291 // Age of object during scavenge 292 inline uint age() const; 293 inline void incr_age(); 294 295 template <typename OopClosureType> 296 inline void oop_iterate(OopClosureType* cl); 297 298 template <typename OopClosureType> 299 inline void oop_iterate(OopClosureType* cl, MemRegion mr); 300 301 template <typename OopClosureType> 302 inline int oop_iterate_size(OopClosureType* cl); 303 304 template <typename OopClosureType> 305 inline int oop_iterate_size(OopClosureType* cl, MemRegion mr); 306 307 template <typename OopClosureType> 308 inline void oop_iterate_backwards(OopClosureType* cl); 309 310 template <typename OopClosureType> 311 inline void oop_iterate_backwards(OopClosureType* cl, Klass* klass); 312 313 inline static bool is_instanceof_or_null(oop obj, Klass* klass); 314 315 // identity hash; returns the identity hash key (computes it if necessary) 316 // NOTE with the introduction of UseBiasedLocking that identity_hash() might reach a 317 // safepoint if called on a biased object. Calling code must be aware of that. 318 inline intptr_t identity_hash(); 319 intptr_t slow_identity_hash(); 320 321 // marks are forwarded to stack when object is locked 322 inline bool has_displaced_mark() const; 323 inline markWord displaced_mark() const; 324 inline void set_displaced_mark(markWord m); 325 326 // Checks if the mark word needs to be preserved 327 inline bool mark_must_be_preserved() const; 328 inline bool mark_must_be_preserved(markWord m) const; 329 inline bool mark_must_be_preserved_for_promotion_failure(markWord m) const; 330 331 static bool has_klass_gap(); 332 333 // for code generation 334 static int mark_offset_in_bytes() { return offset_of(oopDesc, _mark); } 335 static int klass_gap_offset_in_bytes() { 336 assert(has_klass_gap(), "only applicable to compressed klass pointers"); 337 assert(!UseCompactObjectHeaders, "don't use klass_offset_in_bytes() with compact headers"); 338 return klass_offset_in_bytes() + sizeof(narrowKlass); 339 } 340 341 static int klass_offset_in_bytes() { 342 #ifdef _LP64 343 if (UseCompactObjectHeaders) { 344 STATIC_ASSERT(markWord::klass_shift % 8 == 0); 345 return mark_offset_in_bytes() + markWord::klass_shift / 8; 346 } else 347 #endif 348 return offset_of(oopDesc, _metadata._klass); 349 } 350 351 static int base_offset_in_bytes() { 352 #ifdef _LP64 353 if (UseCompactObjectHeaders) { 354 // With compact headers, the Klass* field is not used for the Klass* 355 // and is used for the object fields instead. 356 assert(sizeof(markWord) == 8, "sanity"); 357 return sizeof(markWord); 358 } else if (UseCompressedClassPointers) { 359 return sizeof(markWord) + sizeof(narrowKlass); 360 } else 361 #endif 362 return sizeof(oopDesc); 363 } 364 365 // for error reporting 366 static void* load_klass_raw(oop obj); 367 static void* load_oop_raw(oop obj, int offset); 368 369 // Avoid include gc_globals.hpp in oop.inline.hpp 370 DEBUG_ONLY(bool get_UseParallelGC();) 371 DEBUG_ONLY(bool get_UseG1GC();) 372 }; 373 374 #endif // SHARE_OOPS_OOP_HPP