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_VM_OOPS_OBJARRAYKLASS_HPP
  26 #define SHARE_VM_OOPS_OBJARRAYKLASS_HPP
  27 
  28 #include "classfile/classLoaderData.hpp"
  29 #include "memory/specialized_oop_closures.hpp"
  30 #include "oops/arrayKlass.hpp"
  31 #include "utilities/macros.hpp"
  32 
  33 // ObjArrayKlass is the klass for objArrays
  34 
  35 class ObjArrayKlass : public ArrayKlass {
  36   friend class VMStructs;
  37  private:
  38   Klass* _element_klass;            // The klass of the elements of this array type
  39   Klass* _bottom_klass;             // The one-dimensional type (InstanceKlass or TypeArrayKlass)
  40 
  41   // Constructor
  42   ObjArrayKlass(int n, KlassHandle element_klass, Symbol* name);
  43   static ObjArrayKlass* allocate(ClassLoaderData* loader_data, int n, KlassHandle klass_handle, Symbol* name, TRAPS);
  44  public:
  45   // For dummy objects
  46   ObjArrayKlass() {}
  47 
  48   // Instance variables
  49   Klass* element_klass() const      { return _element_klass; }
  50   void set_element_klass(Klass* k)  { _element_klass = k; }
  51   Klass** element_klass_addr()      { return &_element_klass; }
  52 
  53   Klass* bottom_klass() const       { return _bottom_klass; }
  54   void set_bottom_klass(Klass* k)   { _bottom_klass = k; }
  55   Klass** bottom_klass_addr()       { return &_bottom_klass; }
  56 
  57   // Compiler/Interpreter offset
  58   static ByteSize element_klass_offset() { return in_ByteSize(offset_of(ObjArrayKlass, _element_klass)); }
  59 
  60   // Dispatched operation
  61   bool can_be_primary_super_slow() const;
  62   GrowableArray<Klass*>* compute_secondary_supers(int num_extra_slots);
  63   bool compute_is_subtype_of(Klass* k);
  64   bool oop_is_objArray_slow()  const  { return true; }
  65   int oop_size(oop obj) const;
  66 
  67   // Allocation
  68   static Klass* allocate_objArray_klass(ClassLoaderData* loader_data,
  69                                           int n, KlassHandle element_klass, TRAPS);
  70 
  71   objArrayOop allocate(int length, TRAPS);
  72   oop multi_allocate(int rank, jint* sizes, TRAPS);
  73 
  74   // Copying
  75   void  copy_array(arrayOop s, int src_pos, arrayOop d, int dst_pos, int length, TRAPS);
  76 
  77   // Compute protection domain
  78   oop protection_domain() const { return bottom_klass()->protection_domain(); }
  79 
  80  private:
  81   // Either oop or narrowOop depending on UseCompressedOops.
  82   // must be called from within ObjArrayKlass.cpp
  83   template <class T> void do_copy(arrayOop s, T* src, arrayOop d,
  84                                   T* dst, int length, TRAPS);
  85  protected:
  86   // Returns the ObjArrayKlass for n'th dimension.
  87   virtual Klass* array_klass_impl(bool or_null, int n, TRAPS);
  88 
  89   // Returns the array class with this class as element type.
  90   virtual Klass* array_klass_impl(bool or_null, TRAPS);
  91 
  92  public:
  93   // Casting from Klass*
  94   static ObjArrayKlass* cast(Klass* k) {
  95     assert(k->oop_is_objArray(), "cast to ObjArrayKlass");
  96     return (ObjArrayKlass*) k;
  97   }
  98 
  99   static const ObjArrayKlass* cast(const Klass* k) {
 100     assert(k->oop_is_objArray(), "cast to ObjArrayKlass");
 101     return static_cast<const ObjArrayKlass*>(k);
 102   }
 103 
 104   // Sizing
 105   static int header_size()                { return sizeof(ObjArrayKlass)/HeapWordSize; }
 106   int size() const                        { return ArrayKlass::static_size(header_size()); }
 107 
 108   // Initialization (virtual from Klass)
 109   void initialize(TRAPS);
 110 
 111   // Garbage collection
 112   void oop_follow_contents(oop obj);
 113   inline void oop_follow_contents(oop obj, int index);
 114   template <class T> inline void objarray_follow_contents(oop obj, int index);
 115 
 116   int  oop_adjust_pointers(oop obj);
 117 
 118   // Parallel Scavenge and Parallel Old
 119   PARALLEL_GC_DECLS
 120 #if INCLUDE_ALL_GCS
 121   inline void oop_follow_contents(ParCompactionManager* cm, oop obj, int index);
 122   template <class T> inline void
 123     objarray_follow_contents(ParCompactionManager* cm, oop obj, int index);
 124 #endif // INCLUDE_ALL_GCS
 125 
 126   // Iterators
 127   int oop_oop_iterate(oop obj, ExtendedOopClosure* blk) {
 128     return oop_oop_iterate_v(obj, blk);
 129   }
 130   int oop_oop_iterate_m(oop obj, ExtendedOopClosure* blk, MemRegion mr) {
 131     return oop_oop_iterate_v_m(obj, blk, mr);
 132   }
 133 #define ObjArrayKlass_OOP_OOP_ITERATE_DECL(OopClosureType, nv_suffix)   \
 134   int oop_oop_iterate##nv_suffix(oop obj, OopClosureType* blk);         \
 135   int oop_oop_iterate##nv_suffix##_m(oop obj, OopClosureType* blk,      \
 136                                      MemRegion mr);                     \
 137   int oop_oop_iterate_range##nv_suffix(oop obj, OopClosureType* blk,    \
 138                                      int start, int end);
 139 
 140   ALL_OOP_OOP_ITERATE_CLOSURES_1(ObjArrayKlass_OOP_OOP_ITERATE_DECL)
 141   ALL_OOP_OOP_ITERATE_CLOSURES_2(ObjArrayKlass_OOP_OOP_ITERATE_DECL)
 142 
 143   // JVM support
 144   jint compute_modifier_flags(TRAPS) const;
 145 
 146  public:
 147   // Printing
 148   void print_on(outputStream* st) const;
 149   void print_value_on(outputStream* st) const;
 150 
 151   void oop_print_value_on(oop obj, outputStream* st);
 152 #ifndef PRODUCT
 153   void oop_print_on      (oop obj, outputStream* st);
 154 #endif //PRODUCT
 155 
 156   const char* internal_name() const;
 157 
 158   // Verification
 159   void verify_on(outputStream* st);
 160 
 161   void oop_verify_on(oop obj, outputStream* st);
 162 };
 163 
 164 #endif // SHARE_VM_OOPS_OBJARRAYKLASS_HPP