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