001/*
002 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package jdk.internal.jvmci.hotspot;
024
025import static jdk.internal.jvmci.common.UnsafeAccess.*;
026import jdk.internal.jvmci.code.*;
027import sun.misc.*;
028
029/**
030 * Implementation of {@link InstalledCode} for HotSpot.
031 */
032public abstract class HotSpotInstalledCode extends InstalledCode {
033
034    /**
035     * Total size of the code blob.
036     */
037    @SuppressFBWarnings(value = "UWF_UNWRITTEN_FIELD", justification = "field is set by the native part") private int size;
038
039    /**
040     * Start address of the code.
041     */
042    @SuppressFBWarnings(value = "UWF_UNWRITTEN_FIELD", justification = "field is set by the native part") private long codeStart;
043
044    /**
045     * Size of the code.
046     */
047    @SuppressFBWarnings(value = "UWF_UNWRITTEN_FIELD", justification = "field is set by the native part") private int codeSize;
048
049    public HotSpotInstalledCode(String name) {
050        super(name);
051    }
052
053    /**
054     * @return the total size of this code blob
055     */
056    public int getSize() {
057        return size;
058    }
059
060    /**
061     * @return a copy of this code blob if it is {@linkplain #isValid() valid}, null otherwise.
062     */
063    public byte[] getBlob() {
064        if (!isValid()) {
065            return null;
066        }
067        byte[] blob = new byte[size];
068        unsafe.copyMemory(null, getAddress(), blob, Unsafe.ARRAY_BYTE_BASE_OFFSET, size);
069        return blob;
070    }
071
072    @Override
073    public abstract String toString();
074
075    @Override
076    public long getStart() {
077        return codeStart;
078    }
079
080    @Override
081    public long getCodeSize() {
082        return codeSize;
083    }
084
085    @Override
086    public byte[] getCode() {
087        if (!isValid()) {
088            return null;
089        }
090        byte[] code = new byte[codeSize];
091        unsafe.copyMemory(null, codeStart, code, Unsafe.ARRAY_BYTE_BASE_OFFSET, codeSize);
092        return code;
093    }
094}