1 #ifdef USE_PRAGMA_IDENT_HDR
2 #pragma ident "@(#)arrayOop.hpp 1.35 07/05/05 17:06:00 JVM"
3 #endif
4 /*
5 * Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved.
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7 *
8 * This code is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License version 2 only, as
10 * published by the Free Software Foundation.
11 *
12 * This code is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * version 2 for more details (a copy is included in the LICENSE file that
16 * accompanied this code).
17 *
18 * You should have received a copy of the GNU General Public License version
19 * 2 along with this work; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21 *
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
24 * have any questions.
25 *
26 */
27
28 // arrayOopDesc is the abstract baseclass for all arrays.
29
30 class arrayOopDesc : public oopDesc {
31 friend class VMStructs;
32 private:
33 int _length; // number of elements in the array
34
35 public:
36 // Interpreter/Compiler offsets
37 static int length_offset_in_bytes() { return offset_of(arrayOopDesc, _length); }
38 static int base_offset_in_bytes(BasicType type) { return header_size(type) * HeapWordSize; }
39
40 // Returns the address of the first element.
41 void* base(BasicType type) const { return (void*) (((intptr_t) this) + base_offset_in_bytes(type)); }
42
43 // Tells whether index is within bounds.
44 bool is_within_bounds(int index) const { return 0 <= index && index < length(); }
45
46 // Accessores for instance variable
47 int length() const { return _length; }
48 void set_length(int length) { _length = length; }
49
50 // Header size computation.
51 // Should only be called with constants as argument (will not constant fold otherwise)
52 static int header_size(BasicType type) {
53 return Universe::element_type_should_be_aligned(type)
54 ? align_object_size(sizeof(arrayOopDesc)/HeapWordSize)
55 : sizeof(arrayOopDesc)/HeapWordSize;
56 }
57
58 // This method returns the maximum length that can passed into
59 // typeArrayOop::object_size(scale, length, header_size) without causing an
60 // overflow. We substract an extra 2*wordSize to guard against double word
61 // alignments. It gets the scale from the type2aelembytes array.
62 static int32_t max_array_length(BasicType type) {
63 assert(type >= 0 && type < T_CONFLICT, "wrong type");
64 assert(type2aelembytes[type] != 0, "wrong type");
65 // We use max_jint, since object_size is internally represented by an 'int'
66 // This gives us an upper bound of max_jint words for the size of the oop.
67 int32_t max_words = (max_jint - header_size(type) - 2);
68 int elembytes = (type == T_OBJECT) ? T_OBJECT_aelem_bytes : type2aelembytes[type];
69 jlong len = ((jlong)max_words * HeapWordSize) / elembytes;
70 return (len > max_jint) ? max_jint : (int32_t)len;
71 }
72
73 };
|
1 #ifdef USE_PRAGMA_IDENT_HDR
2 #pragma ident "@(#)arrayOop.hpp 1.35 07/05/05 17:06:00 JVM"
3 #endif
4 /*
5 * Copyright 1997-2008 Sun Microsystems, Inc. All Rights Reserved.
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7 *
8 * This code is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License version 2 only, as
10 * published by the Free Software Foundation.
11 *
12 * This code is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * version 2 for more details (a copy is included in the LICENSE file that
16 * accompanied this code).
17 *
18 * You should have received a copy of the GNU General Public License version
19 * 2 along with this work; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21 *
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
24 * have any questions.
25 *
26 */
27
28 // arrayOopDesc is the abstract baseclass for all arrays. It doesn't
29 // declare pure virtual to enforce this because that would allocate a vtbl
30 // in each instance, which we don't want.
31
32 // The layout of array Oops is:
33 //
34 // markOop
35 // klassOop // 32 bits if compressed but declared 64 in LP64.
36 // length // shares klass memory or allocated after declared fields.
37
38
39 class arrayOopDesc : public oopDesc {
40 friend class VMStructs;
41
42 // Interpreter/Compiler offsets
43
44 // Header size computation.
45 // The header is considered the oop part of this type plus the length.
46 // Returns the aligned header_size_in_bytes. This is not equivalent to
47 // sizeof(arrayOopDesc) which should not appear in the code.
48 static int header_size_in_bytes() {
49 size_t hs = align_size_up(length_offset_in_bytes() + sizeof(int),
50 HeapWordSize);
51 #ifdef ASSERT
52 // make sure it isn't called before UseCompressedOops is initialized.
53 static size_t arrayoopdesc_hs = 0;
54 if (arrayoopdesc_hs == 0) arrayoopdesc_hs = hs;
55 assert(arrayoopdesc_hs == hs, "header size can't change");
56 #endif // ASSERT
57 return (int)hs;
58 }
59
60 public:
61 // The _length field is not declared in C++. It is allocated after the
62 // declared nonstatic fields in arrayOopDesc if not compressed, otherwise
63 // it occupies the second half of the _klass field in oopDesc.
64 static int length_offset_in_bytes() {
65 return UseCompressedOops ? klass_gap_offset_in_bytes() :
66 sizeof(arrayOopDesc);
67 }
68
69 // Returns the offset of the first element.
70 static int base_offset_in_bytes(BasicType type) {
71 return header_size(type) * HeapWordSize;
72 }
73
74 // Returns the address of the first element.
75 void* base(BasicType type) const {
76 return (void*) (((intptr_t) this) + base_offset_in_bytes(type));
77 }
78
79 // Tells whether index is within bounds.
80 bool is_within_bounds(int index) const { return 0 <= index && index < length(); }
81
82 // Accessors for instance variable which is not a C++ declared nonstatic
83 // field.
84 int length() const {
85 return *(int*)(((intptr_t)this) + length_offset_in_bytes());
86 }
87 void set_length(int length) {
88 *(int*)(((intptr_t)this) + length_offset_in_bytes()) = length;
89 }
90
91 // Should only be called with constants as argument
92 // (will not constant fold otherwise)
93 // Returns the header size in words aligned to the requirements of the
94 // array object type.
95 static int header_size(BasicType type) {
96 size_t typesize_in_bytes = header_size_in_bytes();
97 return (int)(Universe::element_type_should_be_aligned(type)
98 ? align_object_size(typesize_in_bytes/HeapWordSize)
99 : typesize_in_bytes/HeapWordSize);
100 }
101
102 // Return the maximum length of an array of BasicType. The length can passed
103 // to typeArrayOop::object_size(scale, length, header_size) without causing an
104 // overflow.
105 static int32_t max_array_length(BasicType type) {
106 assert(type >= 0 && type < T_CONFLICT, "wrong type");
107 assert(type2aelembytes(type) != 0, "wrong type");
108 const int bytes_per_element = type2aelembytes(type);
109 if (bytes_per_element < HeapWordSize) {
110 return max_jint;
111 }
112
113 const int32_t max_words = align_size_down(max_jint, MinObjAlignment);
114 const int32_t max_element_words = max_words - header_size(type);
115 const int32_t words_per_element = bytes_per_element >> LogHeapWordSize;
116 return max_element_words / words_per_element;
117 }
118 };
|