Java Code Examples for jdk.internal.misc.Unsafe#arrayBaseOffset()

The following examples show how to use jdk.internal.misc.Unsafe#arrayBaseOffset() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GetPutLong.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String args[]) throws Exception {
    Unsafe unsafe = Unsafe.getUnsafe();
    Test t = new Test();
    Field field = Test.class.getField("l");

    long offset = unsafe.objectFieldOffset(field);
    assertEquals(-1L, unsafe.getLong(t, offset));
    unsafe.putLong(t, offset, 0L);
    assertEquals(0L, unsafe.getLong(t, offset));

    long address = unsafe.allocateMemory(8);
    unsafe.putLong(address, 1L);
    assertEquals(1L, unsafe.getLong(address));
    unsafe.freeMemory(address);

    long arrayLong[] = { -1, 0, 1, 2 };
    int scale = unsafe.arrayIndexScale(arrayLong.getClass());
    offset = unsafe.arrayBaseOffset(arrayLong.getClass());
    for (int i = 0; i < arrayLong.length; i++) {
        assertEquals(unsafe.getLong(arrayLong, offset), arrayLong[i]);
        offset += scale;
    }
}
 
Example 2
Source File: GetPutChar.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String args[]) throws Exception {
    Unsafe unsafe = Unsafe.getUnsafe();
    Test t = new Test();
    Field field = Test.class.getField("c");

    long offset = unsafe.objectFieldOffset(field);
    assertEquals('\u0000', unsafe.getChar(t, offset));
    unsafe.putChar(t, offset, '\u0001');
    assertEquals('\u0001', unsafe.getChar(t, offset));

    long address = unsafe.allocateMemory(8);
    unsafe.putChar(address, '\u0002');
    assertEquals('\u0002', unsafe.getChar(address));
    unsafe.freeMemory(address);

    char arrayChar[] = { '\uabcd', '\u00ff', '\uff00', };
    int scale = unsafe.arrayIndexScale(arrayChar.getClass());
    offset = unsafe.arrayBaseOffset(arrayChar.getClass());
    for (int i = 0; i < arrayChar.length; i++) {
        assertEquals(unsafe.getChar(arrayChar, offset), arrayChar[i]);
        offset += scale;
    }
}
 
Example 3
Source File: GetPutInt.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String args[]) throws Exception {
    Unsafe unsafe = Unsafe.getUnsafe();
    Test t = new Test();
    Field field = Test.class.getField("i");

    long offset = unsafe.objectFieldOffset(field);
    assertEquals(-1, unsafe.getInt(t, offset));
    unsafe.putInt(t, offset, 0);
    assertEquals(0, unsafe.getInt(t, offset));

    long address = unsafe.allocateMemory(8);
    unsafe.putInt(address, 1);
    assertEquals(1, unsafe.getInt(address));
    unsafe.freeMemory(address);

    int arrayInt[] = { -1, 0, 1, 2 };
    int scale = unsafe.arrayIndexScale(arrayInt.getClass());
    offset = unsafe.arrayBaseOffset(arrayInt.getClass());
    for (int i = 0; i < arrayInt.length; i++) {
        assertEquals(unsafe.getInt(arrayInt, offset), arrayInt[i]);
        offset += scale;
    }
}
 
Example 4
Source File: GetPutObject.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String args[]) throws Exception {
    Unsafe unsafe = Unsafe.getUnsafe();
    Test t = new Test();
    Object o = new Object();
    Field field = Test.class.getField("o");

    long offset = unsafe.objectFieldOffset(field);
    assertEquals(t.o, unsafe.getObject(t, offset));

    unsafe.putObject(t, offset, o);
    assertEquals(o, unsafe.getObject(t, offset));

    Object arrayObject[] = { unsafe, null, new Object() };
    int scale = unsafe.arrayIndexScale(arrayObject.getClass());
    offset = unsafe.arrayBaseOffset(arrayObject.getClass());
    for (int i = 0; i < arrayObject.length; i++) {
        assertEquals(unsafe.getObject(arrayObject, offset), arrayObject[i]);
        offset += scale;
    }
}
 
Example 5
Source File: GetPutDouble.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String args[]) throws Exception {
    Unsafe unsafe = Unsafe.getUnsafe();
    Test t = new Test();
    Field field = Test.class.getField("d");

    long offset = unsafe.objectFieldOffset(field);
    assertEquals(-1.0, unsafe.getDouble(t, offset));
    unsafe.putDouble(t, offset, 0.0);
    assertEquals(0.0, unsafe.getDouble(t, offset));

    long address = unsafe.allocateMemory(8);
    unsafe.putDouble(address, 1.0);
    assertEquals(1.0, unsafe.getDouble(address));
    unsafe.freeMemory(address);

    double arrayDouble[] = { -1.0, 0.0, 1.0, 2.0 };
    int scale = unsafe.arrayIndexScale(arrayDouble.getClass());
    offset = unsafe.arrayBaseOffset(arrayDouble.getClass());
    for (int i = 0; i < arrayDouble.length; i++) {
        assertEquals(unsafe.getDouble(arrayDouble, offset), arrayDouble[i]);
        offset += scale;
    }
}
 
Example 6
Source File: GetPutFloat.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String args[]) throws Exception {
    Unsafe unsafe = Unsafe.getUnsafe();
    Test t = new Test();
    Field field = Test.class.getField("f");

    long offset = unsafe.objectFieldOffset(field);
    assertEquals(-1.0f, unsafe.getFloat(t, offset));
    unsafe.putFloat(t, offset, 0.0f);
    assertEquals(0.0f, unsafe.getFloat(t, offset));

    long address = unsafe.allocateMemory(8);
    unsafe.putFloat(address, 1.0f);
    assertEquals(1.0f, unsafe.getFloat(address));
    unsafe.freeMemory(address);

    float arrayFloat[] = { -1.0f, 0.0f, 1.0f, 2.0f };
    int scale = unsafe.arrayIndexScale(arrayFloat.getClass());
    offset = unsafe.arrayBaseOffset(arrayFloat.getClass());
    for (int i = 0; i < arrayFloat.length; i++) {
        assertEquals(unsafe.getFloat(arrayFloat, offset), arrayFloat[i]);
        offset += scale;
    }
}
 
Example 7
Source File: GetPutShort.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String args[]) throws Exception {
    Unsafe unsafe = Unsafe.getUnsafe();
    Test t = new Test();
    Field field = Test.class.getField("s");

    long offset = unsafe.objectFieldOffset(field);
    assertEquals((short)-1, unsafe.getShort(t, offset));
    unsafe.putShort(t, offset, (short)0);
    assertEquals((short)0, unsafe.getShort(t, offset));

    long address = unsafe.allocateMemory(8);
    unsafe.putShort(address, (short)1);
    assertEquals((short)1, unsafe.getShort(address));
    unsafe.freeMemory(address);

    short arrayShort[] = { -1, 0, 1, 2 };
    int scale = unsafe.arrayIndexScale(arrayShort.getClass());
    offset = unsafe.arrayBaseOffset(arrayShort.getClass());
    for (int i = 0; i < arrayShort.length; i++) {
        assertEquals(unsafe.getShort(arrayShort, offset), arrayShort[i]);
        offset += scale;
    }
}
 
Example 8
Source File: GetPutBoolean.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String args[]) throws Exception {
    Unsafe unsafe = Unsafe.getUnsafe();
    Test t = new Test();
    Field field = Test.class.getField("b1");

    long offset = unsafe.objectFieldOffset(field);
    assertEquals(false, unsafe.getBoolean(t, offset));
    unsafe.putBoolean(t, offset, true);
    assertEquals(true, unsafe.getBoolean(t, offset));

    boolean arrayBoolean[] = { true, false, false, true };
    int scale = unsafe.arrayIndexScale(arrayBoolean.getClass());
    offset = unsafe.arrayBaseOffset(arrayBoolean.getClass());
    for (int i = 0; i < arrayBoolean.length; i++) {
        assertEquals(unsafe.getBoolean(arrayBoolean, offset), arrayBoolean[i]);
        offset += scale;
    }
}
 
Example 9
Source File: GetPutByte.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String args[]) throws Exception {
    Unsafe unsafe = Unsafe.getUnsafe();
    Test t = new Test();
    Field field = Test.class.getField("b");

    long offset = unsafe.objectFieldOffset(field);
    assertEquals((byte)0, unsafe.getByte(t, offset));
    unsafe.putByte(t, offset, (byte)1);
    assertEquals((byte)1, unsafe.getByte(t, offset));

    long address = unsafe.allocateMemory(8);
    unsafe.putByte(address, (byte)2);
    assertEquals((byte)2, unsafe.getByte(address));
    unsafe.freeMemory(address);

    byte arrayByte[] = { -1, 0, 1, 2 };
    int scale = unsafe.arrayIndexScale(arrayByte.getClass());
    offset = unsafe.arrayBaseOffset(arrayByte.getClass());
    for (int i = 0; i < arrayByte.length; i++) {
        assertEquals(unsafe.getByte(arrayByte, offset), arrayByte[i]);
        offset += scale;
    }
}