1 /*
   2  * Copyright (c) 1997, 2012, 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 #ifndef SHARE_VM_OOPS_TYPEARRAYOOP_HPP
  26 #define SHARE_VM_OOPS_TYPEARRAYOOP_HPP
  27 
  28 #include "gc/shared/barrierSet.hpp"
  29 #include "oops/arrayOop.hpp"
  30 #include "oops/typeArrayKlass.hpp"
  31 #include "runtime/orderAccess.inline.hpp"
  32 
  33 // A typeArrayOop is an array containing basic types (non oop elements).
  34 // It is used for arrays of {characters, singles, doubles, bytes, shorts, integers, longs}
  35 #include <limits.h>
  36 
  37 class typeArrayOopDesc : public arrayOopDesc {
  38  protected:
  39   jchar*    char_base()   const { return (jchar*)   base(T_CHAR); }
  40   jboolean* bool_base()   const { return (jboolean*)base(T_BOOLEAN); }
  41   jbyte*    byte_base()   const { return (jbyte*)   base(T_BYTE); }
  42   jint*     int_base()    const { return (jint*)    base(T_INT); }
  43   jlong*    long_base()   const { return (jlong*)   base(T_LONG); }
  44   jshort*   short_base()  const { return (jshort*)  base(T_SHORT); }
  45   jfloat*   float_base()  const { return (jfloat*)  base(T_FLOAT); }
  46   jdouble*  double_base() const { return (jdouble*) base(T_DOUBLE); }
  47 
  48   friend class TypeArrayKlass;
  49 
  50  public:
  51   jbyte* byte_at_addr(int which) const {
  52     assert(is_within_bounds(which), "index out of bounds");
  53     return &byte_base()[which];
  54   }
  55 
  56   jboolean* bool_at_addr(int which) const {
  57     assert(is_within_bounds(which), "index out of bounds");
  58     return &bool_base()[which];
  59   }
  60 
  61   jchar* char_at_addr(int which) const {
  62     assert(is_within_bounds(which), "index out of bounds");
  63     return &char_base()[which];
  64   }
  65 
  66   jint* int_at_addr(int which) const {
  67     assert(is_within_bounds(which), "index out of bounds");
  68     return &int_base()[which];
  69   }
  70 
  71   jshort* short_at_addr(int which) const {
  72     assert(is_within_bounds(which), "index out of bounds");
  73     return &short_base()[which];
  74   }
  75 
  76   jushort* ushort_at_addr(int which) const {  // for field descriptor arrays
  77     assert(is_within_bounds(which), "index out of bounds");
  78     return (jushort*) &short_base()[which];
  79   }
  80 
  81   jlong* long_at_addr(int which) const {
  82     assert(is_within_bounds(which), "index out of bounds");
  83     return &long_base()[which];
  84   }
  85 
  86   jfloat* float_at_addr(int which) const {
  87     assert(is_within_bounds(which), "index out of bounds");
  88     return &float_base()[which];
  89   }
  90 
  91   jdouble* double_at_addr(int which) const {
  92     assert(is_within_bounds(which), "index out of bounds");
  93     return &double_base()[which];
  94   }
  95 
  96   jbyte byte_at(int which) const                  {
  97     typeArrayOop p = typeArrayOop(oopDesc::bs()->resolve_oop((oop) this));
  98     return *p->byte_at_addr(which);
  99   }
 100   void byte_at_put(int which, jbyte contents)     {
 101     typeArrayOop p = typeArrayOop(oopDesc::bs()->resolve_and_maybe_copy_oop(this));
 102     *p->byte_at_addr(which) = contents;
 103   }
 104 
 105   jboolean bool_at(int which) const               {
 106     typeArrayOop p = typeArrayOop(oopDesc::bs()->resolve_oop((oop) this));
 107     return *p->bool_at_addr(which);
 108   }
 109   void bool_at_put(int which, jboolean contents)  {
 110     typeArrayOop p = typeArrayOop(oopDesc::bs()->resolve_and_maybe_copy_oop(this));
 111     *p->bool_at_addr(which) = contents;
 112   }
 113 
 114   jchar char_at(int which) const                  {
 115     typeArrayOop p = typeArrayOop(oopDesc::bs()->resolve_oop((oop) this));
 116     return *p->char_at_addr(which);
 117   }
 118   void char_at_put(int which, jchar contents)     {
 119     typeArrayOop p = typeArrayOop(oopDesc::bs()->resolve_and_maybe_copy_oop(this));
 120     *p->char_at_addr(which) = contents;
 121   }
 122 
 123   jint int_at(int which) const                    {
 124     typeArrayOop p = typeArrayOop(oopDesc::bs()->resolve_oop((oop) this));
 125     return *p->int_at_addr(which);
 126   }
 127   void int_at_put(int which, jint contents)       {
 128     typeArrayOop p = typeArrayOop(oopDesc::bs()->resolve_and_maybe_copy_oop(this));
 129     *p->int_at_addr(which) = contents;
 130   }
 131 
 132   jshort short_at(int which) const                {
 133     typeArrayOop p = typeArrayOop(oopDesc::bs()->resolve_oop((oop) this));
 134     return *p->short_at_addr(which);
 135   }
 136   void short_at_put(int which, jshort contents)   {
 137     typeArrayOop p = typeArrayOop(oopDesc::bs()->resolve_and_maybe_copy_oop(this));
 138     *p->short_at_addr(which) = contents;
 139   }
 140 
 141   jushort ushort_at(int which) const              {
 142     typeArrayOop p = typeArrayOop(oopDesc::bs()->resolve_oop((oop) this));
 143     return *p->ushort_at_addr(which);
 144   }
 145   void ushort_at_put(int which, jushort contents) {
 146     typeArrayOop p = typeArrayOop(oopDesc::bs()->resolve_and_maybe_copy_oop(this));
 147     *p->ushort_at_addr(which) = contents;
 148   }
 149 
 150   jlong long_at(int which) const                  {
 151     typeArrayOop p = typeArrayOop(oopDesc::bs()->resolve_oop((oop) this));
 152     return *p->long_at_addr(which);
 153   }
 154   void long_at_put(int which, jlong contents)     {
 155     typeArrayOop p = typeArrayOop(oopDesc::bs()->resolve_and_maybe_copy_oop(this));
 156     *p->long_at_addr(which) = contents;
 157   }
 158 
 159   jfloat float_at(int which) const                {
 160     typeArrayOop p = typeArrayOop(oopDesc::bs()->resolve_oop((oop) this));
 161     return *p->float_at_addr(which);
 162   }
 163   void float_at_put(int which, jfloat contents)   {
 164     typeArrayOop p = typeArrayOop(oopDesc::bs()->resolve_and_maybe_copy_oop(this));
 165     *p->float_at_addr(which) = contents;
 166   }
 167 
 168   jdouble double_at(int which) const              {
 169     typeArrayOop p = typeArrayOop(oopDesc::bs()->resolve_oop((oop) this));
 170     return *p->double_at_addr(which);
 171   }
 172   void double_at_put(int which, jdouble contents) {
 173     typeArrayOop p = typeArrayOop(oopDesc::bs()->resolve_and_maybe_copy_oop(this));
 174     *p->double_at_addr(which) = contents;
 175   }
 176 
 177   jbyte byte_at_acquire(int which) const              {
 178     typeArrayOop p = typeArrayOop(oopDesc::bs()->resolve_oop((oop) this));
 179     return OrderAccess::load_acquire(p->byte_at_addr(which));
 180   }
 181   void release_byte_at_put(int which, jbyte contents) {
 182     typeArrayOop p = typeArrayOop(oopDesc::bs()->resolve_and_maybe_copy_oop(this));
 183     OrderAccess::release_store(p->byte_at_addr(which), contents);
 184   }
 185 
 186   // Java thinks metadata arrays are just arrays of either long or int, since
 187   // there doesn't seem to be T_ADDRESS, so this is a bit of unfortunate
 188   // casting
 189 #ifdef _LP64
 190   Metadata* metadata_at(int which) const {
 191     typeArrayOop p = typeArrayOop(oopDesc::bs()->resolve_oop((oop) this));
 192     return (Metadata*)*p->long_at_addr(which);
 193   }
 194   void metadata_at_put(int which, Metadata* contents) {
 195     typeArrayOop p = typeArrayOop(oopDesc::bs()->resolve_and_maybe_copy_oop(this));
 196     *p->long_at_addr(which) = (long)contents;
 197   }
 198 #else
 199   Metadata* metadata_at(int which) const {
 200     typeArrayOop p = typeArrayOop(oopDesc::bs()->resolve_oop((oop) this));
 201     return (Metadata*)*p->int_at_addr(which);
 202   }
 203   void metadata_at_put(int which, Metadata* contents) {
 204     typeArrayOop p = typeArrayOop(oopDesc::bs()->resolve_and_maybe_copy_oop(this));
 205     *p->int_at_addr(which) = (int)contents;
 206   }
 207 #endif // _LP64
 208 
 209   // Sizing
 210 
 211   // Returns the number of words necessary to hold an array of "len"
 212   // elements each of the given "byte_size".
 213  private:
 214   static int object_size(int lh, int length) {
 215     int instance_header_size = Klass::layout_helper_header_size(lh);
 216     int element_shift = Klass::layout_helper_log2_element_size(lh);
 217     DEBUG_ONLY(BasicType etype = Klass::layout_helper_element_type(lh));
 218     assert(length <= arrayOopDesc::max_array_length(etype), "no overflow");
 219 
 220     julong size_in_bytes = (juint)length;
 221     size_in_bytes <<= element_shift;
 222     size_in_bytes += instance_header_size;
 223     julong size_in_words = ((size_in_bytes + (HeapWordSize-1)) >> LogHeapWordSize);
 224     assert(size_in_words <= (julong)max_jint, "no overflow");
 225 
 226     return align_object_size((intptr_t)size_in_words);
 227   }
 228 
 229  public:
 230   int object_size() {
 231     TypeArrayKlass* tk = TypeArrayKlass::cast(klass());
 232     return object_size(tk->layout_helper(), length());
 233   }
 234 };
 235 
 236 #endif // SHARE_VM_OOPS_TYPEARRAYOOP_HPP