Java Code Examples for sun.misc.Unsafe#getAddress()

The following examples show how to use sun.misc.Unsafe#getAddress() . 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: UnsafeGetAddressTest.java    From TencentKona-8 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("sun.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 sun.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 2
Source File: UnsafeGetAddressTest.java    From jdk8u60 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("sun.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 sun.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 3
Source File: UnsafeGetAddressTest.java    From openjdk-jdk8u 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("sun.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 sun.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 4
Source File: UnsafeGetAddressTest.java    From openjdk-jdk8u-backup 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("sun.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 sun.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 5
Source File: UnsafeSubstitutionsTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings("all")
public static long unsafePutAddress(Unsafe unsafe, long offset, long value) {
    long res = 1;
    unsafe.putAddress(offset, value);
    res += unsafe.getAddress(offset);
    return res;
}
 
Example 6
Source File: UnsafeGetAddressTest.java    From hottub 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("sun.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 sun.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 7
Source File: UnsafeSubstitutionsTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@SuppressWarnings("all")
public static long unsafeGetAddress(Unsafe unsafe, long offset) {
    return unsafe.getAddress(offset);
}