1 /*
  2  * Copyright (c) 1997, 2018, 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_MEMORY_ITERATOR_HPP
 26 #define SHARE_VM_MEMORY_ITERATOR_HPP
 27 
 28 #include "memory/allocation.hpp"
 29 #include "memory/memRegion.hpp"
 30 #include "oops/oopsHierarchy.hpp"
 31 
 32 class CodeBlob;
 33 class nmethod;
 34 class ReferenceDiscoverer;
 35 class DataLayout;
 36 class KlassClosure;
 37 class ClassLoaderData;
 38 class Symbol;
 39 
 40 // The following classes are C++ `closures` for iterating over objects, roots and spaces
 41 
 42 class Closure : public StackObj { };
 43 
 44 // OopClosure is used for iterating through references to Java objects.
 45 class OopClosure : public Closure {
 46  public:
 47   virtual void do_oop(oop* o) = 0;
 48   virtual void do_oop(narrowOop* o) = 0;
 49 };
 50 
 51 class DoNothingClosure : public OopClosure {
 52  public:
 53   virtual void do_oop(oop* p)       {}
 54   virtual void do_oop(narrowOop* p) {}
 55 };
 56 extern DoNothingClosure do_nothing_cl;
 57 
 58 // OopIterateClosure adds extra code to be run during oop iterations.
 59 // This is needed by the GC and is extracted to a separate type to not
 60 // pollute the OopClosure interface.
 61 class OopIterateClosure : public OopClosure {
 62  private:
 63   ReferenceDiscoverer* _ref_discoverer;
 64 
 65  protected:
 66   OopIterateClosure(ReferenceDiscoverer* rd) : _ref_discoverer(rd) { }
 67   OopIterateClosure() : _ref_discoverer(NULL) { }
 68   ~OopIterateClosure() { }
 69 
 70   void set_ref_discoverer_internal(ReferenceDiscoverer* rd) { _ref_discoverer = rd; }
 71 
 72  public:
 73   ReferenceDiscoverer* ref_discoverer() const { return _ref_discoverer; }
 74 
 75   // Iteration of InstanceRefKlasses differ depending on the closure,
 76   // the below enum describes the different alternatives.
 77   enum ReferenceIterationMode {
 78     DO_DISCOVERY,                // Apply closure and discover references
 79     DO_DISCOVERED_AND_DISCOVERY, // Apply closure to discovered field and do discovery
 80     DO_FIELDS,                   // Apply closure to all fields
 81     DO_FIELDS_EXCEPT_REFERENT    // Apply closure to all fields except the referent field
 82   };
 83 
 84   // The default iteration mode is to do discovery.
 85   virtual ReferenceIterationMode reference_iteration_mode() { return DO_DISCOVERY; }
 86 
 87   // If the do_metadata functions return "true",
 88   // we invoke the following when running oop_iterate():
 89   //
 90   // 1) do_klass on the header klass pointer.
 91   // 2) do_klass on the klass pointer in the mirrors.
 92   // 3) do_cld   on the class loader data in class loaders.
 93 
 94   virtual bool do_metadata() = 0;
 95   virtual void do_klass(Klass* k) = 0;
 96   virtual void do_cld(ClassLoaderData* cld) = 0;
 97 
 98 #ifdef ASSERT
 99   // Default verification of each visited oop field.
100   template <typename T> void verify(T* p);
101 
102   // Can be used by subclasses to turn off the default verification of oop fields.
103   virtual bool should_verify_oops() { return true; }
104 #endif
105 };
106 
107 // An OopIterateClosure that can be used when there's no need to visit the Metadata.
108 class BasicOopIterateClosure : public OopIterateClosure {
109 public:
110   BasicOopIterateClosure(ReferenceDiscoverer* rd = NULL) : OopIterateClosure(rd) {}
111 
112   virtual bool do_metadata() { return false; }
113   virtual void do_klass(Klass* k) { ShouldNotReachHere(); }
114   virtual void do_cld(ClassLoaderData* cld) { ShouldNotReachHere(); }
115 };
116 
117 class KlassClosure : public Closure {
118  public:
119   virtual void do_klass(Klass* k) = 0;
120 };
121 
122 class CLDClosure : public Closure {
123  public:
124   virtual void do_cld(ClassLoaderData* cld) = 0;
125 };
126 
127 
128 class CLDToOopClosure : public CLDClosure {
129   OopClosure*       _oop_closure;
130   int               _cld_claim;
131 
132  public:
133   CLDToOopClosure(OopClosure* oop_closure,
134                   int cld_claim) :
135       _oop_closure(oop_closure),
136       _cld_claim(cld_claim) {}
137 
138   void do_cld(ClassLoaderData* cld);
139 };
140 
141 // The base class for all concurrent marking closures,
142 // that participates in class unloading.
143 // It's used to proxy through the metadata to the oops defined in them.
144 class MetadataVisitingOopIterateClosure: public OopIterateClosure {
145  public:
146   MetadataVisitingOopIterateClosure(ReferenceDiscoverer* rd = NULL) : OopIterateClosure(rd) { }
147 
148   virtual bool do_metadata() { return true; }
149   virtual void do_klass(Klass* k);
150   virtual void do_cld(ClassLoaderData* cld);
151 };
152 
153 // ObjectClosure is used for iterating through an object space
154 
155 class ObjectClosure : public Closure {
156  public:
157   // Called for each object.
158   virtual void do_object(oop obj) = 0;
159 };
160 
161 
162 class BoolObjectClosure : public Closure {
163  public:
164   virtual bool do_object_b(oop obj) = 0;
165 };
166 
167 class AlwaysTrueClosure: public BoolObjectClosure {
168  public:
169   bool do_object_b(oop p) { return true; }
170 };
171 
172 class AlwaysFalseClosure : public BoolObjectClosure {
173  public:
174   bool do_object_b(oop p) { return false; }
175 };
176 
177 // Applies an oop closure to all ref fields in objects iterated over in an
178 // object iteration.
179 class ObjectToOopClosure: public ObjectClosure {
180   OopIterateClosure* _cl;
181 public:
182   void do_object(oop obj);
183   ObjectToOopClosure(OopIterateClosure* cl) : _cl(cl) {}
184 };
185 
186 // A version of ObjectClosure that is expected to be robust
187 // in the face of possibly uninitialized objects.
188 class ObjectClosureCareful : public ObjectClosure {
189  public:
190   virtual size_t do_object_careful_m(oop p, MemRegion mr) = 0;
191   virtual size_t do_object_careful(oop p) = 0;
192 };
193 
194 // The following are used in CompactibleFreeListSpace and
195 // ConcurrentMarkSweepGeneration.
196 
197 // Blk closure (abstract class)
198 class BlkClosure : public StackObj {
199  public:
200   virtual size_t do_blk(HeapWord* addr) = 0;
201 };
202 
203 // A version of BlkClosure that is expected to be robust
204 // in the face of possibly uninitialized objects.
205 class BlkClosureCareful : public BlkClosure {
206  public:
207   size_t do_blk(HeapWord* addr) {
208     guarantee(false, "call do_blk_careful instead");
209     return 0;
210   }
211   virtual size_t do_blk_careful(HeapWord* addr) = 0;
212 };
213 
214 // SpaceClosure is used for iterating over spaces
215 
216 class Space;
217 class CompactibleSpace;
218 
219 class SpaceClosure : public StackObj {
220  public:
221   // Called for each space
222   virtual void do_space(Space* s) = 0;
223 };
224 
225 class CompactibleSpaceClosure : public StackObj {
226  public:
227   // Called for each compactible space
228   virtual void do_space(CompactibleSpace* s) = 0;
229 };
230 
231 
232 // CodeBlobClosure is used for iterating through code blobs
233 // in the code cache or on thread stacks
234 
235 class CodeBlobClosure : public Closure {
236  public:
237   // Called for each code blob.
238   virtual void do_code_blob(CodeBlob* cb) = 0;
239 };
240 
241 // Applies an oop closure to all ref fields in code blobs
242 // iterated over in an object iteration.
243 class CodeBlobToOopClosure : public CodeBlobClosure {
244   OopClosure* _cl;
245   bool _fix_relocations;
246  protected:
247   void do_nmethod(nmethod* nm);
248  public:
249   // If fix_relocations(), then cl must copy objects to their new location immediately to avoid
250   // patching nmethods with the old locations.
251   CodeBlobToOopClosure(OopClosure* cl, bool fix_relocations) : _cl(cl), _fix_relocations(fix_relocations) {}
252   virtual void do_code_blob(CodeBlob* cb);
253 
254   bool fix_relocations() const { return _fix_relocations; }
255   const static bool FixRelocations = true;
256 };
257 
258 class MarkingCodeBlobClosure : public CodeBlobToOopClosure {
259  public:
260   MarkingCodeBlobClosure(OopClosure* cl, bool fix_relocations) : CodeBlobToOopClosure(cl, fix_relocations) {}
261   // Called for each code blob, but at most once per unique blob.
262 
263   virtual void do_code_blob(CodeBlob* cb);
264 };
265 
266 // MonitorClosure is used for iterating over monitors in the monitors cache
267 
268 class ObjectMonitor;
269 
270 class MonitorClosure : public StackObj {
271  public:
272   // called for each monitor in cache
273   virtual void do_monitor(ObjectMonitor* m) = 0;
274 };
275 
276 // A closure that is applied without any arguments.
277 class VoidClosure : public StackObj {
278  public:
279   // I would have liked to declare this a pure virtual, but that breaks
280   // in mysterious ways, for unknown reasons.
281   virtual void do_void();
282 };
283 
284 
285 // YieldClosure is intended for use by iteration loops
286 // to incrementalize their work, allowing interleaving
287 // of an interruptable task so as to allow other
288 // threads to run (which may not otherwise be able to access
289 // exclusive resources, for instance). Additionally, the
290 // closure also allows for aborting an ongoing iteration
291 // by means of checking the return value from the polling
292 // call.
293 class YieldClosure : public StackObj {
294 public:
295  virtual bool should_return() = 0;
296 
297  // Yield on a fine-grain level. The check in case of not yielding should be very fast.
298  virtual bool should_return_fine_grain() { return false; }
299 };
300 
301 // Abstract closure for serializing data (read or write).
302 
303 class SerializeClosure : public Closure {
304 public:
305   // Return bool indicating whether closure implements read or write.
306   virtual bool reading() const = 0;
307 
308   // Read/write the void pointer pointed to by p.
309   virtual void do_ptr(void** p) = 0;
310 
311   // Read/write the 32-bit unsigned integer pointed to by p.
312   virtual void do_u4(u4* p) = 0;
313 
314   // Read/write the region specified.
315   virtual void do_region(u_char* start, size_t size) = 0;
316 
317   // Check/write the tag.  If reading, then compare the tag against
318   // the passed in value and fail is they don't match.  This allows
319   // for verification that sections of the serialized data are of the
320   // correct length.
321   virtual void do_tag(int tag) = 0;
322 
323   // Read/write the oop
324   virtual void do_oop(oop* o) = 0;
325 
326   bool writing() {
327     return !reading();
328   }
329 };
330 
331 class SymbolClosure : public StackObj {
332  public:
333   virtual void do_symbol(Symbol**) = 0;
334 
335   // Clear LSB in symbol address; it can be set by CPSlot.
336   static Symbol* load_symbol(Symbol** p) {
337     return (Symbol*)(intptr_t(*p) & ~1);
338   }
339 
340   // Store symbol, adjusting new pointer if the original pointer was adjusted
341   // (symbol references in constant pool slots have their LSB set to 1).
342   static void store_symbol(Symbol** p, Symbol* sym) {
343     *p = (Symbol*)(intptr_t(sym) | (intptr_t(*p) & 1));
344   }
345 };
346 
347 // Dispatches to the non-virtual functions if OopClosureType has
348 // a concrete implementation, otherwise a virtual call is taken.
349 class Devirtualizer {
350  public:
351   template <typename OopClosureType, typename T> static void do_oop_no_verify(OopClosureType* closure, T* p);
352   template <typename OopClosureType, typename T> static void do_oop(OopClosureType* closure, T* p);
353   template <typename OopClosureType>             static void do_klass(OopClosureType* closure, Klass* k);
354   template <typename OopClosureType>             static void do_cld(OopClosureType* closure, ClassLoaderData* cld);
355   template <typename OopClosureType>             static bool do_metadata(OopClosureType* closure);
356 };
357 
358 class OopIteratorClosureDispatch {
359  public:
360   template <typename OopClosureType> static void oop_oop_iterate(OopClosureType* cl, oop obj, Klass* klass);
361   template <typename OopClosureType> static void oop_oop_iterate(OopClosureType* cl, oop obj, Klass* klass, MemRegion mr);
362   template <typename OopClosureType> static void oop_oop_iterate_backwards(OopClosureType* cl, oop obj, Klass* klass);
363 };
364 
365 #endif // SHARE_VM_MEMORY_ITERATOR_HPP