Path: blob/master/src/jdk.hotspot.agent/share/classes/sun/jvm/hotspot/utilities/GenericArray.java
41161 views
/*1* Copyright (c) 2012, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324package sun.jvm.hotspot.utilities;2526import sun.jvm.hotspot.debugger.Address;27import sun.jvm.hotspot.oops.ArrayKlass;28import sun.jvm.hotspot.oops.CIntField;29import sun.jvm.hotspot.oops.Oop;30import sun.jvm.hotspot.runtime.VM;31import sun.jvm.hotspot.runtime.VMObject;32import sun.jvm.hotspot.types.AddressField;33import sun.jvm.hotspot.types.Type;34import sun.jvm.hotspot.types.TypeDataBase;35import sun.jvm.hotspot.types.WrongTypeException;36import sun.jvm.hotspot.utilities.Observable;37import sun.jvm.hotspot.utilities.Observer;3839/**40* The base class for the mirrors of the Array<T> C++ classes.41*/42public abstract class GenericArray extends VMObject {43static {44VM.registerVMInitializedObserver(new Observer() {45public void update(Observable o, Object data) {46initialize(VM.getVM().getTypeDataBase());47}48});49}5051private static synchronized void initialize(TypeDataBase db) throws WrongTypeException {52// Array<int> is arbitrarily chosen to get the fields in Array<T>.53Type type = db.lookupType("Array<int>");54lengthField = new CIntField(type.getCIntegerField("_length"), 0);55}5657private static long sizeOfArray;58private static CIntField lengthField;5960private long dataFieldOffset;6162public GenericArray(Address addr, long dataOffset) {63super(addr);64dataFieldOffset = dataOffset;65}6667public int length() {68return (int)lengthField.getValue(this);69}7071// for compatibility with TypeArray72public int getLength() {73return length();74}7576/**77* Gets the element at the given index.78*/79protected long getIntegerAt(int index) {80if (index < 0 || index >= length()) throw new ArrayIndexOutOfBoundsException(index + " " + length());8182Type elemType = getElemType();83if (!getElemType().isCIntegerType()) throw new RuntimeException("elemType must be of CInteger type");8485Address data = getAddress().addOffsetTo(dataFieldOffset);86long elemSize = elemType.getSize();8788return data.getCIntegerAt(index * elemSize, elemSize, false);89}9091protected Address getAddressAt(int index) {92if (index < 0 || index >= length()) throw new ArrayIndexOutOfBoundsException(index);9394Type elemType = getElemType();95if (getElemType().isCIntegerType()) throw new RuntimeException("elemType must not be of CInteger type");9697Address data = getAddress().addOffsetTo(dataFieldOffset);98long elemSize = elemType.getSize();99100return data.getAddressAt(index * elemSize);101}102103private long byteSizeof(int length) { return sizeOfArray + length * getElemType().getSize(); }104105public long getSize() {106return VM.getVM().alignUp(byteSizeof(length()), VM.getVM().getBytesPerWord()) / VM.getVM().getBytesPerWord();107}108109/**110* The element type of this array.111*/112public abstract Type getElemType();113}114115116