1 /*
   2  * Copyright (c) 1997, 2012, 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_IMPLEMENTATION_SHARED_MARKSWEEP_HPP
  26 #define SHARE_VM_GC_IMPLEMENTATION_SHARED_MARKSWEEP_HPP
  27 
  28 #include "gc_interface/collectedHeap.hpp"
  29 #include "memory/universe.hpp"
  30 #include "oops/markOop.hpp"
  31 #include "oops/oop.hpp"
  32 #include "runtime/timer.hpp"
  33 #include "utilities/growableArray.hpp"
  34 #include "utilities/stack.hpp"
  35 #include "utilities/taskqueue.hpp"
  36 
  37 class ReferenceProcessor;
  38 class DataLayout;
  39 
  40 // MarkSweep takes care of global mark-compact garbage collection for a
  41 // GenCollectedHeap using a four-phase pointer forwarding algorithm.  All
  42 // generations are assumed to support marking; those that can also support
  43 // compaction.
  44 //
  45 // Class unloading will only occur when a full gc is invoked.
  46 
  47 // declared at end
  48 class PreservedMark;
  49 
  50 class MarkSweep : AllStatic {
  51   //
  52   // Inline closure decls
  53   //
  54   class FollowRootClosure: public OopsInGenClosure {
  55    public:
  56     virtual void do_oop(oop* p);
  57     virtual void do_oop(narrowOop* p);
  58   };
  59 
  60   class MarkAndPushClosure: public OopClosure {
  61    public:
  62     virtual void do_oop(oop* p);
  63     virtual void do_oop(narrowOop* p);
  64   };
  65 
  66   // The one and only place to start following the classes.
  67   // Should only be applied to the ClassLoaderData klasses list.
  68   class FollowKlassClosure : public KlassClosure {
  69    public:
  70     void do_klass(Klass* klass);
  71   };
  72   class AdjustKlassClosure : public KlassClosure {
  73    public:
  74     void do_klass(Klass* klass);
  75   };
  76 
  77   class FollowStackClosure: public VoidClosure {
  78    public:
  79     virtual void do_void();
  80   };
  81 
  82   class AdjustPointerClosure: public OopsInGenClosure {
  83    public:
  84     virtual void do_oop(oop* p);
  85     virtual void do_oop(narrowOop* p);
  86   };
  87 
  88   // Used for java/lang/ref handling
  89   class IsAliveClosure: public BoolObjectClosure {
  90    public:
  91     virtual bool do_object_b(oop p);
  92   };
  93 
  94   class KeepAliveClosure: public OopClosure {
  95    protected:
  96     template <class T> void do_oop_work(T* p);
  97    public:
  98     virtual void do_oop(oop* p);
  99     virtual void do_oop(narrowOop* p);
 100   };
 101 
 102   //
 103   // Friend decls
 104   //
 105   friend class AdjustPointerClosure;
 106   friend class KeepAliveClosure;
 107   friend class VM_MarkSweep;
 108   friend void marksweep_init();
 109 
 110   //
 111   // Vars
 112   //
 113  protected:
 114   // Total invocations of a MarkSweep collection
 115   static uint _total_invocations;
 116 
 117   // Traversal stacks used during phase1
 118   static Stack<oop, mtGC>                      _marking_stack;
 119   static Stack<ObjArrayTask, mtGC>             _objarray_stack;
 120 
 121   // Space for storing/restoring mark word
 122   static Stack<markOop, mtGC>                  _preserved_mark_stack;
 123   static Stack<oop, mtGC>                      _preserved_oop_stack;
 124   static size_t                          _preserved_count;
 125   static size_t                          _preserved_count_max;
 126   static PreservedMark*                  _preserved_marks;
 127 
 128   // Reference processing (used in ...follow_contents)
 129   static ReferenceProcessor*             _ref_processor;
 130 
 131   // Non public closures
 132   static KeepAliveClosure keep_alive;
 133 
 134   // Debugging
 135   static void trace(const char* msg) PRODUCT_RETURN;
 136 
 137  public:
 138   // Public closures
 139   static IsAliveClosure       is_alive;
 140   static FollowRootClosure    follow_root_closure;
 141   static CodeBlobToOopClosure follow_code_root_closure; // => follow_root_closure
 142   static MarkAndPushClosure   mark_and_push_closure;
 143   static FollowKlassClosure   follow_klass_closure;
 144   static FollowStackClosure   follow_stack_closure;
 145   static AdjustPointerClosure adjust_pointer_closure;
 146   static AdjustKlassClosure   adjust_klass_closure;
 147 
 148   // Accessors
 149   static uint total_invocations() { return _total_invocations; }
 150 
 151   // Reference Processing
 152   static ReferenceProcessor* const ref_processor() { return _ref_processor; }
 153 
 154   // Call backs for marking
 155   static void mark_object(oop obj);
 156   // Mark pointer and follow contents.  Empty marking stack afterwards.
 157   template <class T> static inline void follow_root(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   static inline void push_objarray(oop obj, size_t index);
 163 
 164   static void follow_stack();   // Empty marking stack.
 165 
 166   static void follow_klass(Klass* klass);
 167   static void adjust_klass(Klass* klass);
 168 
 169   static void follow_class_loader(ClassLoaderData* cld);
 170   static void adjust_class_loader(ClassLoaderData* cld);
 171 
 172   static void preserve_mark(oop p, markOop mark);
 173                                 // Save the mark word so it can be restored later
 174   static void adjust_marks();   // Adjust the pointers in the preserved marks table
 175   static void restore_marks();  // Restore the marks that we saved in preserve_mark
 176 
 177   template <class T> static inline void adjust_pointer(T* p);
 178 };
 179 
 180 class PreservedMark VALUE_OBJ_CLASS_SPEC {
 181 private:
 182   oop _obj;
 183   markOop _mark;
 184 
 185 public:
 186   void init(oop obj, markOop mark) {
 187     _obj = obj;
 188     _mark = mark;
 189   }
 190 
 191   void adjust_pointer() {
 192     MarkSweep::adjust_pointer(&_obj);
 193   }
 194 
 195   void restore() {
 196     _obj->set_mark(_mark);
 197   }
 198 };
 199 
 200 #endif // SHARE_VM_GC_IMPLEMENTATION_SHARED_MARKSWEEP_HPP