1 /*
2 * Copyright (c) 2015, 2019, Red Hat, Inc. 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_SHENANDOAH_SHENANDOAHFORWARDING_INLINE_HPP
26 #define SHARE_GC_SHENANDOAH_SHENANDOAHFORWARDING_INLINE_HPP
27
28 #include "gc/shenandoah/shenandoahForwarding.hpp"
29
30 #include "gc/shenandoah/shenandoahAsserts.hpp"
31 #include "oops/markWord.hpp"
32 #include "runtime/javaThread.hpp"
33
34 inline oop ShenandoahForwarding::get_forwardee_raw(oop obj) {
35 shenandoah_assert_in_heap_bounds(nullptr, obj);
36 return get_forwardee_raw_unchecked(obj);
37 }
38
39 inline oop ShenandoahForwarding::get_forwardee_raw_unchecked(oop obj) {
40 // JVMTI and JFR code use mark words for marking objects for their needs.
41 // On this path, we can encounter the "marked" object, but with null
42 // fwdptr. That object is still not forwarded, and we need to return
43 // the object itself.
44 markWord mark = obj->mark();
45 if (mark.is_marked()) {
46 HeapWord* fwdptr = (HeapWord*) mark.clear_lock_bits().to_pointer();
47 if (fwdptr != nullptr) {
48 return cast_to_oop(fwdptr);
49 }
50 }
51 // Self-forwarded (evacuation failure): the object stays put; the
52 // self-fwd bit is set alongside normal lock bits.
53 return obj;
54 }
55
56 inline oop ShenandoahForwarding::get_forwardee_mutator(oop obj) {
57 // Same as above, but mutator thread cannot ever see null forwardee.
58 shenandoah_assert_correct(nullptr, obj);
59 assert(Thread::current()->is_Java_thread(), "Must be a mutator thread");
60
61 markWord mark = obj->mark();
62 if (mark.is_marked()) {
63 HeapWord* fwdptr = (HeapWord*) mark.clear_lock_bits().to_pointer();
64 assert(fwdptr != nullptr, "Forwarding pointer is never null here");
65 return cast_to_oop(fwdptr);
66 }
67 // Self-forwarded or not forwarded: return the object itself.
68 return obj;
69 }
70
71 inline oop ShenandoahForwarding::get_forwardee(oop obj) {
72 shenandoah_assert_correct(nullptr, obj);
73 return get_forwardee_raw_unchecked(obj);
74 }
75
76 inline bool ShenandoahForwarding::is_forwarded(oop obj) {
77 return obj->mark().is_forwarded();
78 }
79
80 inline bool ShenandoahForwarding::is_self_forwarded(oop obj) {
81 return obj->mark().is_self_forwarded();
82 }
83
84 inline oop ShenandoahForwarding::try_update_forwardee(oop obj, oop update) {
85 markWord old_mark = obj->mark();
86 if (old_mark.is_marked()) {
87 return cast_to_oop(old_mark.clear_lock_bits().to_pointer());
88 }
89 if (old_mark.is_self_forwarded()) {
90 // Another thread lost the evacuation race; the object stays put.
91 return obj;
92 }
93
94 markWord new_mark = markWord::encode_pointer_as_mark(update);
95 markWord prev_mark = obj->cas_set_mark(new_mark, old_mark, memory_order_conservative);
96 if (prev_mark == old_mark) {
97 return update;
98 }
99 // Concurrent writers on a cset object's mark can only be other evacuation
100 // threads installing forwarding (real or self). Mutators cannot reach the
101 // mark of a not-yet-forwarded cset object: LRB + stack watermark barriers
102 // redirect all reference uses before a Java-level operation can touch it.
103 // So the only possible failure modes are a regular forwardee (marked) or
104 // a self-forward (possibly with mutator lock/hash mods layered on top
105 // after the self-forward became visible).
106 if (prev_mark.is_marked()) {
107 return cast_to_oop(prev_mark.clear_lock_bits().to_pointer());
108 }
109 assert(prev_mark.is_self_forwarded(),
110 "concurrent writers on cset objects must install forwarding: prev=" INTPTR_FORMAT,
111 prev_mark.value());
112 return obj;
113 }
114
115 inline oop ShenandoahForwarding::try_forward_to_self(oop obj, markWord old_mark) {
116 assert(!old_mark.is_forwarded(),
117 "caller must pass a non-forwarded mark: old=" INTPTR_FORMAT, old_mark.value());
118 markWord new_mark = old_mark.set_self_forwarded();
119 markWord prev_mark = obj->cas_set_mark(new_mark, old_mark, memory_order_conservative);
120 if (prev_mark == old_mark) {
121 // We installed the self-forward.
122 return nullptr;
123 }
124 // Same invariant as in try_update_forwardee: the only races on a
125 // cset object's mark come from other evac threads installing forwarding.
126 if (prev_mark.is_marked()) {
127 return cast_to_oop(prev_mark.clear_lock_bits().to_pointer());
128 }
129 assert(prev_mark.is_self_forwarded(),
130 "concurrent writers on cset objects must install forwarding: prev=" INTPTR_FORMAT,
131 prev_mark.value());
132 return obj;
133 }
134
135 inline Klass* ShenandoahForwarding::klass(oop obj) {
136 if (UseCompactObjectHeaders) {
137 markWord mark = obj->mark();
138 if (mark.is_marked()) {
139 oop fwd = cast_to_oop(mark.clear_lock_bits().to_pointer());
140 mark = fwd->mark();
141 }
142 return mark.klass();
143 } else {
144 return obj->klass();
145 }
146 }
147
148 inline size_t ShenandoahForwarding::size(oop obj) {
149 return obj->size_given_klass(klass(obj));
150 }
151
152 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHFORWARDING_INLINE_HPP