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