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_OOPS_OBJARRAYKLASS_HPP
 26 #define SHARE_VM_OOPS_OBJARRAYKLASS_HPP
 27 
 28 #include "classfile/classLoaderData.hpp"
 29 #include "oops/arrayKlass.hpp"
 30 #include "utilities/macros.hpp"
 31 
 32 // ObjArrayKlass is the klass for objArrays
 33 
 34 class ObjArrayKlass : public ArrayKlass {
 35   friend class VMStructs;
 36   friend class JVMCIVMStructs;
 37 
 38  public:
 39   static const KlassID ID = ObjArrayKlassID;
 40 
 41  private:
 42   // If you add a new field that points to any metaspace object, you
 43   // must add this field to ObjArrayKlass::metaspace_pointers_do().
 44   Klass* _element_klass;            // The klass of the elements of this array type
 45   Klass* _bottom_klass;             // The one-dimensional type (InstanceKlass or TypeArrayKlass)
 46 
 47   // Constructor
 48   ObjArrayKlass(int n, Klass* element_klass, Symbol* name);
 49   static ObjArrayKlass* allocate(ClassLoaderData* loader_data, int n, Klass* k, Symbol* name, TRAPS);
 50  public:
 51   // For dummy objects
 52   ObjArrayKlass() {}
 53 
 54   // Instance variables
 55   Klass* element_klass() const      { return _element_klass; }
 56   void set_element_klass(Klass* k)  { _element_klass = k; }
 57   Klass** element_klass_addr()      { return &_element_klass; }
 58 
 59   Klass* bottom_klass() const       { return _bottom_klass; }
 60   void set_bottom_klass(Klass* k)   { _bottom_klass = k; }
 61   Klass** bottom_klass_addr()       { return &_bottom_klass; }
 62 
 63   ModuleEntry* module() const;
 64   PackageEntry* package() const;
 65 
 66   // Compiler/Interpreter offset
 67   static ByteSize element_klass_offset() { return in_ByteSize(offset_of(ObjArrayKlass, _element_klass)); }
 68 
 69   // Dispatched operation
 70   bool can_be_primary_super_slow() const;
 71   GrowableArray<Klass*>* compute_secondary_supers(int num_extra_slots,
 72                                                   Array<InstanceKlass*>* transitive_interfaces);
 73   bool compute_is_subtype_of(Klass* k);
 74   DEBUG_ONLY(bool is_objArray_klass_slow()  const  { return true; })
 75   int oop_size(oop obj) const;
 76 
 77   // Allocation
 78   static Klass* allocate_objArray_klass(ClassLoaderData* loader_data,
 79                                           int n, Klass* element_klass, TRAPS);
 80 
 81   objArrayOop allocate(int length, TRAPS);
 82   oop multi_allocate(int rank, jint* sizes, TRAPS);
 83 
 84   // Copying
 85   void  copy_array(arrayOop s, int src_pos, arrayOop d, int dst_pos, int length, TRAPS);
 86 
 87   // Compute protection domain
 88   oop protection_domain() const { return bottom_klass()->protection_domain(); }
 89 
 90   virtual void metaspace_pointers_do(MetaspaceClosure* iter);
 91 
 92  private:
 93   // Either oop or narrowOop depending on UseCompressedOops.
 94   // must be called from within ObjArrayKlass.cpp
 95   void do_copy(arrayOop s, size_t src_offset,
 96                arrayOop d, size_t dst_offset,
 97                int length, TRAPS);
 98  protected:
 99   // Returns the ObjArrayKlass for n'th dimension.
100   virtual Klass* array_klass_impl(bool or_null, int n, TRAPS);
101 
102   // Returns the array class with this class as element type.
103   virtual Klass* array_klass_impl(bool or_null, TRAPS);
104 
105  public:
106 
107   static ObjArrayKlass* cast(Klass* k) {
108     return const_cast<ObjArrayKlass*>(cast(const_cast<const Klass*>(k)));
109   }
110 
111   static const ObjArrayKlass* cast(const Klass* k) {
112     assert(k->is_objArray_klass(), "cast to ObjArrayKlass");
113     return static_cast<const ObjArrayKlass*>(k);
114   }
115 
116   // Sizing
117   static int header_size()                { return sizeof(ObjArrayKlass)/wordSize; }
118   int size() const                        { return ArrayKlass::static_size(header_size()); }
119 
120   // Initialization (virtual from Klass)
121   void initialize(TRAPS);
122 
123   // GC specific object visitors
124   //
125 #if INCLUDE_PARALLELGC
126   // Parallel Compact
127   void oop_pc_update_pointers(oop obj, ParCompactionManager* cm);
128 #endif
129 
130   // Oop fields (and metadata) iterators
131   //
132   // The ObjArrayKlass iterators also visits the Object's klass.
133 
134   // Iterate over oop elements and metadata.
135   template <typename T, typename OopClosureType>
136   inline void oop_oop_iterate(oop obj, OopClosureType* closure);
137 
138   // Iterate over oop elements and metadata.
139   template <typename T, typename OopClosureType>
140   inline void oop_oop_iterate_reverse(oop obj, OopClosureType* closure);
141 
142   // Iterate over oop elements within mr, and metadata.
143   template <typename T, typename OopClosureType>
144   inline void oop_oop_iterate_bounded(oop obj, OopClosureType* closure, MemRegion mr);
145 
146   // Iterate over oop elements within [start, end), and metadata.
147   template <typename T, class OopClosureType>
148   inline void oop_oop_iterate_range(objArrayOop a, OopClosureType* closure, int start, int end);
149 
150  public:
151   // Iterate over all oop elements.
152   template <typename T, class OopClosureType>
153   inline void oop_oop_iterate_elements(objArrayOop a, OopClosureType* closure);
154 
155  private:
156   // Iterate over all oop elements with indices within mr.
157   template <typename T, class OopClosureType>
158   inline void oop_oop_iterate_elements_bounded(objArrayOop a, OopClosureType* closure, void* low, void* high);
159 
160   template <typename T, class OopClosureType>
161   inline void oop_oop_iterate_elements_bounded(objArrayOop a, OopClosureType* closure, MemRegion mr);
162 
163  public:
164   // JVM support
165   jint compute_modifier_flags(TRAPS) const;
166 
167  public:
168   // Printing
169   void print_on(outputStream* st) const;
170   void print_value_on(outputStream* st) const;
171 
172   void oop_print_value_on(oop obj, outputStream* st);
173 #ifndef PRODUCT
174   void oop_print_on      (oop obj, outputStream* st);
175 #endif //PRODUCT
176 
177   const char* internal_name() const;
178 
179   // Verification
180   void verify_on(outputStream* st);
181 
182   void oop_verify_on(oop obj, outputStream* st);
183 };
184 
185 #endif // SHARE_VM_OOPS_OBJARRAYKLASS_HPP