Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/oops/arrayOop.hpp
32285 views
/*1* Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#ifndef SHARE_VM_OOPS_ARRAYOOP_HPP25#define SHARE_VM_OOPS_ARRAYOOP_HPP2627#include "memory/universe.inline.hpp"28#include "oops/oop.hpp"2930// arrayOopDesc is the abstract baseclass for all arrays. It doesn't31// declare pure virtual to enforce this because that would allocate a vtbl32// in each instance, which we don't want.3334// The layout of array Oops is:35//36// markOop37// Klass* // 32 bits if compressed but declared 64 in LP64.38// length // shares klass memory or allocated after declared fields.394041class arrayOopDesc : public oopDesc {42friend class VMStructs;4344// Interpreter/Compiler offsets4546// Header size computation.47// The header is considered the oop part of this type plus the length.48// Returns the aligned header_size_in_bytes. This is not equivalent to49// sizeof(arrayOopDesc) which should not appear in the code.50static int header_size_in_bytes() {51size_t hs = align_size_up(length_offset_in_bytes() + sizeof(int),52HeapWordSize);53#ifdef ASSERT54// make sure it isn't called before UseCompressedOops is initialized.55static size_t arrayoopdesc_hs = 0;56if (arrayoopdesc_hs == 0) arrayoopdesc_hs = hs;57assert(arrayoopdesc_hs == hs, "header size can't change");58#endif // ASSERT59return (int)hs;60}6162public:63// The _length field is not declared in C++. It is allocated after the64// declared nonstatic fields in arrayOopDesc if not compressed, otherwise65// it occupies the second half of the _klass field in oopDesc.66static int length_offset_in_bytes() {67return UseCompressedClassPointers ? klass_gap_offset_in_bytes() :68sizeof(arrayOopDesc);69}7071// Returns the offset of the first element.72static int base_offset_in_bytes(BasicType type) {73return header_size(type) * HeapWordSize;74}7576// Returns the address of the first element.77void* base(BasicType type) const {78return (void*) (((intptr_t) this) + base_offset_in_bytes(type));79}8081// Tells whether index is within bounds.82bool is_within_bounds(int index) const { return 0 <= index && index < length(); }8384// Accessors for instance variable which is not a C++ declared nonstatic85// field.86int length() const {87return *(int*)(((intptr_t)this) + length_offset_in_bytes());88}89void set_length(int length) {90*(int*)(((intptr_t)this) + length_offset_in_bytes()) = length;91}9293// Should only be called with constants as argument94// (will not constant fold otherwise)95// Returns the header size in words aligned to the requirements of the96// array object type.97static int header_size(BasicType type) {98size_t typesize_in_bytes = header_size_in_bytes();99return (int)(Universe::element_type_should_be_aligned(type)100? align_object_offset(typesize_in_bytes/HeapWordSize)101: typesize_in_bytes/HeapWordSize);102}103104// Return the maximum length of an array of BasicType. The length can passed105// to typeArrayOop::object_size(scale, length, header_size) without causing an106// overflow. We also need to make sure that this will not overflow a size_t on107// 32 bit platforms when we convert it to a byte size.108static int32_t max_array_length(BasicType type) {109assert(type >= 0 && type < T_CONFLICT, "wrong type");110assert(type2aelembytes(type) != 0, "wrong type");111112const size_t max_element_words_per_size_t =113align_size_down((SIZE_MAX/HeapWordSize - header_size(type)), MinObjAlignment);114const size_t max_elements_per_size_t =115HeapWordSize * max_element_words_per_size_t / type2aelembytes(type);116if ((size_t)max_jint < max_elements_per_size_t) {117// It should be ok to return max_jint here, but parts of the code118// (CollectedHeap, Klass::oop_oop_iterate(), and more) uses an int for119// passing around the size (in words) of an object. So, we need to avoid120// overflowing an int when we add the header. See CRs 4718400 and 7110613.121return align_size_down(max_jint - header_size(type), MinObjAlignment);122}123return (int32_t)max_elements_per_size_t;124}125126// for unit testing127#ifndef PRODUCT128static bool check_max_length_overflow(BasicType type);129static int32_t old_max_array_length(BasicType type);130static void test_max_array_length();131#endif132};133134#endif // SHARE_VM_OOPS_ARRAYOOP_HPP135136137