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