1 /*
   2  * Copyright (c) 2005, 2015, 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/klass.hpp"
  30 #include "oops/markOop.hpp"
  31 
  32 inline void Klass::set_prototype_header(markOop header) {
  33   assert(!is_value() || header->is_always_locked(), "Unexpected prototype");
  34     _prototype_header = header;
  35 }
  36 
  37 inline bool Klass::is_null(Klass* obj)  { return obj == NULL; }
  38 inline bool Klass::is_null(narrowKlass obj) { return obj == 0; }
  39 
  40 // Encoding and decoding for klass field.
  41 
  42 inline bool check_klass_alignment(Klass* obj) {
  43   return (intptr_t)obj % KlassAlignmentInBytes == 0;
  44 }
  45 
  46 inline narrowKlass Klass::encode_klass_not_null(Klass* v) {
  47   assert(!is_null(v), "klass value can never be zero");
  48   assert(check_klass_alignment(v), "Address not aligned");
  49   int    shift = Universe::narrow_klass_shift();
  50   uint64_t pd = (uint64_t)(pointer_delta((void*)v, Universe::narrow_klass_base(), 1));
  51   assert(KlassEncodingMetaspaceMax > pd, "change encoding max if new encoding");
  52   uint64_t result = pd >> shift;
  53   assert((result & CONST64(0xffffffff00000000)) == 0, "narrow klass pointer overflow");
  54   assert(decode_klass(result) == v, "reversibility");
  55   return (narrowKlass)result;
  56 }
  57 
  58 inline narrowKlass Klass::encode_klass(Klass* v) {
  59   return is_null(v) ? (narrowKlass)0 : encode_klass_not_null(v);
  60 }
  61 
  62 inline Klass* Klass::decode_klass_not_null(narrowKlass v) {
  63   assert(!is_null(v), "narrow klass value can never be zero");
  64   int    shift = Universe::narrow_klass_shift();
  65   Klass* result = (Klass*)(void*)((uintptr_t)Universe::narrow_klass_base() + ((uintptr_t)v << shift));
  66   assert(check_klass_alignment(result), "address not aligned: " INTPTR_FORMAT, p2i((void*) result));
  67   return result;
  68 }
  69 
  70 inline Klass* Klass::decode_klass(narrowKlass v) {
  71   return is_null(v) ? (Klass*)NULL : decode_klass_not_null(v);
  72 }
  73 
  74 inline bool Klass::decode_ptr_is_value_type(narrowKlass v) {
  75  return (v & Universe::oop_metadata_valuetype_mask()) != 0;
  76 }
  77 
  78 inline bool Klass::ptr_is_value_type(Klass* v) {
  79  return ((uintptr_t)v & KlassPtrValueTypeMask) != 0;
  80 }
  81 
  82 template <typename T>
  83 bool Klass::is_instanceof_or_null(T element) {
  84   if (oopDesc::is_null(element)) {
  85     return true;
  86   }
  87   oop obj = oopDesc::decode_heap_oop_not_null(element);
  88   return obj->klass()->is_subtype_of(this);
  89 }
  90 
  91 #endif // SHARE_VM_OOPS_KLASS_INLINE_HPP