1 /*
  2  * Copyright (c) 1997, 2019, 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_OOPS_INSTANCEREFKLASS_HPP
 26 #define SHARE_OOPS_INSTANCEREFKLASS_HPP
 27 
 28 #include "oops/instanceKlass.hpp"
 29 #include "utilities/macros.hpp"
 30 
 31 class ClassFileParser;
 32 
 33 // An InstanceRefKlass is a specialized InstanceKlass for Java
 34 // classes that are subclasses of java/lang/ref/Reference.
 35 //
 36 // These classes are used to implement soft/weak/final/phantom
 37 // references and finalization, and need special treatment by the
 38 // garbage collector.
 39 //
 40 // During GC discovered reference objects are added (chained) to one
 41 // of the four lists below, depending on the type of reference.
 42 // The linked occurs through the next field in class java/lang/ref/Reference.
 43 //
 44 // Afterwards, the discovered references are processed in decreasing
 45 // order of reachability. Reference objects eligible for notification
 46 // are linked to the static pending_list in class java/lang/ref/Reference,
 47 // and the pending list lock object in the same class is notified.
 48 
 49 
 50 class InstanceRefKlass: public InstanceKlass {
 51   friend class InstanceKlass;
 52  public:
 53   static const KlassID ID = InstanceRefKlassID;
 54 
 55  private:
 56   InstanceRefKlass(const ClassFileParser& parser) : InstanceKlass(parser, InstanceKlass::_kind_reference, ID) {}
 57 
 58  public:
 59   InstanceRefKlass() { assert(DumpSharedSpaces || UseSharedSpaces, "only for CDS"); }
 60 
 61   // Oop fields (and metadata) iterators
 62   //
 63   // The InstanceRefKlass iterators also support reference processing.
 64 
 65 
 66   // Forward iteration
 67   // Iterate over all oop fields and metadata.
 68   template <typename T, class OopClosureType>
 69   inline void oop_oop_iterate(oop obj, OopClosureType* closure);
 70 
 71   // Reverse iteration
 72   // Iterate over all oop fields and metadata.
 73   template <typename T, class OopClosureType>
 74   inline void oop_oop_iterate_reverse(oop obj, OopClosureType* closure);
 75 
 76   // Bounded range iteration
 77   // Iterate over all oop fields and metadata.
 78   template <typename T, class OopClosureType>
 79   inline void oop_oop_iterate_bounded(oop obj, OopClosureType* closure, MemRegion mr);
 80 
 81   private:
 82 
 83   // Reference processing part of the iterators.
 84 
 85   template <typename T, class OopClosureType, class Contains>
 86   inline void oop_oop_iterate_ref_processing(oop obj, OopClosureType* closure, Contains& contains);
 87 
 88   // Only perform reference processing if the referent object is within mr.
 89   template <typename T, class OopClosureType>
 90   inline void oop_oop_iterate_ref_processing_bounded(oop obj, OopClosureType* closure, MemRegion mr);
 91 
 92   // Reference processing
 93   template <typename T, class OopClosureType>
 94   inline void oop_oop_iterate_ref_processing(oop obj, OopClosureType* closure);
 95 
 96   // Building blocks for specialized handling.
 97   template <typename T, class OopClosureType, class Contains>
 98   static void do_referent(oop obj, OopClosureType* closure, Contains& contains);
 99 
100   template <typename T, class OopClosureType, class Contains>
101   static void do_next(oop obj, OopClosureType* closure, Contains& contains);
102 
103   template <typename T, class OopClosureType, class Contains>
104   static void do_discovered(oop obj, OopClosureType* closure, Contains& contains);
105 
106   template <typename T, class OopClosureType>
107   static bool try_discover(oop obj, ReferenceType type, OopClosureType* closure);
108 
109   // Do discovery while handling InstanceRefKlasses. Reference discovery
110   // is only done if the closure provides a ReferenceProcessor.
111   template <typename T, class OopClosureType, class Contains>
112   static void oop_oop_iterate_discovery(oop obj, ReferenceType type, OopClosureType* closure, Contains& contains);
113 
114   // Used for a special case in G1 where the closure needs to be applied
115   // to the discovered field. Reference discovery is also done if the
116   // closure provides a ReferenceProcessor.
117   template <typename T, class OopClosureType, class Contains>
118   static void oop_oop_iterate_discovered_and_discovery(oop obj, ReferenceType type, OopClosureType* closure, Contains& contains);
119 
120   // Apply the closure to all fields. No reference discovery is done.
121   template <typename T, class OopClosureType, class Contains>
122   static void oop_oop_iterate_fields(oop obj, OopClosureType* closure, Contains& contains);
123 
124   // Apply the closure to all fields, except the referent field. No reference discovery is done.
125   template <typename T, class OopClosureType, class Contains>
126   static void oop_oop_iterate_fields_except_referent(oop obj, OopClosureType* closure, Contains& contains);
127 
128   template <typename T>
129   void trace_reference_gc(const char *s, oop obj) NOT_DEBUG_RETURN;
130 
131  public:
132   // Update non-static oop maps so 'referent', 'nextPending' and
133   // 'discovered' will look like non-oops
134   static void update_nonstatic_oop_maps(Klass* k);
135 
136  public:
137   // Verification
138   void oop_verify_on(oop obj, outputStream* st);
139 };
140 
141 #endif // SHARE_OOPS_INSTANCEREFKLASS_HPP