duke@435: /* coleenp@4037: * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. duke@435: * duke@435: */ duke@435: stefank@2314: #ifndef SHARE_VM_OOPS_ARRAYOOP_HPP stefank@2314: #define SHARE_VM_OOPS_ARRAYOOP_HPP stefank@2314: stefank@2314: #include "memory/universe.inline.hpp" stefank@2314: #include "oops/oop.hpp" stefank@2314: coleenp@548: // arrayOopDesc is the abstract baseclass for all arrays. It doesn't coleenp@548: // declare pure virtual to enforce this because that would allocate a vtbl coleenp@548: // in each instance, which we don't want. coleenp@548: coleenp@548: // The layout of array Oops is: coleenp@548: // coleenp@548: // markOop coleenp@4037: // Klass* // 32 bits if compressed but declared 64 in LP64. coleenp@548: // length // shares klass memory or allocated after declared fields. coleenp@548: duke@435: duke@435: class arrayOopDesc : public oopDesc { duke@435: friend class VMStructs; coleenp@548: coleenp@548: // Interpreter/Compiler offsets coleenp@548: coleenp@548: // Header size computation. coleenp@548: // The header is considered the oop part of this type plus the length. coleenp@548: // Returns the aligned header_size_in_bytes. This is not equivalent to kvn@600: // sizeof(arrayOopDesc) which should not appear in the code. coleenp@548: static int header_size_in_bytes() { kvn@600: size_t hs = align_size_up(length_offset_in_bytes() + sizeof(int), kvn@600: HeapWordSize); coleenp@548: #ifdef ASSERT coleenp@548: // make sure it isn't called before UseCompressedOops is initialized. coleenp@548: static size_t arrayoopdesc_hs = 0; coleenp@548: if (arrayoopdesc_hs == 0) arrayoopdesc_hs = hs; coleenp@548: assert(arrayoopdesc_hs == hs, "header size can't change"); coleenp@548: #endif // ASSERT coleenp@548: return (int)hs; coleenp@548: } duke@435: duke@435: public: coleenp@548: // The _length field is not declared in C++. It is allocated after the coleenp@548: // declared nonstatic fields in arrayOopDesc if not compressed, otherwise coleenp@548: // it occupies the second half of the _klass field in oopDesc. coleenp@548: static int length_offset_in_bytes() { ehelin@5694: return UseCompressedClassPointers ? klass_gap_offset_in_bytes() : coleenp@548: sizeof(arrayOopDesc); coleenp@548: } coleenp@548: coleenp@548: // Returns the offset of the first element. coleenp@548: static int base_offset_in_bytes(BasicType type) { coleenp@548: return header_size(type) * HeapWordSize; coleenp@548: } duke@435: duke@435: // Returns the address of the first element. coleenp@548: void* base(BasicType type) const { coleenp@548: return (void*) (((intptr_t) this) + base_offset_in_bytes(type)); coleenp@548: } duke@435: duke@435: // Tells whether index is within bounds. duke@435: bool is_within_bounds(int index) const { return 0 <= index && index < length(); } duke@435: coleenp@548: // Accessors for instance variable which is not a C++ declared nonstatic coleenp@548: // field. coleenp@548: int length() const { coleenp@548: return *(int*)(((intptr_t)this) + length_offset_in_bytes()); coleenp@548: } coleenp@548: void set_length(int length) { coleenp@548: *(int*)(((intptr_t)this) + length_offset_in_bytes()) = length; coleenp@548: } duke@435: coleenp@548: // Should only be called with constants as argument coleenp@548: // (will not constant fold otherwise) coleenp@548: // Returns the header size in words aligned to the requirements of the coleenp@548: // array object type. duke@435: static int header_size(BasicType type) { coleenp@548: size_t typesize_in_bytes = header_size_in_bytes(); coleenp@548: return (int)(Universe::element_type_should_be_aligned(type) kvn@1926: ? align_object_offset(typesize_in_bytes/HeapWordSize) coleenp@548: : typesize_in_bytes/HeapWordSize); duke@435: } duke@435: jcoomes@916: // Return the maximum length of an array of BasicType. The length can passed jcoomes@916: // to typeArrayOop::object_size(scale, length, header_size) without causing an brutisso@3266: // overflow. We also need to make sure that this will not overflow a size_t on brutisso@3266: // 32 bit platforms when we convert it to a byte size. duke@435: static int32_t max_array_length(BasicType type) { duke@435: assert(type >= 0 && type < T_CONFLICT, "wrong type"); kvn@464: assert(type2aelembytes(type) != 0, "wrong type"); brutisso@3266: brutisso@3271: const size_t max_element_words_per_size_t = brutisso@3271: align_size_down((SIZE_MAX/HeapWordSize - header_size(type)), MinObjAlignment); brutisso@3271: const size_t max_elements_per_size_t = brutisso@3271: HeapWordSize * max_element_words_per_size_t / type2aelembytes(type); brutisso@3266: if ((size_t)max_jint < max_elements_per_size_t) { brutisso@3271: // It should be ok to return max_jint here, but parts of the code brutisso@3271: // (CollectedHeap, Klass::oop_oop_iterate(), and more) uses an int for brutisso@3271: // passing around the size (in words) of an object. So, we need to avoid brutisso@3271: // overflowing an int when we add the header. See CRs 4718400 and 7110613. brutisso@3271: return align_size_down(max_jint - header_size(type), MinObjAlignment); jcoomes@916: } brutisso@3266: return (int32_t)max_elements_per_size_t; brutisso@3266: } jcoomes@916: brutisso@3266: // for unit testing brutisso@3266: #ifndef PRODUCT brutisso@3266: static bool check_max_length_overflow(BasicType type); brutisso@3266: static int32_t old_max_array_length(BasicType type); stefank@3335: static void test_max_array_length(); brutisso@3266: #endif duke@435: }; stefank@2314: stefank@2314: #endif // SHARE_VM_OOPS_ARRAYOOP_HPP