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