1 /*
   2  * Copyright (c) 1997, 2015, 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_OOPS_INSTANCEREFKLASS_HPP
  26 #define SHARE_VM_OOPS_INSTANCEREFKLASS_HPP
  27 
  28 #include "gc/shared/specialized_oop_closures.hpp"
  29 #include "oops/instanceKlass.hpp"
  30 #include "utilities/macros.hpp"
  31 
  32 // An InstanceRefKlass is a specialized InstanceKlass for Java
  33 // classes that are subclasses of java/lang/ref/Reference.
  34 //
  35 // These classes are used to implement soft/weak/final/phantom
  36 // references and finalization, and need special treatment by the
  37 // garbage collector.
  38 //
  39 // During GC discovered reference objects are added (chained) to one
  40 // of the four lists below, depending on the type of reference.
  41 // The linked occurs through the next field in class java/lang/ref/Reference.
  42 //
  43 // Afterwards, the discovered references are processed in decreasing
  44 // order of reachability. Reference objects eligible for notification
  45 // are linked to the static pending_list in class java/lang/ref/Reference,
  46 // and the pending list lock object in the same class is notified.
  47 
  48 
  49 class InstanceRefKlass: public InstanceKlass {
  50   friend class InstanceKlass;
  51 
  52   // Constructor
  53   InstanceRefKlass(int vtable_len, int itable_len, int static_field_size, int nonstatic_oop_map_size, ReferenceType rt, AccessFlags access_flags, bool is_anonymous)
  54     : InstanceKlass(vtable_len, itable_len, static_field_size, nonstatic_oop_map_size, rt, access_flags, is_anonymous) {}
  55 
  56  public:
  57   InstanceRefKlass() { assert(DumpSharedSpaces || UseSharedSpaces, "only for CDS"); }
  58   // Type testing
  59   bool oop_is_instanceRef() const             { return true; }
  60 
  61   // Casting from Klass*
  62   static InstanceRefKlass* cast(Klass* k) {
  63     assert(k->oop_is_instanceRef(), "cast to InstanceRefKlass");
  64     return (InstanceRefKlass*) k;
  65   }
  66 
  67   // GC specific object visitors
  68   //
  69   // Mark Sweep
  70   int  oop_ms_adjust_pointers(oop obj);
  71 #if INCLUDE_ALL_GCS
  72   // Parallel Scavenge
  73   void oop_ps_push_contents(  oop obj, PSPromotionManager* pm);
  74   // Parallel Compact
  75   void oop_pc_follow_contents(oop obj, ParCompactionManager* cm);
  76   void oop_pc_update_pointers(oop obj);
  77 #endif
  78 
  79   // Oop fields (and metadata) iterators
  80   //  [nv = true]  Use non-virtual calls to do_oop_nv.
  81   //  [nv = false] Use virtual calls to do_oop.
  82   //
  83   // The InstanceRefKlass iterators also support reference processing.
  84 
  85 
  86   // Forward iteration
  87 private:
  88   // Iterate over all oop fields and metadata.
  89   template <bool nv, class OopClosureType>
  90   inline int oop_oop_iterate(oop obj, OopClosureType* closure);
  91 
  92   // Reverse iteration
  93 #if INCLUDE_ALL_GCS
  94   // Iterate over all oop fields and metadata.
  95   template <bool nv, class OopClosureType>
  96   inline int oop_oop_iterate_reverse(oop obj, OopClosureType* closure);
  97 #endif // INCLUDE_ALL_GCS
  98 
  99   // Bounded range iteration
 100   // Iterate over all oop fields and metadata.
 101   template <bool nv, class OopClosureType>
 102   inline int oop_oop_iterate_bounded(oop obj, OopClosureType* closure, MemRegion mr);
 103 
 104   // Reference processing part of the iterators.
 105 
 106   // Specialized for [T = oop] or [T = narrowOop].
 107   template <bool nv, typename T, class OopClosureType, class Contains>
 108   inline void oop_oop_iterate_ref_processing_specialized(oop obj, OopClosureType* closure, Contains& contains);
 109 
 110   // Only perform reference processing if the referent object is within mr.
 111   template <bool nv, class OopClosureType>
 112   inline void oop_oop_iterate_ref_processing_bounded(oop obj, OopClosureType* closure, MemRegion mr);
 113 
 114   // Reference processing
 115   template <bool nv, class OopClosureType>
 116   inline void oop_oop_iterate_ref_processing(oop obj, OopClosureType* closure);
 117 
 118 
 119  public:
 120 
 121   ALL_OOP_OOP_ITERATE_CLOSURES_1(OOP_OOP_ITERATE_DECL)
 122   ALL_OOP_OOP_ITERATE_CLOSURES_2(OOP_OOP_ITERATE_DECL)
 123 
 124 #if INCLUDE_ALL_GCS
 125   ALL_OOP_OOP_ITERATE_CLOSURES_1(OOP_OOP_ITERATE_DECL_BACKWARDS)
 126   ALL_OOP_OOP_ITERATE_CLOSURES_2(OOP_OOP_ITERATE_DECL_BACKWARDS)
 127 #endif // INCLUDE_ALL_GCS
 128 
 129   static void release_and_notify_pending_list_lock(BasicLock *pending_list_basic_lock);
 130   static void acquire_pending_list_lock(BasicLock *pending_list_basic_lock);
 131   static bool owns_pending_list_lock(JavaThread* thread);
 132 
 133   // Update non-static oop maps so 'referent', 'nextPending' and
 134   // 'discovered' will look like non-oops
 135   static void update_nonstatic_oop_maps(Klass* k);
 136 
 137  public:
 138   // Verification
 139   void oop_verify_on(oop obj, outputStream* st);
 140 };
 141 
 142 #endif // SHARE_VM_OOPS_INSTANCEREFKLASS_HPP