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