1 /*
 2  * Copyright (c) 2022, 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_SHENANDOAHOBJECTUTILS_INLINE_HPP
26 #define SHARE_GC_SHENANDOAH_SHENANDOAHOBJECTUTILS_INLINE_HPP
27 
28 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
29 #include "gc/shenandoah/shenandoahObjectUtils.hpp"
30 #include "oops/klass.hpp"
31 #include "oops/markWord.inline.hpp"
32 #include "oops/oop.inline.hpp"
33 #include "runtime/objectMonitor.inline.hpp"
34 #include "runtime/thread.hpp"
35 
36 // This is a variant of ObjectSynchronizer::stable_mark(), which does the same thing, but also
37 // handles forwarded objects. This is intended to be used by concurrent evacuation only. No other
38 // code is supposed to observe from-space objects.
39 #ifdef _LP64
40 markWord ShenandoahObjectUtils::stable_mark(oop obj) {
41   markWord mark = obj->mark_acquire();
42 
43   if (mark.is_neutral() || mark.is_fast_locked()) {
44     return mark;
45   } else if (mark.is_marked()) {
46     // If object is already forwarded, then resolve it, and try again.
47     if (ShenandoahHeap::heap()->is_full_gc_move_in_progress()) {
48       // In these cases, we want to return the header as-is: the Klass* would not be overloaded.
49       return mark;
50     }
51     return stable_mark(cast_to_oop(mark.decode_pointer()));
52   } else {
53     assert(mark.has_monitor(), "must be inflated here");
54     ObjectMonitor* monitor = mark.monitor();
55     markWord dmw = monitor->header();
56     assert(dmw.is_neutral(), "invariant");
57     return dmw;
58   }
59 }
60 #endif
61 
62 Klass* ShenandoahObjectUtils::klass(oop obj) {
63 #ifdef _LP64
64   markWord header = stable_mark(obj);
65   assert(header.narrow_klass() != 0, "klass must not be NULL: " INTPTR_FORMAT, header.value());
66   return header.klass();
67 #else
68   return obj->klass();
69 #endif
70 }
71 
72 size_t ShenandoahObjectUtils::size(oop obj) {
73   Klass* kls = klass(obj);
74   return obj->size_given_klass(kls);
75 }
76 
77 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHOBJECTUTILS_HPP