1 /*
  2  * Copyright (c) 2015, 2021, 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/shenandoahHeap.inline.hpp"
 30 #include "gc/shenandoah/shenandoahTaskqueue.hpp"
 31 #include "gc/shenandoah/shenandoahUtils.hpp"
 32 #include "memory/iterator.hpp"
 33 #include "runtime/thread.hpp"
 34 
 35 enum StringDedupMode {
 36   NO_DEDUP,      // Do not do anything for String deduplication
 37   ENQUEUE_DEDUP, // Enqueue candidate Strings for deduplication, if meet age threshold
 38   ALWAYS_DEDUP   // Enqueue Strings for deduplication
 39 };
 40 
 41 class ShenandoahMarkRefsSuperClosure : public MetadataVisitingOopIterateClosure {
 42 private:
 43   ShenandoahObjToScanQueue* _queue;
 44   ShenandoahMarkingContext* const _mark_context;
 45   bool _weak;
 46 
 47 protected:
 48   template <class T>
 49   void work(T *p);
 50 
 51 public:
 52   ShenandoahMarkRefsSuperClosure(ShenandoahObjToScanQueue* q, ShenandoahReferenceProcessor* rp);
 53 
 54   bool is_weak() const {
 55     return _weak;
 56   }
 57 
 58   void set_weak(bool weak) {
 59     _weak = weak;
 60   }
 61 };
 62 
 63 class ShenandoahMarkUpdateRefsSuperClosure : public ShenandoahMarkRefsSuperClosure {
 64 protected:
 65   ShenandoahHeap* const _heap;
 66 
 67   template <class T>
 68   inline void work(T* p);
 69 
 70 public:
 71   ShenandoahMarkUpdateRefsSuperClosure(ShenandoahObjToScanQueue* q, ShenandoahReferenceProcessor* rp) :
 72     ShenandoahMarkRefsSuperClosure(q, rp),
 73     _heap(ShenandoahHeap::heap()) {
 74     assert(_heap->is_stw_gc_in_progress(), "Can only be used for STW GC");
 75   };
 76 };
 77 
 78 class ShenandoahMarkUpdateRefsClosure : public ShenandoahMarkUpdateRefsSuperClosure {
 79 private:
 80   template <class T>
 81   inline void do_oop_work(T* p)     { work<T>(p); }
 82 
 83 public:
 84   ShenandoahMarkUpdateRefsClosure(ShenandoahObjToScanQueue* q, ShenandoahReferenceProcessor* rp) :
 85     ShenandoahMarkUpdateRefsSuperClosure(q, rp) {}
 86 
 87   virtual void do_oop(narrowOop* p) { do_oop_work(p); }
 88   virtual void do_oop(oop* p)       { do_oop_work(p); }
 89   virtual bool do_metadata()        { return false; }
 90 };
 91 
 92 class ShenandoahMarkUpdateRefsMetadataClosure : public ShenandoahMarkUpdateRefsSuperClosure {
 93 private:
 94   template <class T>
 95   inline void do_oop_work(T* p)     { work<T>(p); }
 96 
 97 public:
 98   ShenandoahMarkUpdateRefsMetadataClosure(ShenandoahObjToScanQueue* q, ShenandoahReferenceProcessor* rp) :
 99     ShenandoahMarkUpdateRefsSuperClosure(q, rp) {}
100 
101   virtual void do_oop(narrowOop* p) { do_oop_work(p); }
102   virtual void do_oop(oop* p)       { do_oop_work(p); }
103   virtual bool do_metadata()        { return true; }
104 };
105 
106 
107 class ShenandoahMarkRefsClosure : public ShenandoahMarkRefsSuperClosure {
108 private:
109   template <class T>
110   inline void do_oop_work(T* p)     { work<T>(p); }
111 
112 public:
113   ShenandoahMarkRefsClosure(ShenandoahObjToScanQueue* q, ShenandoahReferenceProcessor* rp) :
114     ShenandoahMarkRefsSuperClosure(q, rp) {};
115 
116   virtual void do_oop(narrowOop* p) { do_oop_work(p); }
117   virtual void do_oop(oop* p)       { do_oop_work(p); }
118   virtual bool do_metadata()        { return false; }
119 };
120 
121 
122 class ShenandoahMarkRefsMetadataClosure : public ShenandoahMarkRefsSuperClosure {
123 private:
124   template <class T>
125   inline void do_oop_work(T* p)     { work<T>(p); }
126 
127 public:
128   ShenandoahMarkRefsMetadataClosure(ShenandoahObjToScanQueue* q, ShenandoahReferenceProcessor* rp) :
129     ShenandoahMarkRefsSuperClosure(q, rp) {};
130 
131   virtual void do_oop(narrowOop* p) { do_oop_work(p); }
132   virtual void do_oop(oop* p)       { do_oop_work(p); }
133   virtual bool do_metadata()        { return true; }
134 };
135 
136 class ShenandoahUpdateRefsSuperClosure : public BasicOopIterateClosure {
137 protected:
138   ShenandoahHeap* _heap;
139 
140 public:
141   ShenandoahUpdateRefsSuperClosure() :  _heap(ShenandoahHeap::heap()) {}
142 };
143 
144 class ShenandoahSTWUpdateRefsClosure : public ShenandoahUpdateRefsSuperClosure {
145 private:
146   template<class T>
147   inline void work(T* p);
148 
149 public:
150   ShenandoahSTWUpdateRefsClosure() : ShenandoahUpdateRefsSuperClosure() {
151     assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Must only be used at safepoints");
152   }
153 
154   virtual void do_oop(narrowOop* p) { work(p); }
155   virtual void do_oop(oop* p)       { work(p); }
156 };
157 
158 class ShenandoahConcUpdateRefsClosure : public ShenandoahUpdateRefsSuperClosure {
159 private:
160   template<class T>
161   inline void work(T* p);
162 
163 public:
164   ShenandoahConcUpdateRefsClosure() : ShenandoahUpdateRefsSuperClosure() {}
165 
166   virtual void do_oop(narrowOop* p) { work(p); }
167   virtual void do_oop(oop* p)       { work(p); }
168 };
169 
170 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHOOPCLOSURES_HPP