1 /*
  2  * Copyright (c) 2015, 2022, Red Hat, Inc. All rights reserved.
  3  * Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
  4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  5  *
  6  * This code is free software; you can redistribute it and/or modify it
  7  * under the terms of the GNU General Public License version 2 only, as
  8  * published by the Free Software Foundation.
  9  *
 10  * This code is distributed in the hope that it will be useful, but WITHOUT
 11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 13  * version 2 for more details (a copy is included in the LICENSE file that
 14  * accompanied this code).
 15  *
 16  * You should have received a copy of the GNU General Public License version
 17  * 2 along with this work; if not, write to the Free Software Foundation,
 18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 19  *
 20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 21  * or visit www.oracle.com if you need additional information or have any
 22  * questions.
 23  *
 24  */
 25 
 26 #ifndef SHARE_GC_SHENANDOAH_SHENANDOAHOOPCLOSURES_HPP
 27 #define SHARE_GC_SHENANDOAH_SHENANDOAHOOPCLOSURES_HPP
 28 
 29 #include "gc/shared/stringdedup/stringDedup.hpp"
 30 #include "gc/shenandoah/shenandoahClosures.inline.hpp"
 31 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
 32 #include "gc/shenandoah/shenandoahTaskqueue.hpp"
 33 #include "gc/shenandoah/shenandoahUtils.hpp"
 34 #include "memory/iterator.hpp"
 35 #include "runtime/javaThread.hpp"
 36 
 37 enum StringDedupMode {
 38   NO_DEDUP,      // Do not do anything for String deduplication
 39   ENQUEUE_DEDUP, // Enqueue candidate Strings for deduplication, if meet age threshold
 40   ALWAYS_DEDUP   // Enqueue Strings for deduplication
 41 };
 42 
 43 class ShenandoahMarkRefsSuperClosure : public MetadataVisitingOopIterateClosure {
 44 private:
 45   ShenandoahObjToScanQueue* _queue;
 46   ShenandoahObjToScanQueue* _old_queue;
 47   ShenandoahMarkingContext* const _mark_context;
 48   bool _weak;
 49 
 50 protected:
 51   template <class T, ShenandoahGenerationType GENERATION>
 52   void work(T *p);
 53 
 54 public:
 55   ShenandoahMarkRefsSuperClosure(ShenandoahObjToScanQueue* q, ShenandoahReferenceProcessor* rp, ShenandoahObjToScanQueue* old_q);
 56 
 57   bool is_weak() const {
 58     return _weak;
 59   }
 60 
 61   void set_weak(bool weak) {
 62     _weak = weak;
 63   }
 64 
 65   virtual void do_nmethod(nmethod* nm) {
 66     assert(!is_weak(), "Can't handle weak marking of nmethods");
 67     nm->run_nmethod_entry_barrier();
 68   }
 69 };
 70 
 71 class ShenandoahMarkUpdateRefsSuperClosure : public ShenandoahMarkRefsSuperClosure {
 72 protected:
 73   ShenandoahHeap* const _heap;
 74 
 75   template <class T, ShenandoahGenerationType GENERATION>
 76   inline void work(T* p);
 77 
 78 public:
 79   ShenandoahMarkUpdateRefsSuperClosure(ShenandoahObjToScanQueue* q, ShenandoahReferenceProcessor* rp, ShenandoahObjToScanQueue* old_q) :
 80     ShenandoahMarkRefsSuperClosure(q, rp, old_q),
 81     _heap(ShenandoahHeap::heap()) {
 82     assert(_heap->is_stw_gc_in_progress(), "Can only be used for STW GC");
 83   };
 84 };
 85 
 86 template <ShenandoahGenerationType GENERATION>
 87 class ShenandoahMarkUpdateRefsClosure : public ShenandoahMarkUpdateRefsSuperClosure {
 88 private:
 89   template <class T>
 90   inline void do_oop_work(T* p)     { work<T, GENERATION>(p); }
 91 
 92 public:
 93   ShenandoahMarkUpdateRefsClosure(ShenandoahObjToScanQueue* q, ShenandoahReferenceProcessor* rp, ShenandoahObjToScanQueue* old_q) :
 94     ShenandoahMarkUpdateRefsSuperClosure(q, rp, old_q) {}
 95 
 96   virtual void do_oop(narrowOop* p) { do_oop_work(p); }
 97   virtual void do_oop(oop* p)       { do_oop_work(p); }
 98 };
 99 
100 template <ShenandoahGenerationType GENERATION>
101 class ShenandoahMarkRefsClosure : public ShenandoahMarkRefsSuperClosure {
102 private:
103   template <class T>
104   inline void do_oop_work(T* p)     { work<T, GENERATION>(p); }
105 
106 public:
107   ShenandoahMarkRefsClosure(ShenandoahObjToScanQueue* q, ShenandoahReferenceProcessor* rp, ShenandoahObjToScanQueue* old_q) :
108     ShenandoahMarkRefsSuperClosure(q, rp, old_q) {};
109 
110   virtual void do_oop(narrowOop* p) { do_oop_work(p); }
111   virtual void do_oop(oop* p)       { do_oop_work(p); }
112 };
113 
114 class ShenandoahUpdateRefsSuperClosure : public ShenandoahOopClosureBase {
115 protected:
116   ShenandoahHeap* _heap;
117 
118 public:
119   ShenandoahUpdateRefsSuperClosure() :  _heap(ShenandoahHeap::heap()) {}
120 };
121 
122 class ShenandoahSTWUpdateRefsClosure : public ShenandoahUpdateRefsSuperClosure {
123 private:
124   template<class T>
125   inline void work(T* p);
126 
127 public:
128   ShenandoahSTWUpdateRefsClosure() : ShenandoahUpdateRefsSuperClosure() {
129     assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Must only be used at safepoints");
130   }
131 
132   virtual void do_oop(narrowOop* p) { work(p); }
133   virtual void do_oop(oop* p)       { work(p); }
134 };
135 
136 class ShenandoahConcUpdateRefsClosure : public ShenandoahUpdateRefsSuperClosure {
137 private:
138   template<class T>
139   inline void work(T* p);
140 
141 public:
142   ShenandoahConcUpdateRefsClosure() : ShenandoahUpdateRefsSuperClosure() {}
143 
144   virtual void do_oop(narrowOop* p) { work(p); }
145   virtual void do_oop(oop* p)       { work(p); }
146 };
147 
148 class ShenandoahSetRememberedCardsToDirtyClosure : public BasicOopIterateClosure {
149 protected:
150   ShenandoahHeap*    const _heap;
151   RememberedScanner* const _scanner;
152 
153 public:
154   ShenandoahSetRememberedCardsToDirtyClosure() :
155       _heap(ShenandoahHeap::heap()),
156       _scanner(_heap->card_scan()) {}
157 
158   template<class T>
159   inline void work(T* p);
160 
161   virtual void do_oop(narrowOop* p) { work(p); }
162   virtual void do_oop(oop* p)       { work(p); }
163 };
164 
165 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHOOPCLOSURES_HPP