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