1 /*
  2  * Copyright (c) 1997, 2024, 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_GC_SERIAL_SERIALFULLGC_HPP
 26 #define SHARE_GC_SERIAL_SERIALFULLGC_HPP
 27 
 28 #include "gc/shared/collectedHeap.hpp"
 29 #include "gc/shared/preservedMarks.hpp"
 30 #include "gc/shared/referenceProcessor.hpp"
 31 #include "gc/shared/stringdedup/stringDedup.hpp"
 32 #include "gc/shared/taskqueue.hpp"
 33 #include "memory/iterator.hpp"
 34 #include "oops/markWord.hpp"
 35 #include "oops/oop.hpp"
 36 #include "runtime/timer.hpp"
 37 #include "utilities/growableArray.hpp"
 38 #include "utilities/stack.hpp"
 39 
 40 class SerialOldTracer;
 41 class STWGCTimer;
 42 
 43 // Serial full GC takes care of global mark-compact garbage collection for a
 44 // SerialHeap 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 MarkAndPushClosure;
 52 class AdjustPointerClosure;
 53 
 54 class SerialFullGC : AllStatic {
 55   //
 56   // Inline closure decls
 57   //
 58   class FollowRootClosure: public BasicOopIterateClosure {
 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   // Used for java/lang/ref handling
 70   class IsAliveClosure: public BoolObjectClosure {
 71    public:
 72     virtual bool do_object_b(oop p);
 73   };
 74 
 75   class KeepAliveClosure: public OopClosure {
 76    protected:
 77     template <class T> void do_oop_work(T* p);
 78    public:
 79     virtual void do_oop(oop* p);
 80     virtual void do_oop(narrowOop* p);
 81   };
 82 
 83  protected:
 84   // Traversal stacks used during phase1
 85   static Stack<oop, mtGC>                      _marking_stack;
 86   static Stack<ObjArrayTask, mtGC>             _objarray_stack;
 87 
 88   // Space for storing/restoring mark word
 89   static PreservedMarksSet               _preserved_overflow_stack_set;
 90   static size_t                          _preserved_count;
 91   static size_t                          _preserved_count_max;
 92   static PreservedMark*                  _preserved_marks;
 93 
 94   static AlwaysTrueClosure               _always_true_closure;
 95   static ReferenceProcessor*             _ref_processor;
 96 
 97   static STWGCTimer*                     _gc_timer;
 98   static SerialOldTracer*                _gc_tracer;
 99 
100   static StringDedup::Requests* _string_dedup_requests;
101 
102   // Non public closures
103   static KeepAliveClosure keep_alive;
104 
105  public:
106   static void initialize();
107 
108   // Public closures
109   static IsAliveClosure       is_alive;
110   static FollowRootClosure    follow_root_closure;
111   static MarkAndPushClosure   mark_and_push_closure;
112   static FollowStackClosure   follow_stack_closure;
113   static CLDToOopClosure      follow_cld_closure;
114   static AdjustPointerClosure adjust_pointer_closure;
115   static CLDToOopClosure      adjust_cld_closure;
116 
117   static void invoke_at_safepoint(bool clear_all_softrefs);
118 
119   // Reference Processing
120   static ReferenceProcessor* ref_processor() { return _ref_processor; }
121 
122   static STWGCTimer* gc_timer() { return _gc_timer; }
123   static SerialOldTracer* gc_tracer() { return _gc_tracer; }
124 
125   static void preserve_mark(oop p, markWord mark);
126                                 // Save the mark word so it can be restored later
127   static void adjust_marks();   // Adjust the pointers in the preserved marks table
128   static void restore_marks();  // Restore the marks that we saved in preserve_mark
129 
130   static void follow_stack();   // Empty marking stack.
131 
132   template <class T> static void adjust_pointer(T* p);
133 
134   // Check mark and maybe push on marking stack
135   template <class T> static void mark_and_push(T* p);
136 
137  private:
138   // Mark live objects
139   static void phase1_mark(bool clear_all_softrefs);
140 
141   // Temporary data structures for traversal and storing/restoring marks
142   static void allocate_stacks();
143   static void deallocate_stacks();
144 
145   // Call backs for marking
146   static void mark_object(oop obj);
147   // Mark pointer and follow contents.  Empty marking stack afterwards.
148   template <class T> static inline void follow_root(T* p);
149 
150   static inline void push_objarray(objArrayOop obj, size_t index);
151 
152   static void follow_object(oop obj);
153 
154   static void follow_array(objArrayOop array);
155 
156   static void follow_array_chunk(objArrayOop array, int index);
157 };
158 
159 class MarkAndPushClosure: public ClaimMetadataVisitingOopIterateClosure {
160 public:
161   MarkAndPushClosure(int claim) : ClaimMetadataVisitingOopIterateClosure(claim) {}
162 
163   template <typename T> void do_oop_work(T* p);
164   virtual void do_oop(      oop* p);
165   virtual void do_oop(narrowOop* p);
166 
167   void set_ref_discoverer(ReferenceDiscoverer* rd) {
168     set_ref_discoverer_internal(rd);
169   }
170 };
171 
172 class AdjustPointerClosure: public BasicOopIterateClosure {
173  public:
174   template <typename T> void do_oop_work(T* p);
175   virtual void do_oop(oop* p);
176   virtual void do_oop(narrowOop* p);
177   virtual ReferenceIterationMode reference_iteration_mode() { return DO_FIELDS; }
178 };
179 
180 #endif // SHARE_GC_SERIAL_SERIALFULLGC_HPP