1 /*
   2  * Copyright (c) 2017, 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_OOPS_COMPRESSEDOOPS_INLINE_HPP
  26 #define SHARE_OOPS_COMPRESSEDOOPS_INLINE_HPP
  27 
  28 #include "gc/shared/collectedHeap.hpp"
  29 #include "memory/universe.hpp"
  30 #include "oops/oop.hpp"
  31 
  32 // Functions for encoding and decoding compressed oops.
  33 // If the oops are compressed, the type passed to these overloaded functions
  34 // is narrowOop.  All functions are overloaded so they can be called by
  35 // template functions without conditionals (the compiler instantiates via
  36 // the right type and inlines the appropriate code).
  37 
  38 // Algorithm for encoding and decoding oops from 64 bit pointers to 32 bit
  39 // offset from the heap base.  Saving the check for null can save instructions
  40 // in inner GC loops so these are separated.
  41 
  42 namespace CompressedOops {
  43   inline bool is_null(oop obj)       { return obj == NULL; }
  44   inline bool is_null(narrowOop obj) { return obj == 0; }
  45 
  46   inline oop decode_not_null(narrowOop v) {
  47     assert(!is_null(v), "narrow oop value can never be zero");
  48     address base = Universe::narrow_oop_base();
  49     int    shift = Universe::narrow_oop_shift();
  50     oop result = (oop)(void*)((uintptr_t)base + ((uintptr_t)v << shift));
  51     assert(check_obj_alignment(result), "address not aligned: " INTPTR_FORMAT, p2i((void*) result));
  52     return result;
  53   }
  54 
  55   inline oop decode(narrowOop v) {
  56     return is_null(v) ? (oop)NULL : decode_not_null(v);
  57   }
  58 
  59   inline narrowOop encode_not_null(oop v) {
  60     assert(!is_null(v), "oop value can never be zero");
  61     assert(check_obj_alignment(v), "Address not aligned");
  62     assert(Universe::heap()->is_in_reserved(v), "Address not in heap");
  63     address base = Universe::narrow_oop_base();
  64     int    shift = Universe::narrow_oop_shift();
  65     uint64_t  pd = (uint64_t)(pointer_delta((void*)v, (void*)base, 1));
  66     assert(OopEncodingHeapMax > pd, "change encoding max if new encoding");
  67     uint64_t result = pd >> shift;
  68     assert((result & CONST64(0xffffffff00000000)) == 0, "narrow oop overflow");
  69     assert(oopDesc::unsafe_equals(decode(result), v), "reversibility");
  70     return (narrowOop)result;
  71   }
  72 
  73   inline narrowOop encode(oop v) {
  74     return is_null(v) ? (narrowOop)0 : encode_not_null(v);
  75   }
  76 
  77   // No conversions needed for these overloads
  78   inline oop decode_not_null(oop v)             { return v; }
  79   inline oop decode(oop v)                      { return v; }
  80   inline narrowOop encode_not_null(narrowOop v) { return v; }
  81   inline narrowOop encode(narrowOop v)          { return v; }
  82 }
  83 
  84 #endif // SHARE_OOPS_COMPRESSEDOOPS_INLINE_HPP