arrayKlass.cpp 7.3 KB
Newer Older
D
duke 已提交
1
/*
2
 * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
D
duke 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
19 20 21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
D
duke 已提交
22 23 24
 *
 */

25 26 27 28 29 30 31 32 33 34 35 36 37
#include "precompiled.hpp"
#include "classfile/javaClasses.hpp"
#include "classfile/systemDictionary.hpp"
#include "classfile/vmSymbols.hpp"
#include "gc_interface/collectedHeap.inline.hpp"
#include "jvmtifiles/jvmti.h"
#include "memory/gcLocker.hpp"
#include "memory/universe.inline.hpp"
#include "oops/arrayKlass.hpp"
#include "oops/arrayOop.hpp"
#include "oops/instanceKlass.hpp"
#include "oops/objArrayOop.hpp"
#include "oops/oop.inline.hpp"
D
duke 已提交
38

39
int ArrayKlass::static_size(int header_size) {
D
duke 已提交
40
  // size of an array klass object
41
  assert(header_size <= InstanceKlass::header_size(), "bad header size");
D
duke 已提交
42
  // If this assert fails, see comments in base_create_array_klass.
43 44
  header_size = InstanceKlass::header_size();
  int vtable_len = Universe::base_vtable_size();
D
duke 已提交
45
#ifdef _LP64
46
  int size = header_size + align_object_offset(vtable_len);
D
duke 已提交
47
#else
48
  int size = header_size + vtable_len;
D
duke 已提交
49 50 51 52 53
#endif
  return align_object_size(size);
}


54
Klass* ArrayKlass::java_super() const {
D
duke 已提交
55 56 57
  if (super() == NULL)  return NULL;  // bootstrap case
  // Array klasses have primary supertypes which are not reported to Java.
  // Example super chain:  String[][] -> Object[][] -> Object[] -> Object
58
  return SystemDictionary::Object_klass();
D
duke 已提交
59 60 61
}


62
oop ArrayKlass::multi_allocate(int rank, jint* sizes, TRAPS) {
D
duke 已提交
63 64 65 66
  ShouldNotReachHere();
  return NULL;
}

67
Method* ArrayKlass::uncached_lookup_method(Symbol* name, Symbol* signature, MethodLookupMode mode) const {
D
duke 已提交
68 69
  // There are no methods in an array klass but the super class (Object) has some
  assert(super(), "super klass must be present");
70
  return super()->uncached_lookup_method(name, signature, mode);
D
duke 已提交
71 72
}

73
ArrayKlass::ArrayKlass(Symbol* name) {
74
  set_name(name);
D
duke 已提交
75

76 77 78 79 80 81
  set_super(Universe::is_bootstrapping() ? (Klass*)NULL : SystemDictionary::Object_klass());
  set_layout_helper(Klass::_lh_neutral_value);
  set_dimension(1);
  set_higher_dimension(NULL);
  set_lower_dimension(NULL);
  set_component_mirror(NULL);
D
duke 已提交
82 83 84
  // Arrays don't add any new methods, so their vtable is the same size as
  // the vtable of klass Object.
  int vtable_size = Universe::base_vtable_size();
85 86
  set_vtable_length(vtable_size);
  set_is_cloneable(); // All arrays are considered to be cloneable (See JLS 20.1.5)
D
duke 已提交
87 88 89 90
}


// Initialization of vtables and mirror object is done separatly from base_create_array_klass,
91 92
// since a GC can happen. At this point all instance variables of the ArrayKlass must be setup.
void ArrayKlass::complete_create_array_klass(ArrayKlass* k, KlassHandle super_klass, TRAPS) {
D
duke 已提交
93 94 95
  ResourceMark rm(THREAD);
  k->initialize_supers(super_klass(), CHECK);
  k->vtable()->initialize_vtable(false, CHECK);
96
  java_lang_Class::create_mirror(k, Handle(NULL), CHECK);
D
duke 已提交
97 98
}

99
GrowableArray<Klass*>* ArrayKlass::compute_secondary_supers(int num_extra_slots) {
D
duke 已提交
100 101 102
  // interfaces = { cloneable_klass, serializable_klass };
  assert(num_extra_slots == 0, "sanity of primitive array type");
  // Must share this for correct bootstrapping!
103 104
  set_secondary_supers(Universe::the_array_interfaces_array());
  return NULL;
D
duke 已提交
105 106
}

107
bool ArrayKlass::compute_is_subtype_of(Klass* k) {
D
duke 已提交
108
  // An array is a subtype of Serializable, Clonable, and Object
109 110 111
  return    k == SystemDictionary::Object_klass()
         || k == SystemDictionary::Cloneable_klass()
         || k == SystemDictionary::Serializable_klass();
D
duke 已提交
112 113 114
}


115
inline intptr_t* ArrayKlass::start_of_vtable() const {
116 117
  // all vtables start at the same place, that's why we use InstanceKlass::header_size here
  return ((intptr_t*)this) + InstanceKlass::header_size();
D
duke 已提交
118 119 120
}


121
klassVtable* ArrayKlass::vtable() const {
122
  KlassHandle kh(Thread::current(), this);
D
duke 已提交
123 124 125 126
  return new klassVtable(kh, start_of_vtable(), vtable_length() / vtableEntry::size());
}


127
objArrayOop ArrayKlass::allocate_arrayArray(int n, int length, TRAPS) {
D
duke 已提交
128 129 130 131
  if (length < 0) {
    THROW_0(vmSymbols::java_lang_NegativeArraySizeException());
  }
  if (length > arrayOopDesc::max_array_length(T_ARRAY)) {
132
    report_java_out_of_memory("Requested array size exceeds VM limit");
133
    JvmtiExport::post_array_size_exhausted();
D
duke 已提交
134 135 136
    THROW_OOP_0(Universe::out_of_memory_error_array_size());
  }
  int size = objArrayOopDesc::object_size(length);
137
  Klass* k = array_klass(n+dimension(), CHECK_0);
138
  ArrayKlass* ak = ArrayKlass::cast(k);
D
duke 已提交
139 140 141 142 143 144
  objArrayOop o =
    (objArrayOop)CollectedHeap::array_allocate(ak, size, length, CHECK_0);
  // initialization to NULL not necessary, area already cleared
  return o;
}

145
void ArrayKlass::array_klasses_do(void f(Klass* k, TRAPS), TRAPS) {
146 147 148 149
  Klass* k = this;
  // Iterate over this array klass and all higher dimensions
  while (k != NULL) {
    f(k, CHECK);
150
    k = ArrayKlass::cast(k)->higher_dimension();
151 152
  }
}
D
duke 已提交
153

154
void ArrayKlass::array_klasses_do(void f(Klass* k)) {
155
  Klass* k = this;
D
duke 已提交
156 157 158
  // Iterate over this array klass and all higher dimensions
  while (k != NULL) {
    f(k);
159
    k = ArrayKlass::cast(k)->higher_dimension();
D
duke 已提交
160 161 162
  }
}

163 164
// GC support

165
void ArrayKlass::oops_do(OopClosure* cl) {
166 167 168 169 170
  Klass::oops_do(cl);

  cl->do_oop(adr_component_mirror());
}

D
duke 已提交
171 172
// JVM support

173
jint ArrayKlass::compute_modifier_flags(TRAPS) const {
D
duke 已提交
174 175 176 177 178
  return JVM_ACC_ABSTRACT | JVM_ACC_FINAL | JVM_ACC_PUBLIC;
}

// JVMTI support

179
jint ArrayKlass::jvmti_class_status() const {
D
duke 已提交
180 181 182
  return JVMTI_CLASS_STATUS_ARRAY;
}

183
void ArrayKlass::remove_unshareable_info() {
184 185 186 187 188
  Klass::remove_unshareable_info();
  // Clear the java mirror
  set_component_mirror(NULL);
}

189
void ArrayKlass::restore_unshareable_info(TRAPS) {
190 191 192 193
  Klass::restore_unshareable_info(CHECK);
  // Klass recreates the component mirror also
}

D
duke 已提交
194 195
// Printing

196
void ArrayKlass::print_on(outputStream* st) const {
197 198 199 200
  assert(is_klass(), "must be klass");
  Klass::print_on(st);
}

201
void ArrayKlass::print_value_on(outputStream* st) const {
202 203 204 205 206 207
  assert(is_klass(), "must be klass");
  for(int index = 0; index < dimension(); index++) {
    st->print("[]");
  }
}

208
void ArrayKlass::oop_print_on(oop obj, outputStream* st) {
D
duke 已提交
209 210 211 212 213
  assert(obj->is_array(), "must be array");
  Klass::oop_print_on(obj, st);
  st->print_cr(" - length: %d", arrayOop(obj)->length());
}

214

D
duke 已提交
215 216
// Verification

217 218
void ArrayKlass::verify_on(outputStream* st) {
  Klass::verify_on(st);
219 220 221 222 223 224

  if (component_mirror() != NULL) {
    guarantee(component_mirror()->klass() != NULL, "should have a class");
  }
}

225
void ArrayKlass::oop_verify_on(oop obj, outputStream* st) {
D
duke 已提交
226 227 228 229
  guarantee(obj->is_array(), "must be array");
  arrayOop a = arrayOop(obj);
  guarantee(a->length() >= 0, "array with negative length?");
}