0N/A/*
2362N/A * Copyright (c) 2000, 2009, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage sun.misc;
0N/A
0N/Aimport java.security.*;
0N/Aimport java.lang.reflect.*;
0N/A
6338N/Aimport sun.reflect.CallerSensitive;
6338N/Aimport sun.reflect.Reflection;
6338N/A
0N/A
0N/A/**
0N/A * A collection of methods for performing low-level, unsafe operations.
0N/A * Although the class and all methods are public, use of this class is
0N/A * limited because only trusted code can obtain instances of it.
0N/A *
0N/A * @author John R. Rose
0N/A * @see #getUnsafe
0N/A */
0N/A
0N/Apublic final class Unsafe {
0N/A
0N/A private static native void registerNatives();
0N/A static {
0N/A registerNatives();
0N/A sun.reflect.Reflection.registerMethodsToFilter(Unsafe.class, "getUnsafe");
0N/A }
0N/A
0N/A private Unsafe() {}
0N/A
0N/A private static final Unsafe theUnsafe = new Unsafe();
0N/A
0N/A /**
0N/A * Provides the caller with the capability of performing unsafe
0N/A * operations.
0N/A *
0N/A * <p> The returned <code>Unsafe</code> object should be carefully guarded
0N/A * by the caller, since it can be used to read and write data at arbitrary
0N/A * memory addresses. It must never be passed to untrusted code.
0N/A *
0N/A * <p> Most methods in this class are very low-level, and correspond to a
0N/A * small number of hardware instructions (on typical machines). Compilers
0N/A * are encouraged to optimize these methods accordingly.
0N/A *
0N/A * <p> Here is a suggested idiom for using unsafe operations:
0N/A *
0N/A * <blockquote><pre>
0N/A * class MyTrustedClass {
0N/A * private static final Unsafe unsafe = Unsafe.getUnsafe();
0N/A * ...
0N/A * private long myCountAddress = ...;
0N/A * public int getCount() { return unsafe.getByte(myCountAddress); }
0N/A * }
0N/A * </pre></blockquote>
0N/A *
0N/A * (It may assist compilers to make the local variable be
0N/A * <code>final</code>.)
0N/A *
0N/A * @exception SecurityException if a security manager exists and its
0N/A * <code>checkPropertiesAccess</code> method doesn't allow
0N/A * access to the system properties.
0N/A */
6338N/A @CallerSensitive
0N/A public static Unsafe getUnsafe() {
6338N/A Class cc = Reflection.getCallerClass();
0N/A if (cc.getClassLoader() != null)
0N/A throw new SecurityException("Unsafe");
0N/A return theUnsafe;
0N/A }
0N/A
0N/A /// peek and poke operations
0N/A /// (compilers should optimize these to memory ops)
0N/A
0N/A // These work on object fields in the Java heap.
0N/A // They will not work on elements of packed arrays.
0N/A
0N/A /**
0N/A * Fetches a value from a given Java variable.
0N/A * More specifically, fetches a field or array element within the given
0N/A * object <code>o</code> at the given offset, or (if <code>o</code> is
0N/A * null) from the memory address whose numerical value is the given
0N/A * offset.
0N/A * <p>
0N/A * The results are undefined unless one of the following cases is true:
0N/A * <ul>
0N/A * <li>The offset was obtained from {@link #objectFieldOffset} on
0N/A * the {@link java.lang.reflect.Field} of some Java field and the object
0N/A * referred to by <code>o</code> is of a class compatible with that
0N/A * field's class.
0N/A *
0N/A * <li>The offset and object reference <code>o</code> (either null or
0N/A * non-null) were both obtained via {@link #staticFieldOffset}
0N/A * and {@link #staticFieldBase} (respectively) from the
0N/A * reflective {@link Field} representation of some Java field.
0N/A *
0N/A * <li>The object referred to by <code>o</code> is an array, and the offset
0N/A * is an integer of the form <code>B+N*S</code>, where <code>N</code> is
0N/A * a valid index into the array, and <code>B</code> and <code>S</code> are
0N/A * the values obtained by {@link #arrayBaseOffset} and {@link
0N/A * #arrayIndexScale} (respectively) from the array's class. The value
0N/A * referred to is the <code>N</code><em>th</em> element of the array.
0N/A *
0N/A * </ul>
0N/A * <p>
0N/A * If one of the above cases is true, the call references a specific Java
0N/A * variable (field or array element). However, the results are undefined
0N/A * if that variable is not in fact of the type returned by this method.
0N/A * <p>
0N/A * This method refers to a variable by means of two parameters, and so
0N/A * it provides (in effect) a <em>double-register</em> addressing mode
0N/A * for Java variables. When the object reference is null, this method
0N/A * uses its offset as an absolute address. This is similar in operation
0N/A * to methods such as {@link #getInt(long)}, which provide (in effect) a
0N/A * <em>single-register</em> addressing mode for non-Java variables.
0N/A * However, because Java variables may have a different layout in memory
0N/A * from non-Java variables, programmers should not assume that these
0N/A * two addressing modes are ever equivalent. Also, programmers should
0N/A * remember that offsets from the double-register addressing mode cannot
0N/A * be portably confused with longs used in the single-register addressing
0N/A * mode.
0N/A *
0N/A * @param o Java heap object in which the variable resides, if any, else
0N/A * null
0N/A * @param offset indication of where the variable resides in a Java heap
0N/A * object, if any, else a memory address locating the variable
0N/A * statically
0N/A * @return the value fetched from the indicated Java variable
0N/A * @throws RuntimeException No defined exceptions are thrown, not even
0N/A * {@link NullPointerException}
0N/A */
0N/A public native int getInt(Object o, long offset);
0N/A
0N/A /**
0N/A * Stores a value into a given Java variable.
0N/A * <p>
0N/A * The first two parameters are interpreted exactly as with
0N/A * {@link #getInt(Object, long)} to refer to a specific
0N/A * Java variable (field or array element). The given value
0N/A * is stored into that variable.
0N/A * <p>
0N/A * The variable must be of the same type as the method
0N/A * parameter <code>x</code>.
0N/A *
0N/A * @param o Java heap object in which the variable resides, if any, else
0N/A * null
0N/A * @param offset indication of where the variable resides in a Java heap
0N/A * object, if any, else a memory address locating the variable
0N/A * statically
0N/A * @param x the value to store into the indicated Java variable
0N/A * @throws RuntimeException No defined exceptions are thrown, not even
0N/A * {@link NullPointerException}
0N/A */
0N/A public native void putInt(Object o, long offset, int x);
0N/A
0N/A /**
0N/A * Fetches a reference value from a given Java variable.
0N/A * @see #getInt(Object, long)
0N/A */
0N/A public native Object getObject(Object o, long offset);
0N/A
0N/A /**
0N/A * Stores a reference value into a given Java variable.
0N/A * <p>
0N/A * Unless the reference <code>x</code> being stored is either null
0N/A * or matches the field type, the results are undefined.
0N/A * If the reference <code>o</code> is non-null, car marks or
0N/A * other store barriers for that object (if the VM requires them)
0N/A * are updated.
0N/A * @see #putInt(Object, int, int)
0N/A */
0N/A public native void putObject(Object o, long offset, Object x);
0N/A
0N/A /** @see #getInt(Object, long) */
0N/A public native boolean getBoolean(Object o, long offset);
0N/A /** @see #putInt(Object, int, int) */
0N/A public native void putBoolean(Object o, long offset, boolean x);
0N/A /** @see #getInt(Object, long) */
0N/A public native byte getByte(Object o, long offset);
0N/A /** @see #putInt(Object, int, int) */
0N/A public native void putByte(Object o, long offset, byte x);
0N/A /** @see #getInt(Object, long) */
0N/A public native short getShort(Object o, long offset);
0N/A /** @see #putInt(Object, int, int) */
0N/A public native void putShort(Object o, long offset, short x);
0N/A /** @see #getInt(Object, long) */
0N/A public native char getChar(Object o, long offset);
0N/A /** @see #putInt(Object, int, int) */
0N/A public native void putChar(Object o, long offset, char x);
0N/A /** @see #getInt(Object, long) */
0N/A public native long getLong(Object o, long offset);
0N/A /** @see #putInt(Object, int, int) */
0N/A public native void putLong(Object o, long offset, long x);
0N/A /** @see #getInt(Object, long) */
0N/A public native float getFloat(Object o, long offset);
0N/A /** @see #putInt(Object, int, int) */
0N/A public native void putFloat(Object o, long offset, float x);
0N/A /** @see #getInt(Object, long) */
0N/A public native double getDouble(Object o, long offset);
0N/A /** @see #putInt(Object, int, int) */
0N/A public native void putDouble(Object o, long offset, double x);
0N/A
0N/A /**
0N/A * This method, like all others with 32-bit offsets, was native
0N/A * in a previous release but is now a wrapper which simply casts
0N/A * the offset to a long value. It provides backward compatibility
0N/A * with bytecodes compiled against 1.4.
0N/A * @deprecated As of 1.4.1, cast the 32-bit offset argument to a long.
0N/A * See {@link #staticFieldOffset}.
0N/A */
0N/A @Deprecated
0N/A public int getInt(Object o, int offset) {
0N/A return getInt(o, (long)offset);
0N/A }
0N/A
0N/A /**
0N/A * @deprecated As of 1.4.1, cast the 32-bit offset argument to a long.
0N/A * See {@link #staticFieldOffset}.
0N/A */
0N/A @Deprecated
0N/A public void putInt(Object o, int offset, int x) {
0N/A putInt(o, (long)offset, x);
0N/A }
0N/A
0N/A /**
0N/A * @deprecated As of 1.4.1, cast the 32-bit offset argument to a long.
0N/A * See {@link #staticFieldOffset}.
0N/A */
0N/A @Deprecated
0N/A public Object getObject(Object o, int offset) {
0N/A return getObject(o, (long)offset);
0N/A }
0N/A
0N/A /**
0N/A * @deprecated As of 1.4.1, cast the 32-bit offset argument to a long.
0N/A * See {@link #staticFieldOffset}.
0N/A */
0N/A @Deprecated
0N/A public void putObject(Object o, int offset, Object x) {
0N/A putObject(o, (long)offset, x);
0N/A }
0N/A
0N/A /**
0N/A * @deprecated As of 1.4.1, cast the 32-bit offset argument to a long.
0N/A * See {@link #staticFieldOffset}.
0N/A */
0N/A @Deprecated
0N/A public boolean getBoolean(Object o, int offset) {
0N/A return getBoolean(o, (long)offset);
0N/A }
0N/A
0N/A /**
0N/A * @deprecated As of 1.4.1, cast the 32-bit offset argument to a long.
0N/A * See {@link #staticFieldOffset}.
0N/A */
0N/A @Deprecated
0N/A public void putBoolean(Object o, int offset, boolean x) {
0N/A putBoolean(o, (long)offset, x);
0N/A }
0N/A
0N/A /**
0N/A * @deprecated As of 1.4.1, cast the 32-bit offset argument to a long.
0N/A * See {@link #staticFieldOffset}.
0N/A */
0N/A @Deprecated
0N/A public byte getByte(Object o, int offset) {
0N/A return getByte(o, (long)offset);
0N/A }
0N/A
0N/A /**
0N/A * @deprecated As of 1.4.1, cast the 32-bit offset argument to a long.
0N/A * See {@link #staticFieldOffset}.
0N/A */
0N/A @Deprecated
0N/A public void putByte(Object o, int offset, byte x) {
0N/A putByte(o, (long)offset, x);
0N/A }
0N/A
0N/A /**
0N/A * @deprecated As of 1.4.1, cast the 32-bit offset argument to a long.
0N/A * See {@link #staticFieldOffset}.
0N/A */
0N/A @Deprecated
0N/A public short getShort(Object o, int offset) {
0N/A return getShort(o, (long)offset);
0N/A }
0N/A
0N/A /**
0N/A * @deprecated As of 1.4.1, cast the 32-bit offset argument to a long.
0N/A * See {@link #staticFieldOffset}.
0N/A */
0N/A @Deprecated
0N/A public void putShort(Object o, int offset, short x) {
0N/A putShort(o, (long)offset, x);
0N/A }
0N/A
0N/A /**
0N/A * @deprecated As of 1.4.1, cast the 32-bit offset argument to a long.
0N/A * See {@link #staticFieldOffset}.
0N/A */
0N/A @Deprecated
0N/A public char getChar(Object o, int offset) {
0N/A return getChar(o, (long)offset);
0N/A }
0N/A
0N/A /**
0N/A * @deprecated As of 1.4.1, cast the 32-bit offset argument to a long.
0N/A * See {@link #staticFieldOffset}.
0N/A */
0N/A @Deprecated
0N/A public void putChar(Object o, int offset, char x) {
0N/A putChar(o, (long)offset, x);
0N/A }
0N/A
0N/A /**
0N/A * @deprecated As of 1.4.1, cast the 32-bit offset argument to a long.
0N/A * See {@link #staticFieldOffset}.
0N/A */
0N/A @Deprecated
0N/A public long getLong(Object o, int offset) {
0N/A return getLong(o, (long)offset);
0N/A }
0N/A
0N/A /**
0N/A * @deprecated As of 1.4.1, cast the 32-bit offset argument to a long.
0N/A * See {@link #staticFieldOffset}.
0N/A */
0N/A @Deprecated
0N/A public void putLong(Object o, int offset, long x) {
0N/A putLong(o, (long)offset, x);
0N/A }
0N/A
0N/A /**
0N/A * @deprecated As of 1.4.1, cast the 32-bit offset argument to a long.
0N/A * See {@link #staticFieldOffset}.
0N/A */
0N/A @Deprecated
0N/A public float getFloat(Object o, int offset) {
0N/A return getFloat(o, (long)offset);
0N/A }
0N/A
0N/A /**
0N/A * @deprecated As of 1.4.1, cast the 32-bit offset argument to a long.
0N/A * See {@link #staticFieldOffset}.
0N/A */
0N/A @Deprecated
0N/A public void putFloat(Object o, int offset, float x) {
0N/A putFloat(o, (long)offset, x);
0N/A }
0N/A
0N/A /**
0N/A * @deprecated As of 1.4.1, cast the 32-bit offset argument to a long.
0N/A * See {@link #staticFieldOffset}.
0N/A */
0N/A @Deprecated
0N/A public double getDouble(Object o, int offset) {
0N/A return getDouble(o, (long)offset);
0N/A }
0N/A
0N/A /**
0N/A * @deprecated As of 1.4.1, cast the 32-bit offset argument to a long.
0N/A * See {@link #staticFieldOffset}.
0N/A */
0N/A @Deprecated
0N/A public void putDouble(Object o, int offset, double x) {
0N/A putDouble(o, (long)offset, x);
0N/A }
0N/A
0N/A // These work on values in the C heap.
0N/A
0N/A /**
0N/A * Fetches a value from a given memory address. If the address is zero, or
0N/A * does not point into a block obtained from {@link #allocateMemory}, the
0N/A * results are undefined.
0N/A *
0N/A * @see #allocateMemory
0N/A */
0N/A public native byte getByte(long address);
0N/A
0N/A /**
0N/A * Stores a value into a given memory address. If the address is zero, or
0N/A * does not point into a block obtained from {@link #allocateMemory}, the
0N/A * results are undefined.
0N/A *
0N/A * @see #getByte(long)
0N/A */
0N/A public native void putByte(long address, byte x);
0N/A
0N/A /** @see #getByte(long) */
0N/A public native short getShort(long address);
0N/A /** @see #putByte(long, byte) */
0N/A public native void putShort(long address, short x);
0N/A /** @see #getByte(long) */
0N/A public native char getChar(long address);
0N/A /** @see #putByte(long, byte) */
0N/A public native void putChar(long address, char x);
0N/A /** @see #getByte(long) */
0N/A public native int getInt(long address);
0N/A /** @see #putByte(long, byte) */
0N/A public native void putInt(long address, int x);
0N/A /** @see #getByte(long) */
0N/A public native long getLong(long address);
0N/A /** @see #putByte(long, byte) */
0N/A public native void putLong(long address, long x);
0N/A /** @see #getByte(long) */
0N/A public native float getFloat(long address);
0N/A /** @see #putByte(long, byte) */
0N/A public native void putFloat(long address, float x);
0N/A /** @see #getByte(long) */
0N/A public native double getDouble(long address);
0N/A /** @see #putByte(long, byte) */
0N/A public native void putDouble(long address, double x);
0N/A
0N/A /**
0N/A * Fetches a native pointer from a given memory address. If the address is
0N/A * zero, or does not point into a block obtained from {@link
0N/A * #allocateMemory}, the results are undefined.
0N/A *
0N/A * <p> If the native pointer is less than 64 bits wide, it is extended as
0N/A * an unsigned number to a Java long. The pointer may be indexed by any
0N/A * given byte offset, simply by adding that offset (as a simple integer) to
0N/A * the long representing the pointer. The number of bytes actually read
0N/A * from the target address maybe determined by consulting {@link
0N/A * #addressSize}.
0N/A *
0N/A * @see #allocateMemory
0N/A */
0N/A public native long getAddress(long address);
0N/A
0N/A /**
0N/A * Stores a native pointer into a given memory address. If the address is
0N/A * zero, or does not point into a block obtained from {@link
0N/A * #allocateMemory}, the results are undefined.
0N/A *
0N/A * <p> The number of bytes actually written at the target address maybe
0N/A * determined by consulting {@link #addressSize}.
0N/A *
0N/A * @see #getAddress(long)
0N/A */
0N/A public native void putAddress(long address, long x);
0N/A
0N/A /// wrappers for malloc, realloc, free:
0N/A
0N/A /**
0N/A * Allocates a new block of native memory, of the given size in bytes. The
0N/A * contents of the memory are uninitialized; they will generally be
0N/A * garbage. The resulting native pointer will never be zero, and will be
0N/A * aligned for all value types. Dispose of this memory by calling {@link
0N/A * #freeMemory}, or resize it with {@link #reallocateMemory}.
0N/A *
0N/A * @throws IllegalArgumentException if the size is negative or too large
0N/A * for the native size_t type
0N/A *
0N/A * @throws OutOfMemoryError if the allocation is refused by the system
0N/A *
0N/A * @see #getByte(long)
0N/A * @see #putByte(long, byte)
0N/A */
0N/A public native long allocateMemory(long bytes);
0N/A
0N/A /**
0N/A * Resizes a new block of native memory, to the given size in bytes. The
0N/A * contents of the new block past the size of the old block are
0N/A * uninitialized; they will generally be garbage. The resulting native
0N/A * pointer will be zero if and only if the requested size is zero. The
0N/A * resulting native pointer will be aligned for all value types. Dispose
0N/A * of this memory by calling {@link #freeMemory}, or resize it with {@link
0N/A * #reallocateMemory}. The address passed to this method may be null, in
0N/A * which case an allocation will be performed.
0N/A *
0N/A * @throws IllegalArgumentException if the size is negative or too large
0N/A * for the native size_t type
0N/A *
0N/A * @throws OutOfMemoryError if the allocation is refused by the system
0N/A *
0N/A * @see #allocateMemory
0N/A */
0N/A public native long reallocateMemory(long address, long bytes);
0N/A
0N/A /**
0N/A * Sets all bytes in a given block of memory to a fixed value
0N/A * (usually zero).
0N/A *
0N/A * <p>This method determines a block's base address by means of two parameters,
0N/A * and so it provides (in effect) a <em>double-register</em> addressing mode,
0N/A * as discussed in {@link #getInt(Object,long)}. When the object reference is null,
0N/A * the offset supplies an absolute base address.
0N/A *
0N/A * <p>The stores are in coherent (atomic) units of a size determined
0N/A * by the address and length parameters. If the effective address and
0N/A * length are all even modulo 8, the stores take place in 'long' units.
0N/A * If the effective address and length are (resp.) even modulo 4 or 2,
0N/A * the stores take place in units of 'int' or 'short'.
0N/A *
0N/A * @since 1.7
0N/A */
0N/A public native void setMemory(Object o, long offset, long bytes, byte value);
0N/A
0N/A /**
0N/A * Sets all bytes in a given block of memory to a fixed value
0N/A * (usually zero). This provides a <em>single-register</em> addressing mode,
0N/A * as discussed in {@link #getInt(Object,long)}.
0N/A *
0N/A * <p>Equivalent to <code>setMemory(null, address, bytes, value)</code>.
0N/A */
0N/A public void setMemory(long address, long bytes, byte value) {
0N/A setMemory(null, address, bytes, value);
0N/A }
0N/A
0N/A /**
0N/A * Sets all bytes in a given block of memory to a copy of another
0N/A * block.
0N/A *
0N/A * <p>This method determines each block's base address by means of two parameters,
0N/A * and so it provides (in effect) a <em>double-register</em> addressing mode,
0N/A * as discussed in {@link #getInt(Object,long)}. When the object reference is null,
0N/A * the offset supplies an absolute base address.
0N/A *
0N/A * <p>The transfers are in coherent (atomic) units of a size determined
0N/A * by the address and length parameters. If the effective addresses and
0N/A * length are all even modulo 8, the transfer takes place in 'long' units.
0N/A * If the effective addresses and length are (resp.) even modulo 4 or 2,
0N/A * the transfer takes place in units of 'int' or 'short'.
0N/A *
0N/A * @since 1.7
0N/A */
0N/A public native void copyMemory(Object srcBase, long srcOffset,
0N/A Object destBase, long destOffset,
0N/A long bytes);
0N/A /**
0N/A * Sets all bytes in a given block of memory to a copy of another
0N/A * block. This provides a <em>single-register</em> addressing mode,
0N/A * as discussed in {@link #getInt(Object,long)}.
0N/A *
0N/A * Equivalent to <code>copyMemory(null, srcAddress, null, destAddress, bytes)</code>.
0N/A */
0N/A public void copyMemory(long srcAddress, long destAddress, long bytes) {
0N/A copyMemory(null, srcAddress, null, destAddress, bytes);
0N/A }
0N/A
0N/A /**
0N/A * Disposes of a block of native memory, as obtained from {@link
0N/A * #allocateMemory} or {@link #reallocateMemory}. The address passed to
0N/A * this method may be null, in which case no action is taken.
0N/A *
0N/A * @see #allocateMemory
0N/A */
0N/A public native void freeMemory(long address);
0N/A
0N/A /// random queries
0N/A
0N/A /**
0N/A * This constant differs from all results that will ever be returned from
0N/A * {@link #staticFieldOffset}, {@link #objectFieldOffset},
0N/A * or {@link #arrayBaseOffset}.
0N/A */
0N/A public static final int INVALID_FIELD_OFFSET = -1;
0N/A
0N/A /**
0N/A * Returns the offset of a field, truncated to 32 bits.
0N/A * This method is implemented as follows:
0N/A * <blockquote><pre>
0N/A * public int fieldOffset(Field f) {
0N/A * if (Modifier.isStatic(f.getModifiers()))
0N/A * return (int) staticFieldOffset(f);
0N/A * else
0N/A * return (int) objectFieldOffset(f);
0N/A * }
0N/A * </pre></blockquote>
0N/A * @deprecated As of 1.4.1, use {@link #staticFieldOffset} for static
0N/A * fields and {@link #objectFieldOffset} for non-static fields.
0N/A */
0N/A @Deprecated
0N/A public int fieldOffset(Field f) {
0N/A if (Modifier.isStatic(f.getModifiers()))
0N/A return (int) staticFieldOffset(f);
0N/A else
0N/A return (int) objectFieldOffset(f);
0N/A }
0N/A
0N/A /**
0N/A * Returns the base address for accessing some static field
0N/A * in the given class. This method is implemented as follows:
0N/A * <blockquote><pre>
0N/A * public Object staticFieldBase(Class c) {
0N/A * Field[] fields = c.getDeclaredFields();
0N/A * for (int i = 0; i < fields.length; i++) {
0N/A * if (Modifier.isStatic(fields[i].getModifiers())) {
0N/A * return staticFieldBase(fields[i]);
0N/A * }
0N/A * }
0N/A * return null;
0N/A * }
0N/A * </pre></blockquote>
0N/A * @deprecated As of 1.4.1, use {@link #staticFieldBase(Field)}
0N/A * to obtain the base pertaining to a specific {@link Field}.
0N/A * This method works only for JVMs which store all statics
0N/A * for a given class in one place.
0N/A */
0N/A @Deprecated
0N/A public Object staticFieldBase(Class c) {
0N/A Field[] fields = c.getDeclaredFields();
0N/A for (int i = 0; i < fields.length; i++) {
0N/A if (Modifier.isStatic(fields[i].getModifiers())) {
0N/A return staticFieldBase(fields[i]);
0N/A }
0N/A }
0N/A return null;
0N/A }
0N/A
0N/A /**
0N/A * Report the location of a given field in the storage allocation of its
0N/A * class. Do not expect to perform any sort of arithmetic on this offset;
0N/A * it is just a cookie which is passed to the unsafe heap memory accessors.
0N/A *
0N/A * <p>Any given field will always have the same offset and base, and no
0N/A * two distinct fields of the same class will ever have the same offset
0N/A * and base.
0N/A *
0N/A * <p>As of 1.4.1, offsets for fields are represented as long values,
0N/A * although the Sun JVM does not use the most significant 32 bits.
0N/A * However, JVM implementations which store static fields at absolute
0N/A * addresses can use long offsets and null base pointers to express
0N/A * the field locations in a form usable by {@link #getInt(Object,long)}.
0N/A * Therefore, code which will be ported to such JVMs on 64-bit platforms
0N/A * must preserve all bits of static field offsets.
0N/A * @see #getInt(Object, long)
0N/A */
0N/A public native long staticFieldOffset(Field f);
0N/A
0N/A /**
0N/A * Report the location of a given static field, in conjunction with {@link
0N/A * #staticFieldBase}.
0N/A * <p>Do not expect to perform any sort of arithmetic on this offset;
0N/A * it is just a cookie which is passed to the unsafe heap memory accessors.
0N/A *
0N/A * <p>Any given field will always have the same offset, and no two distinct
0N/A * fields of the same class will ever have the same offset.
0N/A *
0N/A * <p>As of 1.4.1, offsets for fields are represented as long values,
0N/A * although the Sun JVM does not use the most significant 32 bits.
0N/A * It is hard to imagine a JVM technology which needs more than
0N/A * a few bits to encode an offset within a non-array object,
0N/A * However, for consistency with other methods in this class,
0N/A * this method reports its result as a long value.
0N/A * @see #getInt(Object, long)
0N/A */
0N/A public native long objectFieldOffset(Field f);
0N/A
0N/A /**
0N/A * Report the location of a given static field, in conjunction with {@link
0N/A * #staticFieldOffset}.
0N/A * <p>Fetch the base "Object", if any, with which static fields of the
0N/A * given class can be accessed via methods like {@link #getInt(Object,
0N/A * long)}. This value may be null. This value may refer to an object
0N/A * which is a "cookie", not guaranteed to be a real Object, and it should
0N/A * not be used in any way except as argument to the get and put routines in
0N/A * this class.
0N/A */
0N/A public native Object staticFieldBase(Field f);
0N/A
0N/A /**
5459N/A * Detect if the given class may need to be initialized. This is often
5459N/A * needed in conjunction with obtaining the static field base of a
5459N/A * class.
5459N/A * @return false only if a call to {@code ensureClassInitialized} would have no effect
5459N/A */
5459N/A public native boolean shouldBeInitialized(Class<?> c);
5459N/A
5459N/A /**
0N/A * Ensure the given class has been initialized. This is often
0N/A * needed in conjunction with obtaining the static field base of a
0N/A * class.
0N/A */
0N/A public native void ensureClassInitialized(Class c);
0N/A
0N/A /**
0N/A * Report the offset of the first element in the storage allocation of a
0N/A * given array class. If {@link #arrayIndexScale} returns a non-zero value
0N/A * for the same class, you may use that scale factor, together with this
0N/A * base offset, to form new offsets to access elements of arrays of the
0N/A * given class.
0N/A *
0N/A * @see #getInt(Object, long)
0N/A * @see #putInt(Object, long, int)
0N/A */
0N/A public native int arrayBaseOffset(Class arrayClass);
0N/A
0N/A /** The value of {@code arrayBaseOffset(boolean[].class)} */
0N/A public static final int ARRAY_BOOLEAN_BASE_OFFSET
0N/A = theUnsafe.arrayBaseOffset(boolean[].class);
0N/A
0N/A /** The value of {@code arrayBaseOffset(byte[].class)} */
0N/A public static final int ARRAY_BYTE_BASE_OFFSET
0N/A = theUnsafe.arrayBaseOffset(byte[].class);
0N/A
0N/A /** The value of {@code arrayBaseOffset(short[].class)} */
0N/A public static final int ARRAY_SHORT_BASE_OFFSET
0N/A = theUnsafe.arrayBaseOffset(short[].class);
0N/A
0N/A /** The value of {@code arrayBaseOffset(char[].class)} */
0N/A public static final int ARRAY_CHAR_BASE_OFFSET
0N/A = theUnsafe.arrayBaseOffset(char[].class);
0N/A
0N/A /** The value of {@code arrayBaseOffset(int[].class)} */
0N/A public static final int ARRAY_INT_BASE_OFFSET
0N/A = theUnsafe.arrayBaseOffset(int[].class);
0N/A
0N/A /** The value of {@code arrayBaseOffset(long[].class)} */
0N/A public static final int ARRAY_LONG_BASE_OFFSET
0N/A = theUnsafe.arrayBaseOffset(long[].class);
0N/A
0N/A /** The value of {@code arrayBaseOffset(float[].class)} */
0N/A public static final int ARRAY_FLOAT_BASE_OFFSET
0N/A = theUnsafe.arrayBaseOffset(float[].class);
0N/A
0N/A /** The value of {@code arrayBaseOffset(double[].class)} */
0N/A public static final int ARRAY_DOUBLE_BASE_OFFSET
0N/A = theUnsafe.arrayBaseOffset(double[].class);
0N/A
0N/A /** The value of {@code arrayBaseOffset(Object[].class)} */
0N/A public static final int ARRAY_OBJECT_BASE_OFFSET
0N/A = theUnsafe.arrayBaseOffset(Object[].class);
0N/A
0N/A /**
0N/A * Report the scale factor for addressing elements in the storage
0N/A * allocation of a given array class. However, arrays of "narrow" types
0N/A * will generally not work properly with accessors like {@link
0N/A * #getByte(Object, int)}, so the scale factor for such classes is reported
0N/A * as zero.
0N/A *
0N/A * @see #arrayBaseOffset
0N/A * @see #getInt(Object, long)
0N/A * @see #putInt(Object, long, int)
0N/A */
0N/A public native int arrayIndexScale(Class arrayClass);
0N/A
0N/A /** The value of {@code arrayIndexScale(boolean[].class)} */
0N/A public static final int ARRAY_BOOLEAN_INDEX_SCALE
0N/A = theUnsafe.arrayIndexScale(boolean[].class);
0N/A
0N/A /** The value of {@code arrayIndexScale(byte[].class)} */
0N/A public static final int ARRAY_BYTE_INDEX_SCALE
0N/A = theUnsafe.arrayIndexScale(byte[].class);
0N/A
0N/A /** The value of {@code arrayIndexScale(short[].class)} */
0N/A public static final int ARRAY_SHORT_INDEX_SCALE
0N/A = theUnsafe.arrayIndexScale(short[].class);
0N/A
0N/A /** The value of {@code arrayIndexScale(char[].class)} */
0N/A public static final int ARRAY_CHAR_INDEX_SCALE
0N/A = theUnsafe.arrayIndexScale(char[].class);
0N/A
0N/A /** The value of {@code arrayIndexScale(int[].class)} */
0N/A public static final int ARRAY_INT_INDEX_SCALE
0N/A = theUnsafe.arrayIndexScale(int[].class);
0N/A
0N/A /** The value of {@code arrayIndexScale(long[].class)} */
0N/A public static final int ARRAY_LONG_INDEX_SCALE
0N/A = theUnsafe.arrayIndexScale(long[].class);
0N/A
0N/A /** The value of {@code arrayIndexScale(float[].class)} */
0N/A public static final int ARRAY_FLOAT_INDEX_SCALE
0N/A = theUnsafe.arrayIndexScale(float[].class);
0N/A
0N/A /** The value of {@code arrayIndexScale(double[].class)} */
0N/A public static final int ARRAY_DOUBLE_INDEX_SCALE
0N/A = theUnsafe.arrayIndexScale(double[].class);
0N/A
0N/A /** The value of {@code arrayIndexScale(Object[].class)} */
0N/A public static final int ARRAY_OBJECT_INDEX_SCALE
0N/A = theUnsafe.arrayIndexScale(Object[].class);
0N/A
0N/A /**
0N/A * Report the size in bytes of a native pointer, as stored via {@link
0N/A * #putAddress}. This value will be either 4 or 8. Note that the sizes of
0N/A * other primitive types (as stored in native memory blocks) is determined
0N/A * fully by their information content.
0N/A */
0N/A public native int addressSize();
0N/A
0N/A /** The value of {@code addressSize()} */
0N/A public static final int ADDRESS_SIZE = theUnsafe.addressSize();
0N/A
0N/A /**
0N/A * Report the size in bytes of a native memory page (whatever that is).
0N/A * This value will always be a power of two.
0N/A */
0N/A public native int pageSize();
0N/A
0N/A
0N/A /// random trusted operations from JNI:
0N/A
0N/A /**
0N/A * Tell the VM to define a class, without security checks. By default, the
0N/A * class loader and protection domain come from the caller's class.
0N/A */
0N/A public native Class defineClass(String name, byte[] b, int off, int len,
0N/A ClassLoader loader,
0N/A ProtectionDomain protectionDomain);
0N/A
6338N/A /**
6338N/A * @deprecated Use defineClass(String, byte[], int, int, ClassLoader, ProtectionDomain)
6338N/A * instead. This method will be removed in JDK 8.
6338N/A */
6338N/A @Deprecated
6338N/A @CallerSensitive
0N/A public native Class defineClass(String name, byte[] b, int off, int len);
0N/A
1193N/A /**
1193N/A * Define a class but do not make it known to the class loader or system dictionary.
1193N/A * <p>
1193N/A * For each CP entry, the corresponding CP patch must either be null or have
1193N/A * the a format that matches its tag:
1193N/A * <ul>
1193N/A * <li>Integer, Long, Float, Double: the corresponding wrapper object type from java.lang
1193N/A * <li>Utf8: a string (must have suitable syntax if used as signature or name)
1193N/A * <li>Class: any java.lang.Class object
1193N/A * <li>String: any object (not just a java.lang.String)
1193N/A * <li>InterfaceMethodRef: (NYI) a method handle to invoke on that call site's arguments
1193N/A * </ul>
1193N/A * @params hostClass context for linkage, access control, protection domain, and class loader
1193N/A * @params data bytes of a class file
1193N/A * @params cpPatches where non-null entries exist, they replace corresponding CP entries in data
1193N/A */
1193N/A public native Class defineAnonymousClass(Class hostClass, byte[] data, Object[] cpPatches);
1193N/A
1193N/A
0N/A /** Allocate an instance but do not run any constructor.
0N/A Initializes the class if it has not yet been. */
0N/A public native Object allocateInstance(Class cls)
0N/A throws InstantiationException;
0N/A
0N/A /** Lock the object. It must get unlocked via {@link #monitorExit}. */
0N/A public native void monitorEnter(Object o);
0N/A
0N/A /**
0N/A * Unlock the object. It must have been locked via {@link
0N/A * #monitorEnter}.
0N/A */
0N/A public native void monitorExit(Object o);
0N/A
0N/A /**
0N/A * Tries to lock the object. Returns true or false to indicate
0N/A * whether the lock succeeded. If it did, the object must be
0N/A * unlocked via {@link #monitorExit}.
0N/A */
0N/A public native boolean tryMonitorEnter(Object o);
0N/A
0N/A /** Throw the exception without telling the verifier. */
0N/A public native void throwException(Throwable ee);
0N/A
0N/A
0N/A /**
0N/A * Atomically update Java variable to <tt>x</tt> if it is currently
0N/A * holding <tt>expected</tt>.
0N/A * @return <tt>true</tt> if successful
0N/A */
0N/A public final native boolean compareAndSwapObject(Object o, long offset,
0N/A Object expected,
0N/A Object x);
0N/A
0N/A /**
0N/A * Atomically update Java variable to <tt>x</tt> if it is currently
0N/A * holding <tt>expected</tt>.
0N/A * @return <tt>true</tt> if successful
0N/A */
0N/A public final native boolean compareAndSwapInt(Object o, long offset,
0N/A int expected,
0N/A int x);
0N/A
0N/A /**
0N/A * Atomically update Java variable to <tt>x</tt> if it is currently
0N/A * holding <tt>expected</tt>.
0N/A * @return <tt>true</tt> if successful
0N/A */
0N/A public final native boolean compareAndSwapLong(Object o, long offset,
0N/A long expected,
0N/A long x);
0N/A
0N/A /**
0N/A * Fetches a reference value from a given Java variable, with volatile
0N/A * load semantics. Otherwise identical to {@link #getObject(Object, long)}
0N/A */
0N/A public native Object getObjectVolatile(Object o, long offset);
0N/A
0N/A /**
0N/A * Stores a reference value into a given Java variable, with
0N/A * volatile store semantics. Otherwise identical to {@link #putObject(Object, long, Object)}
0N/A */
0N/A public native void putObjectVolatile(Object o, long offset, Object x);
0N/A
0N/A /** Volatile version of {@link #getInt(Object, long)} */
0N/A public native int getIntVolatile(Object o, long offset);
0N/A
0N/A /** Volatile version of {@link #putInt(Object, long, int)} */
0N/A public native void putIntVolatile(Object o, long offset, int x);
0N/A
0N/A /** Volatile version of {@link #getBoolean(Object, long)} */
0N/A public native boolean getBooleanVolatile(Object o, long offset);
0N/A
0N/A /** Volatile version of {@link #putBoolean(Object, long, boolean)} */
0N/A public native void putBooleanVolatile(Object o, long offset, boolean x);
0N/A
0N/A /** Volatile version of {@link #getByte(Object, long)} */
0N/A public native byte getByteVolatile(Object o, long offset);
0N/A
0N/A /** Volatile version of {@link #putByte(Object, long, byte)} */
0N/A public native void putByteVolatile(Object o, long offset, byte x);
0N/A
0N/A /** Volatile version of {@link #getShort(Object, long)} */
0N/A public native short getShortVolatile(Object o, long offset);
0N/A
0N/A /** Volatile version of {@link #putShort(Object, long, short)} */
0N/A public native void putShortVolatile(Object o, long offset, short x);
0N/A
0N/A /** Volatile version of {@link #getChar(Object, long)} */
0N/A public native char getCharVolatile(Object o, long offset);
0N/A
0N/A /** Volatile version of {@link #putChar(Object, long, char)} */
0N/A public native void putCharVolatile(Object o, long offset, char x);
0N/A
0N/A /** Volatile version of {@link #getLong(Object, long)} */
0N/A public native long getLongVolatile(Object o, long offset);
0N/A
0N/A /** Volatile version of {@link #putLong(Object, long, long)} */
0N/A public native void putLongVolatile(Object o, long offset, long x);
0N/A
0N/A /** Volatile version of {@link #getFloat(Object, long)} */
0N/A public native float getFloatVolatile(Object o, long offset);
0N/A
0N/A /** Volatile version of {@link #putFloat(Object, long, float)} */
0N/A public native void putFloatVolatile(Object o, long offset, float x);
0N/A
0N/A /** Volatile version of {@link #getDouble(Object, long)} */
0N/A public native double getDoubleVolatile(Object o, long offset);
0N/A
0N/A /** Volatile version of {@link #putDouble(Object, long, double)} */
0N/A public native void putDoubleVolatile(Object o, long offset, double x);
0N/A
0N/A /**
0N/A * Version of {@link #putObjectVolatile(Object, long, Object)}
0N/A * that does not guarantee immediate visibility of the store to
0N/A * other threads. This method is generally only useful if the
0N/A * underlying field is a Java volatile (or if an array cell, one
0N/A * that is otherwise only accessed using volatile accesses).
0N/A */
0N/A public native void putOrderedObject(Object o, long offset, Object x);
0N/A
0N/A /** Ordered/Lazy version of {@link #putIntVolatile(Object, long, int)} */
0N/A public native void putOrderedInt(Object o, long offset, int x);
0N/A
0N/A /** Ordered/Lazy version of {@link #putLongVolatile(Object, long, long)} */
0N/A public native void putOrderedLong(Object o, long offset, long x);
0N/A
0N/A /**
0N/A * Unblock the given thread blocked on <tt>park</tt>, or, if it is
0N/A * not blocked, cause the subsequent call to <tt>park</tt> not to
0N/A * block. Note: this operation is "unsafe" solely because the
0N/A * caller must somehow ensure that the thread has not been
0N/A * destroyed. Nothing special is usually required to ensure this
0N/A * when called from Java (in which there will ordinarily be a live
0N/A * reference to the thread) but this is not nearly-automatically
0N/A * so when calling from native code.
0N/A * @param thread the thread to unpark.
0N/A *
0N/A */
0N/A public native void unpark(Object thread);
0N/A
0N/A /**
0N/A * Block current thread, returning when a balancing
0N/A * <tt>unpark</tt> occurs, or a balancing <tt>unpark</tt> has
0N/A * already occurred, or the thread is interrupted, or, if not
0N/A * absolute and time is not zero, the given time nanoseconds have
0N/A * elapsed, or if absolute, the given deadline in milliseconds
0N/A * since Epoch has passed, or spuriously (i.e., returning for no
0N/A * "reason"). Note: This operation is in the Unsafe class only
0N/A * because <tt>unpark</tt> is, so it would be strange to place it
0N/A * elsewhere.
0N/A */
0N/A public native void park(boolean isAbsolute, long time);
0N/A
0N/A /**
0N/A * Gets the load average in the system run queue assigned
0N/A * to the available processors averaged over various periods of time.
0N/A * This method retrieves the given <tt>nelem</tt> samples and
0N/A * assigns to the elements of the given <tt>loadavg</tt> array.
0N/A * The system imposes a maximum of 3 samples, representing
0N/A * averages over the last 1, 5, and 15 minutes, respectively.
0N/A *
0N/A * @params loadavg an array of double of size nelems
0N/A * @params nelems the number of samples to be retrieved and
0N/A * must be 1 to 3.
0N/A *
0N/A * @return the number of samples actually retrieved; or -1
0N/A * if the load average is unobtainable.
0N/A */
0N/A public native int getLoadAverage(double[] loadavg, int nelems);
0N/A}