markSweep.hpp 6.0 KB
Newer Older
D
duke 已提交
1
/*
S
sla 已提交
2
 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
D
duke 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
19 20 21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
D
duke 已提交
22 23 24
 *
 */

25 26 27 28 29 30 31 32 33 34 35 36
#ifndef SHARE_VM_GC_IMPLEMENTATION_SHARED_MARKSWEEP_HPP
#define SHARE_VM_GC_IMPLEMENTATION_SHARED_MARKSWEEP_HPP

#include "gc_interface/collectedHeap.hpp"
#include "memory/universe.hpp"
#include "oops/markOop.hpp"
#include "oops/oop.hpp"
#include "runtime/timer.hpp"
#include "utilities/growableArray.hpp"
#include "utilities/stack.hpp"
#include "utilities/taskqueue.hpp"

D
duke 已提交
37
class ReferenceProcessor;
Y
ysr 已提交
38
class DataLayout;
S
sla 已提交
39 40
class SerialOldTracer;
class STWGCTimer;
D
duke 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53

// MarkSweep takes care of global mark-compact garbage collection for a
// GenCollectedHeap using a four-phase pointer forwarding algorithm.  All
// generations are assumed to support marking; those that can also support
// compaction.
//
// Class unloading will only occur when a full gc is invoked.

// declared at end
class PreservedMark;

class MarkSweep : AllStatic {
  //
54
  // Inline closure decls
D
duke 已提交
55
  //
56
  class FollowRootClosure: public OopsInGenClosure {
D
duke 已提交
57
   public:
58 59
    virtual void do_oop(oop* p);
    virtual void do_oop(narrowOop* p);
D
duke 已提交
60 61 62 63
  };

  class MarkAndPushClosure: public OopClosure {
   public:
64 65
    virtual void do_oop(oop* p);
    virtual void do_oop(narrowOop* p);
66 67 68 69 70 71 72 73 74 75 76
  };

  // The one and only place to start following the classes.
  // Should only be applied to the ClassLoaderData klasses list.
  class FollowKlassClosure : public KlassClosure {
   public:
    void do_klass(Klass* klass);
  };
  class AdjustKlassClosure : public KlassClosure {
   public:
    void do_klass(Klass* klass);
D
duke 已提交
77 78 79 80
  };

  class FollowStackClosure: public VoidClosure {
   public:
81
    virtual void do_void();
D
duke 已提交
82 83 84 85
  };

  class AdjustPointerClosure: public OopsInGenClosure {
   public:
86 87
    virtual void do_oop(oop* p);
    virtual void do_oop(narrowOop* p);
D
duke 已提交
88 89 90 91 92
  };

  // Used for java/lang/ref handling
  class IsAliveClosure: public BoolObjectClosure {
   public:
93
    virtual bool do_object_b(oop p);
D
duke 已提交
94 95 96
  };

  class KeepAliveClosure: public OopClosure {
97 98
   protected:
    template <class T> void do_oop_work(T* p);
D
duke 已提交
99
   public:
100 101
    virtual void do_oop(oop* p);
    virtual void do_oop(narrowOop* p);
D
duke 已提交
102 103 104 105 106 107 108 109 110 111 112 113 114 115
  };

  //
  // Friend decls
  //
  friend class AdjustPointerClosure;
  friend class KeepAliveClosure;
  friend class VM_MarkSweep;
  friend void marksweep_init();

  //
  // Vars
  //
 protected:
116
  // Total invocations of a MarkSweep collection
117
  static uint _total_invocations;
118

119
  // Traversal stacks used during phase1
Z
zgu 已提交
120 121
  static Stack<oop, mtGC>                      _marking_stack;
  static Stack<ObjArrayTask, mtGC>             _objarray_stack;
D
duke 已提交
122 123

  // Space for storing/restoring mark word
Z
zgu 已提交
124 125
  static Stack<markOop, mtGC>                  _preserved_mark_stack;
  static Stack<oop, mtGC>                      _preserved_oop_stack;
D
duke 已提交
126 127 128 129 130 131 132
  static size_t                          _preserved_count;
  static size_t                          _preserved_count_max;
  static PreservedMark*                  _preserved_marks;

  // Reference processing (used in ...follow_contents)
  static ReferenceProcessor*             _ref_processor;

S
sla 已提交
133 134 135
  static STWGCTimer*                     _gc_timer;
  static SerialOldTracer*                _gc_tracer;

D
duke 已提交
136 137 138 139 140 141 142 143
  // Non public closures
  static KeepAliveClosure keep_alive;

  // Debugging
  static void trace(const char* msg) PRODUCT_RETURN;

 public:
  // Public closures
144
  static IsAliveClosure       is_alive;
145 146
  static FollowRootClosure    follow_root_closure;
  static MarkAndPushClosure   mark_and_push_closure;
147
  static FollowKlassClosure   follow_klass_closure;
148
  static FollowStackClosure   follow_stack_closure;
D
duke 已提交
149
  static AdjustPointerClosure adjust_pointer_closure;
150 151 152
  static AdjustKlassClosure   adjust_klass_closure;

  // Accessors
153
  static uint total_invocations() { return _total_invocations; }
D
duke 已提交
154 155 156 157

  // Reference Processing
  static ReferenceProcessor* const ref_processor() { return _ref_processor; }

S
sla 已提交
158 159 160
  static STWGCTimer* gc_timer() { return _gc_timer; }
  static SerialOldTracer* gc_tracer() { return _gc_tracer; }

D
duke 已提交
161 162
  // Call backs for marking
  static void mark_object(oop obj);
163 164
  // Mark pointer and follow contents.  Empty marking stack afterwards.
  template <class T> static inline void follow_root(T* p);
165

166
  // Check mark and maybe push on marking stack
167 168
  template <class T> static void mark_and_push(T* p);

169
  static inline void push_objarray(oop obj, size_t index);
D
duke 已提交
170

171
  static void follow_stack();   // Empty marking stack.
D
duke 已提交
172

173 174 175 176
  static void follow_klass(Klass* klass);

  static void follow_class_loader(ClassLoaderData* cld);

177 178 179 180
  static void preserve_mark(oop p, markOop mark);
                                // Save the mark word so it can be restored later
  static void adjust_marks();   // Adjust the pointers in the preserved marks table
  static void restore_marks();  // Restore the marks that we saved in preserve_mark
D
duke 已提交
181

182
  template <class T> static inline void adjust_pointer(T* p);
D
duke 已提交
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
};

class PreservedMark VALUE_OBJ_CLASS_SPEC {
private:
  oop _obj;
  markOop _mark;

public:
  void init(oop obj, markOop mark) {
    _obj = obj;
    _mark = mark;
  }

  void adjust_pointer() {
    MarkSweep::adjust_pointer(&_obj);
  }

  void restore() {
    _obj->set_mark(_mark);
  }
};
204 205

#endif // SHARE_VM_GC_IMPLEMENTATION_SHARED_MARKSWEEP_HPP