1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/vm/oops/typeArrayKlass.cpp Wed Apr 27 01:25:04 2016 +0800 1.3 @@ -0,0 +1,393 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + * 1.26 + */ 1.27 + 1.28 +#include "precompiled.hpp" 1.29 +#include "classfile/symbolTable.hpp" 1.30 +#include "classfile/systemDictionary.hpp" 1.31 +#include "classfile/vmSymbols.hpp" 1.32 +#include "gc_interface/collectedHeap.hpp" 1.33 +#include "gc_interface/collectedHeap.inline.hpp" 1.34 +#include "memory/metadataFactory.hpp" 1.35 +#include "memory/resourceArea.hpp" 1.36 +#include "memory/universe.hpp" 1.37 +#include "memory/universe.inline.hpp" 1.38 +#include "oops/instanceKlass.hpp" 1.39 +#include "oops/klass.inline.hpp" 1.40 +#include "oops/objArrayKlass.hpp" 1.41 +#include "oops/oop.inline.hpp" 1.42 +#include "oops/typeArrayKlass.hpp" 1.43 +#include "oops/typeArrayOop.hpp" 1.44 +#include "runtime/handles.inline.hpp" 1.45 +#include "utilities/macros.hpp" 1.46 + 1.47 +bool TypeArrayKlass::compute_is_subtype_of(Klass* k) { 1.48 + if (!k->oop_is_typeArray()) { 1.49 + return ArrayKlass::compute_is_subtype_of(k); 1.50 + } 1.51 + 1.52 + TypeArrayKlass* tak = TypeArrayKlass::cast(k); 1.53 + if (dimension() != tak->dimension()) return false; 1.54 + 1.55 + return element_type() == tak->element_type(); 1.56 +} 1.57 + 1.58 +TypeArrayKlass* TypeArrayKlass::create_klass(BasicType type, 1.59 + const char* name_str, TRAPS) { 1.60 + Symbol* sym = NULL; 1.61 + if (name_str != NULL) { 1.62 + sym = SymbolTable::new_permanent_symbol(name_str, CHECK_NULL); 1.63 + } 1.64 + 1.65 + ClassLoaderData* null_loader_data = ClassLoaderData::the_null_class_loader_data(); 1.66 + 1.67 + TypeArrayKlass* ak = TypeArrayKlass::allocate(null_loader_data, type, sym, CHECK_NULL); 1.68 + 1.69 + // Add all classes to our internal class loader list here, 1.70 + // including classes in the bootstrap (NULL) class loader. 1.71 + // GC walks these as strong roots. 1.72 + null_loader_data->add_class(ak); 1.73 + 1.74 + // Call complete_create_array_klass after all instance variables have been initialized. 1.75 + complete_create_array_klass(ak, ak->super(), CHECK_NULL); 1.76 + 1.77 + return ak; 1.78 +} 1.79 + 1.80 +TypeArrayKlass* TypeArrayKlass::allocate(ClassLoaderData* loader_data, BasicType type, Symbol* name, TRAPS) { 1.81 + assert(TypeArrayKlass::header_size() <= InstanceKlass::header_size(), 1.82 + "array klasses must be same size as InstanceKlass"); 1.83 + 1.84 + int size = ArrayKlass::static_size(TypeArrayKlass::header_size()); 1.85 + 1.86 + return new (loader_data, size, THREAD) TypeArrayKlass(type, name); 1.87 +} 1.88 + 1.89 +TypeArrayKlass::TypeArrayKlass(BasicType type, Symbol* name) : ArrayKlass(name) { 1.90 + set_layout_helper(array_layout_helper(type)); 1.91 + assert(oop_is_array(), "sanity"); 1.92 + assert(oop_is_typeArray(), "sanity"); 1.93 + 1.94 + set_max_length(arrayOopDesc::max_array_length(type)); 1.95 + assert(size() >= TypeArrayKlass::header_size(), "bad size"); 1.96 + 1.97 + set_class_loader_data(ClassLoaderData::the_null_class_loader_data()); 1.98 +} 1.99 + 1.100 +typeArrayOop TypeArrayKlass::allocate_common(int length, bool do_zero, TRAPS) { 1.101 + assert(log2_element_size() >= 0, "bad scale"); 1.102 + if (length >= 0) { 1.103 + if (length <= max_length()) { 1.104 + size_t size = typeArrayOopDesc::object_size(layout_helper(), length); 1.105 + KlassHandle h_k(THREAD, this); 1.106 + typeArrayOop t; 1.107 + CollectedHeap* ch = Universe::heap(); 1.108 + if (do_zero) { 1.109 + t = (typeArrayOop)CollectedHeap::array_allocate(h_k, (int)size, length, CHECK_NULL); 1.110 + } else { 1.111 + t = (typeArrayOop)CollectedHeap::array_allocate_nozero(h_k, (int)size, length, CHECK_NULL); 1.112 + } 1.113 + return t; 1.114 + } else { 1.115 + report_java_out_of_memory("Requested array size exceeds VM limit"); 1.116 + JvmtiExport::post_array_size_exhausted(); 1.117 + THROW_OOP_0(Universe::out_of_memory_error_array_size()); 1.118 + } 1.119 + } else { 1.120 + THROW_0(vmSymbols::java_lang_NegativeArraySizeException()); 1.121 + } 1.122 +} 1.123 + 1.124 +oop TypeArrayKlass::multi_allocate(int rank, jint* last_size, TRAPS) { 1.125 + // For typeArrays this is only called for the last dimension 1.126 + assert(rank == 1, "just checking"); 1.127 + int length = *last_size; 1.128 + return allocate(length, THREAD); 1.129 +} 1.130 + 1.131 + 1.132 +void TypeArrayKlass::copy_array(arrayOop s, int src_pos, arrayOop d, int dst_pos, int length, TRAPS) { 1.133 + assert(s->is_typeArray(), "must be type array"); 1.134 + 1.135 + // Check destination 1.136 + if (!d->is_typeArray() || element_type() != TypeArrayKlass::cast(d->klass())->element_type()) { 1.137 + THROW(vmSymbols::java_lang_ArrayStoreException()); 1.138 + } 1.139 + 1.140 + // Check is all offsets and lengths are non negative 1.141 + if (src_pos < 0 || dst_pos < 0 || length < 0) { 1.142 + THROW(vmSymbols::java_lang_ArrayIndexOutOfBoundsException()); 1.143 + } 1.144 + // Check if the ranges are valid 1.145 + if ( (((unsigned int) length + (unsigned int) src_pos) > (unsigned int) s->length()) 1.146 + || (((unsigned int) length + (unsigned int) dst_pos) > (unsigned int) d->length()) ) { 1.147 + THROW(vmSymbols::java_lang_ArrayIndexOutOfBoundsException()); 1.148 + } 1.149 + // Check zero copy 1.150 + if (length == 0) 1.151 + return; 1.152 + 1.153 + // This is an attempt to make the copy_array fast. 1.154 + int l2es = log2_element_size(); 1.155 + int ihs = array_header_in_bytes() / wordSize; 1.156 + char* src = (char*) ((oop*)s + ihs) + ((size_t)src_pos << l2es); 1.157 + char* dst = (char*) ((oop*)d + ihs) + ((size_t)dst_pos << l2es); 1.158 + Copy::conjoint_memory_atomic(src, dst, (size_t)length << l2es); 1.159 +} 1.160 + 1.161 + 1.162 +// create a klass of array holding typeArrays 1.163 +Klass* TypeArrayKlass::array_klass_impl(bool or_null, int n, TRAPS) { 1.164 + int dim = dimension(); 1.165 + assert(dim <= n, "check order of chain"); 1.166 + if (dim == n) 1.167 + return this; 1.168 + 1.169 + if (higher_dimension() == NULL) { 1.170 + if (or_null) return NULL; 1.171 + 1.172 + ResourceMark rm; 1.173 + JavaThread *jt = (JavaThread *)THREAD; 1.174 + { 1.175 + MutexLocker mc(Compile_lock, THREAD); // for vtables 1.176 + // Atomic create higher dimension and link into list 1.177 + MutexLocker mu(MultiArray_lock, THREAD); 1.178 + 1.179 + if (higher_dimension() == NULL) { 1.180 + Klass* oak = ObjArrayKlass::allocate_objArray_klass( 1.181 + class_loader_data(), dim + 1, this, CHECK_NULL); 1.182 + ObjArrayKlass* h_ak = ObjArrayKlass::cast(oak); 1.183 + h_ak->set_lower_dimension(this); 1.184 + OrderAccess::storestore(); 1.185 + set_higher_dimension(h_ak); 1.186 + assert(h_ak->oop_is_objArray(), "incorrect initialization of ObjArrayKlass"); 1.187 + } 1.188 + } 1.189 + } else { 1.190 + CHECK_UNHANDLED_OOPS_ONLY(Thread::current()->clear_unhandled_oops()); 1.191 + } 1.192 + ObjArrayKlass* h_ak = ObjArrayKlass::cast(higher_dimension()); 1.193 + if (or_null) { 1.194 + return h_ak->array_klass_or_null(n); 1.195 + } 1.196 + return h_ak->array_klass(n, CHECK_NULL); 1.197 +} 1.198 + 1.199 +Klass* TypeArrayKlass::array_klass_impl(bool or_null, TRAPS) { 1.200 + return array_klass_impl(or_null, dimension() + 1, THREAD); 1.201 +} 1.202 + 1.203 +int TypeArrayKlass::oop_size(oop obj) const { 1.204 + assert(obj->is_typeArray(),"must be a type array"); 1.205 + typeArrayOop t = typeArrayOop(obj); 1.206 + return t->object_size(); 1.207 +} 1.208 + 1.209 +void TypeArrayKlass::oop_follow_contents(oop obj) { 1.210 + assert(obj->is_typeArray(),"must be a type array"); 1.211 + // Performance tweak: We skip iterating over the klass pointer since we 1.212 + // know that Universe::TypeArrayKlass never moves. 1.213 +} 1.214 + 1.215 +#if INCLUDE_ALL_GCS 1.216 +void TypeArrayKlass::oop_follow_contents(ParCompactionManager* cm, oop obj) { 1.217 + assert(obj->is_typeArray(),"must be a type array"); 1.218 + // Performance tweak: We skip iterating over the klass pointer since we 1.219 + // know that Universe::TypeArrayKlass never moves. 1.220 +} 1.221 +#endif // INCLUDE_ALL_GCS 1.222 + 1.223 +int TypeArrayKlass::oop_adjust_pointers(oop obj) { 1.224 + assert(obj->is_typeArray(),"must be a type array"); 1.225 + typeArrayOop t = typeArrayOop(obj); 1.226 + // Performance tweak: We skip iterating over the klass pointer since we 1.227 + // know that Universe::TypeArrayKlass never moves. 1.228 + return t->object_size(); 1.229 +} 1.230 + 1.231 +int TypeArrayKlass::oop_oop_iterate(oop obj, ExtendedOopClosure* blk) { 1.232 + assert(obj->is_typeArray(),"must be a type array"); 1.233 + typeArrayOop t = typeArrayOop(obj); 1.234 + // Performance tweak: We skip iterating over the klass pointer since we 1.235 + // know that Universe::TypeArrayKlass never moves. 1.236 + return t->object_size(); 1.237 +} 1.238 + 1.239 +int TypeArrayKlass::oop_oop_iterate_m(oop obj, ExtendedOopClosure* blk, MemRegion mr) { 1.240 + assert(obj->is_typeArray(),"must be a type array"); 1.241 + typeArrayOop t = typeArrayOop(obj); 1.242 + // Performance tweak: We skip iterating over the klass pointer since we 1.243 + // know that Universe::TypeArrayKlass never moves. 1.244 + return t->object_size(); 1.245 +} 1.246 + 1.247 +#if INCLUDE_ALL_GCS 1.248 +void TypeArrayKlass::oop_push_contents(PSPromotionManager* pm, oop obj) { 1.249 + ShouldNotReachHere(); 1.250 + assert(obj->is_typeArray(),"must be a type array"); 1.251 +} 1.252 + 1.253 +int 1.254 +TypeArrayKlass::oop_update_pointers(ParCompactionManager* cm, oop obj) { 1.255 + assert(obj->is_typeArray(),"must be a type array"); 1.256 + return typeArrayOop(obj)->object_size(); 1.257 +} 1.258 +#endif // INCLUDE_ALL_GCS 1.259 + 1.260 +void TypeArrayKlass::initialize(TRAPS) { 1.261 + // Nothing to do. Having this function is handy since objArrayKlasses can be 1.262 + // initialized by calling initialize on their bottom_klass, see ObjArrayKlass::initialize 1.263 +} 1.264 + 1.265 +const char* TypeArrayKlass::external_name(BasicType type) { 1.266 + switch (type) { 1.267 + case T_BOOLEAN: return "[Z"; 1.268 + case T_CHAR: return "[C"; 1.269 + case T_FLOAT: return "[F"; 1.270 + case T_DOUBLE: return "[D"; 1.271 + case T_BYTE: return "[B"; 1.272 + case T_SHORT: return "[S"; 1.273 + case T_INT: return "[I"; 1.274 + case T_LONG: return "[J"; 1.275 + default: ShouldNotReachHere(); 1.276 + } 1.277 + return NULL; 1.278 +} 1.279 + 1.280 + 1.281 +// Printing 1.282 + 1.283 +void TypeArrayKlass::print_on(outputStream* st) const { 1.284 +#ifndef PRODUCT 1.285 + assert(is_klass(), "must be klass"); 1.286 + print_value_on(st); 1.287 + Klass::print_on(st); 1.288 +#endif //PRODUCT 1.289 +} 1.290 + 1.291 +void TypeArrayKlass::print_value_on(outputStream* st) const { 1.292 + assert(is_klass(), "must be klass"); 1.293 + st->print("{type array "); 1.294 + switch (element_type()) { 1.295 + case T_BOOLEAN: st->print("bool"); break; 1.296 + case T_CHAR: st->print("char"); break; 1.297 + case T_FLOAT: st->print("float"); break; 1.298 + case T_DOUBLE: st->print("double"); break; 1.299 + case T_BYTE: st->print("byte"); break; 1.300 + case T_SHORT: st->print("short"); break; 1.301 + case T_INT: st->print("int"); break; 1.302 + case T_LONG: st->print("long"); break; 1.303 + default: ShouldNotReachHere(); 1.304 + } 1.305 + st->print("}"); 1.306 +} 1.307 + 1.308 +#ifndef PRODUCT 1.309 + 1.310 +static void print_boolean_array(typeArrayOop ta, int print_len, outputStream* st) { 1.311 + for (int index = 0; index < print_len; index++) { 1.312 + st->print_cr(" - %3d: %s", index, (ta->bool_at(index) == 0) ? "false" : "true"); 1.313 + } 1.314 +} 1.315 + 1.316 + 1.317 +static void print_char_array(typeArrayOop ta, int print_len, outputStream* st) { 1.318 + for (int index = 0; index < print_len; index++) { 1.319 + jchar c = ta->char_at(index); 1.320 + st->print_cr(" - %3d: %x %c", index, c, isprint(c) ? c : ' '); 1.321 + } 1.322 +} 1.323 + 1.324 + 1.325 +static void print_float_array(typeArrayOop ta, int print_len, outputStream* st) { 1.326 + for (int index = 0; index < print_len; index++) { 1.327 + st->print_cr(" - %3d: %g", index, ta->float_at(index)); 1.328 + } 1.329 +} 1.330 + 1.331 + 1.332 +static void print_double_array(typeArrayOop ta, int print_len, outputStream* st) { 1.333 + for (int index = 0; index < print_len; index++) { 1.334 + st->print_cr(" - %3d: %g", index, ta->double_at(index)); 1.335 + } 1.336 +} 1.337 + 1.338 + 1.339 +static void print_byte_array(typeArrayOop ta, int print_len, outputStream* st) { 1.340 + for (int index = 0; index < print_len; index++) { 1.341 + jbyte c = ta->byte_at(index); 1.342 + st->print_cr(" - %3d: %x %c", index, c, isprint(c) ? c : ' '); 1.343 + } 1.344 +} 1.345 + 1.346 + 1.347 +static void print_short_array(typeArrayOop ta, int print_len, outputStream* st) { 1.348 + for (int index = 0; index < print_len; index++) { 1.349 + int v = ta->ushort_at(index); 1.350 + st->print_cr(" - %3d: 0x%x\t %d", index, v, v); 1.351 + } 1.352 +} 1.353 + 1.354 + 1.355 +static void print_int_array(typeArrayOop ta, int print_len, outputStream* st) { 1.356 + for (int index = 0; index < print_len; index++) { 1.357 + jint v = ta->int_at(index); 1.358 + st->print_cr(" - %3d: 0x%x %d", index, v, v); 1.359 + } 1.360 +} 1.361 + 1.362 + 1.363 +static void print_long_array(typeArrayOop ta, int print_len, outputStream* st) { 1.364 + for (int index = 0; index < print_len; index++) { 1.365 + jlong v = ta->long_at(index); 1.366 + st->print_cr(" - %3d: 0x%x 0x%x", index, high(v), low(v)); 1.367 + } 1.368 +} 1.369 + 1.370 + 1.371 +void TypeArrayKlass::oop_print_on(oop obj, outputStream* st) { 1.372 + ArrayKlass::oop_print_on(obj, st); 1.373 + typeArrayOop ta = typeArrayOop(obj); 1.374 + int print_len = MIN2((intx) ta->length(), MaxElementPrintSize); 1.375 + switch (element_type()) { 1.376 + case T_BOOLEAN: print_boolean_array(ta, print_len, st); break; 1.377 + case T_CHAR: print_char_array(ta, print_len, st); break; 1.378 + case T_FLOAT: print_float_array(ta, print_len, st); break; 1.379 + case T_DOUBLE: print_double_array(ta, print_len, st); break; 1.380 + case T_BYTE: print_byte_array(ta, print_len, st); break; 1.381 + case T_SHORT: print_short_array(ta, print_len, st); break; 1.382 + case T_INT: print_int_array(ta, print_len, st); break; 1.383 + case T_LONG: print_long_array(ta, print_len, st); break; 1.384 + default: ShouldNotReachHere(); 1.385 + } 1.386 + int remaining = ta->length() - print_len; 1.387 + if (remaining > 0) { 1.388 + st->print_cr(" - <%d more elements, increase MaxElementPrintSize to print>", remaining); 1.389 + } 1.390 +} 1.391 + 1.392 +#endif // PRODUCT 1.393 + 1.394 +const char* TypeArrayKlass::internal_name() const { 1.395 + return Klass::external_name(); 1.396 +}