arrayOop.hpp revision 0:a61af66fc99e
1/*
2 * Copyright 1997-2006 Sun Microsystems, Inc.  All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 *
23 */
24
25// arrayOopDesc is the abstract baseclass for all arrays.
26
27class arrayOopDesc : public oopDesc {
28  friend class VMStructs;
29 private:
30  int _length; // number of elements in the array
31
32 public:
33  // Interpreter/Compiler offsets
34  static int length_offset_in_bytes()             { return offset_of(arrayOopDesc, _length); }
35  static int base_offset_in_bytes(BasicType type) { return header_size(type) * HeapWordSize; }
36
37  // Returns the address of the first element.
38  void* base(BasicType type) const              { return (void*) (((intptr_t) this) + base_offset_in_bytes(type)); }
39
40  // Tells whether index is within bounds.
41  bool is_within_bounds(int index) const        { return 0 <= index && index < length(); }
42
43  // Accessores for instance variable
44  int length() const                            { return _length;   }
45  void set_length(int length)                   { _length = length; }
46
47  // Header size computation.
48  // Should only be called with constants as argument (will not constant fold otherwise)
49  static int header_size(BasicType type) {
50    return Universe::element_type_should_be_aligned(type)
51      ? align_object_size(sizeof(arrayOopDesc)/HeapWordSize)
52      : sizeof(arrayOopDesc)/HeapWordSize;
53  }
54
55  // This method returns the  maximum length that can passed into
56  // typeArrayOop::object_size(scale, length, header_size) without causing an
57  // overflow. We substract an extra 2*wordSize to guard against double word
58  // alignments.  It gets the scale from the type2aelembytes array.
59  static int32_t max_array_length(BasicType type) {
60    assert(type >= 0 && type < T_CONFLICT, "wrong type");
61    assert(type2aelembytes[type] != 0, "wrong type");
62    // We use max_jint, since object_size is internally represented by an 'int'
63    // This gives us an upper bound of max_jint words for the size of the oop.
64    int32_t max_words = (max_jint - header_size(type) - 2);
65    int elembytes = (type == T_OBJECT) ? T_OBJECT_aelem_bytes : type2aelembytes[type];
66    jlong len = ((jlong)max_words * HeapWordSize) / elembytes;
67    return (len > max_jint) ? max_jint : (int32_t)len;
68  }
69
70};
71