1 /*
   2  * Copyright (c) 1997, 2018, 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 
  31 // A typeArrayOop is an array containing basic types (non oop elements).
  32 // It is used for arrays of {characters, singles, doubles, bytes, shorts, integers, longs}
  33 #include <limits.h>
  34 
  35 namespace TypeToBT {
  36   template<typename T> BasicType to_basic_type();
  37   template<> inline BasicType to_basic_type<jboolean>() { return T_BOOLEAN; }
  38   template<> inline BasicType to_basic_type<jbyte>()    { return T_BYTE;    }
  39   template<> inline BasicType to_basic_type<jchar>()    { return T_CHAR;    }
  40   template<> inline BasicType to_basic_type<jshort>()   { return T_SHORT;   }
  41   template<> inline BasicType to_basic_type<jint>()     { return T_INT;     }
  42   template<> inline BasicType to_basic_type<jlong>()    { return T_LONG;    }
  43   template<> inline BasicType to_basic_type<jfloat>()   { return T_FLOAT;   }
  44   template<> inline BasicType to_basic_type<jdouble>()  { return T_DOUBLE;  }
  45 };
  46 
  47 class typeArrayOopDesc : public arrayOopDesc {
  48 private:
  49   template <typename T>
  50   static BasicType bt() { return TypeToBT::to_basic_type<T>(); }
  51 
  52  protected:
  53   jchar*    char_base()   const;
  54   jboolean* bool_base()   const;
  55   jbyte*    byte_base()   const;
  56   jint*     int_base()    const;
  57   jlong*    long_base()   const;
  58   jshort*   short_base()  const;
  59   jfloat*   float_base()  const;
  60   jdouble*  double_base() const;
  61 
  62   friend class TypeArrayKlass;
  63 
  64  public:
  65   template <typename T>
  66   static ptrdiff_t element_offset(int index) {
  67     return arrayOopDesc::base_offset_in_bytes(bt<T>()) + sizeof(T) * index;
  68   }
  69 
  70   jbyte* byte_at_addr(int which) const;
  71   jboolean* bool_at_addr(int which) const;
  72   jchar* char_at_addr(int which) const;
  73   jint* int_at_addr(int which) const;
  74   jshort* short_at_addr(int which) const;
  75   jushort* ushort_at_addr(int which) const;
  76   jlong* long_at_addr(int which) const;
  77   jfloat* float_at_addr(int which) const;
  78   jdouble* double_at_addr(int which) const;
  79 
  80   jbyte byte_at(int which) const;
  81   void byte_at_put(int which, jbyte contents);
  82 
  83   jboolean bool_at(int which) const;
  84   void bool_at_put(int which, jboolean contents);
  85 
  86   jchar char_at(int which) const;
  87   void char_at_put(int which, jchar contents);
  88 
  89   jint int_at(int which) const;
  90   void int_at_put(int which, jint contents);
  91 
  92   jshort short_at(int which) const;
  93   void short_at_put(int which, jshort contents);
  94 
  95   jushort ushort_at(int which) const;
  96   void ushort_at_put(int which, jushort contents);
  97 
  98   jlong long_at(int which) const;
  99   void long_at_put(int which, jlong contents);
 100 
 101   jfloat float_at(int which) const;
 102   void float_at_put(int which, jfloat contents);
 103 
 104   jdouble double_at(int which) const;
 105   void double_at_put(int which, jdouble contents);
 106 
 107   jbyte byte_at_acquire(int which) const;
 108   void release_byte_at_put(int which, jbyte contents);
 109 
 110   Symbol* symbol_at(int which) const;
 111   void symbol_at_put(int which, Symbol* contents);
 112 
 113   // Sizing
 114 
 115   // Returns the number of words necessary to hold an array of "len"
 116   // elements each of the given "byte_size".
 117  private:
 118   static int object_size(int lh, int length) {
 119     int instance_header_size = Klass::layout_helper_header_size(lh);
 120     int element_shift = Klass::layout_helper_log2_element_size(lh);
 121     DEBUG_ONLY(BasicType etype = Klass::layout_helper_element_type(lh));
 122     assert(length <= arrayOopDesc::max_array_length(etype), "no overflow");
 123 
 124     julong size_in_bytes = (juint)length;
 125     size_in_bytes <<= element_shift;
 126     size_in_bytes += instance_header_size;
 127     julong size_in_words = ((size_in_bytes + (HeapWordSize-1)) >> LogHeapWordSize);
 128     assert(size_in_words <= (julong)max_jint, "no overflow");
 129 
 130     return align_object_size((intptr_t)size_in_words);
 131   }
 132 
 133  public:
 134   inline int object_size();
 135 };
 136 
 137 #endif // SHARE_VM_OOPS_TYPEARRAYOOP_HPP