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