public class UnsafeHelper extends Object
Constructor and Description |
---|
UnsafeHelper() |
Modifier and Type | Method and Description |
---|---|
static void |
copyMemory(long srcAddress,
Object dest) |
static void |
copyMemory(Object src,
long srcOffset,
Object dest,
long destOffset,
long len)
Copies the memory from srcAddress into dest
|
static void |
copyMemoryFieldByField(long srcAddress,
Object dest)
Copies from srcAddress to dest one field at a time.
|
static long |
firstFieldOffset(Class clazz)
Returns the offset of the first field in the range [headerSize, sizeOf].
|
static Object |
fromAddress(long address)
Returns the object located at the address.
|
static sun.misc.Unsafe |
getUnsafe() |
static long |
headerSize(Class clazz)
Returns the size of the header for an instance of this class (in bytes).
|
static long |
headerSize(Object obj) |
static void |
hexDump(PrintStream out,
Object obj)
Prints out the object (including header, padding, and all fields) as hex.
|
static void |
hexDumpAddress(PrintStream out,
long address,
long length) |
static void |
hexDumpBytes(PrintStream out,
long offset,
byte[] bytes) |
static long |
jvm7_32_sizeOf(Object object) |
static long |
sizeOf(Class clazz)
Returns the size of an instance of this class (in bytes).
|
static long |
sizeOf(Object obj) |
static long |
toAddress(Object obj)
Returns the address the object is located at
|
static byte[] |
toByteArray(Object obj)
Returns the object as a byte array, including header, padding and all fields.
|
public static sun.misc.Unsafe getUnsafe()
public static long toAddress(Object obj)
WARNING: This does not return a pointer, so be warned pointer arithmetic will not work.
obj
- The objectpublic static Object fromAddress(long address)
address
- The addresspublic static void copyMemory(long srcAddress, Object dest)
public static void copyMemory(Object src, long srcOffset, Object dest, long destOffset, long len)
This is our own implementation because Unsafe.copyMemory(Object src, .. Object dest, ...) only works if dest in an array, so we wrote our only implementations.
public static void copyMemoryFieldByField(long srcAddress, Object dest)
srcAddress
- dest
- public static long jvm7_32_sizeOf(Object object)
public static long headerSize(Object obj)
public static long sizeOf(Object obj)
public static long headerSize(Class clazz)
More information http://www.codeinstructions.com/2008/12/java-objects-memory-structure.html and http://stackoverflow.com/a/17348396/88646
,------------------+------------------+------------------ +---------------. | mark word(8) | klass pointer(4) | array size (opt) | padding | `------------------+------------------+-------------------+---------------'
clazz
- public static long firstFieldOffset(Class clazz)
clazz
- public static long sizeOf(Class clazz)
clazz
- public static byte[] toByteArray(Object obj)
obj
- public static void hexDump(PrintStream out, Object obj)
Some examples:
/** * Longs are always 8 byte aligned, so 4 bytes of padding * 0x00000000: 01 00 00 00 00 00 00 00 9B 81 61 DF 00 00 00 00 * 0x00000010: EF CD AB 89 67 45 23 01 */ static class Class8 { long l = 0x0123456789ABCDEFL; } /** * 0x00000000: 01 00 00 00 00 00 00 00 8A BF 62 DF 67 45 23 01 */ static class Class4 { int i = 0x01234567; } /** * 0x00000000: 01 00 00 00 00 00 00 00 28 C0 62 DF 34 12 00 00 */ static class Class2 { short s = 0x01234; } /** * 0x00000000: 01 00 00 00 00 00 00 00 E3 C0 62 DF 12 00 00 00 */ static class Class1 { byte b = 0x12; } /** * 0x00000000: 01 00 00 00 00 00 00 00 96 C1 62 DF 12 00 00 00 * 0x00000010: EF CD AB 89 67 45 23 01 */ static class ClassMixed18 { byte b = 0x12; long l = 0x0123456789ABCDEFL; } /** * 0x00000000: 01 00 00 00 00 00 00 00 4C C2 62 DF 12 00 00 00 * 0x00000010: EF CD AB 89 67 45 23 01 */ static class ClassMixed81 { long l = 0x0123456789ABCDEFL; byte b = 0x12; } public static void printMemoryLayout() { UnsafeHelper.hexDump(System.out, new Class8()); UnsafeHelper.hexDump(System.out, new Class4()); UnsafeHelper.hexDump(System.out, new Class2()); UnsafeHelper.hexDump(System.out, new Class1()); UnsafeHelper.hexDump(System.out, new ClassMixed18()); UnsafeHelper.hexDump(System.out, new ClassMixed81()); }
out
- PrintStream to print the hex toobj
- The object to printpublic static void hexDumpBytes(PrintStream out, long offset, byte[] bytes)
public static void hexDumpAddress(PrintStream out, long address, long length)
Copyright © 2015. All rights reserved.