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     assert(!VerifyObjectEquals, "Missing oopDesc::equals(..)");
 106     return obj() == o.obj();
 107   }
 108   bool operator==(void *p) const      { return obj() == p; }
 109   bool operator!=(const volatile oop o) const  {
 110     assert(!VerifyObjectEquals, "Missing !oopDesc::equals(..)");
 111     return obj() != o.obj();
 112   }
 113   bool operator!=(void *p) const      { return obj() != p; }
 114 
 115   // Assignment
 116   oop& operator=(const oop& o)                            { _o = o.obj(); return *this; }
 117   volatile oop& operator=(const oop& o) volatile          { _o = o.obj(); return *this; }
 118   volatile oop& operator=(const volatile oop& o) volatile { _o = o.obj(); return *this; }
 119 
 120   // Explict user conversions
 121   operator void* () const             { return (void *)obj(); }
 122 #ifndef SOLARIS
 123   operator void* () const volatile    { return (void *)obj(); }
 124 #endif
 125   operator HeapWord* () const         { return (HeapWord*)obj(); }
 126   operator oopDesc* () const volatile { return obj(); }
 127   operator intptr_t* () const         { return (intptr_t*)obj(); }
 128   operator PromotedObject* () const   { return (PromotedObject*)obj(); }
 129   operator markOop () const volatile  { return markOop(obj()); }
 130   operator address   () const         { return (address)obj(); }
 131 
 132   // from javaCalls.cpp
 133   operator jobject () const           { return (jobject)obj(); }
 134 
 135   // from parNewGeneration and other things that want to get to the end of
 136   // an oop for stuff (like ObjArrayKlass.cpp)
 137   operator oop* () const              { return (oop *)obj(); }
 138 };
 139 
 140 template<>
 141 struct PrimitiveConversions::Translate<oop> : public TrueType {
 142   typedef oop Value;
 143   typedef oopDesc* Decayed;
 144 
 145   static Decayed decay(Value x) { return x.obj(); }
 146   static Value recover(Decayed x) { return oop(x); }
 147 };
 148 
 149 #define DEF_OOP(type)                                                      \
 150    class type##OopDesc;                                                    \
 151    class type##Oop : public oop {                                          \
 152      public:                                                               \
 153        type##Oop() : oop() {}                                              \
 154        type##Oop(const oop& o) : oop(o) {}                                 \
 155        type##Oop(const volatile oop& o) : oop(o) {}                        \
 156        type##Oop(const void* p) : oop(p) {}                                \
 157        operator type##OopDesc* () const { return (type##OopDesc*)obj(); }  \
 158        type##OopDesc* operator->() const {                                 \
 159             return (type##OopDesc*)obj();                                  \
 160        }                                                                   \
 161        type##Oop& operator=(const type##Oop& o) {                          \
 162             oop::operator=(o);                                             \
 163             return *this;                                                  \
 164        }                                                                   \
 165        volatile type##Oop& operator=(const type##Oop& o) volatile {        \
 166             (void)const_cast<oop&>(oop::operator=(o));                     \
 167             return *this;                                                  \
 168        }                                                                   \
 169        volatile type##Oop& operator=(const volatile type##Oop& o) volatile {\
 170             (void)const_cast<oop&>(oop::operator=(o));                     \
 171             return *this;                                                  \
 172        }                                                                   \
 173    };                                                                      \
 174                                                                            \
 175    template<>                                                              \
 176    struct PrimitiveConversions::Translate<type##Oop> : public TrueType {   \
 177      typedef type##Oop Value;                                              \
 178      typedef type##OopDesc* Decayed;                                       \
 179                                                                            \
 180      static Decayed decay(Value x) { return (type##OopDesc*)x.obj(); }     \
 181      static Value recover(Decayed x) { return type##Oop(x); }              \
 182    };
 183 
 184 DEF_OOP(instance);
 185 DEF_OOP(array);
 186 DEF_OOP(objArray);
 187 DEF_OOP(typeArray);
 188 
 189 #endif // CHECK_UNHANDLED_OOPS
 190 
 191 // For CHECK_UNHANDLED_OOPS, it is ambiguous C++ behavior to have the oop
 192 // structure contain explicit user defined conversions of both numerical
 193 // and pointer type. Define inline methods to provide the numerical conversions.
 194 template <class T> inline oop cast_to_oop(T value) {
 195   return (oop)(CHECK_UNHANDLED_OOPS_ONLY((void *))(value));
 196 }
 197 template <class T> inline T cast_from_oop(oop o) {
 198   return (T)(CHECK_UNHANDLED_OOPS_ONLY((void*))o);
 199 }
 200 
 201 inline bool check_obj_alignment(oop obj) {
 202   return (cast_from_oop<intptr_t>(obj) & MinObjAlignmentInBytesMask) == 0;
 203 }
 204 
 205 // The metadata hierarchy is separate from the oop hierarchy
 206 
 207 //      class MetaspaceObj
 208 class   ConstMethod;
 209 class   ConstantPoolCache;
 210 class   MethodData;
 211 //      class Metadata
 212 class   Method;
 213 class   ConstantPool;
 214 //      class CHeapObj
 215 class   CompiledICHolder;
 216 
 217 
 218 // The klass hierarchy is separate from the oop hierarchy.
 219 
 220 class Klass;
 221 class   InstanceKlass;
 222 class     InstanceMirrorKlass;
 223 class     InstanceClassLoaderKlass;
 224 class     InstanceRefKlass;
 225 class   ArrayKlass;
 226 class     ObjArrayKlass;
 227 class     TypeArrayKlass;
 228 
 229 #endif // SHARE_VM_OOPS_OOPSHIERARCHY_HPP