1 /*
   2  * Copyright (c) 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_GC_IMPLEMENTATION_G1_G1INCSETSTATE_HPP
  26 #define SHARE_VM_GC_IMPLEMENTATION_G1_G1INCSETSTATE_HPP
  27 
  28 #include "gc_implementation/g1/g1BiasedArray.hpp"
  29 #include "memory/allocation.hpp"
  30 
  31 typedef int8_t in_cset_state_t;
  32 
  33 // Helper class used to examine in_cset_t values.
  34 class InCSetState : AllStatic {
  35 public:
  36   enum {
  37     // Selection of the values were driven to micro-optimize the encoding and
  38     // frequency of the checks.
  39     // The most common check is whether the region is in the collection set or not.
  40     // This encoding allows us to use an != 0 check which in some architectures
  41     // (x86*) can be encoded slightly more efficently than a normal comparison
  42     // against zero.
  43     // The same situation occurs when checking whether the region is humongous
  44     // or not, which is encoded by values < 0.
  45     // The other values are simply encoded in increasing generation order, which
  46     // makes getting the next generation fast by a simple increment.
  47     Humongous    = -1,    // The region is humongous - note that actually any value < 0 would be possible here.
  48     NotInCSet    =  0,    // The region is not in the collection set.
  49     Young        =  1,    // The region is in the collection set and a young region.
  50     Old          =  2,    // The region is in the collection set and an old region.
  51     Num
  52   };
  53 
  54   static bool is_not_in_cset(in_cset_state_t state) { return state == NotInCSet; }
  55   static bool is_in_cset_or_humongous(in_cset_state_t state) { return state != NotInCSet; }
  56   static bool is_in_cset(in_cset_state_t state) { return state > NotInCSet; }
  57   static bool is_humongous(in_cset_state_t state) { return state < NotInCSet; }
  58 };
  59 
  60 // Instances of this class are used for quick tests on whether a reference points
  61 // into the collection set and into which generation or is a humongous object
  62 //
  63 // Each of the array's elements indicates whether the corresponding region is in
  64 // the collection set and if so in which generation, or a humongous region.
  65 //
  66 // We use this to speed up reference processing during young collection and
  67 // quickly reclaim humongous objects. For the latter, by making a humongous region
  68 // succeed this test, we sort-of add it to the collection set. During the reference
  69 // iteration closures, when we see a humongous region, we then simply mark it as
  70 // referenced, i.e. live.
  71 class G1InCSetStateFastTestBiasedMappedArray : public G1BiasedMappedArray<in_cset_state_t> {
  72  protected:
  73   in_cset_state_t default_value() const { return InCSetState::NotInCSet; }
  74  public:
  75   void set_humongous(uintptr_t index) {
  76     assert(get_by_index(index) == default_value(), "should be default");
  77     set_by_index(index, InCSetState::Humongous);
  78   }
  79 
  80   void clear_humongous(uintptr_t index) {
  81     set_by_index(index, InCSetState::NotInCSet);
  82   }
  83 
  84   void set_in_young(uintptr_t index) {
  85     assert(get_by_index(index) == default_value(), "should be default");
  86     set_by_index(index, InCSetState::Young);
  87   }
  88 
  89   void set_in_old(uintptr_t index) {
  90     assert(get_by_index(index) == default_value(), "should be default");
  91     set_by_index(index, InCSetState::Old);
  92   }
  93 
  94   bool is_in_cset_or_humongous(HeapWord* addr) const { return InCSetState::is_in_cset_or_humongous(at(addr)); }
  95   bool is_in_cset(HeapWord* addr) const { return InCSetState::is_in_cset(at(addr)); }
  96   in_cset_state_t at(HeapWord* addr) const { return (in_cset_state_t) get_by_address(addr); }
  97   void clear() { G1BiasedMappedArray<in_cset_state_t>::clear(); }
  98 };
  99 
 100 #endif // SHARE_VM_GC_IMPLEMENTATION_G1_G1INCSETSTATE_HPP