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   bool              _must_claim_cld;
131 
132  public:
133   CLDToOopClosure(OopClosure* oop_closure, bool must_claim_cld = true) :
134       _oop_closure(oop_closure),
135       _must_claim_cld(must_claim_cld) {}
136 
137   void do_cld(ClassLoaderData* cld);
138 };
139 
140 // The base class for all concurrent marking closures,
141 // that participates in class unloading.
142 // It's used to proxy through the metadata to the oops defined in them.
143 class MetadataVisitingOopIterateClosure: public OopIterateClosure {
144  public:
145   MetadataVisitingOopIterateClosure(ReferenceDiscoverer* rd = NULL) : OopIterateClosure(rd) { }
146 
147   virtual bool do_metadata() { return true; }
148   virtual void do_klass(Klass* k);
149   virtual void do_cld(ClassLoaderData* cld);
150 };
151 
152 // ObjectClosure is used for iterating through an object space
153 
154 class ObjectClosure : public Closure {
155  public:
156   // Called for each object.
157   virtual void do_object(oop obj) = 0;
158 };
159 
160 
161 class BoolObjectClosure : public Closure {
162  public:
163   virtual bool do_object_b(oop obj) = 0;
164 };
165 
166 class AlwaysTrueClosure: public BoolObjectClosure {
167  public:
168   bool do_object_b(oop p) { return true; }
169 };
170 
171 class AlwaysFalseClosure : public BoolObjectClosure {
172  public:
173   bool do_object_b(oop p) { return false; }
174 };
175 
176 // Applies an oop closure to all ref fields in objects iterated over in an
177 // object iteration.
178 class ObjectToOopClosure: public ObjectClosure {
179   OopIterateClosure* _cl;
180 public:
181   void do_object(oop obj);
182   ObjectToOopClosure(OopIterateClosure* cl) : _cl(cl) {}
183 };
184 
185 // A version of ObjectClosure that is expected to be robust
186 // in the face of possibly uninitialized objects.
187 class ObjectClosureCareful : public ObjectClosure {
188  public:
189   virtual size_t do_object_careful_m(oop p, MemRegion mr) = 0;
190   virtual size_t do_object_careful(oop p) = 0;
191 };
192 
193 // The following are used in CompactibleFreeListSpace and
194 // ConcurrentMarkSweepGeneration.
195 
196 // Blk closure (abstract class)
197 class BlkClosure : public StackObj {
198  public:
199   virtual size_t do_blk(HeapWord* addr) = 0;
200 };
201 
202 // A version of BlkClosure that is expected to be robust
203 // in the face of possibly uninitialized objects.
204 class BlkClosureCareful : public BlkClosure {
205  public:
206   size_t do_blk(HeapWord* addr) {
207     guarantee(false, "call do_blk_careful instead");
208     return 0;
209   }
210   virtual size_t do_blk_careful(HeapWord* addr) = 0;
211 };
212 
213 // SpaceClosure is used for iterating over spaces
214 
215 class Space;
216 class CompactibleSpace;
217 
218 class SpaceClosure : public StackObj {
219  public:
220   // Called for each space
221   virtual void do_space(Space* s) = 0;
222 };
223 
224 class CompactibleSpaceClosure : public StackObj {
225  public:
226   // Called for each compactible space
227   virtual void do_space(CompactibleSpace* s) = 0;
228 };
229 
230 
231 // CodeBlobClosure is used for iterating through code blobs
232 // in the code cache or on thread stacks
233 
234 class CodeBlobClosure : public Closure {
235  public:
236   // Called for each code blob.
237   virtual void do_code_blob(CodeBlob* cb) = 0;
238 };
239 
240 // Applies an oop closure to all ref fields in code blobs
241 // iterated over in an object iteration.
242 class CodeBlobToOopClosure : public CodeBlobClosure {
243   OopClosure* _cl;
244   bool _fix_relocations;
245  protected:
246   void do_nmethod(nmethod* nm);
247  public:
248   // If fix_relocations(), then cl must copy objects to their new location immediately to avoid
249   // patching nmethods with the old locations.
250   CodeBlobToOopClosure(OopClosure* cl, bool fix_relocations) : _cl(cl), _fix_relocations(fix_relocations) {}
251   virtual void do_code_blob(CodeBlob* cb);
252 
253   bool fix_relocations() const { return _fix_relocations; }
254   const static bool FixRelocations = true;
255 };
256 
257 class MarkingCodeBlobClosure : public CodeBlobToOopClosure {
258  public:
259   MarkingCodeBlobClosure(OopClosure* cl, bool fix_relocations) : CodeBlobToOopClosure(cl, fix_relocations) {}
260   // Called for each code blob, but at most once per unique blob.
261 
262   virtual void do_code_blob(CodeBlob* cb);
263 };
264 
265 // MonitorClosure is used for iterating over monitors in the monitors cache
266 
267 class ObjectMonitor;
268 
269 class MonitorClosure : public StackObj {
270  public:
271   // called for each monitor in cache
272   virtual void do_monitor(ObjectMonitor* m) = 0;
273 };
274 
275 // A closure that is applied without any arguments.
276 class VoidClosure : public StackObj {
277  public:
278   // I would have liked to declare this a pure virtual, but that breaks
279   // in mysterious ways, for unknown reasons.
280   virtual void do_void();
281 };
282 
283 
284 // YieldClosure is intended for use by iteration loops
285 // to incrementalize their work, allowing interleaving
286 // of an interruptable task so as to allow other
287 // threads to run (which may not otherwise be able to access
288 // exclusive resources, for instance). Additionally, the
289 // closure also allows for aborting an ongoing iteration
290 // by means of checking the return value from the polling
291 // call.
292 class YieldClosure : public StackObj {
293 public:
294  virtual bool should_return() = 0;
295 
296  // Yield on a fine-grain level. The check in case of not yielding should be very fast.
297  virtual bool should_return_fine_grain() { return false; }
298 };
299 
300 // Abstract closure for serializing data (read or write).
301 
302 class SerializeClosure : public Closure {
303 public:
304   // Return bool indicating whether closure implements read or write.
305   virtual bool reading() const = 0;
306 
307   // Read/write the void pointer pointed to by p.
308   virtual void do_ptr(void** p) = 0;
309 
310   // Read/write the 32-bit unsigned integer pointed to by p.
311   virtual void do_u4(u4* p) = 0;
312 
313   // Read/write the region specified.
314   virtual void do_region(u_char* start, size_t size) = 0;
315 
316   // Check/write the tag.  If reading, then compare the tag against
317   // the passed in value and fail is they don't match.  This allows
318   // for verification that sections of the serialized data are of the
319   // correct length.
320   virtual void do_tag(int tag) = 0;
321 
322   // Read/write the oop
323   virtual void do_oop(oop* o) = 0;
324 
325   bool writing() {
326     return !reading();
327   }
328 };
329 
330 class SymbolClosure : public StackObj {
331  public:
332   virtual void do_symbol(Symbol**) = 0;
333 
334   // Clear LSB in symbol address; it can be set by CPSlot.
335   static Symbol* load_symbol(Symbol** p) {
336     return (Symbol*)(intptr_t(*p) & ~1);
337   }
338 
339   // Store symbol, adjusting new pointer if the original pointer was adjusted
340   // (symbol references in constant pool slots have their LSB set to 1).
341   static void store_symbol(Symbol** p, Symbol* sym) {
342     *p = (Symbol*)(intptr_t(sym) | (intptr_t(*p) & 1));
343   }
344 };
345 
346 // Dispatches to the non-virtual functions if OopClosureType has
347 // a concrete implementation, otherwise a virtual call is taken.
348 class Devirtualizer {
349  public:
350   template <typename OopClosureType, typename T> static void do_oop_no_verify(OopClosureType* closure, T* p);
351   template <typename OopClosureType, typename T> static void do_oop(OopClosureType* closure, T* p);
352   template <typename OopClosureType>             static void do_klass(OopClosureType* closure, Klass* k);
353   template <typename OopClosureType>             static void do_cld(OopClosureType* closure, ClassLoaderData* cld);
354   template <typename OopClosureType>             static bool do_metadata(OopClosureType* closure);
355 };
356 
357 class OopIteratorClosureDispatch {
358  public:
359   template <typename OopClosureType> static void oop_oop_iterate(OopClosureType* cl, oop obj, Klass* klass);
360   template <typename OopClosureType> static void oop_oop_iterate(OopClosureType* cl, oop obj, Klass* klass, MemRegion mr);
361   template <typename OopClosureType> static void oop_oop_iterate_backwards(OopClosureType* cl, oop obj, Klass* klass);
362 };
363 
364 #endif // SHARE_VM_MEMORY_ITERATOR_HPP