1 /*
   2  * Copyright (c) 1997, 2013, 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_OOPSHIERARCHY_HPP
  26 #define SHARE_VM_OOPS_OOPSHIERARCHY_HPP
  27 
  28 #include "runtime/globals.hpp"
  29 #include "utilities/globalDefinitions.hpp"
  30 #if INCLUDE_ALL_GCS
  31 #include "gc_implementation/shenandoah/shenandoah_globals.hpp"
  32 #endif
  33 
  34 // OBJECT hierarchy
  35 // This hierarchy is a representation hierarchy, i.e. if A is a superclass
  36 // of B, A's representation is a prefix of B's representation.
  37 
  38 typedef juint narrowOop; // Offset instead of address for an oop within a java object
  39 
  40 // If compressed klass pointers then use narrowKlass.
  41 typedef juint  narrowKlass;
  42 
  43 typedef void* OopOrNarrowOopStar;
  44 typedef class   markOopDesc*                markOop;
  45 
  46 #ifndef CHECK_UNHANDLED_OOPS
  47 
  48 typedef class oopDesc*                            oop;
  49 typedef class   instanceOopDesc*            instanceOop;
  50 typedef class   arrayOopDesc*                    arrayOop;
  51 typedef class     objArrayOopDesc*            objArrayOop;
  52 typedef class     typeArrayOopDesc*            typeArrayOop;
  53 
  54 #else
  55 
  56 // When CHECK_UNHANDLED_OOPS is defined, an "oop" is a class with a
  57 // carefully chosen set of constructors and conversion operators to go
  58 // to and from the underlying oopDesc pointer type.
  59 //
  60 // Because oop and its subclasses <type>Oop are class types, arbitrary
  61 // conversions are not accepted by the compiler.  Applying a cast to
  62 // an oop will cause the best matched conversion operator to be
  63 // invoked returning the underlying oopDesc* type if appropriate.
  64 // No copy constructors, explicit user conversions or operators of
  65 // numerical type should be defined within the oop class. Most C++
  66 // compilers will issue a compile time error concerning the overloading
  67 // ambiguity between operators of numerical and pointer types. If
  68 // a conversion to or from an oop to a numerical type is needed,
  69 // use the inline template methods, cast_*_oop, defined below.
  70 //
  71 // Converting NULL to oop to Handle implicit is no longer accepted by the
  72 // compiler because there are too many steps in the conversion.  Use Handle()
  73 // instead, which generates less code anyway.
  74 
  75 class Thread;
  76 class PromotedObject;
  77 
  78 
  79 class oop {
  80   oopDesc* _o;
  81 
  82   void register_oop();
  83   void unregister_oop();
  84 
  85   // friend class markOop;
  86 public:
  87   void set_obj(const void* p)         {
  88     raw_set_obj(p);
  89     if (CheckUnhandledOops) register_oop();
  90   }
  91   void raw_set_obj(const void* p)     { _o = (oopDesc*)p; }
  92 
  93   oop()                               { set_obj(NULL); }
  94   oop(const oop& o)                   { set_obj(o.obj()); }
  95   oop(const volatile oop& o)          { set_obj(o.obj()); }
  96   oop(const void* p)                  { set_obj(p); }
  97   ~oop()                              {
  98     if (CheckUnhandledOops) unregister_oop();
  99   }
 100 
 101   oopDesc* obj()  const volatile      { return _o; }
 102 
 103   // General access
 104   oopDesc*  operator->() const        { return obj(); }
 105   bool operator==(const oop o) const  {
 106 #if INCLUDE_ALL_GCS
 107     if (VerifyStrictOopOperations) {
 108       ShouldNotReachHere();
 109     }
 110 #endif
 111     return obj() == o.obj();
 112   }
 113   bool operator==(void *p) const      {
 114 #if INCLUDE_ALL_GCS
 115     if (p != NULL && VerifyStrictOopOperations) {
 116       ShouldNotReachHere();
 117     }
 118 #endif
 119     return obj() == p;
 120   }
 121   bool operator!=(const volatile oop o) const  {
 122 #if INCLUDE_ALL_GCS
 123     if (VerifyStrictOopOperations) {
 124       ShouldNotReachHere();
 125     }
 126 #endif
 127     return obj() != o.obj();
 128   }
 129   bool operator!=(void *p) const      {
 130 #if INCLUDE_ALL_GCS
 131     if (p != NULL && VerifyStrictOopOperations) {
 132       ShouldNotReachHere();
 133     }
 134 #endif
 135     return obj() != p;
 136   }
 137 
 138   bool operator<(oop o) const         { return obj() < o.obj(); }
 139   bool operator>(oop o) const         { return obj() > o.obj(); }
 140   bool operator<=(oop o) const        { return obj() <= o.obj(); }
 141   bool operator>=(oop o) const        { return obj() >= o.obj(); }
 142   bool operator!() const              { return !obj(); }
 143 
 144   // Assignment
 145   oop& operator=(const oop& o)                            { _o = o.obj(); return *this; }
 146 #ifndef SOLARIS
 147   volatile oop& operator=(const oop& o) volatile          { _o = o.obj(); return *this; }
 148 #endif
 149   volatile oop& operator=(const volatile oop& o) volatile { _o = o.obj(); return *this; }
 150 
 151   // Explict user conversions
 152   operator void* () const             { return (void *)obj(); }
 153 #ifndef SOLARIS
 154   operator void* () const volatile    { return (void *)obj(); }
 155 #endif
 156   operator HeapWord* () const         { return (HeapWord*)obj(); }
 157   operator oopDesc* () const          { return obj(); }
 158   operator intptr_t* () const         { return (intptr_t*)obj(); }
 159   operator PromotedObject* () const   { return (PromotedObject*)obj(); }
 160   operator markOop () const           { return markOop(obj()); }
 161 
 162   operator address   () const         { return (address)obj(); }
 163 
 164   // from javaCalls.cpp
 165   operator jobject () const           { return (jobject)obj(); }
 166   // from javaClasses.cpp
 167   operator JavaThread* () const       { return (JavaThread*)obj(); }
 168 
 169 #ifndef _LP64
 170   // from jvm.cpp
 171   operator jlong* () const            { return (jlong*)obj(); }
 172 #endif
 173 
 174   // from parNewGeneration and other things that want to get to the end of
 175   // an oop for stuff (like ObjArrayKlass.cpp)
 176   operator oop* () const              { return (oop *)obj(); }
 177 };
 178 
 179 #define DEF_OOP(type)                                                      \
 180    class type##OopDesc;                                                    \
 181    class type##Oop : public oop {                                          \
 182      public:                                                               \
 183        type##Oop() : oop() {}                                              \
 184        type##Oop(const oop& o) : oop(o) {}                                 \
 185        type##Oop(const volatile oop& o) : oop(o) {}                        \
 186        type##Oop(const void* p) : oop(p) {}                                \
 187        operator type##OopDesc* () const { return (type##OopDesc*)obj(); }  \
 188        type##OopDesc* operator->() const {                                 \
 189             return (type##OopDesc*)obj();                                  \
 190        }                                                                   \
 191        type##Oop& operator=(const type##Oop& o) {                          \
 192             oop::operator=(o);                                             \
 193             return *this;                                                  \
 194        }                                                                   \
 195        NOT_SOLARIS(                                                        \
 196        volatile type##Oop& operator=(const type##Oop& o) volatile {        \
 197             (void)const_cast<oop&>(oop::operator=(o));                     \
 198             return *this;                                                  \
 199        })                                                                  \
 200        volatile type##Oop& operator=(const volatile type##Oop& o) volatile {\
 201             (void)const_cast<oop&>(oop::operator=(o));                     \
 202             return *this;                                                  \
 203        }                                                                   \
 204    };
 205 
 206 DEF_OOP(instance);
 207 DEF_OOP(array);
 208 DEF_OOP(objArray);
 209 DEF_OOP(typeArray);
 210 
 211 #endif // CHECK_UNHANDLED_OOPS
 212 
 213 // For CHECK_UNHANDLED_OOPS, it is ambiguous C++ behavior to have the oop
 214 // structure contain explicit user defined conversions of both numerical
 215 // and pointer type. Define inline methods to provide the numerical conversions.
 216 template <class T> inline oop cast_to_oop(T value) {
 217   return (oop)(CHECK_UNHANDLED_OOPS_ONLY((void *))(value));
 218 }
 219 template <class T> inline T cast_from_oop(oop o) {
 220   return (T)(CHECK_UNHANDLED_OOPS_ONLY((void*))o);
 221 }
 222 
 223 // The metadata hierarchy is separate from the oop hierarchy
 224 
 225 //      class MetaspaceObj
 226 class   ConstMethod;
 227 class   ConstantPoolCache;
 228 class   MethodData;
 229 //      class Metadata
 230 class   Method;
 231 class   ConstantPool;
 232 //      class CHeapObj
 233 class   CompiledICHolder;
 234 
 235 
 236 // The klass hierarchy is separate from the oop hierarchy.
 237 
 238 class Klass;
 239 class   InstanceKlass;
 240 class     InstanceMirrorKlass;
 241 class     InstanceClassLoaderKlass;
 242 class     InstanceRefKlass;
 243 class   ArrayKlass;
 244 class     ObjArrayKlass;
 245 class     TypeArrayKlass;
 246 
 247 #endif // SHARE_VM_OOPS_OOPSHIERARCHY_HPP