49 // klass:22 hash:31 valhalla:4 age:4 self-fwd:1 lock:2
50 //
51 // - lock bits are used to describe lock states: locked/unlocked/monitor-locked
52 // and to indicate that an object has been GC marked / forwarded.
53 //
54 // [header | 00] locked locked regular object header (fast-locking in use)
55 // [header | 01] unlocked regular object header
56 // [header | 10] monitor inflated lock (UseObjectMonitorTable == true)
57 // [ptr | 10] monitor inflated lock (UseObjectMonitorTable == false, header is swapped out)
58 // [ptr | 11] marked used to mark an object (header is swapped out)
59 //
60 // - self-fwd - used by some GCs to indicate in-place forwarding.
61 //
62 // Note the position of 'self-fwd' is not by accident. When forwarding an
63 // object to a new heap position, HeapWord alignment guarantees the lower
64 // bits, including 'self-fwd' are 0. "is_self_forwarded()" will be correctly
65 // set to false. Otherwise encode_pointer_as_mark() may have 'self-fwd' set.
66 //
67 // - age - used by some GCs to track the age of objects.
68 //
69 // - valhalla - reserved for valhalla
70 //
71 // - hash - contains the identity hash value: largest value is 31 bits, see
72 // os::random(). Also, 64-bit VMs require a hash value no bigger than 32
73 // bits because they will not properly generate a mask larger than that:
74 // see library_call.cpp
75 //
76 // - klass - klass identifier used when UseCompactObjectHeaders == true
77
78 class ObjectMonitor;
79 class outputStream;
80
81 class markWord {
82 private:
83 uintptr_t _value;
84
85 public:
86 explicit markWord(uintptr_t value) : _value(value) {}
87
88 markWord() = default; // Doesn't initialize _value.
89
99 void* to_pointer() const {
100 return (void*)_value;
101 }
102
103 bool operator==(const markWord& other) const {
104 return _value == other._value;
105 }
106 bool operator!=(const markWord& other) const {
107 return !operator==(other);
108 }
109
110 // Conversion
111 uintptr_t value() const { return _value; }
112
113 // Constants, in least significant bit order
114
115 // Number of bits
116 static const int lock_bits = 2;
117 static const int self_fwd_bits = 1;
118 static const int age_bits = 4;
119 static const int valhalla_reserved_bits = LP64_ONLY(4) NOT_LP64(0);
120 static const int max_hash_bits = BitsPerWord - age_bits - lock_bits - self_fwd_bits - valhalla_reserved_bits;
121 static const int hash_bits = max_hash_bits > 31 ? 31 : max_hash_bits;
122
123 // Shifts
124 static const int lock_shift = 0;
125 static const int self_fwd_shift = lock_shift + lock_bits;
126 static const int age_shift = self_fwd_shift + self_fwd_bits;
127 static const int valhalla_reserved_shift = age_shift + age_bits;
128 static const int hash_shift = valhalla_reserved_shift + valhalla_reserved_bits;
129
130 // Masks (in-place)
131 static const uintptr_t lock_mask_in_place = right_n_bits(lock_bits) << lock_shift;
132 static const uintptr_t self_fwd_bit_in_place = right_n_bits(self_fwd_bits) << self_fwd_shift;
133 static const uintptr_t age_mask_in_place = right_n_bits(age_bits) << age_shift;
134 static const uintptr_t hash_mask_in_place = right_n_bits(hash_bits) << hash_shift;
135
136 // Verify that _bit_in_place refers to constants with only one bit.
137 static_assert(is_power_of_2(self_fwd_bit_in_place));
138
139 // Masks (unshifted)
140 static const uintptr_t lock_mask = lock_mask_in_place >> lock_shift;
141 static const uintptr_t age_mask = age_mask_in_place >> age_shift;
142 static const uintptr_t hash_mask = hash_mask_in_place >> hash_shift;
143
144 #ifdef _LP64
145 // Used only with compact headers:
146 // We store the (narrow) Klass* in the bits 43 to 64.
147
148 // These are for bit-precise extraction of the narrow Klass* from the 64-bit markWord
149 static constexpr int klass_offset_in_bytes = 4;
150 static constexpr int klass_shift = hash_shift + hash_bits;
151 static constexpr int klass_shift_at_offset = klass_shift - klass_offset_in_bytes * BitsPerByte;
152 static constexpr int klass_bits = 22;
153 static constexpr uintptr_t klass_mask = right_n_bits(klass_bits);
154 static constexpr uintptr_t klass_mask_in_place = klass_mask << klass_shift;
155 #endif
156
157 static const uintptr_t locked_value = 0;
158 static const uintptr_t unlocked_value = 1;
159 static const uintptr_t monitor_value = 2;
160 static const uintptr_t marked_value = 3;
161
162 static const uintptr_t no_hash = 0 ; // no hash value assigned
163 static const uintptr_t no_hash_in_place = (uintptr_t)no_hash << hash_shift;
164 static const uintptr_t no_lock_in_place = unlocked_value;
165
166 static const uint max_age = age_mask;
167
168 // Creates a markWord with all bits set to zero.
169 static markWord zero() { return markWord(uintptr_t(0)); }
170
171 // lock accessors (note that these assume lock_shift == 0)
172 bool is_locked() const {
173 return (mask_bits(value(), lock_mask_in_place) != unlocked_value);
174 }
175 bool is_unlocked() const {
176 return (mask_bits(value(), lock_mask_in_place) == unlocked_value);
177 }
178 bool is_marked() const {
179 return (mask_bits(value(), lock_mask_in_place) == marked_value);
180 }
181
182 bool is_neutral() const { // Not locked, or marked - a "clean" neutral state
183 return (mask_bits(value(), lock_mask_in_place) == unlocked_value);
184 }
185
186 bool is_forwarded() const {
187 // Returns true for normal forwarded (0b011) and self-forwarded (0b1xx).
188 return mask_bits(value(), lock_mask_in_place | self_fwd_bit_in_place) >= static_cast<intptr_t>(marked_value);
189 }
190
191 // Should this header be preserved during GC?
192 bool must_be_preserved() const {
193 return !is_unlocked() || !has_no_hash();
194 }
195
196 // WARNING: The following routines are used EXCLUSIVELY by
197 // synchronization functions. They are not really gc safe.
198 // They must get updated if markWord layout get changed.
199 markWord set_unlocked() const {
200 return markWord(value() | unlocked_value);
201 }
202
203 bool is_fast_locked() const {
204 return (value() & lock_mask_in_place) == locked_value;
205 }
206 markWord set_fast_locked() const {
207 // Clear the lock_mask_in_place bits to set locked_value:
208 return markWord(value() & ~lock_mask_in_place);
209 }
210
211 bool has_monitor() const {
212 return ((value() & lock_mask_in_place) == monitor_value);
244 // age operations
245 markWord set_marked() { return markWord((value() & ~lock_mask_in_place) | marked_value); }
246 markWord set_unmarked() { return markWord((value() & ~lock_mask_in_place) | unlocked_value); }
247
248 uint age() const { return (uint) mask_bits(value() >> age_shift, age_mask); }
249 markWord set_age(uint v) const {
250 assert((v & ~age_mask) == 0, "shouldn't overflow age field");
251 return markWord((value() & ~age_mask_in_place) | ((v & age_mask) << age_shift));
252 }
253 markWord incr_age() const { return age() == max_age ? markWord(_value) : set_age(age() + 1); }
254
255 // hash operations
256 intptr_t hash() const {
257 return mask_bits(value() >> hash_shift, hash_mask);
258 }
259
260 bool has_no_hash() const {
261 return hash() == no_hash;
262 }
263
264 markWord copy_set_hash(intptr_t hash) const {
265 uintptr_t tmp = value() & (~hash_mask_in_place);
266 tmp |= ((hash & hash_mask) << hash_shift);
267 return markWord(tmp);
268 }
269
270 inline Klass* klass() const;
271 inline Klass* klass_or_null() const;
272 inline Klass* klass_without_asserts() const;
273 inline narrowKlass narrow_klass() const;
274 inline markWord set_narrow_klass(narrowKlass narrow_klass) const;
275
276 // Prototype mark for initialization
277 static markWord prototype() {
278 return markWord(unlocked_value);
279 }
280
281 // Debugging
282 void print_on(outputStream* st, bool print_monitor_info = true) const;
283
284 // Prepare address of oop for placement into mark
285 inline static markWord encode_pointer_as_mark(void* p) { return from_pointer(p).set_marked(); }
286
287 // Recover address of oop from encoded form used in mark
288 inline void* decode_pointer() const { return (void*)clear_lock_bits().value(); }
289
290 inline bool is_self_forwarded() const {
291 return mask_bits(value(), self_fwd_bit_in_place) != 0;
292 }
293
294 inline markWord set_self_forwarded() const {
295 return markWord(value() | self_fwd_bit_in_place);
296 }
297
298 inline markWord unset_self_forwarded() const {
299 return markWord(value() & ~self_fwd_bit_in_place);
300 }
|
49 // klass:22 hash:31 valhalla:4 age:4 self-fwd:1 lock:2
50 //
51 // - lock bits are used to describe lock states: locked/unlocked/monitor-locked
52 // and to indicate that an object has been GC marked / forwarded.
53 //
54 // [header | 00] locked locked regular object header (fast-locking in use)
55 // [header | 01] unlocked regular object header
56 // [header | 10] monitor inflated lock (UseObjectMonitorTable == true)
57 // [ptr | 10] monitor inflated lock (UseObjectMonitorTable == false, header is swapped out)
58 // [ptr | 11] marked used to mark an object (header is swapped out)
59 //
60 // - self-fwd - used by some GCs to indicate in-place forwarding.
61 //
62 // Note the position of 'self-fwd' is not by accident. When forwarding an
63 // object to a new heap position, HeapWord alignment guarantees the lower
64 // bits, including 'self-fwd' are 0. "is_self_forwarded()" will be correctly
65 // set to false. Otherwise encode_pointer_as_mark() may have 'self-fwd' set.
66 //
67 // - age - used by some GCs to track the age of objects.
68 //
69 // - valhalla - only supported on 64-bit VMs
70 //
71 // * inline types: A value class instance
72 // * flat arrays: An array with flattened value class elements
73 // * null-free arrays: An array instance without null elements
74 // * valhalla reserved: Reserved for future use
75 //
76 // Inline types cannot be locked and do not have an identity hash.
77 //
78 // - hash - contains the identity hash value: largest value is 31 bits, see
79 // os::random(). Also, 64-bit VMs require a hash value no bigger than 32
80 // bits because they will not properly generate a mask larger than that:
81 // see library_call.cpp
82 //
83 // - klass - klass identifier used when UseCompactObjectHeaders == true
84
85 class ObjectMonitor;
86 class outputStream;
87
88 class markWord {
89 private:
90 uintptr_t _value;
91
92 public:
93 explicit markWord(uintptr_t value) : _value(value) {}
94
95 markWord() = default; // Doesn't initialize _value.
96
106 void* to_pointer() const {
107 return (void*)_value;
108 }
109
110 bool operator==(const markWord& other) const {
111 return _value == other._value;
112 }
113 bool operator!=(const markWord& other) const {
114 return !operator==(other);
115 }
116
117 // Conversion
118 uintptr_t value() const { return _value; }
119
120 // Constants, in least significant bit order
121
122 // Number of bits
123 static const int lock_bits = 2;
124 static const int self_fwd_bits = 1;
125 static const int age_bits = 4;
126 static const int inline_type_bits = LP64_ONLY(1) NOT_LP64(0);
127 static const int null_free_array_bits = LP64_ONLY(1) NOT_LP64(0);
128 static const int flat_array_bits = LP64_ONLY(1) NOT_LP64(0);
129 static const int valhalla_reserved_bits = LP64_ONLY(1) NOT_LP64(0);
130 static const int max_hash_bits = BitsPerWord - age_bits - lock_bits - inline_type_bits - valhalla_reserved_bits - flat_array_bits - null_free_array_bits - self_fwd_bits;
131 static const int hash_bits = max_hash_bits > 31 ? 31 : max_hash_bits;
132
133 // Shifts
134 static const int lock_shift = 0;
135 static const int self_fwd_shift = lock_shift + lock_bits;
136 static const int age_shift = self_fwd_shift + self_fwd_bits;
137 static const int inline_type_shift = age_shift + age_bits;
138 static const int null_free_array_shift = inline_type_shift + inline_type_bits;
139 static const int flat_array_shift = null_free_array_shift + null_free_array_bits;
140 static const int valhalla_reserved_shift = flat_array_shift + flat_array_bits;
141 static const int hash_shift = valhalla_reserved_shift + valhalla_reserved_bits;
142
143 // Masks (in-place)
144 static const uintptr_t lock_mask_in_place = right_n_bits(lock_bits) << lock_shift;
145 static const uintptr_t self_fwd_bit_in_place = right_n_bits(self_fwd_bits) << self_fwd_shift;
146 static const uintptr_t age_mask_in_place = right_n_bits(age_bits) << age_shift;
147 static const uintptr_t inline_type_bit_in_place = right_n_bits(inline_type_bits) << inline_type_shift;
148 static const uintptr_t null_free_array_bit_in_place = right_n_bits(null_free_array_bits) << null_free_array_shift;
149 static const uintptr_t flat_array_bit_in_place = right_n_bits(flat_array_bits) << flat_array_shift;
150 static const uintptr_t valhalla_reserved_bit_in_place = right_n_bits(valhalla_reserved_bits) << valhalla_reserved_shift;
151 static const uintptr_t hash_mask_in_place = right_n_bits(hash_bits) << hash_shift;
152
153 // Verify that _bit_in_place refers to constants with only one bit.
154 static_assert(is_power_of_2(self_fwd_bit_in_place));
155 #ifdef _LP64
156 static_assert(is_power_of_2(inline_type_bit_in_place));
157 static_assert(is_power_of_2(null_free_array_bit_in_place));
158 static_assert(is_power_of_2(flat_array_bit_in_place));
159 static_assert(is_power_of_2(valhalla_reserved_bit_in_place));
160 #endif
161
162 // Masks (unshifted)
163 static const uintptr_t lock_mask = lock_mask_in_place >> lock_shift;
164 static const uintptr_t age_mask = age_mask_in_place >> age_shift;
165 static const uintptr_t hash_mask = hash_mask_in_place >> hash_shift;
166
167 #ifdef _LP64
168 // Used only with compact headers:
169 // We store the (narrow) Klass* in the bits 43 to 64.
170
171 // These are for bit-precise extraction of the narrow Klass* from the 64-bit markWord
172 static constexpr int klass_offset_in_bytes = 4;
173 static constexpr int klass_shift = hash_shift + hash_bits;
174 static constexpr int klass_shift_at_offset = klass_shift - klass_offset_in_bytes * BitsPerByte;
175 static constexpr int klass_bits = 22;
176 static constexpr uintptr_t klass_mask = right_n_bits(klass_bits);
177 static constexpr uintptr_t klass_mask_in_place = klass_mask << klass_shift;
178 #endif
179
180 static const uintptr_t locked_value = 0;
181 static const uintptr_t unlocked_value = 1;
182 static const uintptr_t monitor_value = 2;
183 static const uintptr_t marked_value = 3;
184
185 static const uintptr_t inline_type_pattern = inline_type_bit_in_place | unlocked_value;
186 static const uintptr_t inline_type_pattern_mask = inline_type_bit_in_place | lock_mask_in_place;
187
188 static const uintptr_t no_hash = 0 ; // no hash value assigned
189 static const uintptr_t no_hash_in_place = (uintptr_t)no_hash << hash_shift;
190 static const uintptr_t no_lock_in_place = unlocked_value;
191
192 static const uint max_age = age_mask;
193
194 // Creates a markWord with all bits set to zero.
195 static markWord zero() { return markWord(uintptr_t(0)); }
196
197 bool is_inline_type() const {
198 #ifdef _LP64 // 64 bit encodings only
199 return (mask_bits(value(), inline_type_pattern_mask) == inline_type_pattern);
200 #else
201 return false;
202 #endif
203 }
204
205 // lock accessors (note that these assume lock_shift == 0)
206 bool is_locked() const {
207 return (mask_bits(value(), lock_mask_in_place) != unlocked_value);
208 }
209 bool is_unlocked() const {
210 return (mask_bits(value(), lock_mask_in_place) == unlocked_value);
211 }
212 bool is_marked() const {
213 return (mask_bits(value(), lock_mask_in_place) == marked_value);
214 }
215
216 bool is_neutral() const { // Not locked, or marked - a "clean" neutral state
217 LP64_ONLY(assert(!is_unlocked() || mask_bits(value(), inline_type_bit_in_place) == 0,
218 "Inline types should not be used for locking. _value: " PTR_FORMAT, _value));
219 return (mask_bits(value(), lock_mask_in_place) == unlocked_value);
220 }
221
222 bool is_forwarded() const {
223 // Returns true for normal forwarded (0b011) and self-forwarded (0b1xx).
224 return mask_bits(value(), lock_mask_in_place | self_fwd_bit_in_place) >= static_cast<intptr_t>(marked_value);
225 }
226
227 // Should this header be preserved during GC?
228 bool must_be_preserved() const {
229 // The reserved bits are only guaranteed to be unset if the mark word is "unlocked"
230 LP64_ONLY(assert(!is_unlocked() || mask_bits(value(), valhalla_reserved_bit_in_place) == 0,
231 "Reserved bits should not be used. _value: " PTR_FORMAT, _value));
232 return !is_unlocked() || !has_no_hash();
233 }
234
235 // WARNING: The following routines are used EXCLUSIVELY by
236 // synchronization functions. They are not really gc safe.
237 // They must get updated if markWord layout get changed.
238 markWord set_unlocked() const {
239 return markWord(value() | unlocked_value);
240 }
241
242 bool is_fast_locked() const {
243 return (value() & lock_mask_in_place) == locked_value;
244 }
245 markWord set_fast_locked() const {
246 // Clear the lock_mask_in_place bits to set locked_value:
247 return markWord(value() & ~lock_mask_in_place);
248 }
249
250 bool has_monitor() const {
251 return ((value() & lock_mask_in_place) == monitor_value);
283 // age operations
284 markWord set_marked() { return markWord((value() & ~lock_mask_in_place) | marked_value); }
285 markWord set_unmarked() { return markWord((value() & ~lock_mask_in_place) | unlocked_value); }
286
287 uint age() const { return (uint) mask_bits(value() >> age_shift, age_mask); }
288 markWord set_age(uint v) const {
289 assert((v & ~age_mask) == 0, "shouldn't overflow age field");
290 return markWord((value() & ~age_mask_in_place) | ((v & age_mask) << age_shift));
291 }
292 markWord incr_age() const { return age() == max_age ? markWord(_value) : set_age(age() + 1); }
293
294 // hash operations
295 intptr_t hash() const {
296 return mask_bits(value() >> hash_shift, hash_mask);
297 }
298
299 bool has_no_hash() const {
300 return hash() == no_hash;
301 }
302
303 bool is_flat_array() const {
304 assert(!has_monitor_pointer(), "Bits are not valid if replaced by a monitor pointer: " PTR_FORMAT, value());
305 assert(!is_marked(), "Bits might not be valid if marked by the GC: " PTR_FORMAT, value());
306 #ifdef _LP64 // 64 bit encodings only
307 return (mask_bits(value(), flat_array_bit_in_place) != 0);
308 #else
309 return false;
310 #endif
311 }
312
313 bool is_null_free_array() const {
314 assert(!has_monitor_pointer(), "Bits are not valid if replaced by a monitor pointer: " PTR_FORMAT, value());
315 assert(!is_marked(), "Bits might not be valid if marked by the GC: " PTR_FORMAT, value());
316 #ifdef _LP64 // 64 bit encodings only
317 return (mask_bits(value(), null_free_array_bit_in_place) != 0);
318 #else
319 return false;
320 #endif
321 }
322
323 markWord copy_set_hash(intptr_t hash) const {
324 uintptr_t tmp = value() & (~hash_mask_in_place);
325 tmp |= ((hash & hash_mask) << hash_shift);
326 return markWord(tmp);
327 }
328
329 inline Klass* klass() const;
330 inline Klass* klass_or_null() const;
331 inline Klass* klass_without_asserts() const;
332 inline narrowKlass narrow_klass() const;
333 inline markWord set_narrow_klass(narrowKlass narrow_klass) const;
334
335 // Prototype marks for initialization
336
337 static markWord prototype() {
338 return markWord(unlocked_value);
339 }
340
341 static markWord inline_type_prototype() {
342 NOT_LP64(assert(false, "Should not be called in 32 bit mode"));
343 return markWord(unlocked_value | inline_type_bit_in_place);
344 }
345
346 static markWord flat_array_prototype(bool null_free) {
347 NOT_LP64(assert(false, "Should not be called in 32 bit mode"));
348 if (null_free) {
349 return markWord(unlocked_value | flat_array_bit_in_place | null_free_array_bit_in_place);
350 } else {
351 return markWord(unlocked_value | flat_array_bit_in_place);
352 }
353 }
354
355 static markWord null_free_array_prototype() {
356 NOT_LP64(assert(false, "Should not be called in 32 bit mode"));
357 return markWord(unlocked_value | null_free_array_bit_in_place);
358 }
359
360 // Debugging
361 void print_on(outputStream* st, bool print_monitor_info = true) const;
362
363 // Prepare address of oop for placement into mark
364 inline static markWord encode_pointer_as_mark(void* p) { return from_pointer(p).set_marked(); }
365
366 // Recover address of oop from encoded form used in mark
367 inline void* decode_pointer() const { return (void*)clear_lock_bits().value(); }
368
369 inline bool is_self_forwarded() const {
370 return mask_bits(value(), self_fwd_bit_in_place) != 0;
371 }
372
373 inline markWord set_self_forwarded() const {
374 return markWord(value() | self_fwd_bit_in_place);
375 }
376
377 inline markWord unset_self_forwarded() const {
378 return markWord(value() & ~self_fwd_bit_in_place);
379 }
|