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

The following examples show how to use jdk.internal.misc.Unsafe#allocateMemory() . 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: 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 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: GetUncompressedObject.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();

    // Allocate some memory and fill it with non-zero values.
    final int size = 32;
    final long address = unsafe.allocateMemory(size);
    unsafe.setMemory(address, size, (byte) 0x23);

    // The only thing we can do is check for null-ness.
    // So, store a null somewhere.
    unsafe.putAddress(address + 16, 0);

    Object nullObj = unsafe.getUncompressedObject(address + 16);
    if (nullObj != null) {
        throw new InternalError("should be null");
    }
}
 
Example 7
Source File: GetPutAddress.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();
    int addressSize = unsafe.addressSize();
    // Ensure the size returned from Unsafe.addressSize is correct
    assertEquals(unsafe.addressSize(), Platform.is32bit() ? 4 : 8);

    // Write the address, read it back and make sure it's the same value
    long address = unsafe.allocateMemory(addressSize);
    unsafe.putAddress(address, address);
    long readAddress = unsafe.getAddress(address);
    if (addressSize == 4) {
      readAddress &= 0x00000000FFFFFFFFL;
    }
    assertEquals(address, readAddress);
    unsafe.freeMemory(address);
}
 
Example 8
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 9
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 10
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 11
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");
}
 
Example 12
Source File: SetMemory.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);
    unsafe.setMemory(address, 1, (byte)17);
    assertEquals((byte)17, unsafe.getByte(address));
    unsafe.freeMemory(address);
}
 
Example 13
Source File: UnsafeGetAddressTest.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 {
    Class c = UnsafeGetAddressTest.class.getClassLoader().loadClass("jdk.internal.misc.Unsafe");
    Field f = c.getDeclaredField("theUnsafe");
    f.setAccessible(true);
    unsafe = (Unsafe)f.get(c);

    long address = unsafe.allocateMemory(unsafe.addressSize());
    unsafe.putAddress(address, 0x0000000080000000L);
    // from jdk.internal.misc.Unsafe.getAddress' documentation:
    // "If the native pointer is less than 64 bits wide, it is
    // extended as an unsigned number to a Java long."
    result = unsafe.getAddress(address);
    System.out.printf("1: was 0x%x, expected 0x%x\n", result,
            0x0000000080000000L);
    for (int i = 0; i < 1000000; i++) {
        result = unsafe.getAddress(address);
    }

    // The code has got compiled, check the result now
    System.out.printf("2: was 0x%x, expected 0x%x\n", result,
            0x0000000080000000L);
    if (result != 0x0000000080000000L) {
        System.out.println("Test Failed");
        System.exit(97);
    } else {
        System.out.println("Test Passed");
    }
}
 
Example 14
Source File: UnsafeRaw.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
  Unsafe unsafe = Unsafe.getUnsafe();
  final int array_size = 128;
  final int element_size = 4;
  final int magic = 0x12345678;

  Random rnd = Utils.getRandomInstance();

  long array = unsafe.allocateMemory(array_size * element_size); // 128 ints
  long addr = array + array_size * element_size / 2; // something in the middle to work with
  unsafe.putInt(addr, magic);
  for (int j = 0; j < 100000; j++) {
     if (Tests.int_index(unsafe, addr, 0) != magic) throw new Exception();
     if (Tests.long_index(unsafe, addr, 0) != magic) throw new Exception();
     if (Tests.int_index_mul(unsafe, addr, 0) != magic) throw new Exception();
     if (Tests.long_index_mul(unsafe, addr, 0) != magic) throw new Exception();
     {
       long idx1 = rnd.nextLong();
       long addr1 = addr - (idx1 << 2);
       if (Tests.long_index(unsafe, addr1, idx1) != magic) throw new Exception();
     }
     {
       long idx2 = rnd.nextLong();
       long addr2 = addr - (idx2 >> 2);
       if (Tests.long_index_back_ashift(unsafe, addr2, idx2) != magic) throw new Exception();
     }
     {
       long idx3 = rnd.nextLong();
       long addr3 = addr - (idx3 >>> 2);
       if (Tests.long_index_back_lshift(unsafe, addr3, idx3) != magic) throw new Exception();
     }
     {
       long idx4 = 0x12345678;
       long addr4 = addr - idx4;
       if (Tests.int_const_12345678_index(unsafe, addr4) != magic) throw new Exception();
     }
     {
       long idx5 = 0x1234567890abcdefL;
       long addr5 = addr - idx5;
       if (Tests.long_const_1234567890abcdef_index(unsafe, addr5) != magic) throw new Exception();
     }
     {
       int idx6 = rnd.nextInt();
       long addr6 = addr - (idx6 >> 2);
       if (Tests.int_index_back_ashift(unsafe, addr6, idx6) != magic) throw new Exception();
     }
     {
       int idx7 = rnd.nextInt();
       long addr7 = addr - (idx7 >>> 2);
       if (Tests.int_index_back_lshift(unsafe, addr7, idx7) != magic) throw new Exception();
     }
     {
       int idx8 = rnd.nextInt();
       long addr8 = addr - (idx8 * 16);
       if (Tests.int_index_mul_scale_16(unsafe, addr8, idx8) != magic) throw new Exception();
     }
     {
       long idx9 = rnd.nextLong();
       long addr9 = addr - (idx9 * 16);
       if (Tests.long_index_mul_scale_16(unsafe, addr9, idx9) != magic) throw new Exception();
     }
  }
}