1 /* 2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #include "precompiled.hpp" 26 #include "classfile/symbolTable.hpp" 27 #include "classfile/systemDictionary.hpp" 28 #include "classfile/vmSymbols.hpp" 29 #include "gc/shared/collectedHeap.hpp" 30 #include "gc/shared/collectedHeap.inline.hpp" 31 #include "memory/metadataFactory.hpp" 32 #include "memory/resourceArea.hpp" 33 #include "memory/universe.hpp" 34 #include "memory/universe.inline.hpp" 35 #include "oops/instanceKlass.hpp" 36 #include "oops/klass.inline.hpp" 37 #include "oops/objArrayKlass.hpp" 38 #include "oops/oop.inline.hpp" 39 #include "oops/arrayKlass.hpp" 40 #include "oops/arrayOop.hpp" 41 #include "oops/valueKlass.hpp" 42 #include "oops/valueArrayOop.hpp" 43 #include "runtime/handles.inline.hpp" 44 #include "runtime/orderAccess.inline.hpp" 45 #include "utilities/macros.hpp" 46 47 #include "oops/valueArrayKlass.hpp" 48 49 // Allocation... 50 51 ValueArrayKlass::ValueArrayKlass(KlassHandle element_klass_handle, Symbol* name) : ArrayKlass(name) { 52 Klass* k = element_klass_handle(); 53 assert(k->is_value(), "Expected Value"); 54 set_element_klass(ValueKlass::cast(k)); 55 set_class_loader_data(k->class_loader_data()); 56 57 set_layout_helper(array_layout_helper(element_klass())); 58 assert(is_array_klass(), "sanity"); 59 assert(is_valueArray_klass(), "sanity"); 60 61 CMH("tweak name symbol refcnt ?") 62 #ifndef PRODUCT 63 if (PrintValueArrayLayout) { 64 print(); 65 } 66 #endif 67 } 68 69 ValueArrayKlass* ValueArrayKlass::allocate_klass(KlassHandle element_klass, 70 Symbol* name, 71 TRAPS) { 72 assert(ValueArrayFlatten, "Flatten array not allowed"); 73 assert(ValueKlass::cast(element_klass())->is_atomic() || (!ValueArrayAtomicAccess), "Atomic by-default"); 74 75 ClassLoaderData* loader_data = element_klass->class_loader_data(); 76 int size = ArrayKlass::static_size(ValueArrayKlass::header_size()); 77 ValueArrayKlass* vak = new (loader_data, size, THREAD) ValueArrayKlass(element_klass, name); 78 if (vak == NULL) { 79 return NULL; 80 } 81 loader_data->add_class(vak); 82 complete_create_array_klass(vak, vak->super(), CHECK_NULL); 83 return vak; 84 } 85 86 ValueArrayKlass* ValueArrayKlass::allocate_klass(KlassHandle element_klass, TRAPS) { 87 Symbol* name = ArrayKlass::create_element_klass_array_name(element_klass, CHECK_NULL); 88 return allocate_klass(element_klass, name, THREAD); 89 } 90 91 // Oops allocation... 92 oop ValueArrayKlass::allocate(int length, bool do_zero, TRAPS) { 93 CMH("this is virtually identical in all arrayKlasses, refactor") 94 if (length < 0) { 95 THROW_0(vmSymbols::java_lang_NegativeArraySizeException()); 96 } 97 if (length > max_elements()) { 98 report_java_out_of_memory("Requested array size exceeds VM limit"); 99 JvmtiExport::post_array_size_exhausted(); 100 THROW_OOP_0(Universe::out_of_memory_error_array_size()); 101 } 102 103 size_t size = valueArrayOopDesc::object_size(layout_helper(), length); 104 KlassHandle h_k(THREAD, this); 105 if (do_zero) { 106 return CollectedHeap::array_allocate(h_k, (int)size, length, CHECK_NULL); 107 } else { 108 return CollectedHeap::array_allocate_nozero(h_k, (int)size, length, CHECK_NULL); 109 } 110 } 111 112 113 oop ValueArrayKlass::multi_allocate(int rank, jint* last_size, TRAPS) { 114 // For valueArrays this is only called for the last dimension 115 assert(rank == 1, "just checking"); 116 int length = *last_size; 117 return allocate(length, true, THREAD); 118 } 119 120 jint ValueArrayKlass::array_layout_helper(ValueKlass* vk) { 121 BasicType etype = T_VALUETYPE; 122 int atag = _lh_array_tag_type_value; 123 int esize = upper_log2(vk->raw_value_byte_size()); 124 int hsize = arrayOopDesc::base_offset_in_bytes(etype); 125 126 int lh = (atag << _lh_array_tag_shift) 127 | (1 << _lh_valuetype_bit) 128 | (hsize << _lh_header_size_shift) 129 | ((int)etype << _lh_element_type_shift) 130 | ((esize) << _lh_log2_element_size_shift); 131 132 #ifdef DEBUG 133 assert(lh < (int)_lh_neutral_value, "must look like an array layout"); 134 assert(layout_helper_is_array(lh), "correct kind"); 135 assert(layout_helper_is_valueArray(lh), "correct kind"); 136 assert(layout_helper_header_size(lh) == hsize, "correct decode"); 137 assert(layout_helper_element_type(lh) == etype, "correct decode"); 138 assert(layout_helper_is_typeArray(lh), "correct kind"); 139 assert(1 << layout_helper_log2_element_size(lh) == esize, "correct decode"); 140 #endif 141 return lh; 142 } 143 144 int ValueArrayKlass::oop_size(oop obj) const { 145 assert(obj->is_valueArray(),"must be a value array"); 146 valueArrayOop array = valueArrayOop(obj); 147 return array->object_size(); 148 } 149 150 jint ValueArrayKlass::max_elements() const { 151 return arrayOopDesc::max_array_length(arrayOopDesc::header_size(T_VALUETYPE), element_byte_size()); 152 } 153 154 oop ValueArrayKlass::protection_domain() const { 155 return element_klass()->protection_domain(); 156 } 157 158 void ValueArrayKlass::copy_array(arrayOop s, int src_pos, 159 arrayOop d, int dst_pos, int length, TRAPS) { 160 assert(s->is_valueArray(), "must be value array"); 161 162 // Check destination 163 if (!d->is_valueArray() || element_klass() != ValueArrayKlass::cast(d->klass())->element_klass()) { 164 THROW(vmSymbols::java_lang_ArrayStoreException()); 165 } 166 167 // Check is all offsets and lengths are non negative 168 if (src_pos < 0 || dst_pos < 0 || length < 0) { 169 THROW(vmSymbols::java_lang_ArrayIndexOutOfBoundsException()); 170 } 171 // Check if the ranges are valid 172 if ( (((unsigned int) length + (unsigned int) src_pos) > (unsigned int) s->length()) 173 || (((unsigned int) length + (unsigned int) dst_pos) > (unsigned int) d->length()) ) { 174 THROW(vmSymbols::java_lang_ArrayIndexOutOfBoundsException()); 175 } 176 // Check zero copy 177 if (length == 0) 178 return; 179 180 valueArrayOop da = valueArrayOop(d); 181 valueArrayOop sa = valueArrayOop(s); 182 int end_src_pos = src_pos + length; 183 while (src_pos < end_src_pos) { 184 element_klass()->value_store(sa->value_at_addr(src_pos++, layout_helper()), 185 da->value_at_addr(dst_pos++, layout_helper()), 186 true, 187 false); 188 } 189 } 190 191 192 Klass* ValueArrayKlass::array_klass_impl(bool or_null, int n, TRAPS) { 193 194 assert(dimension() <= n, "check order of chain"); 195 int dim = dimension(); 196 if (dim == n) return this; 197 198 if (higher_dimension() == NULL) { 199 if (or_null) return NULL; 200 201 ResourceMark rm; 202 JavaThread *jt = (JavaThread *)THREAD; 203 { 204 MutexLocker mc(Compile_lock, THREAD); // for vtables 205 // Ensure atomic creation of higher dimensions 206 MutexLocker mu(MultiArray_lock, THREAD); 207 208 // Check if another thread beat us 209 if (higher_dimension() == NULL) { 210 211 // Create multi-dim klass object and link them together 212 Klass* k = 213 ObjArrayKlass::allocate_objArray_klass(class_loader_data(), dim + 1, this, CHECK_NULL); 214 ObjArrayKlass* ak = ObjArrayKlass::cast(k); 215 ak->set_lower_dimension(this); 216 OrderAccess::storestore(); 217 set_higher_dimension(ak); 218 assert(ak->is_objArray_klass(), "incorrect initialization of ObjArrayKlass"); 219 } 220 } 221 } else { 222 CHECK_UNHANDLED_OOPS_ONLY(Thread::current()->clear_unhandled_oops()); 223 } 224 225 ObjArrayKlass *ak = ObjArrayKlass::cast(higher_dimension()); 226 if (or_null) { 227 return ak->array_klass_or_null(n); 228 } 229 return ak->array_klass(n, THREAD); 230 } 231 232 Klass* ValueArrayKlass::array_klass_impl(bool or_null, TRAPS) { 233 return array_klass_impl(or_null, dimension() + 1, THREAD); 234 } 235 236 void ValueArrayKlass::print_on(outputStream* st) const { 237 #ifndef PRODUCT 238 assert(!is_objArray_klass(), "Unimplemented"); 239 240 st->print("Value Type Array: "); 241 Klass::print_on(st); 242 243 st->print(" - element klass: "); 244 element_klass()->print_value_on(st); 245 st->cr(); 246 247 int elem_size = element_byte_size(); 248 st->print(" - element size %i ", elem_size); 249 st->print("aligned layout size %i", 1 << layout_helper_log2_element_size(layout_helper())); 250 st->cr(); 251 #endif //PRODUCT 252 } 253 254 #ifndef PRODUCT 255 void ValueArrayKlass::oop_print_on(oop obj, outputStream* st) { 256 ArrayKlass::oop_print_on(obj, st); 257 valueArrayOop va = valueArrayOop(obj); 258 ValueKlass* vk = element_klass(); 259 int print_len = MIN2((intx) va->length(), MaxElementPrintSize); 260 for(int index = 0; index < print_len; index++) { 261 st->print_cr(" - %d : ", index); 262 oop obj = (oop) ((address)va->value_at_addr(index, layout_helper()) - vk->first_field_offset()); 263 FieldPrinter print_field(st, obj); 264 vk->do_nonstatic_fields(&print_field); 265 st->cr(); 266 } 267 int remaining = va->length() - print_len; 268 if (remaining > 0) { 269 st->print_cr(" - <%d more elements, increase MaxElementPrintSize to print>", remaining); 270 } 271 } 272 #endif //PRODUCT 273