markSweep.hpp 6.0 KB
Newer Older
D
duke 已提交
1
/*
2
 * Copyright (c) 1997, 2012, 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;
D
duke 已提交
39 40 41 42 43 44 45 46 47 48 49 50 51

// 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 {
  //
52
  // Inline closure decls
D
duke 已提交
53
  //
54
  class FollowRootClosure: public OopsInGenClosure {
D
duke 已提交
55
   public:
56 57
    virtual void do_oop(oop* p);
    virtual void do_oop(narrowOop* p);
D
duke 已提交
58 59 60 61
  };

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

  // 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 已提交
75 76 77 78
  };

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

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

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

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

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

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

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

  // Space for storing/restoring mark word
Z
zgu 已提交
123 124
  static Stack<markOop, mtGC>                  _preserved_mark_stack;
  static Stack<oop, mtGC>                      _preserved_oop_stack;
D
duke 已提交
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
  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;

  // Non public closures
  static KeepAliveClosure keep_alive;

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

 public:
  // Public closures
140
  static IsAliveClosure       is_alive;
141
  static FollowRootClosure    follow_root_closure;
142
  static CodeBlobToOopClosure follow_code_root_closure; // => follow_root_closure
143
  static MarkAndPushClosure   mark_and_push_closure;
144
  static FollowKlassClosure   follow_klass_closure;
145
  static FollowStackClosure   follow_stack_closure;
D
duke 已提交
146
  static AdjustPointerClosure adjust_pointer_closure;
147 148 149
  static AdjustKlassClosure   adjust_klass_closure;

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

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

  // Call backs for marking
  static void mark_object(oop obj);
157 158
  // Mark pointer and follow contents.  Empty marking stack afterwards.
  template <class T> static inline void follow_root(T* p);
159

160
  // Check mark and maybe push on marking stack
161 162
  template <class T> static void mark_and_push(T* p);

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

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

167 168 169 170 171 172
  static void follow_klass(Klass* klass);
  static void adjust_klass(Klass* klass);

  static void follow_class_loader(ClassLoaderData* cld);
  static void adjust_class_loader(ClassLoaderData* cld);

173 174 175 176
  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 已提交
177

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

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);
  }
};
200 201

#endif // SHARE_VM_GC_IMPLEMENTATION_SHARED_MARKSWEEP_HPP