1 /* 2 * Copyright (c) 1997, 2019, 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_HPP 26 #define SHARE_GC_SERIAL_MARKSWEEP_HPP 27 28 #include "gc/shared/collectedHeap.hpp" 29 #include "gc/shared/genOopClosures.hpp" 30 #include "gc/shared/stringdedup/stringDedup.hpp" 31 #include "gc/shared/taskqueue.hpp" 32 #include "memory/iterator.hpp" 33 #include "oops/markWord.hpp" 34 #include "oops/oop.hpp" 35 #include "runtime/timer.hpp" 36 #include "utilities/growableArray.hpp" 37 #include "utilities/stack.hpp" 38 39 class ReferenceProcessor; 40 class DataLayout; 41 class SerialOldTracer; 42 class STWGCTimer; 43 44 // MarkSweep takes care of global mark-compact garbage collection for a 45 // GenCollectedHeap using a four-phase pointer forwarding algorithm. All 46 // generations are assumed to support marking; those that can also support 47 // compaction. 48 // 49 // Class unloading will only occur when a full gc is invoked. 50 51 // declared at end 52 class PreservedMark; 53 class MarkAndPushClosure; 54 class AdjustPointerClosure; 55 56 class MarkSweep : AllStatic { 57 // 58 // Inline closure decls 59 // 60 class FollowRootClosure: public BasicOopIterateClosure { 61 public: 62 virtual void do_oop(oop* p); 63 virtual void do_oop(narrowOop* p); 64 }; 65 66 class FollowStackClosure: public VoidClosure { 67 public: 68 virtual void do_void(); 69 }; 70 71 // Used for java/lang/ref handling 72 class IsAliveClosure: public BoolObjectClosure { 73 public: 74 virtual bool do_object_b(oop p); 75 }; 76 77 class KeepAliveClosure: public OopClosure { 78 protected: 79 template <class T> void do_oop_work(T* p); 80 public: 81 virtual void do_oop(oop* p); 82 virtual void do_oop(narrowOop* p); 83 }; 84 85 // 86 // Friend decls 87 // 88 friend class AdjustPointerClosure; 89 friend class KeepAliveClosure; 90 friend class VM_MarkSweep; 91 92 // 93 // Vars 94 // 95 protected: 96 // Total invocations of a MarkSweep collection 97 static uint _total_invocations; 98 99 // Traversal stacks used during phase1 100 static Stack<oop, mtGC> _marking_stack; 101 static Stack<ObjArrayTask, mtGC> _objarray_stack; 102 103 // Space for storing/restoring mark word 104 static Stack<PreservedMark, mtGC> _preserved_overflow_stack; 105 static size_t _preserved_count; 106 static size_t _preserved_count_max; 107 static PreservedMark* _preserved_marks; 108 109 // Reference processing (used in ...follow_contents) 110 static ReferenceProcessor* _ref_processor; 111 112 static STWGCTimer* _gc_timer; 113 static SerialOldTracer* _gc_tracer; 114 115 static StringDedup::Requests* _string_dedup_requests; 116 117 // Non public closures 118 static KeepAliveClosure keep_alive; 119 120 public: 121 static void initialize(); 122 123 // Public closures 124 static IsAliveClosure is_alive; 125 static FollowRootClosure follow_root_closure; 126 static MarkAndPushClosure mark_and_push_closure; 127 static FollowStackClosure follow_stack_closure; 128 static CLDToOopClosure follow_cld_closure; 129 static AdjustPointerClosure adjust_pointer_closure; 130 static CLDToOopClosure adjust_cld_closure; 131 132 // Accessors 133 static uint total_invocations() { return _total_invocations; } 134 135 // Reference Processing 136 static ReferenceProcessor* const ref_processor() { return _ref_processor; } 137 static void set_ref_processor(ReferenceProcessor* rp); 138 139 static STWGCTimer* gc_timer() { return _gc_timer; } 140 static SerialOldTracer* gc_tracer() { return _gc_tracer; } 141 142 static void preserve_mark(oop p, markWord mark); 143 // Save the mark word so it can be restored later 144 static void adjust_marks(); // Adjust the pointers in the preserved marks table 145 static void restore_marks(); // Restore the marks that we saved in preserve_mark 146 147 static size_t adjust_pointers(oop obj); 148 149 static void follow_stack(); // Empty marking stack. 150 151 static void follow_klass(Klass* klass); 152 153 static void follow_cld(ClassLoaderData* cld); 154 155 template <class T> static inline void adjust_pointer(T* p); 156 157 // Check mark and maybe push on marking stack 158 template <class T> static void mark_and_push(T* p); 159 160 private: 161 // Call backs for marking 162 static void mark_object(oop obj); 163 // Mark pointer and follow contents. Empty marking stack afterwards. 164 template <class T> static inline void follow_root(T* p); 165 166 static inline void push_objarray(oop obj, size_t index); 167 168 static void follow_object(oop obj); 169 170 static void follow_array(objArrayOop array); 171 172 static void follow_array_chunk(objArrayOop array, int index); 173 }; 174 175 class MarkAndPushClosure: public OopIterateClosure { 176 public: 177 template <typename T> void do_oop_work(T* p); 178 virtual void do_oop(oop* p); 179 virtual void do_oop(narrowOop* p); 180 181 virtual bool do_metadata() { return true; } 182 virtual void do_klass(Klass* k); 183 virtual void do_cld(ClassLoaderData* cld); 184 185 void set_ref_discoverer(ReferenceDiscoverer* rd) { 186 set_ref_discoverer_internal(rd); 187 } 188 }; 189 190 class AdjustPointerClosure: public BasicOopIterateClosure { 191 public: 192 template <typename T> void do_oop_work(T* p); 193 virtual void do_oop(oop* p); 194 virtual void do_oop(narrowOop* p); 195 virtual ReferenceIterationMode reference_iteration_mode() { return DO_FIELDS; } 196 }; 197 198 class PreservedMark { 199 private: 200 oop _obj; 201 markWord _mark; 202 203 public: 204 PreservedMark(oop obj, markWord mark) : _obj(obj), _mark(mark) {} 205 void adjust_pointer(); 206 void restore(); 207 }; 208 209 #endif // SHARE_GC_SERIAL_MARKSWEEP_HPP