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