Path: blob/master/src/hotspot/share/oops/arrayOop.hpp
40951 views
/*1* Copyright (c) 1997, 2020, 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_OOPS_ARRAYOOP_HPP25#define SHARE_OOPS_ARRAYOOP_HPP2627#include "oops/oop.hpp"28#include "utilities/align.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// markWord37// 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;43friend class arrayOopDescTest;4445// Interpreter/Compiler offsets4647// Header size computation.48// The header is considered the oop part of this type plus the length.49// Returns the aligned header_size_in_bytes. This is not equivalent to50// sizeof(arrayOopDesc) which should not appear in the code.51static int header_size_in_bytes() {52size_t hs = align_up(length_offset_in_bytes() + sizeof(int),53HeapWordSize);54#ifdef ASSERT55// make sure it isn't called before UseCompressedOops is initialized.56static size_t arrayoopdesc_hs = 0;57if (arrayoopdesc_hs == 0) arrayoopdesc_hs = hs;58assert(arrayoopdesc_hs == hs, "header size can't change");59#endif // ASSERT60return (int)hs;61}6263// Returns the address of the length "field". See length_offset_in_bytes().64static int* length_addr_impl(void* obj_ptr) {65char* ptr = static_cast<char*>(obj_ptr);66return reinterpret_cast<int*>(ptr + length_offset_in_bytes());67}6869// Check whether an element of a typeArrayOop with the given type must be70// aligned 0 mod 8. The typeArrayOop itself must be aligned at least this71// strongly.72static bool element_type_should_be_aligned(BasicType type) {73return type == T_DOUBLE || type == T_LONG;74}7576public:77// The _length field is not declared in C++. It is allocated after the78// declared nonstatic fields in arrayOopDesc if not compressed, otherwise79// it occupies the second half of the _klass field in oopDesc.80static int length_offset_in_bytes() {81return UseCompressedClassPointers ? klass_gap_offset_in_bytes() :82sizeof(arrayOopDesc);83}8485// Returns the offset of the first element.86static int base_offset_in_bytes(BasicType type) {87return header_size(type) * HeapWordSize;88}8990// Returns the address of the first element. The elements in the array will not91// relocate from this address until a subsequent thread transition.92void* base(BasicType type) const {93return reinterpret_cast<void*>(cast_from_oop<intptr_t>(as_oop()) + base_offset_in_bytes(type));94}9596template <typename T>97static T* obj_offset_to_raw(arrayOop obj, size_t offset_in_bytes, T* raw) {98if (obj != NULL) {99assert(raw == NULL, "either raw or in-heap");100char* base = reinterpret_cast<char*>((void*) obj);101raw = reinterpret_cast<T*>(base + offset_in_bytes);102} else {103assert(raw != NULL, "either raw or in-heap");104}105return raw;106}107108// Tells whether index is within bounds.109bool is_within_bounds(int index) const { return 0 <= index && index < length(); }110111// Accessors for array length. There's not a member variable for112// it; see length_offset_in_bytes().113int length() const { return *length_addr_impl(const_cast<arrayOopDesc*>(this)); }114void set_length(int length) { *length_addr_impl(this) = length; }115116int* length_addr() {117return length_addr_impl(this);118}119120static void set_length(HeapWord* mem, int length) {121*length_addr_impl(mem) = length;122}123124// Should only be called with constants as argument125// (will not constant fold otherwise)126// Returns the header size in words aligned to the requirements of the127// array object type.128static int header_size(BasicType type) {129size_t typesize_in_bytes = header_size_in_bytes();130return (int)(element_type_should_be_aligned(type)131? align_object_offset(typesize_in_bytes/HeapWordSize)132: typesize_in_bytes/HeapWordSize);133}134135// Return the maximum length of an array of BasicType. The length can passed136// to typeArrayOop::object_size(scale, length, header_size) without causing an137// overflow. We also need to make sure that this will not overflow a size_t on138// 32 bit platforms when we convert it to a byte size.139static int32_t max_array_length(BasicType type) {140assert(type >= 0 && type < T_CONFLICT, "wrong type");141assert(type2aelembytes(type) != 0, "wrong type");142143const size_t max_element_words_per_size_t =144align_down((SIZE_MAX/HeapWordSize - header_size(type)), MinObjAlignment);145const size_t max_elements_per_size_t =146HeapWordSize * max_element_words_per_size_t / type2aelembytes(type);147if ((size_t)max_jint < max_elements_per_size_t) {148// It should be ok to return max_jint here, but parts of the code149// (CollectedHeap, Klass::oop_oop_iterate(), and more) uses an int for150// passing around the size (in words) of an object. So, we need to avoid151// overflowing an int when we add the header. See CRs 4718400 and 7110613.152return align_down(max_jint - header_size(type), MinObjAlignment);153}154return (int32_t)max_elements_per_size_t;155}156157};158159#endif // SHARE_OOPS_ARRAYOOP_HPP160161162