1 /*
   2  * Copyright (c) 2005, 2014, 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_KLASS_INLINE_HPP
  26 #define SHARE_VM_OOPS_KLASS_INLINE_HPP
  27 
  28 #include "memory/universe.hpp"
  29 #include "oops/instanceClassLoaderKlass.inline.hpp"
  30 #include "oops/instanceKlass.inline.hpp"
  31 #include "oops/instanceMirrorKlass.inline.hpp"
  32 #include "oops/instanceRefKlass.inline.hpp"
  33 #include "oops/klass.hpp"
  34 #include "oops/markOop.hpp"
  35 #include "oops/objArrayKlass.inline.hpp"
  36 #include "oops/typeArrayKlass.inline.hpp"
  37 
  38 inline void Klass::set_prototype_header(markOop header) {
  39   assert(!header->has_bias_pattern() || oop_is_instance(), "biased locking currently only supported for Java instances");
  40   _prototype_header = header;
  41 }
  42 
  43 inline bool Klass::is_null(Klass* obj)  { return obj == NULL; }
  44 inline bool Klass::is_null(narrowKlass obj) { return obj == 0; }
  45 
  46 // Encoding and decoding for klass field.
  47 
  48 inline bool check_klass_alignment(Klass* obj) {
  49   return (intptr_t)obj % KlassAlignmentInBytes == 0;
  50 }
  51 
  52 inline narrowKlass Klass::encode_klass_not_null(Klass* v) {
  53   assert(!is_null(v), "klass value can never be zero");
  54   assert(check_klass_alignment(v), "Address not aligned");
  55   int    shift = Universe::narrow_klass_shift();
  56   uint64_t pd = (uint64_t)(pointer_delta((void*)v, Universe::narrow_klass_base(), 1));
  57   assert(KlassEncodingMetaspaceMax > pd, "change encoding max if new encoding");
  58   uint64_t result = pd >> shift;
  59   assert((result & CONST64(0xffffffff00000000)) == 0, "narrow klass pointer overflow");
  60   assert(decode_klass(result) == v, "reversibility");
  61   return (narrowKlass)result;
  62 }
  63 
  64 inline narrowKlass Klass::encode_klass(Klass* v) {
  65   return is_null(v) ? (narrowKlass)0 : encode_klass_not_null(v);
  66 }
  67 
  68 inline Klass* Klass::decode_klass_not_null(narrowKlass v) {
  69   assert(!is_null(v), "narrow klass value can never be zero");
  70   int    shift = Universe::narrow_klass_shift();
  71   Klass* result = (Klass*)(void*)((uintptr_t)Universe::narrow_klass_base() + ((uintptr_t)v << shift));
  72   assert(check_klass_alignment(result), err_msg("address not aligned: " INTPTR_FORMAT, p2i((void*) result)));
  73   return result;
  74 }
  75 
  76 inline Klass* Klass::decode_klass(narrowKlass v) {
  77   return is_null(v) ? (Klass*)NULL : decode_klass_not_null(v);
  78 }
  79 
  80 #define CONCRETE_KLASS_DO_AND_RETURN(the_klass, todo) \
  81   do { \
  82     switch ((the_klass)->dispatch_tag()) { \
  83     case Klass::_instance:                         return InstanceKlass::cast(the_klass)->todo; \
  84     case Klass::_instance_ref:                  return InstanceRefKlass::cast(the_klass)->todo; \
  85     case Klass::_instance_mirror:            return InstanceMirrorKlass::cast(the_klass)->todo; \
  86     case Klass::_instance_class_loader: return InstanceClassLoaderKlass::cast(the_klass)->todo; \
  87     case Klass::_obj_array:                        return ObjArrayKlass::cast(the_klass)->todo; \
  88     case Klass::_type_array:                      return TypeArrayKlass::cast(the_klass)->todo; \
  89     default: fatal("Incorrect dispatch index"); return 0; \
  90     }   \
  91   } while (false)
  92 
  93 #endif // SHARE_VM_OOPS_KLASS_INLINE_HPP