1 /*
   2  * Copyright (c) 1997, 2014, 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_OOP_HPP
  26 #define SHARE_VM_OOPS_OOP_HPP
  27 
  28 #include "memory/barrierSet.hpp"
  29 #include "memory/iterator.hpp"
  30 #include "memory/memRegion.hpp"
  31 #include "memory/specialized_oop_closures.hpp"
  32 #include "oops/metadata.hpp"
  33 #include "utilities/macros.hpp"
  34 #include "utilities/top.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 // store into oop with store check
  44 template <class T> void oop_store(T* p, oop v);
  45 template <class T> void oop_store(volatile T* p, oop v);
  46 
  47 extern bool always_do_update_barrier;
  48 
  49 // Forward declarations.
  50 class OopClosure;
  51 class ScanClosure;
  52 class FastScanClosure;
  53 class FilteringClosure;
  54 class CMSIsAliveClosure;
  55 
  56 class PSPromotionManager;
  57 class ParCompactionManager;
  58 
  59 class oopDesc {
  60   friend class VMStructs;
  61  private:
  62   volatile markOop  _mark;
  63   union _metadata {
  64     Klass*      _klass;
  65     narrowKlass _compressed_klass;
  66   } _metadata;
  67 
  68   // Fast access to barrier set.  Must be initialized.
  69   static BarrierSet* _bs;
  70 
  71  public:
  72   markOop  mark()      const {
  73     oop p = bs()->read_barrier((oop) this);
  74     return p->_mark;
  75   }
  76   markOop* mark_addr() const    { return (markOop*) &_mark; }
  77 
  78   void set_mark(volatile markOop m) {
  79     oop p = bs()->write_barrier(this);
  80     p->_mark = m;
  81   }
  82 
  83   void set_mark_raw(volatile markOop m) {
  84     _mark = m;
  85   }
  86 
  87   void    release_set_mark(markOop m);
  88   markOop cas_set_mark(markOop new_mark, markOop old_mark);
  89 
  90   // Used only to re-initialize the mark word (e.g., of promoted
  91   // objects during a GC) -- requires a valid klass pointer
  92   void init_mark();
  93 
  94   Klass* klass() const;
  95   Klass* klass_or_null() const volatile;
  96   Klass** klass_addr();
  97   narrowKlass* compressed_klass_addr();
  98 
  99   void set_klass(Klass* k);
 100 
 101   // For klass field compression
 102   int klass_gap() const;
 103   void set_klass_gap(int z);
 104   // For when the klass pointer is being used as a linked list "next" field.
 105   void set_klass_to_list_ptr(oop k);
 106   oop list_ptr_from_klass();
 107 
 108   // size of object header, aligned to platform wordSize
 109   static int header_size()          { return sizeof(oopDesc)/HeapWordSize; }
 110 
 111   // Returns whether this is an instance of k or an instance of a subclass of k
 112   bool is_a(Klass* k)  const;
 113 
 114   // Returns the actual oop size of the object
 115   int size();
 116 
 117   // Sometimes (for complicated concurrency-related reasons), it is useful
 118   // to be able to figure out the size of an object knowing its klass.
 119   int size_given_klass(Klass* klass);
 120 
 121   // type test operations (inlined in oop.inline.h)
 122   bool is_instance()            const;
 123   bool is_instanceMirror()      const;
 124   bool is_instanceClassLoader() const;
 125   bool is_instanceRef()         const;
 126   bool is_array()               const;
 127   bool is_objArray()            const;
 128   bool is_typeArray()           const;
 129 
 130  private:
 131   // field addresses in oop
 132   void*     field_base(int offset)        const;
 133 
 134   jbyte*    byte_field_addr(int offset)   const;
 135   jchar*    char_field_addr(int offset)   const;
 136   jboolean* bool_field_addr(int offset)   const;
 137   jint*     int_field_addr(int offset)    const;
 138   jshort*   short_field_addr(int offset)  const;
 139   jlong*    long_field_addr(int offset)   const;
 140   jfloat*   float_field_addr(int offset)  const;
 141   jdouble*  double_field_addr(int offset) const;
 142   Metadata** metadata_field_addr(int offset) const;
 143 
 144  public:
 145   // Need this as public for garbage collection.
 146   template <class T> T* obj_field_addr(int offset) const;
 147 
 148   // Needed for javaClasses
 149   address*  address_field_addr(int offset) const;
 150 
 151   static bool is_null(oop obj);
 152   static bool is_null(narrowOop obj);
 153   static bool is_null(Klass* obj);
 154 
 155   inline static bool equals(oop o1, oop o2) {
 156     return bs()->obj_equals(o1, o2);
 157   }
 158 
 159   inline static bool equals(narrowOop o1, narrowOop o2) {
 160     return bs()->obj_equals(o1, o2);
 161   }
 162 
 163   inline static bool unsafe_equals(oop o1, oop o2) {
 164 #ifdef CHECK_UNHANDLED_OOPS
 165     return o1.obj() == o2.obj();
 166 #else
 167     return o1 == o2;
 168 #endif
 169   }
 170 
 171   inline static bool unsafe_equals(narrowOop o1, narrowOop o2) {
 172     return o1 == o2;
 173   }
 174 
 175   // Decode an oop pointer from a narrowOop if compressed.
 176   // These are overloaded for oop and narrowOop as are the other functions
 177   // below so that they can be called in template functions.
 178   static oop decode_heap_oop_not_null(oop v);
 179   static oop decode_heap_oop_not_null(narrowOop v);
 180   static oop decode_heap_oop(oop v);
 181   static oop decode_heap_oop(narrowOop v);
 182 
 183   // Encode an oop pointer to a narrow oop.  The or_null versions accept
 184   // null oop pointer, others do not in order to eliminate the
 185   // null checking branches.
 186   static narrowOop encode_heap_oop_not_null(oop v);
 187   static narrowOop encode_heap_oop(oop v);
 188 
 189   // Load an oop out of the Java heap
 190   static narrowOop load_heap_oop(narrowOop* p);
 191   static oop       load_heap_oop(oop* p);
 192 
 193   // Load an oop out of Java heap and decode it to an uncompressed oop.
 194   static oop load_decode_heap_oop_not_null(narrowOop* p);
 195   static oop load_decode_heap_oop_not_null(oop* p);
 196   static oop load_decode_heap_oop(narrowOop* p);
 197   static oop load_decode_heap_oop(oop* p);
 198 
 199   // Store an oop into the heap.
 200   static void store_heap_oop(narrowOop* p, narrowOop v);
 201   static void store_heap_oop(oop* p, oop v);
 202 
 203   // Encode oop if UseCompressedOops and store into the heap.
 204   static void encode_store_heap_oop_not_null(narrowOop* p, oop v);
 205   static void encode_store_heap_oop_not_null(oop* p, oop v);
 206   static void encode_store_heap_oop(narrowOop* p, oop v);
 207   static void encode_store_heap_oop(oop* p, oop v);
 208 
 209   static void release_store_heap_oop(volatile narrowOop* p, narrowOop v);
 210   static void release_store_heap_oop(volatile oop* p, oop v);
 211 
 212   static void release_encode_store_heap_oop_not_null(volatile narrowOop* p, oop v);
 213   static void release_encode_store_heap_oop_not_null(volatile oop* p, oop v);
 214   static void release_encode_store_heap_oop(volatile narrowOop* p, oop v);
 215   static void release_encode_store_heap_oop(volatile oop* p, oop v);
 216 
 217   static oop atomic_exchange_oop(oop exchange_value, volatile HeapWord *dest);
 218   static oop atomic_compare_exchange_oop(oop exchange_value,
 219                                          volatile HeapWord *dest,
 220                                          oop compare_value,
 221                                          bool prebarrier = false);
 222 
 223   // Access to fields in a instanceOop through these methods.
 224   oop obj_field(int offset) const;
 225   volatile oop obj_field_volatile(int offset) const;
 226   void obj_field_put(int offset, oop value);
 227   void obj_field_put_raw(int offset, oop value);
 228   void obj_field_put_volatile(int offset, oop value);
 229 
 230   Metadata* metadata_field(int offset) const;
 231   void metadata_field_put(int offset, Metadata* value);
 232 
 233   jbyte byte_field(int offset) const;
 234   void byte_field_put(int offset, jbyte contents);
 235 
 236   jchar char_field(int offset) const;
 237   void char_field_put(int offset, jchar contents);
 238 
 239   jboolean bool_field(int offset) const;
 240   void bool_field_put(int offset, jboolean contents);
 241 
 242   jint int_field(int offset) const;
 243   void int_field_put(int offset, jint contents);
 244   void int_field_put_raw(int offset, jint contents);
 245 
 246   jshort short_field(int offset) const;
 247   void short_field_put(int offset, jshort contents);
 248 
 249   jlong long_field(int offset) const;
 250   void long_field_put(int offset, jlong contents);
 251 
 252   jfloat float_field(int offset) const;
 253   void float_field_put(int offset, jfloat contents);
 254 
 255   jdouble double_field(int offset) const;
 256   void double_field_put(int offset, jdouble contents);
 257 
 258   address address_field(int offset) const;
 259   void address_field_put(int offset, address contents);
 260 
 261   oop obj_field_acquire(int offset) const;
 262   void release_obj_field_put(int offset, oop value);
 263 
 264   jbyte byte_field_acquire(int offset) const;
 265   void release_byte_field_put(int offset, jbyte contents);
 266 
 267   jchar char_field_acquire(int offset) const;
 268   void release_char_field_put(int offset, jchar contents);
 269 
 270   jboolean bool_field_acquire(int offset) const;
 271   void release_bool_field_put(int offset, jboolean contents);
 272 
 273   jint int_field_acquire(int offset) const;
 274   void release_int_field_put(int offset, jint contents);
 275 
 276   jshort short_field_acquire(int offset) const;
 277   void release_short_field_put(int offset, jshort contents);
 278 
 279   jlong long_field_acquire(int offset) const;
 280   void release_long_field_put(int offset, jlong contents);
 281 
 282   jfloat float_field_acquire(int offset) const;
 283   void release_float_field_put(int offset, jfloat contents);
 284 
 285   jdouble double_field_acquire(int offset) const;
 286   void release_double_field_put(int offset, jdouble contents);
 287 
 288   address address_field_acquire(int offset) const;
 289   void release_address_field_put(int offset, address contents);
 290 
 291   // printing functions for VM debugging
 292   void print_on(outputStream* st) const;         // First level print
 293   void print_value_on(outputStream* st) const;   // Second level print.
 294   void print_address_on(outputStream* st) const; // Address printing
 295 
 296   // printing on default output stream
 297   void print();
 298   void print_value();
 299   void print_address();
 300 
 301   // return the print strings
 302   char* print_string();
 303   char* print_value_string();
 304 
 305   // verification operations
 306   void verify_on(outputStream* st);
 307   void verify();
 308 
 309   // locking operations
 310   bool is_locked()   const;
 311   bool is_unlocked() const;
 312   bool has_bias_pattern() const;
 313 
 314   // asserts
 315   bool is_oop(bool ignore_mark_word = false) const;
 316   bool is_oop_or_null(bool ignore_mark_word = false) const;
 317 #ifndef PRODUCT
 318   bool is_unlocked_oop() const;
 319 #endif
 320 
 321   // garbage collection
 322   bool is_gc_marked() const;
 323   // Apply "MarkSweep::mark_and_push" to (the address of) every non-NULL
 324   // reference field in "this".
 325   void follow_contents(void);
 326 
 327 #if INCLUDE_ALL_GCS
 328   // Parallel Scavenge
 329   void push_contents(PSPromotionManager* pm);
 330 
 331   // Parallel Old
 332   void update_contents(ParCompactionManager* cm);
 333 
 334   void follow_contents(ParCompactionManager* cm);
 335 #endif // INCLUDE_ALL_GCS
 336 
 337   bool is_scavengable() const;
 338 
 339   // Forward pointer operations for scavenge
 340   bool is_forwarded() const;
 341 
 342   void forward_to(oop p);
 343   bool cas_forward_to(oop p, markOop compare);
 344 
 345 #if INCLUDE_ALL_GCS
 346   // Like "forward_to", but inserts the forwarding pointer atomically.
 347   // Exactly one thread succeeds in inserting the forwarding pointer, and
 348   // this call returns "NULL" for that thread; any other thread has the
 349   // value of the forwarding pointer returned and does not modify "this".
 350   oop forward_to_atomic(oop p);
 351 #endif // INCLUDE_ALL_GCS
 352 
 353   oop forwardee() const;
 354 
 355   // Age of object during scavenge
 356   uint age() const;
 357   void incr_age();
 358 
 359   // Adjust all pointers in this object to point at it's forwarded location and
 360   // return the size of this oop.  This is used by the MarkSweep collector.
 361   int adjust_pointers();
 362 
 363   // mark-sweep support
 364   void follow_body(int begin, int end);
 365 
 366   // Fast access to barrier set
 367   static BarrierSet* bs()            { return _bs; }
 368   static void set_bs(BarrierSet* bs) { _bs = bs; }
 369 
 370   // iterators, returns size of object
 371 #define OOP_ITERATE_DECL(OopClosureType, nv_suffix)                      \
 372   int oop_iterate(OopClosureType* blk);                                  \
 373   int oop_iterate(OopClosureType* blk, MemRegion mr);  // Only in mr.
 374 
 375   ALL_OOP_OOP_ITERATE_CLOSURES_1(OOP_ITERATE_DECL)
 376   ALL_OOP_OOP_ITERATE_CLOSURES_2(OOP_ITERATE_DECL)
 377 
 378 #if INCLUDE_ALL_GCS
 379 
 380 #define OOP_ITERATE_BACKWARDS_DECL(OopClosureType, nv_suffix)            \
 381   int oop_iterate_backwards(OopClosureType* blk);
 382 
 383   ALL_OOP_OOP_ITERATE_CLOSURES_1(OOP_ITERATE_BACKWARDS_DECL)
 384   ALL_OOP_OOP_ITERATE_CLOSURES_2(OOP_ITERATE_BACKWARDS_DECL)
 385 #endif
 386 
 387   int oop_iterate_no_header(OopClosure* bk);
 388   int oop_iterate_no_header(OopClosure* bk, MemRegion mr);
 389 
 390   // identity hash; returns the identity hash key (computes it if necessary)
 391   // NOTE with the introduction of UseBiasedLocking that identity_hash() might reach a
 392   // safepoint if called on a biased object. Calling code must be aware of that.
 393   intptr_t identity_hash();
 394   intptr_t slow_identity_hash();
 395 
 396   // Alternate hashing code if string table is rehashed
 397   unsigned int new_hash(juint seed);
 398 
 399   // marks are forwarded to stack when object is locked
 400   bool     has_displaced_mark() const;
 401   markOop  displaced_mark() const;
 402   void     set_displaced_mark(markOop m);
 403 
 404   // for code generation
 405   static int mark_offset_in_bytes()    { return offset_of(oopDesc, _mark); }
 406   static int klass_offset_in_bytes()   { return offset_of(oopDesc, _metadata._klass); }
 407   static int klass_gap_offset_in_bytes();
 408 };
 409 
 410 #endif // SHARE_VM_OOPS_OOP_HPP