1 /*
  2  * Copyright (c) 2000, 2021, Oracle and/or its affiliates. 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_SERIAL_MARKSWEEP_INLINE_HPP
 26 #define SHARE_GC_SERIAL_MARKSWEEP_INLINE_HPP
 27 
 28 #include "gc/serial/markSweep.hpp"
 29 
 30 #include "classfile/classLoaderData.inline.hpp"
 31 #include "classfile/javaClasses.inline.hpp"
 32 #include "code/nmethod.hpp"
 33 #include "gc/serial/serialStringDedup.hpp"
 34 #include "memory/universe.hpp"
 35 #include "oops/markWord.hpp"
 36 #include "oops/access.inline.hpp"
 37 #include "oops/compressedOops.inline.hpp"
 38 #include "oops/method.hpp"
 39 #include "oops/oop.inline.hpp"
 40 #include "utilities/align.hpp"
 41 #include "utilities/stack.inline.hpp"
 42 
 43 inline void MarkSweep::mark_object(oop obj) {
 44   if (StringDedup::is_enabled() &&
 45       java_lang_String::is_instance(obj) &&
 46       SerialStringDedup::is_candidate_from_mark(obj)) {
 47     _string_dedup_requests->add(obj);
 48   }
 49 
 50   // some marks may contain information we need to preserve so we store them away
 51   // and overwrite the mark.  We'll restore it at the end of markSweep.
 52   markWord mark = obj->mark();
 53   obj->set_mark(markWord::prototype().set_marked());
 54 
 55   if (obj->mark_must_be_preserved(mark)) {
 56     preserve_mark(obj, mark);
 57   }
 58 }
 59 
 60 template <class T> inline void MarkSweep::mark_and_push(T* p) {
 61   T heap_oop = RawAccess<>::oop_load(p);
 62   if (!CompressedOops::is_null(heap_oop)) {
 63     oop obj = CompressedOops::decode_not_null(heap_oop);
 64     if (!obj->mark().is_marked()) {
 65       mark_object(obj);
 66       _marking_stack.push(obj);
 67     }
 68   }
 69 }
 70 
 71 inline void MarkSweep::follow_klass(Klass* klass) {
 72   oop op = klass->class_loader_data()->holder_no_keepalive();
 73   MarkSweep::mark_and_push(&op);
 74 }
 75 
 76 inline void MarkSweep::follow_cld(ClassLoaderData* cld) {
 77   MarkSweep::follow_cld_closure.do_cld(cld);
 78 }
 79 
 80 template <typename T>
 81 inline void MarkAndPushClosure::do_oop_work(T* p)            { MarkSweep::mark_and_push(p); }
 82 inline void MarkAndPushClosure::do_oop(oop* p)               { do_oop_work(p); }
 83 inline void MarkAndPushClosure::do_oop(narrowOop* p)         { do_oop_work(p); }
 84 inline void MarkAndPushClosure::do_klass(Klass* k)           { MarkSweep::follow_klass(k); }
 85 inline void MarkAndPushClosure::do_cld(ClassLoaderData* cld) { MarkSweep::follow_cld(cld); }
 86 inline void MarkAndPushClosure::do_method(Method* m)         { m->record_marking_cycle(); }
 87 inline void MarkAndPushClosure::do_nmethod(nmethod* nm)      { nm->follow_nmethod(this); }
 88 
 89 template <class T> inline void MarkSweep::adjust_pointer(T* p) {
 90   T heap_oop = RawAccess<>::oop_load(p);
 91   if (!CompressedOops::is_null(heap_oop)) {
 92     oop obj = CompressedOops::decode_not_null(heap_oop);
 93     assert(Universe::heap()->is_in(obj), "should be in heap");
 94 
 95     oop new_obj = cast_to_oop(obj->mark().decode_pointer());
 96 
 97     assert(new_obj != NULL ||                      // is forwarding ptr?
 98            obj->mark() == markWord::prototype(), // not gc marked?
 99            "should be forwarded");
100 
101     if (new_obj != NULL) {
102       assert(is_object_aligned(new_obj), "oop must be aligned");
103       RawAccess<IS_NOT_NULL>::oop_store(p, new_obj);
104     }
105   }
106 }
107 
108 template <typename T>
109 void AdjustPointerClosure::do_oop_work(T* p)           { MarkSweep::adjust_pointer(p); }
110 inline void AdjustPointerClosure::do_oop(oop* p)       { do_oop_work(p); }
111 inline void AdjustPointerClosure::do_oop(narrowOop* p) { do_oop_work(p); }
112 
113 
114 inline int MarkSweep::adjust_pointers(oop obj) {
115   return obj->oop_iterate_size(&MarkSweep::adjust_pointer_closure);
116 }
117 
118 #endif // SHARE_GC_SERIAL_MARKSWEEP_INLINE_HPP