1 /*
   2  * Copyright (c) 1997, 2010, 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 #include "precompiled.hpp"
  26 #include "classfile/javaClasses.hpp"
  27 #include "classfile/systemDictionary.hpp"
  28 #include "classfile/vmSymbols.hpp"
  29 #include "gc_interface/collectedHeap.inline.hpp"
  30 #include "jvmtifiles/jvmti.h"
  31 #include "memory/gcLocker.hpp"
  32 #include "memory/universe.inline.hpp"
  33 #include "oops/arrayKlass.hpp"
  34 #include "oops/arrayKlassKlass.hpp"
  35 #include "oops/arrayOop.hpp"
  36 #include "oops/instanceKlass.hpp"
  37 #include "oops/objArrayOop.hpp"
  38 #include "oops/oop.inline.hpp"
  39 
  40 int arrayKlass::object_size(int header_size) const {
  41   // size of an array klass object
  42   assert(header_size <= instanceKlass::header_size(), "bad header size");
  43   // If this assert fails, see comments in base_create_array_klass.
  44   header_size = instanceKlass::header_size();
  45 #ifdef _LP64
  46   int size = header_size + align_object_offset(vtable_length());
  47 #else
  48   int size = header_size + vtable_length();
  49 #endif
  50   return align_object_size(size);
  51 }
  52 
  53 
  54 klassOop arrayKlass::java_super() const {
  55   if (super() == NULL)  return NULL;  // bootstrap case
  56   // Array klasses have primary supertypes which are not reported to Java.
  57   // Example super chain:  String[][] -> Object[][] -> Object[] -> Object
  58   return SystemDictionary::Object_klass();
  59 }
  60 
  61 
  62 oop arrayKlass::multi_allocate(int rank, jint* sizes, TRAPS) {
  63   ShouldNotReachHere();
  64   return NULL;
  65 }
  66 
  67 methodOop arrayKlass::uncached_lookup_method(symbolOop name, symbolOop signature) const {
  68   // There are no methods in an array klass but the super class (Object) has some
  69   assert(super(), "super klass must be present");
  70   return Klass::cast(super())->uncached_lookup_method(name, signature);
  71 }
  72 
  73 
  74 arrayKlassHandle arrayKlass::base_create_array_klass(
  75 const Klass_vtbl& cplusplus_vtbl, int header_size, KlassHandle klass, TRAPS) {
  76   // Allocation
  77   // Note: because the Java vtable must start at the same offset in all klasses,
  78   // we must insert filler fields into arrayKlass to make it the same size as instanceKlass.
  79   // If this assert fails, add filler to instanceKlass to make it bigger.
  80   assert(header_size <= instanceKlass::header_size(),
  81          "array klasses must be same size as instanceKlass");
  82   header_size = instanceKlass::header_size();
  83   // Arrays don't add any new methods, so their vtable is the same size as
  84   // the vtable of klass Object.
  85   int vtable_size = Universe::base_vtable_size();
  86   arrayKlassHandle k;
  87   KlassHandle base_klass = Klass::base_create_klass(klass,
  88                                                  header_size + vtable_size,
  89                                                  cplusplus_vtbl, CHECK_(k));
  90 
  91   // No safepoint should be possible until the handle's
  92   // target below becomes parsable
  93   No_Safepoint_Verifier no_safepoint;
  94   k = arrayKlassHandle(THREAD, base_klass());
  95 
  96   assert(!k()->is_parsable(), "not expecting parsability yet.");
  97   k->set_super(Universe::is_bootstrapping() ? (klassOop)NULL : SystemDictionary::Object_klass());
  98   k->set_layout_helper(Klass::_lh_neutral_value);
  99   k->set_dimension(1);
 100   k->set_higher_dimension(NULL);
 101   k->set_lower_dimension(NULL);
 102   k->set_component_mirror(NULL);
 103   k->set_vtable_length(vtable_size);
 104   k->set_is_cloneable(); // All arrays are considered to be cloneable (See JLS 20.1.5)
 105 
 106   assert(k()->is_parsable(), "should be parsable here.");
 107   // Make sure size calculation is right
 108   assert(k()->size() == align_object_size(header_size + vtable_size), "wrong size for object");
 109 
 110   return k;
 111 }
 112 
 113 
 114 // Initialization of vtables and mirror object is done separatly from base_create_array_klass,
 115 // since a GC can happen. At this point all instance variables of the arrayKlass must be setup.
 116 void arrayKlass::complete_create_array_klass(arrayKlassHandle k, KlassHandle super_klass, TRAPS) {
 117   ResourceMark rm(THREAD);
 118   k->initialize_supers(super_klass(), CHECK);
 119   k->vtable()->initialize_vtable(false, CHECK);
 120   java_lang_Class::create_mirror(k, CHECK);
 121 }
 122 
 123 objArrayOop arrayKlass::compute_secondary_supers(int num_extra_slots, TRAPS) {
 124   // interfaces = { cloneable_klass, serializable_klass };
 125   assert(num_extra_slots == 0, "sanity of primitive array type");
 126   // Must share this for correct bootstrapping!
 127   return Universe::the_array_interfaces_array();
 128 }
 129 
 130 bool arrayKlass::compute_is_subtype_of(klassOop k) {
 131   // An array is a subtype of Serializable, Clonable, and Object
 132   return    k == SystemDictionary::Object_klass()
 133          || k == SystemDictionary::Cloneable_klass()
 134          || k == SystemDictionary::Serializable_klass();
 135 }
 136 
 137 
 138 inline intptr_t* arrayKlass::start_of_vtable() const {
 139   // all vtables start at the same place, that's why we use instanceKlass::header_size here
 140   return ((intptr_t*)as_klassOop()) + instanceKlass::header_size();
 141 }
 142 
 143 
 144 klassVtable* arrayKlass::vtable() const {
 145   KlassHandle kh(Thread::current(), as_klassOop());
 146   return new klassVtable(kh, start_of_vtable(), vtable_length() / vtableEntry::size());
 147 }
 148 
 149 
 150 objArrayOop arrayKlass::allocate_arrayArray(int n, int length, TRAPS) {
 151   if (length < 0) {
 152     THROW_0(vmSymbols::java_lang_NegativeArraySizeException());
 153   }
 154   if (length > arrayOopDesc::max_array_length(T_ARRAY)) {
 155     report_java_out_of_memory("Requested array size exceeds VM limit");
 156     THROW_OOP_0(Universe::out_of_memory_error_array_size());
 157   }
 158   int size = objArrayOopDesc::object_size(length);
 159   klassOop k = array_klass(n+dimension(), CHECK_0);
 160   arrayKlassHandle ak (THREAD, k);
 161   objArrayOop o =
 162     (objArrayOop)CollectedHeap::array_allocate(ak, size, length, CHECK_0);
 163   // initialization to NULL not necessary, area already cleared
 164   return o;
 165 }
 166 
 167 
 168 void arrayKlass::array_klasses_do(void f(klassOop k)) {
 169   klassOop k = as_klassOop();
 170   // Iterate over this array klass and all higher dimensions
 171   while (k != NULL) {
 172     f(k);
 173     k = arrayKlass::cast(k)->higher_dimension();
 174   }
 175 }
 176 
 177 
 178 void arrayKlass::with_array_klasses_do(void f(klassOop k)) {
 179   array_klasses_do(f);
 180 }
 181 
 182 // JVM support
 183 
 184 jint arrayKlass::compute_modifier_flags(TRAPS) const {
 185   return JVM_ACC_ABSTRACT | JVM_ACC_FINAL | JVM_ACC_PUBLIC;
 186 }
 187 
 188 // JVMTI support
 189 
 190 jint arrayKlass::jvmti_class_status() const {
 191   return JVMTI_CLASS_STATUS_ARRAY;
 192 }
 193 
 194 // Printing
 195 
 196 void arrayKlass::oop_print_on(oop obj, outputStream* st) {
 197   assert(obj->is_array(), "must be array");
 198   Klass::oop_print_on(obj, st);
 199   st->print_cr(" - length: %d", arrayOop(obj)->length());
 200 }
 201 
 202 // Verification
 203 
 204 void arrayKlass::oop_verify_on(oop obj, outputStream* st) {
 205   guarantee(obj->is_array(), "must be array");
 206   arrayOop a = arrayOop(obj);
 207   guarantee(a->length() >= 0, "array with negative length?");
 208 }