getUnsafe() The returned Unsafe object should be carefully guarded
by the caller, since it can be used to read and write data at arbitrary
memory addresses. It must never be passed to untrusted code.
Most methods in this class are very low-level, and correspond to a small number of hardware instructions (on typical machines). Compilers are encouraged to optimize these methods accordingly.
Here is a suggested idiom for using unsafe operations:
class MyTrustedClass {
private static final Unsafe unsafe = Unsafe.getUnsafe();
...
private long myCountAddress = ...;
public int getCount() { return unsafe.getByte(myCountAddress); }
}
(It may assist compilers to make the local variable be
final.)
java.lang.SecurityException if a security manager exists and its
checkPropertiesAccess method doesn't allow
access to the system properties.o at the given offset, or (if o is
null) from the memory address whose numerical value is the given
offset.
The results are undefined unless one of the following cases is true:
objectFieldOffset(java.lang.reflect.Field) on
the java.lang.reflect.Field of some Java field and the object
referred to by o is of a class compatible with that
field's class.
o (either null or
non-null) were both obtained via staticFieldOffset(java.lang.reflect.Field)
and staticFieldBase(java.lang.Class) (respectively) from the
reflective java.lang.reflect.Field representation of some Java field.
o is an array, and the offset
is an integer of the form B+N*S, where N is
a valid index into the array, and B and S are
the values obtained by arrayBaseOffset(java.lang.Class) and arrayIndexScale(java.lang.Class) (respectively) from the array's class. The value
referred to is the Nth element of the array.
If one of the above cases is true, the call references a specific Java variable (field or array element). However, the results are undefined if that variable is not in fact of the type returned by this method.
This method refers to a variable by means of two parameters, and so
it provides (in effect) a double-register addressing mode
for Java variables. When the object reference is null, this method
uses its offset as an absolute address. This is similar in operation
to methods such as , which provide (in effect) a
single-register addressing mode for non-Java variables.
However, because Java variables may have a different layout in memory
from non-Java variables, programmers should not assume that these
two addressing modes are ever equivalent. Also, programmers should
remember that offsets from the double-register addressing mode cannot
be portably confused with longs used in the single-register addressing
mode.
getInt(long)
o Java heap object in which the variable resides, if any, else
nulloffset indication of where the variable resides in a Java heap
object, if any, else a memory address locating the variable
staticallyjava.lang.RuntimeException No defined exceptions are thrown, not even
java.lang.NullPointerException
The first two parameters are interpreted exactly as with
to refer to a specific
Java variable (field or array element). The given value
is stored into that variable.
getInt(java.lang.Object,long)
The variable must be of the same type as the method
parameter x.
o Java heap object in which the variable resides, if any, else
nulloffset indication of where the variable resides in a Java heap
object, if any, else a memory address locating the variable
staticallyx the value to store into the indicated Java variablejava.lang.RuntimeException No defined exceptions are thrown, not even
java.lang.NullPointerException
Unless the reference x being stored is either null
or matches the field type, the results are undefined.
If the reference o is non-null, car marks or
other store barriers for that object (if the VM requires them)
are updated.
putInt(java.lang.Object,int,int)getInt(java.lang.Object,long)putInt(java.lang.Object,int,int)getInt(java.lang.Object,long)putInt(java.lang.Object,int,int)getInt(java.lang.Object,long)putInt(java.lang.Object,int,int)getInt(java.lang.Object,long)putInt(java.lang.Object,int,int)getInt(java.lang.Object,long)putInt(java.lang.Object,int,int)getInt(java.lang.Object,long)putInt(java.lang.Object,int,int)getInt(java.lang.Object,long)putInt(java.lang.Object,int,int)staticFieldOffset(java.lang.reflect.Field).staticFieldOffset(java.lang.reflect.Field).staticFieldOffset(java.lang.reflect.Field).staticFieldOffset(java.lang.reflect.Field).staticFieldOffset(java.lang.reflect.Field).staticFieldOffset(java.lang.reflect.Field).staticFieldOffset(java.lang.reflect.Field).staticFieldOffset(java.lang.reflect.Field).staticFieldOffset(java.lang.reflect.Field).staticFieldOffset(java.lang.reflect.Field).staticFieldOffset(java.lang.reflect.Field).staticFieldOffset(java.lang.reflect.Field).staticFieldOffset(java.lang.reflect.Field).staticFieldOffset(java.lang.reflect.Field).staticFieldOffset(java.lang.reflect.Field).staticFieldOffset(java.lang.reflect.Field).staticFieldOffset(java.lang.reflect.Field).staticFieldOffset(java.lang.reflect.Field).allocateMemory(long), the
results are undefined.
allocateMemory(long)allocateMemory(long), the
results are undefined.
getByte(long)getByte(long)putByte(long,byte)getByte(long)putByte(long,byte)getByte(long)putByte(long,byte)getByte(long)putByte(long,byte)getByte(long)putByte(long,byte)getByte(long)putByte(long,byte)allocateMemory(long), the results are undefined.
If the native pointer is less than 64 bits wide, it is extended as
an unsigned number to a Java long. The pointer may be indexed by any
given byte offset, simply by adding that offset (as a simple integer) to
the long representing the pointer. The number of bytes actually read
from the target address maybe determined by consulting .
addressSize()
allocateMemory(long)allocateMemory(long), the results are undefined.
The number of bytes actually written at the target address maybe
determined by consulting .
addressSize()
getAddress(long)freeMemory(long), or resize it with reallocateMemory(long,long).
java.lang.IllegalArgumentException if the size is negative or too large
for the native size_t typejava.lang.OutOfMemoryError if the allocation is refused by the systemgetByte(long)putByte(long,byte)freeMemory(long), or resize it with reallocateMemory(long,long). The address passed to this method may be null, in
which case an allocation will be performed.
java.lang.IllegalArgumentException if the size is negative or too large
for the native size_t typejava.lang.OutOfMemoryError if the allocation is refused by the systemallocateMemory(long)allocateMemory(long) or reallocateMemory(long,long). The address passed to
this method may be null, in which case no action is taken.
allocateMemory(long)staticFieldOffset(java.lang.reflect.Field), objectFieldOffset(java.lang.reflect.Field),
or arrayBaseOffset(java.lang.Class).
public int fieldOffset(Field f) {
if (Modifier.isStatic(f.getModifiers()))
return (int) staticFieldOffset(f);
else
return (int) objectFieldOffset(f);
}
staticFieldOffset(java.lang.reflect.Field) for static
fields and objectFieldOffset(java.lang.reflect.Field) for non-static fields.
public Object staticFieldBase(Class c) {
Field[] fields = c.getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
if (Modifier.isStatic(fields[i].getModifiers())) {
return staticFieldBase(fields[i]);
}
}
return null;
}
staticFieldBase(java.lang.reflect.Field)
to obtain the base pertaining to a specific java.lang.reflect.Field.
This method works only for JVMs which store all statics
for a given class in one place.Any given field will always have the same offset and base, and no two distinct fields of the same class will ever have the same offset and base.
As of 1.4.1, offsets for fields are represented as long values,
although the Sun JVM does not use the most significant 32 bits.
However, JVM implementations which store static fields at absolute
addresses can use long offsets and null base pointers to express
the field locations in a form usable by .
Therefore, code which will be ported to such JVMs on 64-bit platforms
must preserve all bits of static field offsets.
getInt(java.lang.Object,long)
getInt(java.lang.Object,long)staticFieldBase(java.lang.Class).
Do not expect to perform any sort of arithmetic on this offset; it is just a cookie which is passed to the unsafe heap memory accessors.
Any given field will always have the same offset, and no two distinct fields of the same class will ever have the same offset.
As of 1.4.1, offsets for fields are represented as long values, although the Sun JVM does not use the most significant 32 bits. It is hard to imagine a JVM technology which needs more than a few bits to encode an offset within a non-array object, However, for consistency with other methods in this class, this method reports its result as a long value.
getInt(java.lang.Object,long)staticFieldOffset(java.lang.reflect.Field).
Fetch the base "Object", if any, with which static fields of the
given class can be accessed via methods like . This value may be null. This value may refer to an object
which is a "cookie", not guaranteed to be a real Object, and it should
not be used in any way except as argument to the get and put routines in
this class.
getInt(java.lang.Object,long)
arrayIndexScale(java.lang.Class) returns a non-zero value
for the same class, you may use that scale factor, together with this
base offset, to form new offsets to access elements of arrays of the
given class.
getByte(java.lang.Object,int), so the scale factor for such classes is reported
as zero.
putAddress(long,long). This value will be either 4 or 8. Note that the sizes of
other primitive types (as stored in native memory blocks) is determined
fully by their information content.
monitorExit(java.lang.Object).monitorExit(java.lang.Object).
getObject(java.lang.Object,long)
putObject(java.lang.Object,long,java.lang.Object)
getInt(java.lang.Object,long) putInt(java.lang.Object,long,int) getBoolean(java.lang.Object,long) putBoolean(java.lang.Object,long,boolean) getByte(java.lang.Object,long) putByte(java.lang.Object,long,byte) getShort(java.lang.Object,long) putShort(java.lang.Object,long,short) getChar(java.lang.Object,long) putChar(java.lang.Object,long,char) getLong(java.lang.Object,long) putLong(java.lang.Object,long,long) getFloat(java.lang.Object,long) putFloat(java.lang.Object,long,float) getDouble(java.lang.Object,long) putDouble(java.lang.Object,long,double) putObjectVolatile(java.lang.Object,long,java.lang.Object)
that does not guarantee immediate visibility of the store to
other threads. This method is generally only useful if the
underlying field is a Java volatile (or if an array cell, one
that is otherwise only accessed using volatile accesses).
putIntVolatile(java.lang.Object,long,int) putLongVolatile(java.lang.Object,long,long)thread the thread to unpark.