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