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