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

The following examples show how to use jdk.internal.misc.Unsafe#putByte() . 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: CopyMemory.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();
    long src = unsafe.allocateMemory(LENGTH);
    long dst = unsafe.allocateMemory(LENGTH);
    assertNotEquals(src, 0L);
    assertNotEquals(dst, 0L);

    // call copyMemory() with different lengths and verify the contents of
    // the destination array
    for (int i = 0; i < LENGTH; i++) {
        unsafe.putByte(src + i, (byte)i);
        unsafe.copyMemory(src, dst, i);
        for (int j = 0; j < i; j++) {
            assertEquals(unsafe.getByte(src + j), unsafe.getByte(src + j));
        }
    }
    unsafe.freeMemory(src);
    unsafe.freeMemory(dst);
}
 
Example 2
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;
    }
}
 
Example 3
Source File: Reallocate.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String args[]) throws Exception {
    Unsafe unsafe = Unsafe.getUnsafe();

    long address = unsafe.allocateMemory(1);
    assertNotEquals(address, 0L);

    // Make sure we reallocate correctly
    unsafe.putByte(address, Byte.MAX_VALUE);
    address = unsafe.reallocateMemory(address, 2);
    assertNotEquals(address, 0L);
    assertEquals(unsafe.getByte(address), Byte.MAX_VALUE);

    // Reallocating with a 0 size should return a null pointer
    address = unsafe.reallocateMemory(address, 0);
    assertEquals(address, 0L);

    // Reallocating with a null pointer should result in a normal allocation
    address = unsafe.reallocateMemory(0L, 1);
    assertNotEquals(address, 0L);
    unsafe.putByte(address, Byte.MAX_VALUE);
    assertEquals(unsafe.getByte(address), Byte.MAX_VALUE);

    // Make sure we can throw an OOME when we fail to reallocate due to OOM
    try {
        unsafe.reallocateMemory(address, 100 * 1024 * 1024 * 8);
    } catch (OutOfMemoryError e) {
        // Expected
        return;
    }
    throw new RuntimeException("Did not get expected OOM");
}