1 /*
   2  * Copyright (c) 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_GC_SHARED_SCAVENGABLENMETHODDATAS_HPP
  26 #define SHARE_GC_SHARED_SCAVENGABLENMETHODDATAS_HPP
  27 
  28 #include "memory/allocation.hpp"
  29 #include "utilities/globalDefinitions.hpp"
  30 
  31 class nmethod;
  32 
  33 class ScavengableNMethodsData : public CHeapObj<mtGC> {
  34   // State bits put into the two lower alignment bits.
  35   static const uintptr_t state_bits = 2;
  36   static const uintptr_t state_mask = (1 << state_bits) - 1;
  37   static const uintptr_t state_on_list = 0x1;
  38   static const uintptr_t state_marked  = 0x2;
  39 
  40   // nmethod containing the GC data.
  41   nmethod* const _nm;
  42 
  43   // The data is stored as a bit pattern in a void* inside the nmethod.
  44   uintptr_t data() const                    { return reinterpret_cast<uintptr_t>(_nm->gc_data<void>()); }
  45   void set_data(uintptr_t data) const       { _nm->set_gc_data(reinterpret_cast<void*>(data)); }
  46 
  47   jbyte state() const                       { return data() & state_mask; }
  48   void set_state(jbyte state) const         { set_data((data() & ~state_mask) | state); }
  49 
  50   uintptr_t from_nmethod(nmethod* nm) const { return reinterpret_cast<uintptr_t>(nm); }
  51   nmethod* to_nmethod(uintptr_t data) const { return reinterpret_cast<nmethod*>(data); }
  52 
  53 public:
  54   ScavengableNMethodsData(nmethod* nm) : _nm(nm) {
  55     assert(is_aligned(nm, 4), "Must be aligned to fit state bits");
  56   }
  57 
  58   // Scavengable oop support
  59   bool  on_list() const { return (state() & state_on_list) != 0; }
  60   void  set_on_list()   { set_state(state_on_list); }
  61   void  clear_on_list() { set_state(0); }
  62 
  63 #ifndef PRODUCT
  64   void  set_marked()   { set_state(state() | state_marked); }
  65   void  clear_marked() { set_state(state() & ~state_marked); }
  66   bool  not_marked()   { return (state() & ~state_on_list) == 0; }
  67   // N.B. there is no positive marked query, and we only use the not_marked query for asserts.
  68 #endif //PRODUCT
  69 
  70   nmethod* next() const     { return to_nmethod(data() & ~state_mask); }
  71   void set_next(nmethod *n) { set_data(from_nmethod(n) | state()); }
  72 };
  73 
  74 #endif // SHARE_GC_SHARED_SCAVENGABLENMETHODDATAS_HPP