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