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