duke@435: /* xdono@631: * Copyright 1997-2008 Sun Microsystems, Inc. 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: * duke@435: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, duke@435: * CA 95054 USA or visit www.sun.com if you need additional information or duke@435: * have any questions. duke@435: * duke@435: */ duke@435: 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@548: // klassOop // 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() { coleenp@548: return UseCompressedOops ? 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) coleenp@548: ? align_object_size(typesize_in_bytes/HeapWordSize) coleenp@548: : typesize_in_bytes/HeapWordSize); duke@435: } duke@435: duke@435: // This method returns the maximum length that can passed into duke@435: // typeArrayOop::object_size(scale, length, header_size) without causing an duke@435: // overflow. We substract an extra 2*wordSize to guard against double word duke@435: // alignments. It gets the scale from the type2aelembytes array. 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"); duke@435: // We use max_jint, since object_size is internally represented by an 'int' duke@435: // This gives us an upper bound of max_jint words for the size of the oop. duke@435: int32_t max_words = (max_jint - header_size(type) - 2); coleenp@548: int elembytes = type2aelembytes(type); duke@435: jlong len = ((jlong)max_words * HeapWordSize) / elembytes; duke@435: return (len > max_jint) ? max_jint : (int32_t)len; duke@435: } duke@435: duke@435: };