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