1 /*
   2  * Copyright (c) 2014, 2017, 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_G1_HEAPREGIONTYPE_HPP
  26 #define SHARE_VM_GC_G1_HEAPREGIONTYPE_HPP
  27 
  28 #include "gc/g1/g1HeapRegionTraceType.hpp"
  29 #include "memory/allocation.hpp"
  30 
  31 #define hrt_assert_is_valid(tag) \
  32   assert(is_valid((tag)), "invalid HR type: %u", (uint) (tag))
  33 
  34 class HeapRegionType VALUE_OBJ_CLASS_SPEC {
  35 friend class VMStructs;
  36 
  37 private:
  38   // We encode the value of the heap region type so the generation can be
  39   // determined quickly. The tag is split into two parts:
  40   //
  41   //   major type (young, old, humongous, archive)           : top N-1 bits
  42   //   minor type (eden / survivor, starts / cont hum, etc.) : bottom 1 bit
  43   //
  44   // If there's need to increase the number of minor types in the
  45   // future, we'll have to increase the size of the latter and hence
  46   // decrease the size of the former.
  47   //
  48   // 00000 0 [ 0] Free
  49   //
  50   // 00001 0 [ 2] Young Mask
  51   // 00001 0 [ 2] Eden
  52   // 00001 1 [ 3] Survivor
  53   //
  54   // 00010 0 [ 4] Humongous Mask
  55   // 00100 0 [ 8] Pinned Mask
  56   // 00110 0 [12] Starts Humongous
  57   // 00110 1 [13] Continues Humongous
  58   //
  59   // 01000 0 [16] Old Mask
  60   //
  61   // 10000 0 [32] Archive Mask
  62   // 11100 0 [56] Open Archive
  63   // 11100 1 [57] Closed Archive
  64   //
  65   typedef enum {
  66     FreeTag               = 0,
  67 
  68     YoungMask             = 2,
  69     EdenTag               = YoungMask,
  70     SurvTag               = YoungMask + 1,
  71 
  72     HumongousMask         = 4,
  73     PinnedMask            = 8,
  74     StartsHumongousTag    = HumongousMask | PinnedMask,
  75     ContinuesHumongousTag = HumongousMask | PinnedMask + 1,
  76 
  77     OldMask               = 16,
  78     OldTag                = OldMask,
  79 
  80     // Archive regions are regions with immutable content (i.e. not reclaimed, and
  81     // not allocated into during regular operation). They differ in the kind of references
  82     // allowed for the contained objects:
  83     // - Closed archive regions form a separate self-contained (closed) object graph
  84     // within the set of all of these regions. No references outside of closed
  85     // archive regions are allowed.
  86     // - Open archive regions have no restrictions on the references of their objects.
  87     // Objects within these regions are allowed to have references to objects
  88     // contained in any other kind of regions.
  89     ArchiveMask           = 32,
  90     OpenArchiveTag        = ArchiveMask | PinnedMask | OldMask,
  91     ClosedArchiveTag      = ArchiveMask | PinnedMask | OldMask + 1
  92   } Tag;
  93 
  94   volatile Tag _tag;
  95 
  96   static bool is_valid(Tag tag);
  97 
  98   Tag get() const {
  99     hrt_assert_is_valid(_tag);
 100     return _tag;
 101   }
 102 
 103   // Sets the type to 'tag'.
 104   void set(Tag tag) {
 105     hrt_assert_is_valid(tag);
 106     hrt_assert_is_valid(_tag);
 107     _tag = tag;
 108   }
 109 
 110   // Sets the type to 'tag', expecting the type to be 'before'. This
 111   // is available for when we want to add sanity checking to the type
 112   // transition.
 113   void set_from(Tag tag, Tag before) {
 114     hrt_assert_is_valid(tag);
 115     hrt_assert_is_valid(before);
 116     hrt_assert_is_valid(_tag);
 117     assert(_tag == before, "HR tag: %u, expected: %u new tag; %u", _tag, before, tag);
 118     _tag = tag;
 119   }
 120 
 121 public:
 122   // Queries
 123 
 124   bool is_free() const { return get() == FreeTag; }
 125 
 126   bool is_young()    const { return (get() & YoungMask) != 0; }
 127   bool is_eden()     const { return get() == EdenTag;  }
 128   bool is_survivor() const { return get() == SurvTag;  }
 129 
 130   bool is_humongous()           const { return (get() & HumongousMask) != 0;   }
 131   bool is_starts_humongous()    const { return get() == StartsHumongousTag;    }
 132   bool is_continues_humongous() const { return get() == ContinuesHumongousTag; }
 133 
 134   bool is_archive()        const { return (get() & ArchiveMask) != 0; }
 135   bool is_open_archive()   const { return get() == OpenArchiveTag; }
 136   bool is_closed_archive() const { return get() == ClosedArchiveTag; }
 137 
 138   // is_old regions may or may not also be pinned
 139   bool is_old() const { return (get() & OldMask) != 0; }
 140 
 141   bool is_old_or_humongous() const { return (get() & (OldMask | HumongousMask)) != 0; }
 142 
 143   // is_pinned regions may be archive or humongous
 144   bool is_pinned() const { return (get() & PinnedMask) != 0; }
 145 
 146   // Setters
 147 
 148   void set_free() { set(FreeTag); }
 149 
 150   void set_eden()        { set_from(EdenTag, FreeTag); }
 151   void set_eden_pre_gc() { set_from(EdenTag, SurvTag); }
 152   void set_survivor()    { set_from(SurvTag, FreeTag); }
 153 
 154   void set_starts_humongous()    { set_from(StartsHumongousTag,    FreeTag); }
 155   void set_continues_humongous() { set_from(ContinuesHumongousTag, FreeTag); }
 156 
 157   void set_old() { set(OldTag); }
 158 
 159   // Change the current region type to be of an old region type if not already done so.
 160   // Returns whether the region type has been changed or not.
 161   bool relabel_as_old() {
 162     //assert(!is_free(), "Should not try to move Free region");
 163     assert(!is_humongous(), "Should not try to move Humongous region");
 164     if (is_old()) {
 165       return false;
 166     }
 167     if (is_eden()) {
 168       set_from(OldTag, EdenTag);
 169       return true;
 170     } else if (is_free()) {
 171       set_from(OldTag, FreeTag);
 172       return true;
 173     } else {
 174       set_from(OldTag, SurvTag);
 175       return true;
 176     }
 177   }
 178   void set_open_archive()   { set_from(OpenArchiveTag, FreeTag); }
 179   void set_closed_archive() { set_from(ClosedArchiveTag, FreeTag); }
 180 
 181   // Misc
 182 
 183   const char* get_str() const;
 184   const char* get_short_str() const;
 185   G1HeapRegionTraceType::Type get_trace_type();
 186 
 187   HeapRegionType() : _tag(FreeTag) { hrt_assert_is_valid(_tag); }
 188 };
 189 
 190 #endif // SHARE_VM_GC_G1_HEAPREGIONTYPE_HPP