arrayKlass.cpp 7.4 KB
Newer Older
D
duke 已提交
1
/*
2
 * Copyright (c) 1997, 2012, 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) 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");
H
hseigel 已提交
70
  return super()->uncached_lookup_method(name, signature);
D
duke 已提交
71 72
}

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

77 78 79 80 81 82
  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 已提交
83 84 85
  // 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();
86 87
  set_vtable_length(vtable_size);
  set_is_cloneable(); // All arrays are considered to be cloneable (See JLS 20.1.5)
D
duke 已提交
88 89 90 91
}


// Initialization of vtables and mirror object is done separatly from base_create_array_klass,
92 93
// 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 已提交
94 95 96
  ResourceMark rm(THREAD);
  k->initialize_supers(super_klass(), CHECK);
  k->vtable()->initialize_vtable(false, CHECK);
97
  java_lang_Class::create_mirror(k, Handle(NULL), CHECK);
D
duke 已提交
98 99
}

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

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


116
inline intptr_t* ArrayKlass::start_of_vtable() const {
117 118
  // 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 已提交
119 120 121
}


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


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

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

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


165
void ArrayKlass::with_array_klasses_do(void f(Klass* k)) {
D
duke 已提交
166 167 168
  array_klasses_do(f);
}

169 170 171

// GC support

172
void ArrayKlass::oops_do(OopClosure* cl) {
173 174 175 176 177
  Klass::oops_do(cl);

  cl->do_oop(adr_component_mirror());
}

D
duke 已提交
178 179
// JVM support

180
jint ArrayKlass::compute_modifier_flags(TRAPS) const {
D
duke 已提交
181 182 183 184 185
  return JVM_ACC_ABSTRACT | JVM_ACC_FINAL | JVM_ACC_PUBLIC;
}

// JVMTI support

186
jint ArrayKlass::jvmti_class_status() const {
D
duke 已提交
187 188 189
  return JVMTI_CLASS_STATUS_ARRAY;
}

190
void ArrayKlass::remove_unshareable_info() {
191 192 193 194 195
  Klass::remove_unshareable_info();
  // Clear the java mirror
  set_component_mirror(NULL);
}

196
void ArrayKlass::restore_unshareable_info(TRAPS) {
197 198 199 200
  Klass::restore_unshareable_info(CHECK);
  // Klass recreates the component mirror also
}

D
duke 已提交
201 202
// Printing

203
void ArrayKlass::print_on(outputStream* st) const {
204 205 206 207
  assert(is_klass(), "must be klass");
  Klass::print_on(st);
}

208
void ArrayKlass::print_value_on(outputStream* st) const {
209 210 211 212 213 214
  assert(is_klass(), "must be klass");
  for(int index = 0; index < dimension(); index++) {
    st->print("[]");
  }
}

215
void ArrayKlass::oop_print_on(oop obj, outputStream* st) {
D
duke 已提交
216 217 218 219 220
  assert(obj->is_array(), "must be array");
  Klass::oop_print_on(obj, st);
  st->print_cr(" - length: %d", arrayOop(obj)->length());
}

221

D
duke 已提交
222 223
// Verification

224 225
void ArrayKlass::verify_on(outputStream* st, bool check_dictionary) {
  Klass::verify_on(st, check_dictionary);
226 227 228 229 230 231

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

232
void ArrayKlass::oop_verify_on(oop obj, outputStream* st) {
D
duke 已提交
233 234 235 236
  guarantee(obj->is_array(), "must be array");
  arrayOop a = arrayOop(obj);
  guarantee(a->length() >= 0, "array with negative length?");
}