1/*
2 * Copyright (c) 1997, 2017, 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
38class ReferenceProcessor;
39class DataLayout;
40class SerialOldTracer;
41class 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
51class PreservedMark;
52class MarkAndPushClosure;
53class AdjustPointerClosure;
54
55class MarkSweep : AllStatic {
56  //
57  // Inline closure decls
58  //
59  class FollowRootClosure: public OopsInGenClosure {
60   public:
61    virtual void do_oop(oop* p);
62    virtual void do_oop(narrowOop* p);
63  };
64
65  class FollowStackClosure: public VoidClosure {
66   public:
67    virtual void do_void();
68  };
69
70  // Used for java/lang/ref handling
71  class IsAliveClosure: public BoolObjectClosure {
72   public:
73    virtual bool do_object_b(oop p);
74  };
75
76  class KeepAliveClosure: public OopClosure {
77   protected:
78    template <class T> void do_oop_work(T* p);
79   public:
80    virtual void do_oop(oop* p);
81    virtual void do_oop(narrowOop* p);
82  };
83
84  //
85  // Friend decls
86  //
87  friend class AdjustPointerClosure;
88  friend class KeepAliveClosure;
89  friend class VM_MarkSweep;
90  friend void marksweep_init();
91
92  //
93  // Vars
94  //
95 protected:
96  // Total invocations of a MarkSweep collection
97  static uint _total_invocations;
98
99  // Traversal stacks used during phase1
100  static Stack<oop, mtGC>                      _marking_stack;
101  static Stack<ObjArrayTask, mtGC>             _objarray_stack;
102
103  // Space for storing/restoring mark word
104  static Stack<markOop, mtGC>                  _preserved_mark_stack;
105  static Stack<oop, mtGC>                      _preserved_oop_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  // Non public closures
117  static KeepAliveClosure keep_alive;
118
119 public:
120  // Public closures
121  static IsAliveClosure       is_alive;
122  static FollowRootClosure    follow_root_closure;
123  static MarkAndPushClosure   mark_and_push_closure;
124  static FollowStackClosure   follow_stack_closure;
125  static CLDToOopClosure      follow_cld_closure;
126  static AdjustPointerClosure adjust_pointer_closure;
127  static CLDToOopClosure      adjust_cld_closure;
128
129  // Accessors
130  static uint total_invocations() { return _total_invocations; }
131
132  // Reference Processing
133  static ReferenceProcessor* const ref_processor() { return _ref_processor; }
134  static void set_ref_processor(ReferenceProcessor* rp);
135
136  // Archive Object handling
137  static inline bool is_closed_archive_object(oop object);
138  static inline bool is_open_archive_object(oop object);
139  static inline bool is_archive_object(oop object);
140
141  static STWGCTimer* gc_timer() { return _gc_timer; }
142  static SerialOldTracer* gc_tracer() { return _gc_tracer; }
143
144  static void preserve_mark(oop p, markOop mark);
145                                // Save the mark word so it can be restored later
146  static void adjust_marks();   // Adjust the pointers in the preserved marks table
147  static void restore_marks();  // Restore the marks that we saved in preserve_mark
148
149  static int adjust_pointers(oop obj);
150
151  static void follow_stack();   // Empty marking stack.
152
153  static void follow_klass(Klass* klass);
154
155  static void follow_cld(ClassLoaderData* cld);
156
157  template <class T> static inline void adjust_pointer(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 private:
163  // Call backs for marking
164  static void mark_object(oop obj);
165  // Mark pointer and follow contents.  Empty marking stack afterwards.
166  template <class T> static inline void follow_root(T* p);
167
168  static inline void push_objarray(oop obj, size_t index);
169
170  static void follow_object(oop obj);
171
172  static void follow_array(objArrayOop array);
173
174  static void follow_array_chunk(objArrayOop array, int index);
175};
176
177class MarkAndPushClosure: public ExtendedOopClosure {
178public:
179  template <typename T> void do_oop_nv(T* p);
180  virtual void do_oop(oop* p);
181  virtual void do_oop(narrowOop* p);
182
183  virtual bool do_metadata();
184  bool do_metadata_nv();
185
186  virtual void do_klass(Klass* k);
187  void do_klass_nv(Klass* k);
188
189  virtual void do_cld(ClassLoaderData* cld);
190  void do_cld_nv(ClassLoaderData* cld);
191
192  void set_ref_processor(ReferenceProcessor* rp) {
193    set_ref_processor_internal(rp);
194  }
195};
196
197class AdjustPointerClosure: public OopsInGenClosure {
198 public:
199  template <typename T> void do_oop_nv(T* p);
200  virtual void do_oop(oop* p);
201  virtual void do_oop(narrowOop* p);
202  virtual ReferenceIterationMode reference_iteration_mode() { return DO_FIELDS; }
203
204  // This closure provides its own oop verification code.
205  debug_only(virtual bool should_verify_oops() { return false; })
206};
207
208class PreservedMark VALUE_OBJ_CLASS_SPEC {
209private:
210  oop _obj;
211  markOop _mark;
212
213public:
214  void init(oop obj, markOop mark) {
215    _obj = obj;
216    _mark = mark;
217  }
218
219  void adjust_pointer();
220  void restore();
221};
222
223#endif // SHARE_VM_GC_SERIAL_MARKSWEEP_HPP
224