1 /*
  2  * Copyright (c) 2015, 2022, Red Hat, Inc. 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_SHENANDOAH_SHENANDOAHOOPCLOSURES_HPP
 26 #define SHARE_GC_SHENANDOAH_SHENANDOAHOOPCLOSURES_HPP
 27 
 28 #include "gc/shared/stringdedup/stringDedup.hpp"
 29 #include "gc/shenandoah/shenandoahClosures.inline.hpp"
 30 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
 31 #include "gc/shenandoah/shenandoahTaskqueue.hpp"
 32 #include "gc/shenandoah/shenandoahUtils.hpp"
 33 #include "memory/iterator.hpp"
 34 #include "runtime/javaThread.hpp"
 35 
 36 enum StringDedupMode {
 37   NO_DEDUP,      // Do not do anything for String deduplication
 38   ENQUEUE_DEDUP, // Enqueue candidate Strings for deduplication, if meet age threshold
 39   ALWAYS_DEDUP   // Enqueue Strings for deduplication
 40 };
 41 
 42 class ShenandoahMarkRefsSuperClosure : public MetadataVisitingOopIterateClosure {
 43 private:
 44   ShenandoahObjToScanQueue* _queue;
 45   ShenandoahMarkingContext* const _mark_context;
 46   bool _weak;
 47 
 48 protected:
 49   template <class T>
 50   void work(T *p);
 51 
 52 public:
 53   ShenandoahMarkRefsSuperClosure(ShenandoahObjToScanQueue* q, ShenandoahReferenceProcessor* rp);
 54 
 55   bool is_weak() const {
 56     return _weak;
 57   }
 58 
 59   void set_weak(bool weak) {
 60     _weak = weak;
 61   }
 62 
 63   virtual void do_nmethod(nmethod* nm) {
 64     assert(!is_weak(), "Can't handle weak marking of nmethods");
 65     nm->run_nmethod_entry_barrier();
 66   }
 67 };
 68 
 69 class ShenandoahMarkUpdateRefsSuperClosure : public ShenandoahMarkRefsSuperClosure {
 70 protected:
 71   ShenandoahHeap* const _heap;
 72 
 73   template <class T>
 74   inline void work(T* p);
 75 
 76 public:
 77   ShenandoahMarkUpdateRefsSuperClosure(ShenandoahObjToScanQueue* q, ShenandoahReferenceProcessor* rp) :
 78     ShenandoahMarkRefsSuperClosure(q, rp),
 79     _heap(ShenandoahHeap::heap()) {
 80     assert(_heap->is_stw_gc_in_progress(), "Can only be used for STW GC");
 81   };
 82 };
 83 
 84 class ShenandoahMarkUpdateRefsClosure : public ShenandoahMarkUpdateRefsSuperClosure {
 85 private:
 86   template <class T>
 87   inline void do_oop_work(T* p)     { work<T>(p); }
 88 
 89 public:
 90   ShenandoahMarkUpdateRefsClosure(ShenandoahObjToScanQueue* q, ShenandoahReferenceProcessor* rp) :
 91     ShenandoahMarkUpdateRefsSuperClosure(q, rp) {}
 92 
 93   virtual void do_oop(narrowOop* p) { do_oop_work(p); }
 94   virtual void do_oop(oop* p)       { do_oop_work(p); }
 95 };
 96 
 97 class ShenandoahMarkRefsClosure : public ShenandoahMarkRefsSuperClosure {
 98 private:
 99   template <class T>
100   inline void do_oop_work(T* p)     { work<T>(p); }
101 
102 public:
103   ShenandoahMarkRefsClosure(ShenandoahObjToScanQueue* q, ShenandoahReferenceProcessor* rp) :
104     ShenandoahMarkRefsSuperClosure(q, rp) {};
105 
106   virtual void do_oop(narrowOop* p) { do_oop_work(p); }
107   virtual void do_oop(oop* p)       { do_oop_work(p); }
108 };
109 
110 
111 class ShenandoahUpdateRefsSuperClosure : public ShenandoahOopClosureBase {
112 protected:
113   ShenandoahHeap* _heap;
114 
115 public:
116   ShenandoahUpdateRefsSuperClosure() :  _heap(ShenandoahHeap::heap()) {}
117 };
118 
119 class ShenandoahSTWUpdateRefsClosure : public ShenandoahUpdateRefsSuperClosure {
120 private:
121   template<class T>
122   inline void work(T* p);
123 
124 public:
125   ShenandoahSTWUpdateRefsClosure() : ShenandoahUpdateRefsSuperClosure() {
126     assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Must only be used at safepoints");
127   }
128 
129   virtual void do_oop(narrowOop* p) { work(p); }
130   virtual void do_oop(oop* p)       { work(p); }
131 };
132 
133 class ShenandoahConcUpdateRefsClosure : public ShenandoahUpdateRefsSuperClosure {
134 private:
135   template<class T>
136   inline void work(T* p);
137 
138 public:
139   ShenandoahConcUpdateRefsClosure() : ShenandoahUpdateRefsSuperClosure() {}
140 
141   virtual void do_oop(narrowOop* p) { work(p); }
142   virtual void do_oop(oop* p)       { work(p); }
143 };
144 
145 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHOOPCLOSURES_HPP