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