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_MARKWORD_HPP
26 #define SHARE_OOPS_MARKWORD_HPP
27
28 #include "metaprogramming/primitiveConversions.hpp"
29 #include "oops/oopsHierarchy.hpp"
30 #include "runtime/globals.hpp"
31
32 #include <type_traits>
33
34 // The markWord describes the header of an object.
35 //
36 // Bit-format of an object header (most significant first, big endian layout below):
37 //
38 // 32 bits:
39 // --------
40 // hash:25 ------------>| age:4 unused_gap:1 lock:2 (normal object)
41 //
42 // 64 bits:
43 // --------
44 // unused:25 hash:31 -->| unused_gap:1 age:4 unused_gap:1 lock:2 (normal object)
45 //
46 // - hash contains the identity hash value: largest value is
47 // 31 bits, see os::random(). Also, 64-bit vm's require
48 // a hash value no bigger than 32 bits because they will not
49 // properly generate a mask larger than that: see library_call.cpp
50 //
51 // - the two lock bits are used to describe three states: locked/unlocked and monitor.
52 //
53 // [ptr | 00] locked ptr points to real header on stack (stack-locking in use)
54 // [header | 00] locked locked regular object header (fast-locking in use)
55 // [header | 01] unlocked regular object header
56 // [ptr | 10] monitor inflated lock (header is swapped out)
57 // [ptr | 11] marked used to mark an object
58 // [0 ............ 0| 00] inflating inflation in progress (stack-locking in use)
59 //
60 // We assume that stack/thread pointers have the lowest two bits cleared.
61 //
62 // - INFLATING() is a distinguished markword value of all zeros that is
63 // used when inflating an existing stack-lock into an ObjectMonitor.
64 // See below for is_being_inflated() and INFLATING().
65
86 static markWord from_pointer(void* ptr) {
87 return markWord((uintptr_t)ptr);
88 }
89 void* to_pointer() const {
90 return (void*)_value;
91 }
92
93 bool operator==(const markWord& other) const {
94 return _value == other._value;
95 }
96 bool operator!=(const markWord& other) const {
97 return !operator==(other);
98 }
99
100 // Conversion
101 uintptr_t value() const { return _value; }
102
103 // Constants
104 static const int age_bits = 4;
105 static const int lock_bits = 2;
106 static const int first_unused_gap_bits = 1;
107 static const int max_hash_bits = BitsPerWord - age_bits - lock_bits - first_unused_gap_bits;
108 static const int hash_bits = max_hash_bits > 31 ? 31 : max_hash_bits;
109 static const int second_unused_gap_bits = LP64_ONLY(1) NOT_LP64(0);
110
111 static const int lock_shift = 0;
112 static const int age_shift = lock_bits + first_unused_gap_bits;
113 static const int hash_shift = age_shift + age_bits + second_unused_gap_bits;
114
115 static const uintptr_t lock_mask = right_n_bits(lock_bits);
116 static const uintptr_t lock_mask_in_place = lock_mask << lock_shift;
117 static const uintptr_t age_mask = right_n_bits(age_bits);
118 static const uintptr_t age_mask_in_place = age_mask << age_shift;
119 static const uintptr_t hash_mask = right_n_bits(hash_bits);
120 static const uintptr_t hash_mask_in_place = hash_mask << hash_shift;
121
122 static const uintptr_t locked_value = 0;
123 static const uintptr_t unlocked_value = 1;
124 static const uintptr_t monitor_value = 2;
125 static const uintptr_t marked_value = 3;
126
127 static const uintptr_t no_hash = 0 ; // no hash value assigned
128 static const uintptr_t no_hash_in_place = (uintptr_t)no_hash << hash_shift;
129 static const uintptr_t no_lock_in_place = unlocked_value;
130
131 static const uint max_age = age_mask;
132
133 // Creates a markWord with all bits set to zero.
134 static markWord zero() { return markWord(uintptr_t(0)); }
135
136 // lock accessors (note that these assume lock_shift == 0)
137 bool is_locked() const {
138 return (mask_bits(value(), lock_mask_in_place) != unlocked_value);
139 }
140 bool is_unlocked() const {
188 // Clear the lock_mask_in_place bits to set locked_value:
189 return markWord(value() & ~lock_mask_in_place);
190 }
191
192 bool has_monitor() const {
193 return ((value() & lock_mask_in_place) == monitor_value);
194 }
195 ObjectMonitor* monitor() const {
196 assert(has_monitor(), "check");
197 // Use xor instead of &~ to provide one extra tag-bit check.
198 return (ObjectMonitor*) (value() ^ monitor_value);
199 }
200 bool has_displaced_mark_helper() const {
201 intptr_t lockbits = value() & lock_mask_in_place;
202 return LockingMode == LM_LIGHTWEIGHT ? lockbits == monitor_value // monitor?
203 : (lockbits & unlocked_value) == 0; // monitor | stack-locked?
204 }
205 markWord displaced_mark_helper() const;
206 void set_displaced_mark_helper(markWord m) const;
207 markWord copy_set_hash(intptr_t hash) const {
208 uintptr_t tmp = value() & (~hash_mask_in_place);
209 tmp |= ((hash & hash_mask) << hash_shift);
210 return markWord(tmp);
211 }
212 // it is only used to be stored into BasicLock as the
213 // indicator that the lock is using heavyweight monitor
214 static markWord unused_mark() {
215 return markWord(marked_value);
216 }
217 // the following two functions create the markWord to be
218 // stored into object header, it encodes monitor info
219 static markWord encode(BasicLock* lock) {
220 return from_pointer(lock);
221 }
222 static markWord encode(ObjectMonitor* monitor) {
223 uintptr_t tmp = (uintptr_t) monitor;
224 return markWord(tmp | monitor_value);
225 }
226
227 // used to encode pointers during GC
228 markWord clear_lock_bits() { return markWord(value() & ~lock_mask_in_place); }
229
230 // age operations
231 markWord set_marked() { return markWord((value() & ~lock_mask_in_place) | marked_value); }
232 markWord set_unmarked() { return markWord((value() & ~lock_mask_in_place) | unlocked_value); }
233
234 uint age() const { return (uint) mask_bits(value() >> age_shift, age_mask); }
235 markWord set_age(uint v) const {
236 assert((v & ~age_mask) == 0, "shouldn't overflow age field");
237 return markWord((value() & ~age_mask_in_place) | ((v & age_mask) << age_shift));
238 }
239 markWord incr_age() const { return age() == max_age ? markWord(_value) : set_age(age() + 1); }
240
241 // hash operations
242 intptr_t hash() const {
243 return mask_bits(value() >> hash_shift, hash_mask);
244 }
245
246 bool has_no_hash() const {
247 return hash() == no_hash;
248 }
249
250 // Prototype mark for initialization
251 static markWord prototype() {
252 return markWord( no_hash_in_place | no_lock_in_place );
253 }
254
255 // Debugging
256 void print_on(outputStream* st, bool print_monitor_info = true) const;
257
258 // Prepare address of oop for placement into mark
259 inline static markWord encode_pointer_as_mark(void* p) { return from_pointer(p).set_marked(); }
260
261 // Recover address of oop from encoded form used in mark
262 inline void* decode_pointer() { return (void*)clear_lock_bits().value(); }
263 };
264
265 // Support atomic operations.
266 template<>
267 struct PrimitiveConversions::Translate<markWord> : public std::true_type {
268 typedef markWord Value;
269 typedef uintptr_t Decayed;
270
271 static Decayed decay(const Value& x) { return x.value(); }
272 static Value recover(Decayed x) { return Value(x); }
273 };
274
275 #endif // SHARE_OOPS_MARKWORD_HPP
|
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_MARKWORD_HPP
26 #define SHARE_OOPS_MARKWORD_HPP
27
28 #include "gc/shared/gc_globals.hpp"
29 #include "metaprogramming/primitiveConversions.hpp"
30 #include "oops/compressedKlass.hpp"
31 #include "oops/oopsHierarchy.hpp"
32 #include "runtime/globals.hpp"
33
34 #include <type_traits>
35
36 // The markWord describes the header of an object.
37 //
38 // Bit-format of an object header (most significant first, big endian layout below):
39 //
40 // 32 bits:
41 // --------
42 // hash:25 ------------>| age:4 unused_gap:1 lock:2 (normal object)
43 //
44 // 64 bits:
45 // --------
46 // unused:25 hash:31 -->| unused_gap:1 age:4 unused_gap:1 lock:2 (normal object)
47 //
48 // 64 bits (with compact headers):
49 // -------------------------------
50 // nklass:32 hash:25 -->| unused_gap:1 age:4 self-fwded:1 lock:2 (normal object)
51 //
52 // - hash contains the identity hash value: largest value is
53 // 31 bits, see os::random(). Also, 64-bit vm's require
54 // a hash value no bigger than 32 bits because they will not
55 // properly generate a mask larger than that: see library_call.cpp
56 //
57 // - the two lock bits are used to describe three states: locked/unlocked and monitor.
58 //
59 // [ptr | 00] locked ptr points to real header on stack (stack-locking in use)
60 // [header | 00] locked locked regular object header (fast-locking in use)
61 // [header | 01] unlocked regular object header
62 // [ptr | 10] monitor inflated lock (header is swapped out)
63 // [ptr | 11] marked used to mark an object
64 // [0 ............ 0| 00] inflating inflation in progress (stack-locking in use)
65 //
66 // We assume that stack/thread pointers have the lowest two bits cleared.
67 //
68 // - INFLATING() is a distinguished markword value of all zeros that is
69 // used when inflating an existing stack-lock into an ObjectMonitor.
70 // See below for is_being_inflated() and INFLATING().
71
92 static markWord from_pointer(void* ptr) {
93 return markWord((uintptr_t)ptr);
94 }
95 void* to_pointer() const {
96 return (void*)_value;
97 }
98
99 bool operator==(const markWord& other) const {
100 return _value == other._value;
101 }
102 bool operator!=(const markWord& other) const {
103 return !operator==(other);
104 }
105
106 // Conversion
107 uintptr_t value() const { return _value; }
108
109 // Constants
110 static const int age_bits = 4;
111 static const int lock_bits = 2;
112 static const int self_forwarded_bits = 1;
113 static const int max_hash_bits = BitsPerWord - age_bits - lock_bits - self_forwarded_bits;
114 static const int hash_bits = max_hash_bits > 31 ? 31 : max_hash_bits;
115 static const int hash_bits_compact = max_hash_bits > 25 ? 25 : max_hash_bits;
116 // Used only without compact headers.
117 static const int unused_gap_bits = LP64_ONLY(1) NOT_LP64(0);
118 #ifdef _LP64
119 // Used only with compact headers.
120 static const int klass_bits = 32;
121 #endif
122
123 static const int lock_shift = 0;
124 static const int self_forwarded_shift = lock_shift + lock_bits;
125 static const int age_shift = self_forwarded_shift + self_forwarded_bits;
126 static const int hash_shift = age_shift + age_bits + unused_gap_bits;
127 static const int hash_shift_compact = age_shift + age_bits;
128 #ifdef _LP64
129 // Used only with compact headers.
130 static const int klass_shift = hash_shift_compact + hash_bits_compact;
131 #endif
132
133 static const uintptr_t lock_mask = right_n_bits(lock_bits);
134 static const uintptr_t lock_mask_in_place = lock_mask << lock_shift;
135 static const uintptr_t self_forwarded_mask = right_n_bits(self_forwarded_bits);
136 static const uintptr_t self_forwarded_mask_in_place = self_forwarded_mask << self_forwarded_shift;
137 static const uintptr_t age_mask = right_n_bits(age_bits);
138 static const uintptr_t age_mask_in_place = age_mask << age_shift;
139 static const uintptr_t hash_mask = right_n_bits(hash_bits);
140 static const uintptr_t hash_mask_in_place = hash_mask << hash_shift;
141 static const uintptr_t hash_mask_compact = right_n_bits(hash_bits_compact);
142 static const uintptr_t hash_mask_compact_in_place = hash_mask_compact << hash_shift_compact;
143 #ifdef _LP64
144 static const uintptr_t klass_mask = right_n_bits(klass_bits);
145 static const uintptr_t klass_mask_in_place = klass_mask << klass_shift;
146 #endif
147
148 static const uintptr_t locked_value = 0;
149 static const uintptr_t unlocked_value = 1;
150 static const uintptr_t monitor_value = 2;
151 static const uintptr_t marked_value = 3;
152
153 static const uintptr_t no_hash = 0 ; // no hash value assigned
154 static const uintptr_t no_hash_in_place = (uintptr_t)no_hash << hash_shift;
155 static const uintptr_t no_lock_in_place = unlocked_value;
156
157 static const uint max_age = age_mask;
158
159 // Creates a markWord with all bits set to zero.
160 static markWord zero() { return markWord(uintptr_t(0)); }
161
162 // lock accessors (note that these assume lock_shift == 0)
163 bool is_locked() const {
164 return (mask_bits(value(), lock_mask_in_place) != unlocked_value);
165 }
166 bool is_unlocked() const {
214 // Clear the lock_mask_in_place bits to set locked_value:
215 return markWord(value() & ~lock_mask_in_place);
216 }
217
218 bool has_monitor() const {
219 return ((value() & lock_mask_in_place) == monitor_value);
220 }
221 ObjectMonitor* monitor() const {
222 assert(has_monitor(), "check");
223 // Use xor instead of &~ to provide one extra tag-bit check.
224 return (ObjectMonitor*) (value() ^ monitor_value);
225 }
226 bool has_displaced_mark_helper() const {
227 intptr_t lockbits = value() & lock_mask_in_place;
228 return LockingMode == LM_LIGHTWEIGHT ? lockbits == monitor_value // monitor?
229 : (lockbits & unlocked_value) == 0; // monitor | stack-locked?
230 }
231 markWord displaced_mark_helper() const;
232 void set_displaced_mark_helper(markWord m) const;
233 markWord copy_set_hash(intptr_t hash) const {
234 if (UseCompactObjectHeaders) {
235 uintptr_t tmp = value() & (~hash_mask_compact_in_place);
236 tmp |= ((hash & hash_mask_compact) << hash_shift_compact);
237 return markWord(tmp);
238 } else {
239 uintptr_t tmp = value() & (~hash_mask_in_place);
240 tmp |= ((hash & hash_mask) << hash_shift);
241 return markWord(tmp);
242 }
243 }
244 // it is only used to be stored into BasicLock as the
245 // indicator that the lock is using heavyweight monitor
246 static markWord unused_mark() {
247 return markWord(marked_value);
248 }
249 // the following two functions create the markWord to be
250 // stored into object header, it encodes monitor info
251 static markWord encode(BasicLock* lock) {
252 return from_pointer(lock);
253 }
254 static markWord encode(ObjectMonitor* monitor) {
255 uintptr_t tmp = (uintptr_t) monitor;
256 return markWord(tmp | monitor_value);
257 }
258
259 // used to encode pointers during GC
260 markWord clear_lock_bits() { return markWord(value() & ~lock_mask_in_place); }
261
262 // age operations
263 markWord set_marked() { return markWord((value() & ~lock_mask_in_place) | marked_value); }
264 markWord set_unmarked() { return markWord((value() & ~lock_mask_in_place) | unlocked_value); }
265
266 uint age() const { return (uint) mask_bits(value() >> age_shift, age_mask); }
267 markWord set_age(uint v) const {
268 assert((v & ~age_mask) == 0, "shouldn't overflow age field");
269 return markWord((value() & ~age_mask_in_place) | ((v & age_mask) << age_shift));
270 }
271 markWord incr_age() const { return age() == max_age ? markWord(_value) : set_age(age() + 1); }
272
273 // hash operations
274 intptr_t hash() const {
275 if (UseCompactObjectHeaders) {
276 return mask_bits(value() >> hash_shift_compact, hash_mask_compact);
277 } else {
278 return mask_bits(value() >> hash_shift, hash_mask);
279 }
280 }
281
282 bool has_no_hash() const {
283 return hash() == no_hash;
284 }
285
286 #ifdef _LP64
287 inline markWord actual_mark() const;
288 inline Klass* klass() const;
289 inline Klass* klass_or_null() const;
290 inline narrowKlass narrow_klass() const;
291 inline markWord set_narrow_klass(narrowKlass nklass) const;
292 inline markWord set_klass(Klass* klass) const;
293 #endif
294
295 // Prototype mark for initialization
296 static markWord prototype() {
297 return markWord( no_hash_in_place | no_lock_in_place );
298 }
299
300 // Debugging
301 void print_on(outputStream* st, bool print_monitor_info = true) const;
302
303 // Prepare address of oop for placement into mark
304 inline static markWord encode_pointer_as_mark(void* p) { return from_pointer(p).set_marked(); }
305
306 // Recover address of oop from encoded form used in mark
307 inline void* decode_pointer() { return (void*)clear_lock_bits().value(); }
308
309 #ifdef _LP64
310 inline bool self_forwarded() const {
311 bool self_fwd = mask_bits(value(), self_forwarded_mask_in_place) != 0;
312 assert(!self_fwd || UseAltGCForwarding, "Only set self-fwd bit when using alt GC forwarding");
313 return self_fwd;
314 }
315
316 inline markWord set_self_forwarded() const {
317 assert(UseAltGCForwarding, "Only call this with alt GC forwarding");
318 return markWord(value() | self_forwarded_mask_in_place | marked_value);
319 }
320 #endif
321 };
322
323 // Support atomic operations.
324 template<>
325 struct PrimitiveConversions::Translate<markWord> : public std::true_type {
326 typedef markWord Value;
327 typedef uintptr_t Decayed;
328
329 static Decayed decay(const Value& x) { return x.value(); }
330 static Value recover(Decayed x) { return Value(x); }
331 };
332
333 #endif // SHARE_OOPS_MARKWORD_HPP
|