1 /*
   2  * Copyright (c) 1997, 2017, 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 #include "runtime/orderAccess.inline.hpp"
  31 
  32 // A typeArrayOop is an array containing basic types (non oop elements).
  33 // It is used for arrays of {characters, singles, doubles, bytes, shorts, integers, longs}
  34 #include <limits.h>
  35 
  36 class typeArrayOopDesc : public arrayOopDesc {
  37  protected:
  38   jchar*    char_base()   const { return (jchar*)   base(T_CHAR); }
  39   jboolean* bool_base()   const { return (jboolean*)base(T_BOOLEAN); }
  40   jbyte*    byte_base()   const { return (jbyte*)   base(T_BYTE); }
  41   jint*     int_base()    const { return (jint*)    base(T_INT); }
  42   jlong*    long_base()   const { return (jlong*)   base(T_LONG); }
  43   jshort*   short_base()  const { return (jshort*)  base(T_SHORT); }
  44   jfloat*   float_base()  const { return (jfloat*)  base(T_FLOAT); }
  45   jdouble*  double_base() const { return (jdouble*) base(T_DOUBLE); }
  46 
  47   static int char_at_offset(int index)   { return base_offset_in_bytes(T_CHAR) + sizeof(jchar) * index; }
  48   static int bool_at_offset(int index)   { return base_offset_in_bytes(T_BOOLEAN) + sizeof(jboolean) * index; }
  49   static int byte_at_offset(int index)   { return base_offset_in_bytes(T_BYTE) + sizeof(jbyte) * index; }
  50   static int int_at_offset(int index)    { return base_offset_in_bytes(T_INT) + sizeof(jint) * index; }
  51   static int long_at_offset(int index)   { return base_offset_in_bytes(T_LONG) + sizeof(jlong) * index; }
  52   static int short_at_offset(int index)  { return base_offset_in_bytes(T_SHORT) + sizeof(jshort) * index; }
  53   static int float_at_offset(int index)  { return base_offset_in_bytes(T_FLOAT) + sizeof(jfloat) * index; }
  54   static int double_at_offset(int index) { return base_offset_in_bytes(T_DOUBLE) + sizeof(jdouble) * index; }
  55 
  56   friend class TypeArrayKlass;
  57 
  58  public:
  59   jbyte* byte_at_addr(int which) const {
  60     assert(is_within_bounds(which), "index %d out of bounds %d", which, length());
  61     return &byte_base()[which];
  62   }
  63 
  64   jboolean* bool_at_addr(int which) const {
  65     assert(is_within_bounds(which), "index %d out of bounds %d", which, length());
  66     return &bool_base()[which];
  67   }
  68 
  69   jchar* char_at_addr(int which) const {
  70     assert(is_within_bounds(which), "index %d out of bounds %d", which, length());
  71     return &char_base()[which];
  72   }
  73 
  74   jint* int_at_addr(int which) const {
  75     assert(is_within_bounds(which), "index %d out of bounds %d", which, length());
  76     return &int_base()[which];
  77   }
  78 
  79   jshort* short_at_addr(int which) const {
  80     assert(is_within_bounds(which), "index %d out of bounds %d", which, length());
  81     return &short_base()[which];
  82   }
  83 
  84   jushort* ushort_at_addr(int which) const {  // for field descriptor arrays
  85     assert(is_within_bounds(which), "index %d out of bounds %d", which, length());
  86     return (jushort*) &short_base()[which];
  87   }
  88 
  89   jlong* long_at_addr(int which) const {
  90     assert(is_within_bounds(which), "index %d out of bounds %d", which, length());
  91     return &long_base()[which];
  92   }
  93 
  94   jfloat* float_at_addr(int which) const {
  95     assert(is_within_bounds(which), "index %d out of bounds %d", which, length());
  96     return &float_base()[which];
  97   }
  98 
  99   jdouble* double_at_addr(int which) const {
 100     assert(is_within_bounds(which), "index %d out of bounds %d", which, length());
 101     return &double_base()[which];
 102   }
 103 
 104   jbyte byte_at(int which) const;
 105   void byte_at_put(int which, jbyte contents);
 106 
 107   jboolean bool_at(int which) const;
 108   void bool_at_put(int which, jboolean contents);
 109 
 110   jchar char_at(int which) const;
 111   void char_at_put(int which, jchar contents);
 112 
 113   jint int_at(int which) const;
 114   void int_at_put(int which, jint contents);
 115 
 116   jshort short_at(int which) const;
 117   void short_at_put(int which, jshort contents);
 118 
 119   jushort ushort_at(int which) const;
 120   void ushort_at_put(int which, jushort contents);
 121 
 122   jlong long_at(int which) const;
 123   void long_at_put(int which, jlong contents);
 124 
 125   jfloat float_at(int which) const;
 126   void float_at_put(int which, jfloat contents);
 127 
 128   jdouble double_at(int which) const;
 129   void double_at_put(int which, jdouble contents);
 130 
 131   jbyte byte_at_acquire(int which) const;
 132   void release_byte_at_put(int which, jbyte contents);
 133 
 134   // Java thinks Symbol arrays are just arrays of either long or int, since
 135   // there doesn't seem to be T_ADDRESS, so this is a bit of unfortunate
 136   // casting
 137 #ifdef _LP64
 138   Symbol* symbol_at(int which) const {
 139     return (Symbol*)*long_at_addr(which); }
 140   void symbol_at_put(int which, Symbol* contents) {
 141     *long_at_addr(which) = (jlong)contents;
 142   }
 143 #else
 144   Symbol* symbol_at(int which) const {
 145     return (Symbol*)*int_at_addr(which); }
 146   void symbol_at_put(int which, Symbol* contents) {
 147     *int_at_addr(which) = (int)contents;
 148   }
 149 #endif // _LP64
 150 
 151   // Sizing
 152 
 153   // Returns the number of words necessary to hold an array of "len"
 154   // elements each of the given "byte_size".
 155  private:
 156   static int object_size(int lh, int length) {
 157     int instance_header_size = Klass::layout_helper_header_size(lh);
 158     int element_shift = Klass::layout_helper_log2_element_size(lh);
 159     DEBUG_ONLY(BasicType etype = Klass::layout_helper_element_type(lh));
 160     assert(length <= arrayOopDesc::max_array_length(etype), "no overflow");
 161 
 162     julong size_in_bytes = (juint)length;
 163     size_in_bytes <<= element_shift;
 164     size_in_bytes += instance_header_size;
 165     julong size_in_words = ((size_in_bytes + (HeapWordSize-1)) >> LogHeapWordSize);
 166     assert(size_in_words <= (julong)max_jint, "no overflow");
 167 
 168     return align_object_size((intptr_t)size_in_words);
 169   }
 170 
 171  public:
 172   inline int object_size();
 173 };
 174 
 175 #endif // SHARE_VM_OOPS_TYPEARRAYOOP_HPP