public final class UnsafeWrapper extends Object
Modifier and Type | Method and Description |
---|---|
static <T> T |
allocateInstance(Class<T> klass)
Allocates an instance but does not run any constructor.
|
static long |
allocateMemory(long bytes)
Allocates a new block of native memory, of the given size in bytes.
|
static int |
arrayBaseOffset(Class<?> arrayClass)
Reports the offset of the first element in the storage allocation of a
given array class.
|
static int |
arrayIndexScale(Class<?> arrayClass)
Reports the scale factor for addressing elements in the storage
allocation of a given array class.
|
static boolean |
compareAndSwapInt(Object o,
long offset,
int expected,
int x)
Atomically updates Java variable to
x if it is currently
holding expected . |
static boolean |
compareAndSwapLong(Object o,
long offset,
long expected,
long x)
Atomically updates Java variable to
x if it is currently
holding expected . |
static boolean |
compareAndSwapObject(Object o,
long offset,
Object expected,
Object x)
Atomically updates Java variable to
x if it is currently
holding expected . |
static void |
freeMemory(long address)
Disposes of a block of native memory, as obtained from
allocateMemory(long) or reallocateMemory(long, long) . |
static boolean |
getBoolean(Object o,
long offset) |
static byte |
getByte(long address)
Fetches a value from a given memory address.
|
static byte |
getByte(Object o,
long offset) |
static char |
getChar(long address) |
static char |
getChar(Object o,
long offset) |
static double |
getDouble(long address) |
static double |
getDouble(Object o,
long offset) |
static float |
getFloat(long address) |
static float |
getFloat(Object o,
long offset) |
static UnsafeWrapper |
getInstance() |
static int |
getInt(long address) |
static int |
getInt(Object o,
long offset)
Fetches a value from a given Java variable.
|
static long |
getLong(long address) |
static long |
getLong(Object o,
long offset) |
static Object |
getObject(Object o,
long offset)
Fetches a reference value from a given Java variable.
|
static short |
getShort(long address) |
static short |
getShort(Object o,
long offset) |
static long |
objectFieldOffset(Field field)
Reports the location of a given field in the storage allocation of its
class.
|
void |
park(boolean isAbsolute,
long time)
Blocks current thread, returning when a balancing
unpark occurs, or a balancing unpark has
already occurred, or the thread is interrupted, or, if not
absolute and time is not zero, the given time nanoseconds have
elapsed, or if absolute, the given deadline in milliseconds
since Epoch has passed, or spuriously (i.e., returning for no
"reason"). |
static void |
putBoolean(Object o,
long offset,
boolean x) |
static void |
putByte(long address,
byte x)
Stores a value into a given memory address.
|
static void |
putByte(Object o,
long offset,
byte x) |
static void |
putByteArray(long startAddress,
byte[] bytes)
把一个byte[]放到指定的内存块中
|
static void |
putChar(long address,
char x) |
static void |
putChar(Object o,
long offset,
char x) |
static void |
putDouble(long address,
double x) |
static void |
putDouble(Object o,
long offset,
double x) |
static void |
putFloat(long address,
float x) |
static void |
putFloat(Object o,
long offset,
float x) |
static void |
putInt(long address,
int x) |
static void |
putInt(Object o,
long offset,
int x)
Stores a value into a given Java variable.
|
static void |
putLong(long address,
long x) |
static void |
putLong(Object o,
long offset,
long x) |
static void |
putObject(Object o,
long offset,
Object x)
Stores a reference value into a given Java variable.
|
static void |
putShort(long address,
short x) |
static void |
putShort(Object o,
long offset,
short x) |
static byte[] |
readByteArray(long startAddress,
int len)
从内存中读取一个数组
|
static long |
reallocateMemory(long address,
long bytes)
Resizes a new block of native memory, to the given size in bytes.
|
static Object |
staticFieldBase(Field field)
Reports the location of a given static field, in conjunction with
staticFieldOffset(java.lang.reflect.Field) . |
static long |
staticFieldOffset(Field field)
Reports the location of a given static field, in conjunction with
staticFieldBase(java.lang.reflect.Field) . |
void |
unpark(Object thread)
Unblocks the given thread blocked on
park , or, if it is
not blocked, causes the subsequent call to park not to
block. |
public static UnsafeWrapper getInstance()
public static long allocateMemory(long bytes)
freeMemory(long)
, or resize it with reallocateMemory(long, long)
.
Note: It is the resposibility of the caller to make
sure arguments are checked before the methods are called. While
some rudimentary checks are performed on the input, the checks
are best effort and when performance is an overriding priority,
as when methods of this class are optimized by the runtime
compiler, some or all checks (if any) may be elided. Hence, the
caller must not rely on the checks and corresponding
exceptions!RuntimeException
- if the size is negative or too large
for the native size_t typeOutOfMemoryError
- if the allocation is refused by the systemgetByte(long)
,
putByte(long, byte)
public static long reallocateMemory(long address, long bytes)
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.
Note: It is the resposibility of the caller to make
sure arguments are checked before the methods are called. While
some rudimentary checks are performed on the input, the checks
are best effort and when performance is an overriding priority,
as when methods of this class are optimized by the runtime
compiler, some or all checks (if any) may be elided. Hence, the
caller must not rely on the checks and corresponding
exceptions!RuntimeException
- if the size is negative or too large
for the native size_t typeOutOfMemoryError
- if the allocation is refused by the systemallocateMemory(long)
public static void freeMemory(long address)
allocateMemory(long)
or reallocateMemory(long, long)
. The address passed to
this method may be null, in which case no action is taken.
Note: It is the resposibility of the caller to make
sure arguments are checked before the methods are called. While
some rudimentary checks are performed on the input, the checks
are best effort and when performance is an overriding priority,
as when methods of this class are optimized by the runtime
compiler, some or all checks (if any) may be elided. Hence, the
caller must not rely on the checks and corresponding
exceptions!RuntimeException
- if any of the arguments is invalidallocateMemory(long)
public static byte getByte(long address)
allocateMemory(long)
, the
results are undefined.allocateMemory(long)
public static void putByte(long address, byte x)
allocateMemory(long)
, the
results are undefined.getByte(long)
public static byte[] readByteArray(long startAddress, int len)
startAddress
- 内存起始地址len
- 要读取的数组的长度public static void putByteArray(long startAddress, byte[] bytes)
startAddress
- 内存块的起始位置bytes
- 要被存放的内存public static short getShort(long address)
getByte(long)
public static void putShort(long address, short x)
putByte(long, byte)
public static char getChar(long address)
getByte(long)
public static void putChar(long address, char x)
putByte(long, byte)
public static int getInt(long address)
getByte(long)
public static void putInt(long address, int x)
putByte(long, byte)
public static long getLong(long address)
getByte(long)
public static void putLong(long address, long x)
putByte(long, byte)
public static float getFloat(long address)
getByte(long)
public static void putFloat(long address, float x)
putByte(long, byte)
public static double getDouble(long address)
getByte(long)
public static void putDouble(long address, double x)
putByte(long, byte)
public static <T> T allocateInstance(Class<T> klass)
public static byte getByte(Object o, long offset)
getInt(Object, long)
public static void putByte(Object o, long offset, byte x)
putInt(Object, long, int)
public static int getInt(Object o, long offset)
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 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.reflect.Field)
(respectively) from the
reflective 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 N
th 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 getInt(long)
, 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.
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
staticallyRuntimeException
- No defined exceptions are thrown, not even
NullPointerException
public static void putInt(Object o, long offset, int x)
The first two parameters are interpreted exactly as with
getInt(Object, long)
to refer to a specific
Java variable (field or array element). The given value
is stored into that variable.
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 variableRuntimeException
- No defined exceptions are thrown, not even
NullPointerException
public static Object getObject(Object o, long offset)
getInt(Object, long)
public static void putObject(Object o, long offset, Object x)
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, card marks or
other store barriers for that object (if the VM requires them)
are updated.
putInt(Object, long, int)
public static boolean getBoolean(Object o, long offset)
getInt(Object, long)
public static void putBoolean(Object o, long offset, boolean x)
putInt(Object, long, int)
public static short getShort(Object o, long offset)
getInt(Object, long)
public static void putShort(Object o, long offset, short x)
putInt(Object, long, int)
public static char getChar(Object o, long offset)
getInt(Object, long)
public static void putChar(Object o, long offset, char x)
putInt(Object, long, int)
public static long getLong(Object o, long offset)
getInt(Object, long)
public static void putLong(Object o, long offset, long x)
putInt(Object, long, int)
public static float getFloat(Object o, long offset)
getInt(Object, long)
public static void putFloat(Object o, long offset, float x)
putInt(Object, long, int)
public static double getDouble(Object o, long offset)
getInt(Object, long)
public static void putDouble(Object o, long offset, double x)
putInt(Object, long, int)
public static long objectFieldOffset(Field field)
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 getInt(Object, long)
.
Therefore, code which will be ported to such JVMs on 64-bit platforms
must preserve all bits of static field offsets.
getInt(Object, long)
public static long staticFieldOffset(Field field)
staticFieldBase(java.lang.reflect.Field)
.
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(Object, long)
public static Object staticFieldBase(Field field)
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 getInt(Object,
long)
. 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.
public static int arrayBaseOffset(Class<?> arrayClass)
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.getInt(Object, long)
,
putInt(Object, long, int)
public static int arrayIndexScale(Class<?> arrayClass)
getByte(Object, long)
, so the scale factor for such classes is reported
as zero.public static boolean compareAndSwapObject(Object o, long offset, Object expected, Object x)
x
if it is currently
holding expected
.
This operation has memory semantics of a volatile
read
and write. Corresponds to C11 atomic_compare_exchange_strong.
true
if successfulpublic static boolean compareAndSwapInt(Object o, long offset, int expected, int x)
x
if it is currently
holding expected
.
This operation has memory semantics of a volatile
read
and write. Corresponds to C11 atomic_compare_exchange_strong.
true
if successfulpublic static boolean compareAndSwapLong(Object o, long offset, long expected, long x)
x
if it is currently
holding expected
.
This operation has memory semantics of a volatile
read
and write. Corresponds to C11 atomic_compare_exchange_strong.
true
if successfulpublic void unpark(Object thread)
park
, or, if it is
not blocked, causes the subsequent call to park
not to
block. Note: this operation is "unsafe" solely because the
caller must somehow ensure that the thread has not been
destroyed. Nothing special is usually required to ensure this
when called from Java (in which there will ordinarily be a live
reference to the thread) but this is not nearly-automatically
so when calling from native code.thread
- the thread to unpark.public void park(boolean isAbsolute, long time)
unpark
occurs, or a balancing unpark
has
already occurred, or the thread is interrupted, or, if not
absolute and time is not zero, the given time nanoseconds have
elapsed, or if absolute, the given deadline in milliseconds
since Epoch has passed, or spuriously (i.e., returning for no
"reason"). Note: This operation is in the Unsafe class only
because unpark
is, so it would be strange to place it
elsewhere.Copyright © 2020. All rights reserved.