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

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