1 /* 2 * Copyright (c) 1997, 2019, 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/access.hpp" 31 #include "oops/arrayStorageProperties.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 // oopDesc::_mark - the "oop mark word" encoding to be found separately in markOop.hpp 44 // 45 // oopDesc::_metadata - encodes both the object's klass pointer and potentially 46 // "storage properties" (currently confined to arrays in the form of 47 // ArrayStorageProperties). Storage properties are peculiar to the 48 // *instance*, and not necessarily the "type". 49 // 50 // The overall size of the _metadata field is dependent on "UseCompressedClassPointers", 51 // hence the terms "narrow" (32 bits) vs "wide" (64 bits). 52 // 53 // "Wide" encoding of _metadata: 54 // bit number |63 0| 55 // bit length |--3|-----61-----| 56 // -------------------------------------- 57 // _klass [xxx| Klass* ] 58 // _wide_storage_props [ sp| ] 59 // -------------------------------------- 60 // with: 61 // xxx = klass_mask(), Klass* = Klass pointer to be masked 62 // sp = storage properties, bit number: wide_storage_props_shift 63 // 64 // "Narrow" encoding of _metadata: 65 // bit number |31 0| 66 // bit length |--3|-----29-----| 67 // ---------------------------------------- 68 // _compressed_klass [xxx| narrowKlass] 69 // _narrow_storage_props [ sp| ] 70 // ---------------------------------------- 71 // with: 72 // xxx = compressed_klass_mask(), narrowKlass = compressed Klass pointer to be masked 73 // narrowKlass may be further decoded (Klass::decode_klass()) to produce Klass* 74 // sp = storage properties, bit number: narrow_storage_props_shift 75 // 76 // Storage properties encodings are current confined to arrayStorageProperties.hpp 77 78 79 extern bool always_do_update_barrier; 80 81 // Forward declarations. 82 class OopClosure; 83 class ScanClosure; 84 class FastScanClosure; 85 class FilteringClosure; 86 class CMSIsAliveClosure; 87 88 class PSPromotionManager; 89 class ParCompactionManager; 90 91 class oopDesc { 92 friend class VMStructs; 93 friend class JVMCIVMStructs; 94 private: 95 volatile markOop _mark; 96 union _metadata { 97 Klass* _klass; 98 narrowKlass _compressed_klass; 99 uintptr_t _wide_storage_props; 100 uint32_t _narrow_storage_props; 101 } _metadata; 102 103 public: 104 inline markOop mark() const; 105 inline markOop mark_raw() const; 106 inline markOop* mark_addr_raw() const; 107 108 inline void set_mark(volatile markOop m); 109 inline void set_mark_raw(volatile markOop m); 110 static inline void set_mark_raw(HeapWord* mem, markOop m); 111 112 inline void release_set_mark(markOop m); 113 inline markOop cas_set_mark(markOop new_mark, markOop old_mark); 114 inline markOop cas_set_mark_raw(markOop new_mark, markOop old_mark, atomic_memory_order order = memory_order_conservative); 115 116 // Used only to re-initialize the mark word (e.g., of promoted 117 // objects during a GC) -- requires a valid klass pointer 118 inline void init_mark(); 119 inline void init_mark_raw(); 120 121 enum { 122 storage_props_nof_bits = LogKlassAlignmentInBytes, // This alignment gives us some "free bits" 123 narrow_storage_props_shift = (sizeof(narrowKlass) << 3) - storage_props_nof_bits, 124 wide_storage_props_shift = (sizeof(Klass*) << 3) - storage_props_nof_bits, 125 }; 126 127 static inline narrowKlass compressed_klass_mask(); 128 static inline narrowKlass compressed_klass_masked(narrowKlass raw); 129 static inline uintptr_t klass_mask(); 130 static inline Klass* klass_masked(uintptr_t raw); 131 132 inline Klass* klass() const; 133 inline Klass* klass_or_null() const volatile; 134 inline Klass* klass_or_null_acquire() const volatile; 135 static inline Klass** klass_addr(HeapWord* mem); 136 static inline narrowKlass* compressed_klass_addr(HeapWord* mem); 137 inline Klass** klass_addr(); 138 inline narrowKlass* compressed_klass_addr(); 139 140 inline void set_klass(Klass* k); 141 static inline void release_set_klass(HeapWord* mem, Klass* klass); 142 143 // Extra container metadata specific to arrays (encoded into high bits of _metadata) 144 static inline uintptr_t* wide_metadata_addr(HeapWord* mem); 145 inline ArrayStorageProperties array_storage_properties() const; 146 inline void set_metadata(ArrayStorageProperties storage_props, Klass* k); 147 static inline void release_set_metadata(HeapWord* mem, ArrayStorageProperties storage_props, Klass* klass); 148 149 150 // For klass field compression 151 inline int klass_gap() const; 152 inline void set_klass_gap(int z); 153 static inline void set_klass_gap(HeapWord* mem, int z); 154 // For when the klass pointer is being used as a linked list "next" field. 155 inline void set_klass_to_list_ptr(oop k); 156 inline oop list_ptr_from_klass(); 157 158 // size of object header, aligned to platform wordSize 159 static int header_size() { return sizeof(oopDesc)/HeapWordSize; } 160 161 // Returns whether this is an instance of k or an instance of a subclass of k 162 inline bool is_a(Klass* k) const; 163 164 // Returns the actual oop size of the object 165 inline int size(); 166 167 // Sometimes (for complicated concurrency-related reasons), it is useful 168 // to be able to figure out the size of an object knowing its klass. 169 inline int size_given_klass(Klass* klass); 170 171 // type test operations (inlined in oop.inline.hpp) 172 inline bool is_instance() const; 173 inline bool is_array() const; 174 inline bool is_objArray() const; 175 inline bool is_typeArray() const; 176 inline bool is_value() const; 177 inline bool is_valueArray() const; 178 179 // type test operations that don't require inclusion of oop.inline.hpp. 180 bool is_instance_noinline() const; 181 bool is_array_noinline() const; 182 bool is_objArray_noinline() const; 183 bool is_typeArray_noinline() const; 184 bool is_value_noinline() const; 185 bool is_valueArray_noinline() const; 186 187 protected: 188 inline oop as_oop() const { return const_cast<oopDesc*>(this); } 189 190 public: 191 // field addresses in oop 192 inline void* field_addr(int offset) const; 193 inline void* field_addr_raw(int offset) const; 194 195 // Need this as public for garbage collection. 196 template <class T> inline T* obj_field_addr_raw(int offset) const; 197 198 template <typename T> inline size_t field_offset(T* p) const; 199 200 // Standard compare function returns negative value if o1 < o2 201 // 0 if o1 == o2 202 // positive value if o1 > o2 203 inline static int compare(oop o1, oop o2) { 204 void* o1_addr = (void*)o1; 205 void* o2_addr = (void*)o2; 206 if (o1_addr < o2_addr) { 207 return -1; 208 } else if (o1_addr > o2_addr) { 209 return 1; 210 } else { 211 return 0; 212 } 213 } 214 215 inline static bool equals(oop o1, oop o2) { return Access<>::equals(o1, o2); } 216 217 inline static bool equals_raw(oop o1, oop o2) { return RawAccess<>::equals(o1, o2); } 218 219 // Access to fields in a instanceOop through these methods. 220 template <DecoratorSet decorator> 221 oop obj_field_access(int offset) const; 222 oop obj_field(int offset) const; 223 void obj_field_put(int offset, oop value); 224 void obj_field_put_raw(int offset, oop value); 225 void obj_field_put_volatile(int offset, oop value); 226 227 Metadata* metadata_field(int offset) const; 228 Metadata* metadata_field_raw(int offset) const; 229 void metadata_field_put(int offset, Metadata* value); 230 231 Metadata* metadata_field_acquire(int offset) const; 232 void release_metadata_field_put(int offset, Metadata* value); 233 234 jbyte byte_field(int offset) const; 235 void byte_field_put(int offset, jbyte contents); 236 237 jchar char_field(int offset) const; 238 void char_field_put(int offset, jchar contents); 239 240 jboolean bool_field(int offset) const; 241 void bool_field_put(int offset, jboolean contents); 242 243 jint int_field(int offset) const; 244 jint int_field_raw(int offset) const; 245 void int_field_put(int offset, jint contents); 246 247 jshort short_field(int offset) const; 248 void short_field_put(int offset, jshort contents); 249 250 jlong long_field(int offset) const; 251 void long_field_put(int offset, jlong contents); 252 253 jfloat float_field(int offset) const; 254 void float_field_put(int offset, jfloat contents); 255 256 jdouble double_field(int offset) const; 257 void double_field_put(int offset, jdouble contents); 258 259 address address_field(int offset) const; 260 void address_field_put(int offset, address contents); 261 262 oop obj_field_acquire(int offset) const; 263 void release_obj_field_put(int offset, oop value); 264 265 jbyte byte_field_acquire(int offset) const; 266 void release_byte_field_put(int offset, jbyte contents); 267 268 jchar char_field_acquire(int offset) const; 269 void release_char_field_put(int offset, jchar contents); 270 271 jboolean bool_field_acquire(int offset) const; 272 void release_bool_field_put(int offset, jboolean contents); 273 274 jint int_field_acquire(int offset) const; 275 void release_int_field_put(int offset, jint contents); 276 277 jshort short_field_acquire(int offset) const; 278 void release_short_field_put(int offset, jshort contents); 279 280 jlong long_field_acquire(int offset) const; 281 void release_long_field_put(int offset, jlong contents); 282 283 jfloat float_field_acquire(int offset) const; 284 void release_float_field_put(int offset, jfloat contents); 285 286 jdouble double_field_acquire(int offset) const; 287 void release_double_field_put(int offset, jdouble contents); 288 289 address address_field_acquire(int offset) const; 290 void release_address_field_put(int offset, address contents); 291 292 // printing functions for VM debugging 293 void print_on(outputStream* st) const; // First level print 294 void print_value_on(outputStream* st) const; // Second level print. 295 void print_address_on(outputStream* st) const; // Address printing 296 297 // printing on default output stream 298 void print(); 299 void print_value(); 300 void print_address(); 301 302 // return the print strings 303 char* print_string(); 304 char* print_value_string(); 305 306 // verification operations 307 static void verify_on(outputStream* st, oopDesc* oop_desc); 308 static void verify(oopDesc* oopDesc); 309 310 // locking operations 311 inline bool is_locked() const; 312 inline bool is_unlocked() const; 313 inline bool has_bias_pattern() const; 314 inline bool has_bias_pattern_raw() const; 315 316 // asserts and guarantees 317 static bool is_oop(oop obj, bool ignore_mark_word = false); 318 static bool is_oop_or_null(oop obj, bool ignore_mark_word = false); 319 #ifndef PRODUCT 320 inline bool is_unlocked_oop() const; 321 static bool is_archived_object(oop p) NOT_CDS_JAVA_HEAP_RETURN_(false); 322 #endif 323 324 // garbage collection 325 inline bool is_gc_marked() const; 326 327 // Forward pointer operations for scavenge 328 inline bool is_forwarded() const; 329 330 inline void forward_to(oop p); 331 inline bool cas_forward_to(oop p, markOop compare, atomic_memory_order order = memory_order_conservative); 332 333 // Like "forward_to", but inserts the forwarding pointer atomically. 334 // Exactly one thread succeeds in inserting the forwarding pointer, and 335 // this call returns "NULL" for that thread; any other thread has the 336 // value of the forwarding pointer returned and does not modify "this". 337 inline oop forward_to_atomic(oop p, markOop compare, atomic_memory_order order = memory_order_conservative); 338 339 inline oop forwardee() const; 340 inline oop forwardee_acquire() const; 341 342 // Age of object during scavenge 343 inline uint age() const; 344 inline void incr_age(); 345 346 // mark-sweep support 347 void follow_body(int begin, int end); 348 349 template <typename OopClosureType> 350 inline void oop_iterate(OopClosureType* cl); 351 352 template <typename OopClosureType> 353 inline void oop_iterate(OopClosureType* cl, MemRegion mr); 354 355 template <typename OopClosureType> 356 inline int oop_iterate_size(OopClosureType* cl); 357 358 template <typename OopClosureType> 359 inline int oop_iterate_size(OopClosureType* cl, MemRegion mr); 360 361 template <typename OopClosureType> 362 inline void oop_iterate_backwards(OopClosureType* cl); 363 364 inline static bool is_instanceof_or_null(oop obj, Klass* klass); 365 366 // identity hash; returns the identity hash key (computes it if necessary) 367 // NOTE with the introduction of UseBiasedLocking that identity_hash() might reach a 368 // safepoint if called on a biased object. Calling code must be aware of that. 369 inline intptr_t identity_hash(); 370 intptr_t slow_identity_hash(); 371 372 // marks are forwarded to stack when object is locked 373 inline bool has_displaced_mark_raw() const; 374 inline markOop displaced_mark_raw() const; 375 inline void set_displaced_mark_raw(markOop m); 376 377 static bool has_klass_gap(); 378 379 // for code generation 380 static int mark_offset_in_bytes() { return offset_of(oopDesc, _mark); } 381 static int klass_offset_in_bytes() { return offset_of(oopDesc, _metadata._klass); } 382 static int klass_gap_offset_in_bytes() { 383 assert(has_klass_gap(), "only applicable to compressed klass pointers"); 384 return klass_offset_in_bytes() + sizeof(narrowKlass); 385 } 386 387 // for error reporting 388 static oop decode_oop_raw(narrowOop narrow_oop); 389 static void* load_klass_raw(oop obj); 390 static void* load_oop_raw(oop obj, int offset); 391 static bool is_valid(oop obj); 392 static oop oop_or_null(address addr); 393 }; 394 395 #endif // SHARE_OOPS_OOP_HPP