src/share/vm/oops/arrayOop.hpp

Tue, 08 Aug 2017 15:57:29 +0800

author
aoqi
date
Tue, 08 Aug 2017 15:57:29 +0800
changeset 6876
710a3c8b516e
parent 6198
55fb97c4c58d
parent 0
f90c822e73f8
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #ifndef SHARE_VM_OOPS_ARRAYOOP_HPP
aoqi@0 26 #define SHARE_VM_OOPS_ARRAYOOP_HPP
aoqi@0 27
aoqi@0 28 #include "memory/universe.inline.hpp"
aoqi@0 29 #include "oops/oop.hpp"
aoqi@0 30
aoqi@0 31 // arrayOopDesc is the abstract baseclass for all arrays. It doesn't
aoqi@0 32 // declare pure virtual to enforce this because that would allocate a vtbl
aoqi@0 33 // in each instance, which we don't want.
aoqi@0 34
aoqi@0 35 // The layout of array Oops is:
aoqi@0 36 //
aoqi@0 37 // markOop
aoqi@0 38 // Klass* // 32 bits if compressed but declared 64 in LP64.
aoqi@0 39 // length // shares klass memory or allocated after declared fields.
aoqi@0 40
aoqi@0 41
aoqi@0 42 class arrayOopDesc : public oopDesc {
aoqi@0 43 friend class VMStructs;
aoqi@0 44
aoqi@0 45 // Interpreter/Compiler offsets
aoqi@0 46
aoqi@0 47 // Header size computation.
aoqi@0 48 // The header is considered the oop part of this type plus the length.
aoqi@0 49 // Returns the aligned header_size_in_bytes. This is not equivalent to
aoqi@0 50 // sizeof(arrayOopDesc) which should not appear in the code.
aoqi@0 51 static int header_size_in_bytes() {
aoqi@0 52 size_t hs = align_size_up(length_offset_in_bytes() + sizeof(int),
aoqi@0 53 HeapWordSize);
aoqi@0 54 #ifdef ASSERT
aoqi@0 55 // make sure it isn't called before UseCompressedOops is initialized.
aoqi@0 56 static size_t arrayoopdesc_hs = 0;
aoqi@0 57 if (arrayoopdesc_hs == 0) arrayoopdesc_hs = hs;
aoqi@0 58 assert(arrayoopdesc_hs == hs, "header size can't change");
aoqi@0 59 #endif // ASSERT
aoqi@0 60 return (int)hs;
aoqi@0 61 }
aoqi@0 62
aoqi@0 63 public:
aoqi@0 64 // The _length field is not declared in C++. It is allocated after the
aoqi@0 65 // declared nonstatic fields in arrayOopDesc if not compressed, otherwise
aoqi@0 66 // it occupies the second half of the _klass field in oopDesc.
aoqi@0 67 static int length_offset_in_bytes() {
aoqi@0 68 return UseCompressedClassPointers ? klass_gap_offset_in_bytes() :
aoqi@0 69 sizeof(arrayOopDesc);
aoqi@0 70 }
aoqi@0 71
aoqi@0 72 // Returns the offset of the first element.
aoqi@0 73 static int base_offset_in_bytes(BasicType type) {
aoqi@0 74 return header_size(type) * HeapWordSize;
aoqi@0 75 }
aoqi@0 76
aoqi@0 77 // Returns the address of the first element.
aoqi@0 78 void* base(BasicType type) const {
aoqi@0 79 return (void*) (((intptr_t) this) + base_offset_in_bytes(type));
aoqi@0 80 }
aoqi@0 81
aoqi@0 82 // Tells whether index is within bounds.
aoqi@0 83 bool is_within_bounds(int index) const { return 0 <= index && index < length(); }
aoqi@0 84
aoqi@0 85 // Accessors for instance variable which is not a C++ declared nonstatic
aoqi@0 86 // field.
aoqi@0 87 int length() const {
aoqi@0 88 return *(int*)(((intptr_t)this) + length_offset_in_bytes());
aoqi@0 89 }
aoqi@0 90 void set_length(int length) {
aoqi@0 91 *(int*)(((intptr_t)this) + length_offset_in_bytes()) = length;
aoqi@0 92 }
aoqi@0 93
aoqi@0 94 // Should only be called with constants as argument
aoqi@0 95 // (will not constant fold otherwise)
aoqi@0 96 // Returns the header size in words aligned to the requirements of the
aoqi@0 97 // array object type.
aoqi@0 98 static int header_size(BasicType type) {
aoqi@0 99 size_t typesize_in_bytes = header_size_in_bytes();
aoqi@0 100 return (int)(Universe::element_type_should_be_aligned(type)
aoqi@0 101 ? align_object_offset(typesize_in_bytes/HeapWordSize)
aoqi@0 102 : typesize_in_bytes/HeapWordSize);
aoqi@0 103 }
aoqi@0 104
aoqi@0 105 // Return the maximum length of an array of BasicType. The length can passed
aoqi@0 106 // to typeArrayOop::object_size(scale, length, header_size) without causing an
aoqi@0 107 // overflow. We also need to make sure that this will not overflow a size_t on
aoqi@0 108 // 32 bit platforms when we convert it to a byte size.
aoqi@0 109 static int32_t max_array_length(BasicType type) {
aoqi@0 110 assert(type >= 0 && type < T_CONFLICT, "wrong type");
aoqi@0 111 assert(type2aelembytes(type) != 0, "wrong type");
aoqi@0 112
aoqi@0 113 const size_t max_element_words_per_size_t =
aoqi@0 114 align_size_down((SIZE_MAX/HeapWordSize - header_size(type)), MinObjAlignment);
aoqi@0 115 const size_t max_elements_per_size_t =
aoqi@0 116 HeapWordSize * max_element_words_per_size_t / type2aelembytes(type);
aoqi@0 117 if ((size_t)max_jint < max_elements_per_size_t) {
aoqi@0 118 // It should be ok to return max_jint here, but parts of the code
aoqi@0 119 // (CollectedHeap, Klass::oop_oop_iterate(), and more) uses an int for
aoqi@0 120 // passing around the size (in words) of an object. So, we need to avoid
aoqi@0 121 // overflowing an int when we add the header. See CRs 4718400 and 7110613.
aoqi@0 122 return align_size_down(max_jint - header_size(type), MinObjAlignment);
aoqi@0 123 }
aoqi@0 124 return (int32_t)max_elements_per_size_t;
aoqi@0 125 }
aoqi@0 126
aoqi@0 127 // for unit testing
aoqi@0 128 #ifndef PRODUCT
aoqi@0 129 static bool check_max_length_overflow(BasicType type);
aoqi@0 130 static int32_t old_max_array_length(BasicType type);
aoqi@0 131 static void test_max_array_length();
aoqi@0 132 #endif
aoqi@0 133 };
aoqi@0 134
aoqi@0 135 #endif // SHARE_VM_OOPS_ARRAYOOP_HPP

mercurial