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 "cppstdlib/type_traits.hpp"
29 #include "metaprogramming/primitiveConversions.hpp"
30 #include "oops/compressedKlass.hpp"
31 #include "oops/oopsHierarchy.hpp"
32 #include "runtime/globals.hpp"
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 self-fwd:1 lock:2 (normal object)
41 //
42 // 64 bits:
43 // --------
44 // unused:22 hash:31 -->| unused_gap:4 age:4 self-fwd:1 lock:2 (normal object)
45 //
46 // 64 bits (with compact headers):
47 // -------------------------------
48 // klass:22 hash:31 -->| unused_gap:4 age:4 self-fwd:1 lock:2 (normal object)
49 //
50 // - hash contains the identity hash value: largest value is
51 // 31 bits, see os::random(). Also, 64-bit vm's require
52 // a hash value no bigger than 32 bits because they will not
53 // properly generate a mask larger than that: see library_call.cpp
54 //
55 // - the two lock bits are used to describe three states: locked/unlocked and monitor.
56 //
57 // [ptr | 00] locked ptr points to real header on stack (stack-locking in use)
58 // [header | 00] locked locked regular object header (fast-locking in use)
59 // [header | 01] unlocked regular object header
60 // [ptr | 10] monitor inflated lock (header is swapped out, UseObjectMonitorTable == false)
61 // [header | 10] monitor inflated lock (UseObjectMonitorTable == true)
62 // [ptr | 11] marked used to mark an object
63
64 class BasicLock;
65 class ObjectMonitor;
66 class JavaThread;
67 class outputStream;
68
69 class markWord {
70 private:
71 uintptr_t _value;
72
73 public:
74 explicit markWord(uintptr_t value) : _value(value) {}
75
76 markWord() = default; // Doesn't initialize _value.
77
78 // It is critical for performance that this class be trivially
79 // destructable, copyable, and assignable.
80 ~markWord() = default;
81 markWord(const markWord&) = default;
82 markWord& operator=(const markWord&) = default;
83
84 static markWord from_pointer(void* ptr) {
85 return markWord((uintptr_t)ptr);
86 }
87 void* to_pointer() const {
88 return (void*)_value;
89 }
90
91 bool operator==(const markWord& other) const {
92 return _value == other._value;
93 }
94 bool operator!=(const markWord& other) const {
95 return !operator==(other);
96 }
97
98 // Conversion
99 uintptr_t value() const { return _value; }
100
101 // Constants
102 static const int age_bits = 4;
103 static const int lock_bits = 2;
104 static const int self_fwd_bits = 1;
105 static const int max_hash_bits = BitsPerWord - age_bits - lock_bits - self_fwd_bits;
106 static const int hash_bits = max_hash_bits > 31 ? 31 : max_hash_bits;
107 static const int unused_gap_bits = LP64_ONLY(4) NOT_LP64(0); // Reserved for Valhalla.
108
109 static const int lock_shift = 0;
110 static const int self_fwd_shift = lock_shift + lock_bits;
111 static const int age_shift = self_fwd_shift + self_fwd_bits;
112 static const int hash_shift = age_shift + age_bits + unused_gap_bits;
113
114 static const uintptr_t lock_mask = right_n_bits(lock_bits);
115 static const uintptr_t lock_mask_in_place = lock_mask << lock_shift;
116 static const uintptr_t self_fwd_mask = right_n_bits(self_fwd_bits);
117 static const uintptr_t self_fwd_mask_in_place = self_fwd_mask << self_fwd_shift;
118 static const uintptr_t age_mask = right_n_bits(age_bits);
119 static const uintptr_t age_mask_in_place = age_mask << age_shift;
120 static const uintptr_t hash_mask = right_n_bits(hash_bits);
121 static const uintptr_t hash_mask_in_place = hash_mask << hash_shift;
122
123 #ifdef _LP64
124 // Used only with compact headers:
125 // We store the (narrow) Klass* in the bits 43 to 64.
126
127 // These are for bit-precise extraction of the narrow Klass* from the 64-bit Markword
128 static constexpr int klass_offset_in_bytes = 4;
129 static constexpr int klass_shift = hash_shift + hash_bits;
130 static constexpr int klass_shift_at_offset = klass_shift - klass_offset_in_bytes * BitsPerByte;
131 static constexpr int klass_bits = 22;
132 static constexpr uintptr_t klass_mask = right_n_bits(klass_bits);
133 static constexpr uintptr_t klass_mask_in_place = klass_mask << klass_shift;
134 #endif
135
136
137 static const uintptr_t locked_value = 0;
138 static const uintptr_t unlocked_value = 1;
139 static const uintptr_t monitor_value = 2;
140 static const uintptr_t marked_value = 3;
141
142 static const uintptr_t no_hash = 0 ; // no hash value assigned
143 static const uintptr_t no_hash_in_place = (uintptr_t)no_hash << hash_shift;
144 static const uintptr_t no_lock_in_place = unlocked_value;
145
146 static const uint max_age = age_mask;
147
148 // Creates a markWord with all bits set to zero.
149 static markWord zero() { return markWord(uintptr_t(0)); }
150
151 // lock accessors (note that these assume lock_shift == 0)
152 bool is_locked() const {
153 return (mask_bits(value(), lock_mask_in_place) != unlocked_value);
154 }
155 bool is_unlocked() const {
156 return (mask_bits(value(), lock_mask_in_place) == unlocked_value);
157 }
158 bool is_marked() const {
159 return (mask_bits(value(), lock_mask_in_place) == marked_value);
160 }
161 bool is_forwarded() const {
162 // Returns true for normal forwarded (0b011) and self-forwarded (0b1xx).
163 return mask_bits(value(), lock_mask_in_place | self_fwd_mask_in_place) >= static_cast<intptr_t>(marked_value);
164 }
165 bool is_neutral() const { // Not locked, or marked - a "clean" neutral state
166 return (mask_bits(value(), lock_mask_in_place) == unlocked_value);
167 }
168
169 // Should this header be preserved during GC?
170 bool must_be_preserved() const {
171 return (!is_unlocked() || !has_no_hash());
172 }
173
174 // WARNING: The following routines are used EXCLUSIVELY by
175 // synchronization functions. They are not really gc safe.
176 // They must get updated if markWord layout get changed.
177 markWord set_unlocked() const {
178 return markWord(value() | unlocked_value);
179 }
180
181 bool is_fast_locked() const {
182 return (value() & lock_mask_in_place) == locked_value;
183 }
184 markWord set_fast_locked() const {
185 // Clear the lock_mask_in_place bits to set locked_value:
186 return markWord(value() & ~lock_mask_in_place);
187 }
188
189 bool has_monitor() const {
190 return ((value() & lock_mask_in_place) == monitor_value);
191 }
218 // age operations
219 markWord set_marked() { return markWord((value() & ~lock_mask_in_place) | marked_value); }
220 markWord set_unmarked() { return markWord((value() & ~lock_mask_in_place) | unlocked_value); }
221
222 uint age() const { return (uint) mask_bits(value() >> age_shift, age_mask); }
223 markWord set_age(uint v) const {
224 assert((v & ~age_mask) == 0, "shouldn't overflow age field");
225 return markWord((value() & ~age_mask_in_place) | ((v & age_mask) << age_shift));
226 }
227 markWord incr_age() const { return age() == max_age ? markWord(_value) : set_age(age() + 1); }
228
229 // hash operations
230 intptr_t hash() const {
231 return mask_bits(value() >> hash_shift, hash_mask);
232 }
233
234 bool has_no_hash() const {
235 return hash() == no_hash;
236 }
237
238 markWord copy_set_hash(intptr_t hash) const {
239 uintptr_t tmp = value() & (~hash_mask_in_place);
240 tmp |= ((hash & hash_mask) << hash_shift);
241 return markWord(tmp);
242 }
243
244 inline Klass* klass() const;
245 inline Klass* klass_or_null() const;
246 inline Klass* klass_without_asserts() const;
247 inline narrowKlass narrow_klass() const;
248 inline markWord set_narrow_klass(narrowKlass narrow_klass) const;
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() const { return (void*)clear_lock_bits().value(); }
263
264 inline bool is_self_forwarded() const {
265 return mask_bits(value(), self_fwd_mask_in_place) != 0;
266 }
267
268 inline markWord set_self_forwarded() const {
269 return markWord(value() | self_fwd_mask_in_place);
270 }
271
272 inline markWord unset_self_forwarded() const {
273 return markWord(value() & ~self_fwd_mask_in_place);
274 }
275
276 inline oop forwardee() const {
277 return cast_to_oop(decode_pointer());
278 }
279 };
280
281 // Support atomic operations.
282 template<>
|
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 "cppstdlib/type_traits.hpp"
29 #include "metaprogramming/primitiveConversions.hpp"
30 #include "oops/compressedKlass.hpp"
31 #include "oops/oopsHierarchy.hpp"
32 #include "runtime/globals.hpp"
33 #include "utilities/vmEnums.hpp"
34
35 // The markWord describes the header of an object.
36 //
37 // Bit-format of an object header (most significant first, big endian layout below):
38 //
39 // 32 bits:
40 // --------
41 // hash:25 ------------>| age:4 self-fwd:1 lock:2 (normal object)
42 //
43 // 64 bits:
44 // --------
45 // unused:22 hash:31 -->| valhalla:4 age:4 self-fwd:1 lock:2 (normal object)
46 //
47 // 64 bits (with compact headers):
48 // -------------------------------
49 // klass:22 hash:31 -->| valhalla:4 age:4 self-fwd:1 lock:2 (normal object)
50 //
51 // - hash contains the identity hash value: largest value is
52 // 31 bits, see os::random(). Also, 64-bit vm's require
53 // a hash value no bigger than 32 bits because they will not
54 // properly generate a mask larger than that: see library_call.cpp
55 //
56 // - the two lock bits are used to describe three states: locked/unlocked and monitor.
57 //
58 // [ptr | 00] locked ptr points to real header on stack (stack-locking in use)
59 // [header | 00] locked locked regular object header (fast-locking in use)
60 // [header | 01] unlocked regular object header
61 // [ptr | 10] monitor inflated lock (header is swapped out, UseObjectMonitorTable == false)
62 // [header | 10] monitor inflated lock (UseObjectMonitorTable == true)
63 // [ptr | 11] marked used to mark an object
64 //
65 // VALHALLA EXTENSIONS:
66 //
67 // N.B.: 32 bit mode is not supported, this section assumes 64 bit systems.
68 //
69 // Project Valhalla uses markWord bits to denote the following oops (listed least to most significant):
70 // * inline types: have alternative bytecode behavior, e.g. can not be locked
71 // * flat arrays: load/decode of klass layout helper is expensive for aaload
72 // * "null free" arrays: load/decode of klass layout helper again for aaload
73 // * inline type: "larval state": mutable state, but only during object init, observable
74 // by only by a single thread (generally do not mutate markWord)
75 //
76 // Inline types cannot be locked, monitored or inflating.
77 //
78 // Note the position of 'self-fwd' is not by accident. When forwarding an
79 // object to a new heap position, HeapWord alignment guarantees the lower
80 // bits, including 'self-fwd' are 0. "is_self_forwarded()" will be correctly
81 // set to false. Otherwise encode_pointer_as_mark() may have 'self-fwd' set.
82
83 class BasicLock;
84 class ObjectMonitor;
85 class JavaThread;
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
97 // It is critical for performance that this class be trivially
98 // destructable, copyable, and assignable.
99 ~markWord() = default;
100 markWord(const markWord&) = default;
101 markWord& operator=(const markWord&) = default;
102
103 static markWord from_pointer(void* ptr) {
104 return markWord((uintptr_t)ptr);
105 }
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 static const int lock_bits = 2;
122 static const int self_fwd_bits = 1;
123 // instance state
124 static const int age_bits = 4;
125 // prototype header bits (fast path instead of klass layout_helper)
126 static const int inline_type_bits = 1;
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 larval_bits = 1;
130 static const int max_hash_bits = BitsPerWord - age_bits - lock_bits - inline_type_bits - larval_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 static const int lock_shift = 0;
134 static const int self_fwd_shift = lock_shift + lock_bits;
135 static const int age_shift = self_fwd_shift + self_fwd_bits;
136 static const int inline_type_shift = age_shift + age_bits;
137 static const int null_free_array_shift = inline_type_shift + inline_type_bits;
138 static const int flat_array_shift = null_free_array_shift + null_free_array_bits;
139 static const int larval_shift = flat_array_shift + flat_array_bits;
140 static const int hash_shift = larval_shift + larval_bits;
141
142 static const uintptr_t lock_mask = right_n_bits(lock_bits);
143 static const uintptr_t lock_mask_in_place = lock_mask << lock_shift;
144 static const uintptr_t self_fwd_mask = right_n_bits(self_fwd_bits);
145 static const uintptr_t self_fwd_mask_in_place = self_fwd_mask << self_fwd_shift;
146 static const uintptr_t inline_type_bit_in_place = right_n_bits(inline_type_bits) << inline_type_shift;
147 static const uintptr_t inline_type_mask_in_place = inline_type_bit_in_place + lock_mask;
148 static const uintptr_t null_free_array_mask = right_n_bits(null_free_array_bits);
149 static const uintptr_t null_free_array_mask_in_place = (null_free_array_mask << null_free_array_shift) | lock_mask_in_place;
150 static const uintptr_t null_free_array_bit_in_place = (right_n_bits(null_free_array_bits) << null_free_array_shift);
151 static const uintptr_t flat_array_mask = right_n_bits(flat_array_bits);
152 static const uintptr_t flat_array_mask_in_place = (flat_array_mask << flat_array_shift) | null_free_array_mask_in_place | lock_mask_in_place;
153 static const uintptr_t flat_array_bit_in_place = right_n_bits(flat_array_bits) << flat_array_shift;
154 static const uintptr_t age_mask = right_n_bits(age_bits);
155 static const uintptr_t age_mask_in_place = age_mask << age_shift;
156
157 static const uintptr_t larval_mask = right_n_bits(larval_bits);
158 static const uintptr_t larval_mask_in_place = (larval_mask << larval_shift) | inline_type_mask_in_place;
159 static const uintptr_t larval_bit_in_place = right_n_bits(larval_bits) << larval_shift;
160
161 static const uintptr_t hash_mask = right_n_bits(hash_bits);
162 static const uintptr_t hash_mask_in_place = hash_mask << hash_shift;
163
164 #ifdef _LP64
165 // Used only with compact headers:
166 // We store the (narrow) Klass* in the bits 43 to 64.
167
168 // These are for bit-precise extraction of the narrow Klass* from the 64-bit Markword
169 static constexpr int klass_offset_in_bytes = 4;
170 static constexpr int klass_shift = hash_shift + hash_bits;
171 static constexpr int klass_shift_at_offset = klass_shift - klass_offset_in_bytes * BitsPerByte;
172 static constexpr int klass_bits = 22;
173 static constexpr uintptr_t klass_mask = right_n_bits(klass_bits);
174 static constexpr uintptr_t klass_mask_in_place = klass_mask << klass_shift;
175 #endif
176
177
178 static const uintptr_t locked_value = 0;
179 static const uintptr_t unlocked_value = 1;
180 static const uintptr_t monitor_value = 2;
181 static const uintptr_t marked_value = 3;
182
183 static const uintptr_t inline_type_pattern = inline_type_bit_in_place | unlocked_value;
184 static const uintptr_t null_free_array_pattern = null_free_array_bit_in_place | unlocked_value;
185 static const uintptr_t null_free_flat_array_pattern = flat_array_bit_in_place | null_free_array_pattern;
186 static const uintptr_t nullable_flat_array_pattern = flat_array_bit_in_place | unlocked_value;
187
188 static const uintptr_t larval_pattern = larval_bit_in_place | inline_type_pattern;
189
190 static const uintptr_t no_hash = 0 ; // no hash value assigned
191 static const uintptr_t no_hash_in_place = (uintptr_t)no_hash << hash_shift;
192 static const uintptr_t no_lock_in_place = unlocked_value;
193
194 static const uint max_age = age_mask;
195
196 // Creates a markWord with all bits set to zero.
197 static markWord zero() { return markWord(uintptr_t(0)); }
198
199 bool is_inline_type() const {
200 return (mask_bits(value(), inline_type_mask_in_place) == inline_type_pattern);
201 }
202
203 // lock accessors (note that these assume lock_shift == 0)
204 bool is_locked() const {
205 return (mask_bits(value(), lock_mask_in_place) != unlocked_value);
206 }
207 bool is_unlocked() const {
208 return (mask_bits(value(), lock_mask_in_place) == unlocked_value);
209 }
210 bool is_marked() const {
211 return (mask_bits(value(), lock_mask_in_place) == marked_value);
212 }
213
214 // is unlocked and not an inline type (which cannot be involved in locking, displacement or inflation)
215 // i.e. test both lock bits and the inline type bit together
216 bool is_neutral() const { // Not locked, or marked - a "clean" neutral state
217 return (mask_bits(value(), inline_type_mask_in_place) == unlocked_value);
218 }
219
220 bool is_forwarded() const {
221 // Returns true for normal forwarded (0b011) and self-forwarded (0b1xx).
222 return mask_bits(value(), lock_mask_in_place | self_fwd_mask_in_place) >= static_cast<intptr_t>(marked_value);
223 }
224
225 // Should this header be preserved during GC?
226 bool must_be_preserved() const {
227 return (!is_unlocked() || !has_no_hash() || is_larval_state());
228 }
229
230 // WARNING: The following routines are used EXCLUSIVELY by
231 // synchronization functions. They are not really gc safe.
232 // They must get updated if markWord layout get changed.
233 markWord set_unlocked() const {
234 return markWord(value() | unlocked_value);
235 }
236
237 bool is_fast_locked() const {
238 return (value() & lock_mask_in_place) == locked_value;
239 }
240 markWord set_fast_locked() const {
241 // Clear the lock_mask_in_place bits to set locked_value:
242 return markWord(value() & ~lock_mask_in_place);
243 }
244
245 bool has_monitor() const {
246 return ((value() & lock_mask_in_place) == monitor_value);
247 }
274 // age operations
275 markWord set_marked() { return markWord((value() & ~lock_mask_in_place) | marked_value); }
276 markWord set_unmarked() { return markWord((value() & ~lock_mask_in_place) | unlocked_value); }
277
278 uint age() const { return (uint) mask_bits(value() >> age_shift, age_mask); }
279 markWord set_age(uint v) const {
280 assert((v & ~age_mask) == 0, "shouldn't overflow age field");
281 return markWord((value() & ~age_mask_in_place) | ((v & age_mask) << age_shift));
282 }
283 markWord incr_age() const { return age() == max_age ? markWord(_value) : set_age(age() + 1); }
284
285 // hash operations
286 intptr_t hash() const {
287 return mask_bits(value() >> hash_shift, hash_mask);
288 }
289
290 bool has_no_hash() const {
291 return hash() == no_hash;
292 }
293
294 // private buffered value operations
295 markWord enter_larval_state() const {
296 return markWord(value() | larval_bit_in_place);
297 }
298 markWord exit_larval_state() const {
299 return markWord(value() & ~larval_bit_in_place);
300 }
301 bool is_larval_state() const {
302 return (mask_bits(value(), larval_mask_in_place) == larval_pattern);
303 }
304
305 bool is_flat_array() const {
306 #ifdef _LP64 // 64 bit encodings only
307 return (mask_bits(value(), flat_array_mask_in_place) == null_free_flat_array_pattern)
308 || (mask_bits(value(), flat_array_mask_in_place) == nullable_flat_array_pattern);
309 #else
310 return false;
311 #endif
312 }
313
314 bool is_null_free_array() const {
315 #ifdef _LP64 // 64 bit encodings only
316 return (mask_bits(value(), null_free_array_mask_in_place) == null_free_array_pattern);
317 #else
318 return false;
319 #endif
320 }
321
322 markWord copy_set_hash(intptr_t hash) const {
323 uintptr_t tmp = value() & (~hash_mask_in_place);
324 tmp |= ((hash & hash_mask) << hash_shift);
325 return markWord(tmp);
326 }
327
328 inline Klass* klass() const;
329 inline Klass* klass_or_null() const;
330 inline Klass* klass_without_asserts() const;
331 inline narrowKlass narrow_klass() const;
332 inline markWord set_narrow_klass(narrowKlass narrow_klass) const;
333
334 // Prototype mark for initialization
335 static markWord prototype() {
336 return markWord( no_hash_in_place | no_lock_in_place );
337 }
338
339 static markWord inline_type_prototype() {
340 return markWord(inline_type_pattern);
341 }
342
343 #ifdef _LP64 // 64 bit encodings only
344 static markWord flat_array_prototype(bool null_free);
345
346 static markWord null_free_array_prototype() {
347 return markWord(null_free_array_pattern);
348 }
349 #endif
350
351 // Debugging
352 void print_on(outputStream* st, bool print_monitor_info = true) const;
353
354 // Prepare address of oop for placement into mark
355 inline static markWord encode_pointer_as_mark(void* p) { return from_pointer(p).set_marked(); }
356
357 inline void* decode_pointer() const {
358 return (void*) (clear_lock_bits().value());
359 }
360
361 inline bool is_self_forwarded() const {
362 return mask_bits(value(), self_fwd_mask_in_place) != 0;
363 }
364
365 inline markWord set_self_forwarded() const {
366 return markWord(value() | self_fwd_mask_in_place);
367 }
368
369 inline markWord unset_self_forwarded() const {
370 return markWord(value() & ~self_fwd_mask_in_place);
371 }
372
373 inline oop forwardee() const {
374 return cast_to_oop(decode_pointer());
375 }
376 };
377
378 // Support atomic operations.
379 template<>
|