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