annotate src/share/vm/oops/typeArrayKlass.cpp @ 2132:d4fca0a6abde

7011386: race in objArrayKlass::array_klass_impl Summary: Move _lower_dimension field initialization before _higher_dimension and add storestore barrier. Reviewed-by: dholmes, iveresov, never
author kvn
date Tue, 11 Jan 2011 20:26:13 -0800
parents f95d63e2154a
children 3582bf76420e
rev   line source
duke@0 1 /*
kvn@2132 2 * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
duke@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@0 4 *
duke@0 5 * This code is free software; you can redistribute it and/or modify it
duke@0 6 * under the terms of the GNU General Public License version 2 only, as
duke@0 7 * published by the Free Software Foundation.
duke@0 8 *
duke@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@0 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@0 13 * accompanied this code).
duke@0 14 *
duke@0 15 * You should have received a copy of the GNU General Public License version
duke@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@0 18 *
trims@1563 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1563 20 * or visit www.oracle.com if you need additional information or have any
trims@1563 21 * questions.
duke@0 22 *
duke@0 23 */
duke@0 24
stefank@1992 25 #include "precompiled.hpp"
stefank@1992 26 #include "classfile/systemDictionary.hpp"
stefank@1992 27 #include "classfile/vmSymbols.hpp"
stefank@1992 28 #include "gc_interface/collectedHeap.hpp"
stefank@1992 29 #include "gc_interface/collectedHeap.inline.hpp"
stefank@1992 30 #include "memory/resourceArea.hpp"
stefank@1992 31 #include "memory/universe.hpp"
stefank@1992 32 #include "memory/universe.inline.hpp"
stefank@1992 33 #include "oops/instanceKlass.hpp"
stefank@1992 34 #include "oops/klassOop.hpp"
stefank@1992 35 #include "oops/objArrayKlassKlass.hpp"
stefank@1992 36 #include "oops/oop.inline.hpp"
stefank@1992 37 #include "oops/typeArrayKlass.hpp"
stefank@1992 38 #include "oops/typeArrayOop.hpp"
stefank@1992 39 #include "runtime/handles.inline.hpp"
duke@0 40
duke@0 41 bool typeArrayKlass::compute_is_subtype_of(klassOop k) {
duke@0 42 if (!k->klass_part()->oop_is_typeArray()) {
duke@0 43 return arrayKlass::compute_is_subtype_of(k);
duke@0 44 }
duke@0 45
duke@0 46 typeArrayKlass* tak = typeArrayKlass::cast(k);
duke@0 47 if (dimension() != tak->dimension()) return false;
duke@0 48
duke@0 49 return element_type() == tak->element_type();
duke@0 50 }
duke@0 51
jcoomes@488 52 klassOop typeArrayKlass::create_klass(BasicType type, int scale,
jcoomes@488 53 const char* name_str, TRAPS) {
duke@0 54 typeArrayKlass o;
duke@0 55
duke@0 56 symbolHandle sym(symbolOop(NULL));
duke@0 57 // bootstrapping: don't create sym if symbolKlass not created yet
jcoomes@488 58 if (Universe::symbolKlassObj() != NULL && name_str != NULL) {
jcoomes@488 59 sym = oopFactory::new_symbol_handle(name_str, CHECK_NULL);
duke@0 60 }
duke@0 61 KlassHandle klassklass (THREAD, Universe::typeArrayKlassKlassObj());
duke@0 62
duke@0 63 arrayKlassHandle k = base_create_array_klass(o.vtbl_value(), header_size(), klassklass, CHECK_NULL);
duke@0 64 typeArrayKlass* ak = typeArrayKlass::cast(k());
duke@0 65 ak->set_name(sym());
duke@0 66 ak->set_layout_helper(array_layout_helper(type));
duke@0 67 assert(scale == (1 << ak->log2_element_size()), "scale must check out");
duke@0 68 assert(ak->oop_is_javaArray(), "sanity");
duke@0 69 assert(ak->oop_is_typeArray(), "sanity");
duke@0 70 ak->set_max_length(arrayOopDesc::max_array_length(type));
duke@0 71 assert(k()->size() > header_size(), "bad size");
duke@0 72
duke@0 73 // Call complete_create_array_klass after all instance variables have been initialized.
duke@0 74 KlassHandle super (THREAD, k->super());
duke@0 75 complete_create_array_klass(k, super, CHECK_NULL);
duke@0 76
duke@0 77 return k();
duke@0 78 }
duke@0 79
duke@0 80 typeArrayOop typeArrayKlass::allocate(int length, TRAPS) {
duke@0 81 assert(log2_element_size() >= 0, "bad scale");
duke@0 82 if (length >= 0) {
duke@0 83 if (length <= max_length()) {
duke@0 84 size_t size = typeArrayOopDesc::object_size(layout_helper(), length);
duke@0 85 KlassHandle h_k(THREAD, as_klassOop());
duke@0 86 typeArrayOop t;
duke@0 87 CollectedHeap* ch = Universe::heap();
duke@0 88 if (size < ch->large_typearray_limit()) {
duke@0 89 t = (typeArrayOop)CollectedHeap::array_allocate(h_k, (int)size, length, CHECK_NULL);
duke@0 90 } else {
duke@0 91 t = (typeArrayOop)CollectedHeap::large_typearray_allocate(h_k, (int)size, length, CHECK_NULL);
duke@0 92 }
duke@0 93 assert(t->is_parsable(), "Don't publish unless parsable");
duke@0 94 return t;
duke@0 95 } else {
martin@927 96 report_java_out_of_memory("Requested array size exceeds VM limit");
duke@0 97 THROW_OOP_0(Universe::out_of_memory_error_array_size());
duke@0 98 }
duke@0 99 } else {
duke@0 100 THROW_0(vmSymbols::java_lang_NegativeArraySizeException());
duke@0 101 }
duke@0 102 }
duke@0 103
duke@0 104 typeArrayOop typeArrayKlass::allocate_permanent(int length, TRAPS) {
duke@0 105 if (length < 0) THROW_0(vmSymbols::java_lang_NegativeArraySizeException());
duke@0 106 int size = typeArrayOopDesc::object_size(layout_helper(), length);
duke@0 107 KlassHandle h_k(THREAD, as_klassOop());
duke@0 108 typeArrayOop t = (typeArrayOop)
duke@0 109 CollectedHeap::permanent_array_allocate(h_k, size, length, CHECK_NULL);
duke@0 110 assert(t->is_parsable(), "Can't publish until parsable");
duke@0 111 return t;
duke@0 112 }
duke@0 113
duke@0 114 oop typeArrayKlass::multi_allocate(int rank, jint* last_size, TRAPS) {
duke@0 115 // For typeArrays this is only called for the last dimension
duke@0 116 assert(rank == 1, "just checking");
duke@0 117 int length = *last_size;
duke@0 118 return allocate(length, THREAD);
duke@0 119 }
duke@0 120
duke@0 121
duke@0 122 void typeArrayKlass::copy_array(arrayOop s, int src_pos, arrayOop d, int dst_pos, int length, TRAPS) {
duke@0 123 assert(s->is_typeArray(), "must be type array");
duke@0 124
duke@0 125 // Check destination
duke@0 126 if (!d->is_typeArray() || element_type() != typeArrayKlass::cast(d->klass())->element_type()) {
duke@0 127 THROW(vmSymbols::java_lang_ArrayStoreException());
duke@0 128 }
duke@0 129
duke@0 130 // Check is all offsets and lengths are non negative
duke@0 131 if (src_pos < 0 || dst_pos < 0 || length < 0) {
duke@0 132 THROW(vmSymbols::java_lang_ArrayIndexOutOfBoundsException());
duke@0 133 }
duke@0 134 // Check if the ranges are valid
duke@0 135 if ( (((unsigned int) length + (unsigned int) src_pos) > (unsigned int) s->length())
duke@0 136 || (((unsigned int) length + (unsigned int) dst_pos) > (unsigned int) d->length()) ) {
duke@0 137 THROW(vmSymbols::java_lang_ArrayIndexOutOfBoundsException());
duke@0 138 }
kvn@1421 139 // Check zero copy
kvn@1421 140 if (length == 0)
kvn@1421 141 return;
duke@0 142
duke@0 143 // This is an attempt to make the copy_array fast.
duke@0 144 int l2es = log2_element_size();
duke@0 145 int ihs = array_header_in_bytes() / wordSize;
kvn@1421 146 char* src = (char*) ((oop*)s + ihs) + ((size_t)src_pos << l2es);
kvn@1421 147 char* dst = (char*) ((oop*)d + ihs) + ((size_t)dst_pos << l2es);
kvn@1421 148 Copy::conjoint_memory_atomic(src, dst, (size_t)length << l2es);
duke@0 149 }
duke@0 150
duke@0 151
duke@0 152 // create a klass of array holding typeArrays
duke@0 153 klassOop typeArrayKlass::array_klass_impl(bool or_null, int n, TRAPS) {
duke@0 154 typeArrayKlassHandle h_this(THREAD, as_klassOop());
duke@0 155 return array_klass_impl(h_this, or_null, n, THREAD);
duke@0 156 }
duke@0 157
duke@0 158 klassOop typeArrayKlass::array_klass_impl(typeArrayKlassHandle h_this, bool or_null, int n, TRAPS) {
duke@0 159 int dimension = h_this->dimension();
duke@0 160 assert(dimension <= n, "check order of chain");
duke@0 161 if (dimension == n)
duke@0 162 return h_this();
duke@0 163
duke@0 164 objArrayKlassHandle h_ak(THREAD, h_this->higher_dimension());
duke@0 165 if (h_ak.is_null()) {
duke@0 166 if (or_null) return NULL;
duke@0 167
duke@0 168 ResourceMark rm;
duke@0 169 JavaThread *jt = (JavaThread *)THREAD;
duke@0 170 {
duke@0 171 MutexLocker mc(Compile_lock, THREAD); // for vtables
duke@0 172 // Atomic create higher dimension and link into list
duke@0 173 MutexLocker mu(MultiArray_lock, THREAD);
duke@0 174
duke@0 175 h_ak = objArrayKlassHandle(THREAD, h_this->higher_dimension());
duke@0 176 if (h_ak.is_null()) {
duke@0 177 klassOop oak = objArrayKlassKlass::cast(
duke@0 178 Universe::objArrayKlassKlassObj())->allocate_objArray_klass(
duke@0 179 dimension + 1, h_this, CHECK_NULL);
duke@0 180 h_ak = objArrayKlassHandle(THREAD, oak);
duke@0 181 h_ak->set_lower_dimension(h_this());
kvn@2132 182 OrderAccess::storestore();
duke@0 183 h_this->set_higher_dimension(h_ak());
duke@0 184 assert(h_ak->oop_is_objArray(), "incorrect initialization of objArrayKlass");
duke@0 185 }
duke@0 186 }
duke@0 187 } else {
duke@0 188 CHECK_UNHANDLED_OOPS_ONLY(Thread::current()->clear_unhandled_oops());
duke@0 189 }
duke@0 190 if (or_null) {
duke@0 191 return h_ak->array_klass_or_null(n);
duke@0 192 }
duke@0 193 return h_ak->array_klass(n, CHECK_NULL);
duke@0 194 }
duke@0 195
duke@0 196 klassOop typeArrayKlass::array_klass_impl(bool or_null, TRAPS) {
duke@0 197 return array_klass_impl(or_null, dimension() + 1, THREAD);
duke@0 198 }
duke@0 199
duke@0 200 int typeArrayKlass::oop_size(oop obj) const {
duke@0 201 assert(obj->is_typeArray(),"must be a type array");
duke@0 202 typeArrayOop t = typeArrayOop(obj);
duke@0 203 return t->object_size();
duke@0 204 }
duke@0 205
duke@0 206 void typeArrayKlass::oop_follow_contents(oop obj) {
duke@0 207 assert(obj->is_typeArray(),"must be a type array");
duke@0 208 // Performance tweak: We skip iterating over the klass pointer since we
duke@0 209 // know that Universe::typeArrayKlass never moves.
duke@0 210 }
duke@0 211
duke@0 212 #ifndef SERIALGC
duke@0 213 void typeArrayKlass::oop_follow_contents(ParCompactionManager* cm, oop obj) {
duke@0 214 assert(obj->is_typeArray(),"must be a type array");
duke@0 215 // Performance tweak: We skip iterating over the klass pointer since we
duke@0 216 // know that Universe::typeArrayKlass never moves.
duke@0 217 }
duke@0 218 #endif // SERIALGC
duke@0 219
duke@0 220 int typeArrayKlass::oop_adjust_pointers(oop obj) {
duke@0 221 assert(obj->is_typeArray(),"must be a type array");
duke@0 222 typeArrayOop t = typeArrayOop(obj);
duke@0 223 // Performance tweak: We skip iterating over the klass pointer since we
duke@0 224 // know that Universe::typeArrayKlass never moves.
duke@0 225 return t->object_size();
duke@0 226 }
duke@0 227
duke@0 228 int typeArrayKlass::oop_oop_iterate(oop obj, OopClosure* blk) {
duke@0 229 assert(obj->is_typeArray(),"must be a type array");
duke@0 230 typeArrayOop t = typeArrayOop(obj);
duke@0 231 // Performance tweak: We skip iterating over the klass pointer since we
duke@0 232 // know that Universe::typeArrayKlass never moves.
duke@0 233 return t->object_size();
duke@0 234 }
duke@0 235
duke@0 236 int typeArrayKlass::oop_oop_iterate_m(oop obj, OopClosure* blk, MemRegion mr) {
duke@0 237 assert(obj->is_typeArray(),"must be a type array");
duke@0 238 typeArrayOop t = typeArrayOop(obj);
duke@0 239 // Performance tweak: We skip iterating over the klass pointer since we
duke@0 240 // know that Universe::typeArrayKlass never moves.
duke@0 241 return t->object_size();
duke@0 242 }
duke@0 243
duke@0 244 #ifndef SERIALGC
duke@0 245 void typeArrayKlass::oop_push_contents(PSPromotionManager* pm, oop obj) {
duke@0 246 assert(obj->is_typeArray(),"must be a type array");
duke@0 247 }
duke@0 248
duke@0 249 int
duke@0 250 typeArrayKlass::oop_update_pointers(ParCompactionManager* cm, oop obj) {
duke@0 251 assert(obj->is_typeArray(),"must be a type array");
duke@0 252 return typeArrayOop(obj)->object_size();
duke@0 253 }
duke@0 254
duke@0 255 int
duke@0 256 typeArrayKlass::oop_update_pointers(ParCompactionManager* cm, oop obj,
duke@0 257 HeapWord* beg_addr, HeapWord* end_addr) {
duke@0 258 assert(obj->is_typeArray(),"must be a type array");
duke@0 259 return typeArrayOop(obj)->object_size();
duke@0 260 }
duke@0 261 #endif // SERIALGC
duke@0 262
duke@0 263 void typeArrayKlass::initialize(TRAPS) {
duke@0 264 // Nothing to do. Having this function is handy since objArrayKlasses can be
duke@0 265 // initialized by calling initialize on their bottom_klass, see objArrayKlass::initialize
duke@0 266 }
duke@0 267
duke@0 268 const char* typeArrayKlass::external_name(BasicType type) {
duke@0 269 switch (type) {
duke@0 270 case T_BOOLEAN: return "[Z";
duke@0 271 case T_CHAR: return "[C";
duke@0 272 case T_FLOAT: return "[F";
duke@0 273 case T_DOUBLE: return "[D";
duke@0 274 case T_BYTE: return "[B";
duke@0 275 case T_SHORT: return "[S";
duke@0 276 case T_INT: return "[I";
duke@0 277 case T_LONG: return "[J";
duke@0 278 default: ShouldNotReachHere();
duke@0 279 }
duke@0 280 return NULL;
duke@0 281 }
duke@0 282
duke@0 283 #ifndef PRODUCT
duke@0 284 // Printing
duke@0 285
duke@0 286 static void print_boolean_array(typeArrayOop ta, int print_len, outputStream* st) {
duke@0 287 for (int index = 0; index < print_len; index++) {
duke@0 288 st->print_cr(" - %3d: %s", index, (ta->bool_at(index) == 0) ? "false" : "true");
duke@0 289 }
duke@0 290 }
duke@0 291
duke@0 292
duke@0 293 static void print_char_array(typeArrayOop ta, int print_len, outputStream* st) {
duke@0 294 for (int index = 0; index < print_len; index++) {
duke@0 295 jchar c = ta->char_at(index);
duke@0 296 st->print_cr(" - %3d: %x %c", index, c, isprint(c) ? c : ' ');
duke@0 297 }
duke@0 298 }
duke@0 299
duke@0 300
duke@0 301 static void print_float_array(typeArrayOop ta, int print_len, outputStream* st) {
duke@0 302 for (int index = 0; index < print_len; index++) {
duke@0 303 st->print_cr(" - %3d: %g", index, ta->float_at(index));
duke@0 304 }
duke@0 305 }
duke@0 306
duke@0 307
duke@0 308 static void print_double_array(typeArrayOop ta, int print_len, outputStream* st) {
duke@0 309 for (int index = 0; index < print_len; index++) {
duke@0 310 st->print_cr(" - %3d: %g", index, ta->double_at(index));
duke@0 311 }
duke@0 312 }
duke@0 313
duke@0 314
duke@0 315 static void print_byte_array(typeArrayOop ta, int print_len, outputStream* st) {
duke@0 316 for (int index = 0; index < print_len; index++) {
duke@0 317 jbyte c = ta->byte_at(index);
duke@0 318 st->print_cr(" - %3d: %x %c", index, c, isprint(c) ? c : ' ');
duke@0 319 }
duke@0 320 }
duke@0 321
duke@0 322
duke@0 323 static void print_short_array(typeArrayOop ta, int print_len, outputStream* st) {
duke@0 324 for (int index = 0; index < print_len; index++) {
duke@0 325 int v = ta->ushort_at(index);
duke@0 326 st->print_cr(" - %3d: 0x%x\t %d", index, v, v);
duke@0 327 }
duke@0 328 }
duke@0 329
duke@0 330
duke@0 331 static void print_int_array(typeArrayOop ta, int print_len, outputStream* st) {
duke@0 332 for (int index = 0; index < print_len; index++) {
duke@0 333 jint v = ta->int_at(index);
duke@0 334 st->print_cr(" - %3d: 0x%x %d", index, v, v);
duke@0 335 }
duke@0 336 }
duke@0 337
duke@0 338
duke@0 339 static void print_long_array(typeArrayOop ta, int print_len, outputStream* st) {
duke@0 340 for (int index = 0; index < print_len; index++) {
duke@0 341 jlong v = ta->long_at(index);
duke@0 342 st->print_cr(" - %3d: 0x%x 0x%x", index, high(v), low(v));
duke@0 343 }
duke@0 344 }
duke@0 345
duke@0 346
duke@0 347 void typeArrayKlass::oop_print_on(oop obj, outputStream* st) {
duke@0 348 arrayKlass::oop_print_on(obj, st);
duke@0 349 typeArrayOop ta = typeArrayOop(obj);
duke@0 350 int print_len = MIN2((intx) ta->length(), MaxElementPrintSize);
duke@0 351 switch (element_type()) {
duke@0 352 case T_BOOLEAN: print_boolean_array(ta, print_len, st); break;
duke@0 353 case T_CHAR: print_char_array(ta, print_len, st); break;
duke@0 354 case T_FLOAT: print_float_array(ta, print_len, st); break;
duke@0 355 case T_DOUBLE: print_double_array(ta, print_len, st); break;
duke@0 356 case T_BYTE: print_byte_array(ta, print_len, st); break;
duke@0 357 case T_SHORT: print_short_array(ta, print_len, st); break;
duke@0 358 case T_INT: print_int_array(ta, print_len, st); break;
duke@0 359 case T_LONG: print_long_array(ta, print_len, st); break;
duke@0 360 default: ShouldNotReachHere();
duke@0 361 }
duke@0 362 int remaining = ta->length() - print_len;
duke@0 363 if (remaining > 0) {
duke@0 364 tty->print_cr(" - <%d more elements, increase MaxElementPrintSize to print>", remaining);
duke@0 365 }
duke@0 366 }
duke@0 367
duke@0 368 #endif // PRODUCT
duke@0 369
duke@0 370 const char* typeArrayKlass::internal_name() const {
duke@0 371 return Klass::external_name();
duke@0 372 }