1 /*
  2  * Copyright (c) 1997, 2021, 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_OOPS_TYPEARRAYOOP_HPP
 26 #define SHARE_OOPS_TYPEARRAYOOP_HPP
 27 
 28 #include "oops/arrayOop.hpp"
 29 #include "oops/typeArrayKlass.hpp"
 30 #include <type_traits>
 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 namespace TypeToBT {
 37   template<typename T> BasicType to_basic_type();
 38   template<> inline BasicType to_basic_type<jboolean>() { return T_BOOLEAN; }
 39   template<> inline BasicType to_basic_type<jbyte>()    { return T_BYTE;    }
 40   template<> inline BasicType to_basic_type<jchar>()    { return T_CHAR;    }
 41   template<> inline BasicType to_basic_type<jshort>()   { return T_SHORT;   }
 42   template<> inline BasicType to_basic_type<jint>()     { return T_INT;     }
 43   template<> inline BasicType to_basic_type<jlong>()    { return T_LONG;    }
 44   template<> inline BasicType to_basic_type<jfloat>()   { return T_FLOAT;   }
 45   template<> inline BasicType to_basic_type<jdouble>()  { return T_DOUBLE;  }
 46 };
 47 
 48 class typeArrayOopDesc : public arrayOopDesc {
 49 private:
 50   template <typename T>
 51   static BasicType bt() { return TypeToBT::to_basic_type<T>(); }
 52 
 53  protected:
 54   jchar*    char_base()   const;
 55   jboolean* bool_base()   const;
 56   jbyte*    byte_base()   const;
 57   jint*     int_base()    const;
 58   jlong*    long_base()   const;
 59   jshort*   short_base()  const;
 60   jfloat*   float_base()  const;
 61   jdouble*  double_base() const;
 62 
 63   friend class TypeArrayKlass;
 64 
 65  public:
 66   template <typename T>
 67   static ptrdiff_t element_offset(int index) {
 68     return arrayOopDesc::base_offset_in_bytes(bt<T>()) + sizeof(T) * index;
 69   }
 70 
 71   jbyte* byte_at_addr(int which) const;
 72   jboolean* bool_at_addr(int which) const;
 73   jchar* char_at_addr(int which) const;
 74   jint* int_at_addr(int which) const;
 75   jshort* short_at_addr(int which) const;
 76   jushort* ushort_at_addr(int which) const;
 77   jlong* long_at_addr(int which) const;
 78   jfloat* float_at_addr(int which) const;
 79   jdouble* double_at_addr(int which) const;
 80 
 81   jbyte byte_at(int which) const;
 82   void byte_at_put(int which, jbyte contents);
 83 
 84   jboolean bool_at(int which) const;
 85   void bool_at_put(int which, jboolean contents);
 86 
 87   jchar char_at(int which) const;
 88   void char_at_put(int which, jchar contents);
 89 
 90   jint int_at(int which) const;
 91   void int_at_put(int which, jint contents);
 92 
 93   jshort short_at(int which) const;
 94   void short_at_put(int which, jshort contents);
 95 
 96   jushort ushort_at(int which) const;
 97   void ushort_at_put(int which, jushort contents);
 98 
 99   jlong long_at(int which) const;
100   void long_at_put(int which, jlong contents);
101 
102   jfloat float_at(int which) const;
103   void float_at_put(int which, jfloat contents);
104 
105   jdouble double_at(int which) const;
106   void double_at_put(int which, jdouble contents);
107 
108   jbyte byte_at_acquire(int which) const;
109   void release_byte_at_put(int which, jbyte contents);
110 
111   Symbol* symbol_at(int which) const;
112   void symbol_at_put(int which, Symbol* contents);
113 
114   // Sizing
115 
116   // Returns the number of words necessary to hold an array of "len"
117   // elements each of the given "byte_size".
118   static size_t 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((size_t)size_in_words);
131   }
132 
133  public:
134   inline size_t object_size(const TypeArrayKlass* tk) const;
135 };
136 
137 // See similar requirement for oopDesc.
138 static_assert(std::is_trivially_default_constructible<typeArrayOopDesc>::value, "required");
139 
140 #endif // SHARE_OOPS_TYPEARRAYOOP_HPP