1 /*
   2  * Copyright (c) 1997, 2013, 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_interface/collectedHeap.hpp"
  30 #include "gc_interface/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/typeArrayKlass.hpp"
  40 #include "oops/typeArrayOop.hpp"
  41 #include "runtime/handles.inline.hpp"
  42 #include "runtime/orderAccess.inline.hpp"
  43 #include "utilities/macros.hpp"
  44 
  45 bool TypeArrayKlass::compute_is_subtype_of(Klass* k) {
  46   if (!k->oop_is_typeArray()) {
  47     return ArrayKlass::compute_is_subtype_of(k);
  48   }
  49 
  50   TypeArrayKlass* tak = TypeArrayKlass::cast(k);
  51   if (dimension() != tak->dimension()) return false;
  52 
  53   return element_type() == tak->element_type();
  54 }
  55 
  56 TypeArrayKlass* TypeArrayKlass::create_klass(BasicType type,
  57                                       const char* name_str, TRAPS) {
  58   Symbol* sym = NULL;
  59   if (name_str != NULL) {
  60     sym = SymbolTable::new_permanent_symbol(name_str, CHECK_NULL);
  61   }
  62 
  63   ClassLoaderData* null_loader_data = ClassLoaderData::the_null_class_loader_data();
  64 
  65   TypeArrayKlass* ak = TypeArrayKlass::allocate(null_loader_data, type, sym, CHECK_NULL);
  66 
  67   // Add all classes to our internal class loader list here,
  68   // including classes in the bootstrap (NULL) class loader.
  69   // GC walks these as strong roots.
  70   null_loader_data->add_class(ak);
  71 
  72   // Call complete_create_array_klass after all instance variables have been initialized.
  73   complete_create_array_klass(ak, ak->super(), CHECK_NULL);
  74 
  75   return ak;
  76 }
  77 
  78 TypeArrayKlass* TypeArrayKlass::allocate(ClassLoaderData* loader_data, BasicType type, Symbol* name, TRAPS) {
  79   assert(TypeArrayKlass::header_size() <= InstanceKlass::header_size(),
  80       "array klasses must be same size as InstanceKlass");
  81 
  82   int size = ArrayKlass::static_size(TypeArrayKlass::header_size());
  83 
  84   return new (loader_data, size, THREAD) TypeArrayKlass(type, name);
  85 }
  86 
  87 TypeArrayKlass::TypeArrayKlass(BasicType type, Symbol* name) : ArrayKlass(name) {
  88   set_layout_helper(array_layout_helper(type));
  89   assert(oop_is_array(), "sanity");
  90   assert(oop_is_typeArray(), "sanity");
  91 
  92   set_max_length(arrayOopDesc::max_array_length(type));
  93   assert(size() >= TypeArrayKlass::header_size(), "bad size");
  94 
  95   set_class_loader_data(ClassLoaderData::the_null_class_loader_data());
  96 }
  97 
  98 typeArrayOop TypeArrayKlass::allocate_common(int length, bool do_zero, TRAPS) {
  99   assert(log2_element_size() >= 0, "bad scale");
 100   if (length >= 0) {
 101     if (length <= max_length()) {
 102       size_t size = typeArrayOopDesc::object_size(layout_helper(), length);
 103       KlassHandle h_k(THREAD, this);
 104       typeArrayOop t;
 105       CollectedHeap* ch = Universe::heap();
 106       if (do_zero) {
 107         t = (typeArrayOop)CollectedHeap::array_allocate(h_k, (int)size, length, CHECK_NULL);
 108       } else {
 109         t = (typeArrayOop)CollectedHeap::array_allocate_nozero(h_k, (int)size, length, CHECK_NULL);
 110       }
 111       return t;
 112     } else {
 113       report_java_out_of_memory("Requested array size exceeds VM limit");
 114       JvmtiExport::post_array_size_exhausted();
 115       THROW_OOP_0(Universe::out_of_memory_error_array_size());
 116     }
 117   } else {
 118     THROW_0(vmSymbols::java_lang_NegativeArraySizeException());
 119   }
 120 }
 121 
 122 oop TypeArrayKlass::multi_allocate(int rank, jint* last_size, TRAPS) {
 123   // For typeArrays this is only called for the last dimension
 124   assert(rank == 1, "just checking");
 125   int length = *last_size;
 126   return allocate(length, THREAD);
 127 }
 128 
 129 
 130 void TypeArrayKlass::copy_array(arrayOop s, int src_pos, arrayOop d, int dst_pos, int length, TRAPS) {
 131   assert(s->is_typeArray(), "must be type array");
 132 
 133   // Check destination
 134   if (!d->is_typeArray() || element_type() != TypeArrayKlass::cast(d->klass())->element_type()) {
 135     THROW(vmSymbols::java_lang_ArrayStoreException());
 136   }
 137 
 138   // Check is all offsets and lengths are non negative
 139   if (src_pos < 0 || dst_pos < 0 || length < 0) {
 140     THROW(vmSymbols::java_lang_ArrayIndexOutOfBoundsException());
 141   }
 142   // Check if the ranges are valid
 143   if  ( (((unsigned int) length + (unsigned int) src_pos) > (unsigned int) s->length())
 144      || (((unsigned int) length + (unsigned int) dst_pos) > (unsigned int) d->length()) ) {
 145     THROW(vmSymbols::java_lang_ArrayIndexOutOfBoundsException());
 146   }
 147   // Check zero copy
 148   if (length == 0)
 149     return;
 150 
 151   // This is an attempt to make the copy_array fast.
 152   int l2es = log2_element_size();
 153   int ihs = array_header_in_bytes() / wordSize;
 154   char* src = (char*) ((oop*)s + ihs) + ((size_t)src_pos << l2es);
 155   char* dst = (char*) ((oop*)d + ihs) + ((size_t)dst_pos << l2es);
 156   Copy::conjoint_memory_atomic(src, dst, (size_t)length << l2es);
 157 }
 158 
 159 
 160 // create a klass of array holding typeArrays
 161 Klass* TypeArrayKlass::array_klass_impl(bool or_null, int n, TRAPS) {
 162   int dim = dimension();
 163   assert(dim <= n, "check order of chain");
 164     if (dim == n)
 165       return this;
 166 
 167   if (higher_dimension() == NULL) {
 168     if (or_null)  return NULL;
 169 
 170     ResourceMark rm;
 171     JavaThread *jt = (JavaThread *)THREAD;
 172     {
 173       MutexLocker mc(Compile_lock, THREAD);   // for vtables
 174       // Atomic create higher dimension and link into list
 175       MutexLocker mu(MultiArray_lock, THREAD);
 176 
 177       if (higher_dimension() == NULL) {
 178         Klass* oak = ObjArrayKlass::allocate_objArray_klass(
 179               class_loader_data(), dim + 1, this, CHECK_NULL);
 180         ObjArrayKlass* h_ak = ObjArrayKlass::cast(oak);
 181         h_ak->set_lower_dimension(this);
 182         OrderAccess::storestore();
 183         set_higher_dimension(h_ak);
 184         assert(h_ak->oop_is_objArray(), "incorrect initialization of ObjArrayKlass");
 185       }
 186     }
 187   } else {
 188     CHECK_UNHANDLED_OOPS_ONLY(Thread::current()->clear_unhandled_oops());
 189   }
 190   ObjArrayKlass* h_ak = ObjArrayKlass::cast(higher_dimension());
 191   if (or_null) {
 192     return h_ak->array_klass_or_null(n);
 193   }
 194   return h_ak->array_klass(n, CHECK_NULL);
 195 }
 196 
 197 Klass* TypeArrayKlass::array_klass_impl(bool or_null, TRAPS) {
 198   return array_klass_impl(or_null, dimension() +  1, THREAD);
 199 }
 200 
 201 int TypeArrayKlass::oop_size(oop obj) const {
 202   assert(obj->is_typeArray(),"must be a type array");
 203   typeArrayOop t = typeArrayOop(obj);
 204   return t->object_size();
 205 }
 206 
 207 void TypeArrayKlass::oop_follow_contents(oop obj) {
 208   assert(obj->is_typeArray(),"must be a type array");
 209   // Performance tweak: We skip iterating over the klass pointer since we
 210   // know that Universe::TypeArrayKlass never moves.
 211 }
 212 
 213 #if INCLUDE_ALL_GCS
 214 void TypeArrayKlass::oop_follow_contents(ParCompactionManager* cm, oop obj) {
 215   assert(obj->is_typeArray(),"must be a type array");
 216   // Performance tweak: We skip iterating over the klass pointer since we
 217   // know that Universe::TypeArrayKlass never moves.
 218 }
 219 #endif // INCLUDE_ALL_GCS
 220 
 221 int TypeArrayKlass::oop_adjust_pointers(oop obj) {
 222   assert(obj->is_typeArray(),"must be a type array");
 223   typeArrayOop t = typeArrayOop(obj);
 224   // Performance tweak: We skip iterating over the klass pointer since we
 225   // know that Universe::TypeArrayKlass never moves.
 226   return t->object_size();
 227 }
 228 
 229 int TypeArrayKlass::get_linear_oop_intervals(oop obj, OopInterval* &start, int &size) {
 230   assert(obj->is_typeArray(),"must be a type array");
 231   typeArrayOop a = typeArrayOop(obj);
 232   size = a->object_size();
 233   return 0; // there are no oops in here!
 234 }
 235 
 236 int TypeArrayKlass::oop_oop_iterate(oop obj, ExtendedOopClosure* blk) {
 237   assert(obj->is_typeArray(),"must be a type array");
 238   typeArrayOop t = typeArrayOop(obj);
 239   // Performance tweak: We skip iterating over the klass pointer since we
 240   // know that Universe::TypeArrayKlass never moves.
 241   return t->object_size();
 242 }
 243 
 244 int TypeArrayKlass::oop_oop_iterate_m(oop obj, ExtendedOopClosure* blk, MemRegion mr) {
 245   assert(obj->is_typeArray(),"must be a type array");
 246   typeArrayOop t = typeArrayOop(obj);
 247   // Performance tweak: We skip iterating over the klass pointer since we
 248   // know that Universe::TypeArrayKlass never moves.
 249   return t->object_size();
 250 }
 251 
 252 #if INCLUDE_ALL_GCS
 253 void TypeArrayKlass::oop_push_contents(PSPromotionManager* pm, oop obj) {
 254   ShouldNotReachHere();
 255   assert(obj->is_typeArray(),"must be a type array");
 256 }
 257 
 258 int
 259 TypeArrayKlass::oop_update_pointers(ParCompactionManager* cm, oop obj) {
 260   assert(obj->is_typeArray(),"must be a type array");
 261   return typeArrayOop(obj)->object_size();
 262 }
 263 #endif // INCLUDE_ALL_GCS
 264 
 265 void TypeArrayKlass::initialize(TRAPS) {
 266   // Nothing to do. Having this function is handy since objArrayKlasses can be
 267   // initialized by calling initialize on their bottom_klass, see ObjArrayKlass::initialize
 268 }
 269 
 270 const char* TypeArrayKlass::external_name(BasicType type) {
 271   switch (type) {
 272     case T_BOOLEAN: return "[Z";
 273     case T_CHAR:    return "[C";
 274     case T_FLOAT:   return "[F";
 275     case T_DOUBLE:  return "[D";
 276     case T_BYTE:    return "[B";
 277     case T_SHORT:   return "[S";
 278     case T_INT:     return "[I";
 279     case T_LONG:    return "[J";
 280     default: ShouldNotReachHere();
 281   }
 282   return NULL;
 283 }
 284 
 285 
 286 // Printing
 287 
 288 void TypeArrayKlass::print_on(outputStream* st) const {
 289 #ifndef PRODUCT
 290   assert(is_klass(), "must be klass");
 291   print_value_on(st);
 292   Klass::print_on(st);
 293 #endif //PRODUCT
 294 }
 295 
 296 void TypeArrayKlass::print_value_on(outputStream* st) const {
 297   assert(is_klass(), "must be klass");
 298   st->print("{type array ");
 299   switch (element_type()) {
 300     case T_BOOLEAN: st->print("bool");    break;
 301     case T_CHAR:    st->print("char");    break;
 302     case T_FLOAT:   st->print("float");   break;
 303     case T_DOUBLE:  st->print("double");  break;
 304     case T_BYTE:    st->print("byte");    break;
 305     case T_SHORT:   st->print("short");   break;
 306     case T_INT:     st->print("int");     break;
 307     case T_LONG:    st->print("long");    break;
 308     default: ShouldNotReachHere();
 309   }
 310   st->print("}");
 311 }
 312 
 313 #ifndef PRODUCT
 314 
 315 static void print_boolean_array(typeArrayOop ta, int print_len, outputStream* st) {
 316   for (int index = 0; index < print_len; index++) {
 317     st->print_cr(" - %3d: %s", index, (ta->bool_at(index) == 0) ? "false" : "true");
 318   }
 319 }
 320 
 321 
 322 static void print_char_array(typeArrayOop ta, int print_len, outputStream* st) {
 323   for (int index = 0; index < print_len; index++) {
 324     jchar c = ta->char_at(index);
 325     st->print_cr(" - %3d: %x %c", index, c, isprint(c) ? c : ' ');
 326   }
 327 }
 328 
 329 
 330 static void print_float_array(typeArrayOop ta, int print_len, outputStream* st) {
 331   for (int index = 0; index < print_len; index++) {
 332     st->print_cr(" - %3d: %g", index, ta->float_at(index));
 333   }
 334 }
 335 
 336 
 337 static void print_double_array(typeArrayOop ta, int print_len, outputStream* st) {
 338   for (int index = 0; index < print_len; index++) {
 339     st->print_cr(" - %3d: %g", index, ta->double_at(index));
 340   }
 341 }
 342 
 343 
 344 static void print_byte_array(typeArrayOop ta, int print_len, outputStream* st) {
 345   for (int index = 0; index < print_len; index++) {
 346     jbyte c = ta->byte_at(index);
 347     st->print_cr(" - %3d: %x %c", index, c, isprint(c) ? c : ' ');
 348   }
 349 }
 350 
 351 
 352 static void print_short_array(typeArrayOop ta, int print_len, outputStream* st) {
 353   for (int index = 0; index < print_len; index++) {
 354     int v = ta->ushort_at(index);
 355     st->print_cr(" - %3d: 0x%x\t %d", index, v, v);
 356   }
 357 }
 358 
 359 
 360 static void print_int_array(typeArrayOop ta, int print_len, outputStream* st) {
 361   for (int index = 0; index < print_len; index++) {
 362     jint v = ta->int_at(index);
 363     st->print_cr(" - %3d: 0x%x %d", index, v, v);
 364   }
 365 }
 366 
 367 
 368 static void print_long_array(typeArrayOop ta, int print_len, outputStream* st) {
 369   for (int index = 0; index < print_len; index++) {
 370     jlong v = ta->long_at(index);
 371     st->print_cr(" - %3d: 0x%x 0x%x", index, high(v), low(v));
 372   }
 373 }
 374 
 375 
 376 void TypeArrayKlass::oop_print_on(oop obj, outputStream* st) {
 377   ArrayKlass::oop_print_on(obj, st);
 378   typeArrayOop ta = typeArrayOop(obj);
 379   int print_len = MIN2((intx) ta->length(), MaxElementPrintSize);
 380   switch (element_type()) {
 381     case T_BOOLEAN: print_boolean_array(ta, print_len, st); break;
 382     case T_CHAR:    print_char_array(ta, print_len, st);    break;
 383     case T_FLOAT:   print_float_array(ta, print_len, st);   break;
 384     case T_DOUBLE:  print_double_array(ta, print_len, st);  break;
 385     case T_BYTE:    print_byte_array(ta, print_len, st);    break;
 386     case T_SHORT:   print_short_array(ta, print_len, st);   break;
 387     case T_INT:     print_int_array(ta, print_len, st);     break;
 388     case T_LONG:    print_long_array(ta, print_len, st);    break;
 389     default: ShouldNotReachHere();
 390   }
 391   int remaining = ta->length() - print_len;
 392   if (remaining > 0) {
 393     st->print_cr(" - <%d more elements, increase MaxElementPrintSize to print>", remaining);
 394   }
 395 }
 396 
 397 #endif // PRODUCT
 398 
 399 const char* TypeArrayKlass::internal_name() const {
 400   return Klass::external_name();
 401 }