1 /*
   2  * Copyright (c) 1997, 2013, 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 "oops/instanceKlass.hpp"
  29 #include "utilities/macros.hpp"
  30 
  31 // An InstanceRefKlass is a specialized InstanceKlass for Java
  32 // classes that are subclasses of java/lang/ref/Reference.
  33 //
  34 // These classes are used to implement soft/weak/final/phantom
  35 // references and finalization, and need special treatment by the
  36 // garbage collector.
  37 //
  38 // During GC discovered reference objects are added (chained) to one
  39 // of the four lists below, depending on the type of reference.
  40 // The linked occurs through the next field in class java/lang/ref/Reference.
  41 //
  42 // Afterwards, the discovered references are processed in decreasing
  43 // order of reachability. Reference objects eligible for notification
  44 // are linked to the static pending_list in class java/lang/ref/Reference,
  45 // and the pending list lock object in the same class is notified.
  46 
  47 
  48 class InstanceRefKlass: public InstanceKlass {
  49   friend class InstanceKlass;
  50 
  51   // Constructor
  52   InstanceRefKlass(int vtable_len, int itable_len, int static_field_size, int nonstatic_oop_map_size, ReferenceType rt, AccessFlags access_flags, bool is_anonymous)
  53     : InstanceKlass(vtable_len, itable_len, static_field_size, nonstatic_oop_map_size, rt, access_flags, is_anonymous) {}
  54 
  55  public:
  56   InstanceRefKlass() { assert(DumpSharedSpaces || UseSharedSpaces, "only for CDS"); }
  57   // Type testing
  58   bool oop_is_instanceRef() const             { return true; }
  59 
  60   // Casting from Klass*
  61   static InstanceRefKlass* cast(Klass* k) {
  62     assert(k->oop_is_instanceRef(), "cast to InstanceRefKlass");
  63     return (InstanceRefKlass*) k;
  64   }
  65 
  66   // Garbage collection
  67   int  oop_adjust_pointers(oop obj);
  68   void oop_follow_contents(oop obj);
  69 
  70   // Parallel Scavenge and Parallel Old
  71   PARALLEL_GC_DECLS
  72 
  73 private:
  74   template<class OopClosureType, class OopType, bool is_extended>
  75   void do_metadata_if_applicable(oop obj, OopClosureType *cl);
  76 
  77 public:
  78   template<class OopClosureType>
  79   int oop_iterate_and_dispatch(oop obj, OopClosureType *cl);
  80 
  81   int get_linear_oop_intervals(oop obj, OopInterval* &start, int &size) { return -_instance_ref_klass; }
  82 
  83   int oop_oop_iterate(oop obj, ExtendedOopClosure* blk) {
  84     return oop_oop_iterate_v(obj, blk);
  85   }
  86   int oop_oop_iterate_m(oop obj, ExtendedOopClosure* blk, MemRegion mr) {
  87     return oop_oop_iterate_v_m(obj, blk, mr);
  88   }
  89 
  90 #define InstanceRefKlass_OOP_OOP_ITERATE_DECL(OopClosureType, nv_suffix)                \
  91   int oop_oop_iterate##nv_suffix(oop obj, OopClosureType* blk);                         \
  92   int oop_oop_iterate##nv_suffix##_m(oop obj, OopClosureType* blk, MemRegion mr);
  93 
  94   ALL_OOP_OOP_ITERATE_CLOSURES_1(InstanceRefKlass_OOP_OOP_ITERATE_DECL)
  95   ALL_OOP_OOP_ITERATE_CLOSURES_2(InstanceRefKlass_OOP_OOP_ITERATE_DECL)
  96 
  97 #if INCLUDE_ALL_GCS
  98 #define InstanceRefKlass_OOP_OOP_ITERATE_BACKWARDS_DECL(OopClosureType, nv_suffix)      \
  99   int oop_oop_iterate_backwards##nv_suffix(oop obj, OopClosureType* blk);
 100 
 101   ALL_OOP_OOP_ITERATE_CLOSURES_1(InstanceRefKlass_OOP_OOP_ITERATE_BACKWARDS_DECL)
 102   ALL_OOP_OOP_ITERATE_CLOSURES_2(InstanceRefKlass_OOP_OOP_ITERATE_BACKWARDS_DECL)
 103 #endif // INCLUDE_ALL_GCS
 104 
 105   static void release_and_notify_pending_list_lock(BasicLock *pending_list_basic_lock);
 106   static void acquire_pending_list_lock(BasicLock *pending_list_basic_lock);
 107   static bool owns_pending_list_lock(JavaThread* thread);
 108 
 109   // Update non-static oop maps so 'referent', 'nextPending' and
 110   // 'discovered' will look like non-oops
 111   static void update_nonstatic_oop_maps(Klass* k);
 112 
 113  public:
 114   // Verification
 115   void oop_verify_on(oop obj, outputStream* st);
 116 };
 117 
 118 #endif // SHARE_VM_OOPS_INSTANCEREFKLASS_HPP