duke@435: /* sspitsyn@3638: * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. duke@435: * duke@435: */ duke@435: stefank@2314: #include "precompiled.hpp" coleenp@4037: #include "classfile/symbolTable.hpp" stefank@2314: #include "classfile/systemDictionary.hpp" stefank@2314: #include "classfile/vmSymbols.hpp" stefank@2314: #include "gc_interface/collectedHeap.hpp" stefank@2314: #include "gc_interface/collectedHeap.inline.hpp" coleenp@4037: #include "memory/metadataFactory.hpp" stefank@2314: #include "memory/resourceArea.hpp" stefank@2314: #include "memory/universe.hpp" stefank@2314: #include "memory/universe.inline.hpp" stefank@2314: #include "oops/instanceKlass.hpp" coleenp@4037: #include "oops/klass.inline.hpp" coleenp@4037: #include "oops/objArrayKlass.hpp" stefank@2314: #include "oops/oop.inline.hpp" stefank@2314: #include "oops/typeArrayKlass.hpp" stefank@2314: #include "oops/typeArrayOop.hpp" stefank@2314: #include "runtime/handles.inline.hpp" jprovino@4542: #include "utilities/macros.hpp" duke@435: coleenp@4142: bool TypeArrayKlass::compute_is_subtype_of(Klass* k) { coleenp@4037: if (!k->oop_is_typeArray()) { coleenp@4142: return ArrayKlass::compute_is_subtype_of(k); duke@435: } duke@435: coleenp@4142: TypeArrayKlass* tak = TypeArrayKlass::cast(k); duke@435: if (dimension() != tak->dimension()) return false; duke@435: duke@435: return element_type() == tak->element_type(); duke@435: } duke@435: coleenp@4142: TypeArrayKlass* TypeArrayKlass::create_klass(BasicType type, jcoomes@916: const char* name_str, TRAPS) { coleenp@2497: Symbol* sym = NULL; coleenp@2497: if (name_str != NULL) { coleenp@3682: sym = SymbolTable::new_permanent_symbol(name_str, CHECK_NULL); duke@435: } duke@435: coleenp@4037: ClassLoaderData* null_loader_data = ClassLoaderData::the_null_class_loader_data(); coleenp@4037: coleenp@4142: TypeArrayKlass* ak = TypeArrayKlass::allocate(null_loader_data, type, sym, CHECK_NULL); coleenp@4037: coleenp@4037: // Add all classes to our internal class loader list here, coleenp@4037: // including classes in the bootstrap (NULL) class loader. coleenp@4037: // GC walks these as strong roots. coleenp@4037: null_loader_data->add_class(ak); duke@435: duke@435: // Call complete_create_array_klass after all instance variables have been initialized. coleenp@4037: complete_create_array_klass(ak, ak->super(), CHECK_NULL); duke@435: coleenp@4037: return ak; coleenp@4037: } coleenp@4037: coleenp@4142: TypeArrayKlass* TypeArrayKlass::allocate(ClassLoaderData* loader_data, BasicType type, Symbol* name, TRAPS) { coleenp@4142: assert(TypeArrayKlass::header_size() <= InstanceKlass::header_size(), coleenp@4037: "array klasses must be same size as InstanceKlass"); coleenp@4037: coleenp@4142: int size = ArrayKlass::static_size(TypeArrayKlass::header_size()); coleenp@4037: coleenp@4142: return new (loader_data, size, THREAD) TypeArrayKlass(type, name); coleenp@4037: } coleenp@4037: coleenp@4142: TypeArrayKlass::TypeArrayKlass(BasicType type, Symbol* name) : ArrayKlass(name) { coleenp@4037: set_layout_helper(array_layout_helper(type)); coleenp@4037: assert(oop_is_array(), "sanity"); coleenp@4037: assert(oop_is_typeArray(), "sanity"); coleenp@4037: coleenp@4037: set_max_length(arrayOopDesc::max_array_length(type)); coleenp@4142: assert(size() >= TypeArrayKlass::header_size(), "bad size"); coleenp@4037: coleenp@4037: set_class_loader_data(ClassLoaderData::the_null_class_loader_data()); duke@435: } duke@435: coleenp@4142: typeArrayOop TypeArrayKlass::allocate_common(int length, bool do_zero, TRAPS) { duke@435: assert(log2_element_size() >= 0, "bad scale"); duke@435: if (length >= 0) { duke@435: if (length <= max_length()) { duke@435: size_t size = typeArrayOopDesc::object_size(layout_helper(), length); coleenp@4037: KlassHandle h_k(THREAD, this); duke@435: typeArrayOop t; duke@435: CollectedHeap* ch = Universe::heap(); kvn@3157: if (do_zero) { kvn@3157: t = (typeArrayOop)CollectedHeap::array_allocate(h_k, (int)size, length, CHECK_NULL); kvn@3157: } else { kvn@3157: t = (typeArrayOop)CollectedHeap::array_allocate_nozero(h_k, (int)size, length, CHECK_NULL); kvn@3157: } duke@435: return t; duke@435: } else { martin@1311: report_java_out_of_memory("Requested array size exceeds VM limit"); sspitsyn@3638: JvmtiExport::post_array_size_exhausted(); duke@435: THROW_OOP_0(Universe::out_of_memory_error_array_size()); duke@435: } duke@435: } else { duke@435: THROW_0(vmSymbols::java_lang_NegativeArraySizeException()); duke@435: } duke@435: } duke@435: coleenp@4142: oop TypeArrayKlass::multi_allocate(int rank, jint* last_size, TRAPS) { duke@435: // For typeArrays this is only called for the last dimension duke@435: assert(rank == 1, "just checking"); duke@435: int length = *last_size; duke@435: return allocate(length, THREAD); duke@435: } duke@435: duke@435: coleenp@4142: void TypeArrayKlass::copy_array(arrayOop s, int src_pos, arrayOop d, int dst_pos, int length, TRAPS) { duke@435: assert(s->is_typeArray(), "must be type array"); duke@435: duke@435: // Check destination coleenp@4142: if (!d->is_typeArray() || element_type() != TypeArrayKlass::cast(d->klass())->element_type()) { duke@435: THROW(vmSymbols::java_lang_ArrayStoreException()); duke@435: } duke@435: duke@435: // Check is all offsets and lengths are non negative duke@435: if (src_pos < 0 || dst_pos < 0 || length < 0) { duke@435: THROW(vmSymbols::java_lang_ArrayIndexOutOfBoundsException()); duke@435: } duke@435: // Check if the ranges are valid duke@435: if ( (((unsigned int) length + (unsigned int) src_pos) > (unsigned int) s->length()) duke@435: || (((unsigned int) length + (unsigned int) dst_pos) > (unsigned int) d->length()) ) { duke@435: THROW(vmSymbols::java_lang_ArrayIndexOutOfBoundsException()); duke@435: } kvn@1769: // Check zero copy kvn@1769: if (length == 0) kvn@1769: return; duke@435: duke@435: // This is an attempt to make the copy_array fast. duke@435: int l2es = log2_element_size(); duke@435: int ihs = array_header_in_bytes() / wordSize; kvn@1769: char* src = (char*) ((oop*)s + ihs) + ((size_t)src_pos << l2es); kvn@1769: char* dst = (char*) ((oop*)d + ihs) + ((size_t)dst_pos << l2es); kvn@1769: Copy::conjoint_memory_atomic(src, dst, (size_t)length << l2es); duke@435: } duke@435: duke@435: duke@435: // create a klass of array holding typeArrays coleenp@4142: Klass* TypeArrayKlass::array_klass_impl(bool or_null, int n, TRAPS) { coleenp@4037: int dim = dimension(); coleenp@4037: assert(dim <= n, "check order of chain"); coleenp@4037: if (dim == n) coleenp@4037: return this; duke@435: coleenp@4037: if (higher_dimension() == NULL) { duke@435: if (or_null) return NULL; duke@435: duke@435: ResourceMark rm; duke@435: JavaThread *jt = (JavaThread *)THREAD; duke@435: { duke@435: MutexLocker mc(Compile_lock, THREAD); // for vtables duke@435: // Atomic create higher dimension and link into list duke@435: MutexLocker mu(MultiArray_lock, THREAD); duke@435: coleenp@4037: if (higher_dimension() == NULL) { coleenp@4142: Klass* oak = ObjArrayKlass::allocate_objArray_klass( coleenp@4037: class_loader_data(), dim + 1, this, CHECK_NULL); coleenp@4142: ObjArrayKlass* h_ak = ObjArrayKlass::cast(oak); coleenp@4037: h_ak->set_lower_dimension(this); kvn@2439: OrderAccess::storestore(); coleenp@4037: set_higher_dimension(h_ak); coleenp@4142: assert(h_ak->oop_is_objArray(), "incorrect initialization of ObjArrayKlass"); duke@435: } duke@435: } duke@435: } else { duke@435: CHECK_UNHANDLED_OOPS_ONLY(Thread::current()->clear_unhandled_oops()); duke@435: } coleenp@4142: ObjArrayKlass* h_ak = ObjArrayKlass::cast(higher_dimension()); duke@435: if (or_null) { duke@435: return h_ak->array_klass_or_null(n); duke@435: } duke@435: return h_ak->array_klass(n, CHECK_NULL); duke@435: } duke@435: coleenp@4142: Klass* TypeArrayKlass::array_klass_impl(bool or_null, TRAPS) { duke@435: return array_klass_impl(or_null, dimension() + 1, THREAD); duke@435: } duke@435: coleenp@4142: int TypeArrayKlass::oop_size(oop obj) const { duke@435: assert(obj->is_typeArray(),"must be a type array"); duke@435: typeArrayOop t = typeArrayOop(obj); duke@435: return t->object_size(); duke@435: } duke@435: coleenp@4142: void TypeArrayKlass::oop_follow_contents(oop obj) { duke@435: assert(obj->is_typeArray(),"must be a type array"); duke@435: // Performance tweak: We skip iterating over the klass pointer since we coleenp@4142: // know that Universe::TypeArrayKlass never moves. duke@435: } duke@435: jprovino@4542: #if INCLUDE_ALL_GCS coleenp@4142: void TypeArrayKlass::oop_follow_contents(ParCompactionManager* cm, oop obj) { duke@435: assert(obj->is_typeArray(),"must be a type array"); duke@435: // Performance tweak: We skip iterating over the klass pointer since we coleenp@4142: // know that Universe::TypeArrayKlass never moves. duke@435: } jprovino@4542: #endif // INCLUDE_ALL_GCS duke@435: coleenp@4142: int TypeArrayKlass::oop_adjust_pointers(oop obj) { duke@435: assert(obj->is_typeArray(),"must be a type array"); duke@435: typeArrayOop t = typeArrayOop(obj); duke@435: // Performance tweak: We skip iterating over the klass pointer since we coleenp@4142: // know that Universe::TypeArrayKlass never moves. duke@435: return t->object_size(); duke@435: } duke@435: coleenp@4142: int TypeArrayKlass::oop_oop_iterate(oop obj, ExtendedOopClosure* blk) { duke@435: assert(obj->is_typeArray(),"must be a type array"); duke@435: typeArrayOop t = typeArrayOop(obj); duke@435: // Performance tweak: We skip iterating over the klass pointer since we coleenp@4142: // know that Universe::TypeArrayKlass never moves. duke@435: return t->object_size(); duke@435: } duke@435: coleenp@4142: int TypeArrayKlass::oop_oop_iterate_m(oop obj, ExtendedOopClosure* blk, MemRegion mr) { duke@435: assert(obj->is_typeArray(),"must be a type array"); duke@435: typeArrayOop t = typeArrayOop(obj); duke@435: // Performance tweak: We skip iterating over the klass pointer since we coleenp@4142: // know that Universe::TypeArrayKlass never moves. duke@435: return t->object_size(); duke@435: } duke@435: jprovino@4542: #if INCLUDE_ALL_GCS coleenp@4142: void TypeArrayKlass::oop_push_contents(PSPromotionManager* pm, oop obj) { coleenp@4037: ShouldNotReachHere(); duke@435: assert(obj->is_typeArray(),"must be a type array"); duke@435: } duke@435: duke@435: int coleenp@4142: TypeArrayKlass::oop_update_pointers(ParCompactionManager* cm, oop obj) { duke@435: assert(obj->is_typeArray(),"must be a type array"); duke@435: return typeArrayOop(obj)->object_size(); duke@435: } jprovino@4542: #endif // INCLUDE_ALL_GCS duke@435: coleenp@4142: void TypeArrayKlass::initialize(TRAPS) { duke@435: // Nothing to do. Having this function is handy since objArrayKlasses can be coleenp@4142: // initialized by calling initialize on their bottom_klass, see ObjArrayKlass::initialize duke@435: } duke@435: coleenp@4142: const char* TypeArrayKlass::external_name(BasicType type) { duke@435: switch (type) { duke@435: case T_BOOLEAN: return "[Z"; duke@435: case T_CHAR: return "[C"; duke@435: case T_FLOAT: return "[F"; duke@435: case T_DOUBLE: return "[D"; duke@435: case T_BYTE: return "[B"; duke@435: case T_SHORT: return "[S"; duke@435: case T_INT: return "[I"; duke@435: case T_LONG: return "[J"; duke@435: default: ShouldNotReachHere(); duke@435: } duke@435: return NULL; duke@435: } duke@435: coleenp@4037: coleenp@4037: // Printing coleenp@4037: coleenp@4142: void TypeArrayKlass::print_on(outputStream* st) const { duke@435: #ifndef PRODUCT coleenp@4037: assert(is_klass(), "must be klass"); coleenp@4037: print_value_on(st); coleenp@4037: Klass::print_on(st); coleenp@4037: #endif //PRODUCT coleenp@4037: } coleenp@4037: coleenp@4142: void TypeArrayKlass::print_value_on(outputStream* st) const { coleenp@4037: assert(is_klass(), "must be klass"); coleenp@4037: st->print("{type array "); coleenp@4037: switch (element_type()) { coleenp@4037: case T_BOOLEAN: st->print("bool"); break; coleenp@4037: case T_CHAR: st->print("char"); break; coleenp@4037: case T_FLOAT: st->print("float"); break; coleenp@4037: case T_DOUBLE: st->print("double"); break; coleenp@4037: case T_BYTE: st->print("byte"); break; coleenp@4037: case T_SHORT: st->print("short"); break; coleenp@4037: case T_INT: st->print("int"); break; coleenp@4037: case T_LONG: st->print("long"); break; coleenp@4037: default: ShouldNotReachHere(); coleenp@4037: } coleenp@4037: st->print("}"); coleenp@4037: } coleenp@4037: coleenp@4037: #ifndef PRODUCT duke@435: duke@435: static void print_boolean_array(typeArrayOop ta, int print_len, outputStream* st) { duke@435: for (int index = 0; index < print_len; index++) { duke@435: st->print_cr(" - %3d: %s", index, (ta->bool_at(index) == 0) ? "false" : "true"); duke@435: } duke@435: } duke@435: duke@435: duke@435: static void print_char_array(typeArrayOop ta, int print_len, outputStream* st) { duke@435: for (int index = 0; index < print_len; index++) { duke@435: jchar c = ta->char_at(index); duke@435: st->print_cr(" - %3d: %x %c", index, c, isprint(c) ? c : ' '); duke@435: } duke@435: } duke@435: duke@435: duke@435: static void print_float_array(typeArrayOop ta, int print_len, outputStream* st) { duke@435: for (int index = 0; index < print_len; index++) { duke@435: st->print_cr(" - %3d: %g", index, ta->float_at(index)); duke@435: } duke@435: } duke@435: duke@435: duke@435: static void print_double_array(typeArrayOop ta, int print_len, outputStream* st) { duke@435: for (int index = 0; index < print_len; index++) { duke@435: st->print_cr(" - %3d: %g", index, ta->double_at(index)); duke@435: } duke@435: } duke@435: duke@435: duke@435: static void print_byte_array(typeArrayOop ta, int print_len, outputStream* st) { duke@435: for (int index = 0; index < print_len; index++) { duke@435: jbyte c = ta->byte_at(index); duke@435: st->print_cr(" - %3d: %x %c", index, c, isprint(c) ? c : ' '); duke@435: } duke@435: } duke@435: duke@435: duke@435: static void print_short_array(typeArrayOop ta, int print_len, outputStream* st) { duke@435: for (int index = 0; index < print_len; index++) { duke@435: int v = ta->ushort_at(index); duke@435: st->print_cr(" - %3d: 0x%x\t %d", index, v, v); duke@435: } duke@435: } duke@435: duke@435: duke@435: static void print_int_array(typeArrayOop ta, int print_len, outputStream* st) { duke@435: for (int index = 0; index < print_len; index++) { duke@435: jint v = ta->int_at(index); duke@435: st->print_cr(" - %3d: 0x%x %d", index, v, v); duke@435: } duke@435: } duke@435: duke@435: duke@435: static void print_long_array(typeArrayOop ta, int print_len, outputStream* st) { duke@435: for (int index = 0; index < print_len; index++) { duke@435: jlong v = ta->long_at(index); duke@435: st->print_cr(" - %3d: 0x%x 0x%x", index, high(v), low(v)); duke@435: } duke@435: } duke@435: duke@435: coleenp@4142: void TypeArrayKlass::oop_print_on(oop obj, outputStream* st) { coleenp@4142: ArrayKlass::oop_print_on(obj, st); duke@435: typeArrayOop ta = typeArrayOop(obj); duke@435: int print_len = MIN2((intx) ta->length(), MaxElementPrintSize); duke@435: switch (element_type()) { duke@435: case T_BOOLEAN: print_boolean_array(ta, print_len, st); break; duke@435: case T_CHAR: print_char_array(ta, print_len, st); break; duke@435: case T_FLOAT: print_float_array(ta, print_len, st); break; duke@435: case T_DOUBLE: print_double_array(ta, print_len, st); break; duke@435: case T_BYTE: print_byte_array(ta, print_len, st); break; duke@435: case T_SHORT: print_short_array(ta, print_len, st); break; duke@435: case T_INT: print_int_array(ta, print_len, st); break; duke@435: case T_LONG: print_long_array(ta, print_len, st); break; duke@435: default: ShouldNotReachHere(); duke@435: } duke@435: int remaining = ta->length() - print_len; duke@435: if (remaining > 0) { stefank@4126: st->print_cr(" - <%d more elements, increase MaxElementPrintSize to print>", remaining); duke@435: } duke@435: } duke@435: duke@435: #endif // PRODUCT duke@435: coleenp@4142: const char* TypeArrayKlass::internal_name() const { duke@435: return Klass::external_name(); duke@435: }