jdk.internal.misc.Unsafe Java Examples

The following examples show how to use jdk.internal.misc.Unsafe. 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: 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 #2
Source File: ArraysSupport.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
static int mismatch(char[] a,
                    char[] b,
                    int length) {
    int i = 0;
    if (length > 3) {
        i = vectorizedMismatch(
                a, Unsafe.ARRAY_CHAR_BASE_OFFSET,
                b, Unsafe.ARRAY_CHAR_BASE_OFFSET,
                length, LOG2_ARRAY_CHAR_INDEX_SCALE);
        if (i >= 0)
            return i;
        i = length - ~i;
    }
    for (; i < length; i++) {
        if (a[i] != b[i])
            return i;
    }
    return -1;
}
 
Example #3
Source File: WindowsNativeDispatcher.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
static NativeBuffer asNativeBuffer(String s) {
    int stringLengthInBytes = s.length() << 1;
    int sizeInBytes = stringLengthInBytes + 2;  // char terminator

    // get a native buffer of sufficient size
    NativeBuffer buffer = NativeBuffers.getNativeBufferFromCache(sizeInBytes);
    if (buffer == null) {
        buffer = NativeBuffers.allocNativeBuffer(sizeInBytes);
    } else {
        // buffer already contains the string contents
        if (buffer.owner() == s)
            return buffer;
    }

    // copy into buffer and zero terminate
    char[] chars = s.toCharArray();
    unsafe.copyMemory(chars, Unsafe.ARRAY_CHAR_BASE_OFFSET, null,
        buffer.address(), (long)stringLengthInBytes);
    unsafe.putChar(buffer.address() + stringLengthInBytes, (char)0);
    buffer.setOwner(s);
    return buffer;
}
 
Example #4
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 #5
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 #6
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 #7
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 #8
Source File: ArraysSupport.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
public static int mismatch(int[] a, int aFromIndex,
                           int[] b, int bFromIndex,
                           int length) {
    int i = 0;
    if (length > 1) {
        if (a[aFromIndex] != b[bFromIndex])
            return 0;
        int aOffset = Unsafe.ARRAY_INT_BASE_OFFSET + (aFromIndex << LOG2_ARRAY_INT_INDEX_SCALE);
        int bOffset = Unsafe.ARRAY_INT_BASE_OFFSET + (bFromIndex << LOG2_ARRAY_INT_INDEX_SCALE);
        i = vectorizedMismatch(
                a, aOffset,
                b, bOffset,
                length, LOG2_ARRAY_INT_INDEX_SCALE);
        if (i >= 0)
            return i;
        i = length - ~i;
    }
    for (; i < length; i++) {
        if (a[aFromIndex + i] != b[bFromIndex + i])
            return i;
    }
    return -1;
}
 
Example #9
Source File: ArraysSupport.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
public static int mismatch(int[] a,
                           int[] b,
                           int length) {
    int i = 0;
    if (length > 1) {
        if (a[0] != b[0])
            return 0;
        i = vectorizedMismatch(
                a, Unsafe.ARRAY_INT_BASE_OFFSET,
                b, Unsafe.ARRAY_INT_BASE_OFFSET,
                length, LOG2_ARRAY_INT_INDEX_SCALE);
        if (i >= 0)
            return i;
        i = length - ~i;
    }
    for (; i < length; i++) {
        if (a[i] != b[i])
            return i;
    }
    return -1;
}
 
Example #10
Source File: ArraysSupport.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
static int mismatch(boolean[] a,
                    boolean[] b,
                    int length) {
    int i = 0;
    if (length > 7) {
        i = vectorizedMismatch(
                a, Unsafe.ARRAY_BOOLEAN_BASE_OFFSET,
                b, Unsafe.ARRAY_BOOLEAN_BASE_OFFSET,
                length, LOG2_ARRAY_BOOLEAN_INDEX_SCALE);
        if (i >= 0)
            return i;
        i = length - ~i;
    }
    for (; i < length; i++) {
        if (a[i] != b[i])
            return i;
    }
    return -1;
}
 
Example #11
Source File: ArraysSupport.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
static int mismatch(int[] a,
                    int[] b,
                    int length) {
    int i = 0;
    if (length > 1) {
        i = vectorizedMismatch(
                a, Unsafe.ARRAY_INT_BASE_OFFSET,
                b, Unsafe.ARRAY_INT_BASE_OFFSET,
                length, LOG2_ARRAY_INT_INDEX_SCALE);
        if (i >= 0)
            return i;
        i = length - ~i;
    }
    for (; i < length; i++) {
        if (a[i] != b[i])
            return i;
    }
    return -1;
}
 
Example #12
Source File: ArraysSupport.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
static int mismatch(int[] a, int aFromIndex,
                    int[] b, int bFromIndex,
                    int length) {
    int i = 0;
    if (length > 1) {
        int aOffset = Unsafe.ARRAY_INT_BASE_OFFSET + (aFromIndex << LOG2_ARRAY_INT_INDEX_SCALE);
        int bOffset = Unsafe.ARRAY_INT_BASE_OFFSET + (bFromIndex << LOG2_ARRAY_INT_INDEX_SCALE);
        i = vectorizedMismatch(
                a, aOffset,
                b, bOffset,
                length, LOG2_ARRAY_INT_INDEX_SCALE);
        if (i >= 0)
            return i;
        i = length - ~i;
    }
    for (; i < length; i++) {
        if (a[aFromIndex + i] != b[bFromIndex + i])
            return i;
    }
    return -1;
}
 
Example #13
Source File: ArraysSupport.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
public static int mismatch(char[] a,
                           char[] b,
                           int length) {
    int i = 0;
    if (length > 3) {
        if (a[0] != b[0])
            return 0;
        i = vectorizedMismatch(
                a, Unsafe.ARRAY_CHAR_BASE_OFFSET,
                b, Unsafe.ARRAY_CHAR_BASE_OFFSET,
                length, LOG2_ARRAY_CHAR_INDEX_SCALE);
        if (i >= 0)
            return i;
        i = length - ~i;
    }
    for (; i < length; i++) {
        if (a[i] != b[i])
            return i;
    }
    return -1;
}
 
Example #14
Source File: ArraysSupport.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
public static int mismatch(boolean[] a, int aFromIndex,
                           boolean[] b, int bFromIndex,
                           int length) {
    int i = 0;
    if (length > 7) {
        if (a[aFromIndex] != b[bFromIndex])
            return 0;
        int aOffset = Unsafe.ARRAY_BOOLEAN_BASE_OFFSET + aFromIndex;
        int bOffset = Unsafe.ARRAY_BOOLEAN_BASE_OFFSET + bFromIndex;
        i = vectorizedMismatch(
                a, aOffset,
                b, bOffset,
                length, LOG2_ARRAY_BOOLEAN_INDEX_SCALE);
        if (i >= 0)
            return i;
        i = length - ~i;
    }
    for (; i < length; i++) {
        if (a[aFromIndex + i] != b[bFromIndex + i])
            return i;
    }
    return -1;
}
 
Example #15
Source File: ArraysSupport.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
public static int mismatch(boolean[] a,
                           boolean[] b,
                           int length) {
    int i = 0;
    if (length > 7) {
        if (a[0] != b[0])
            return 0;
        i = vectorizedMismatch(
                a, Unsafe.ARRAY_BOOLEAN_BASE_OFFSET,
                b, Unsafe.ARRAY_BOOLEAN_BASE_OFFSET,
                length, LOG2_ARRAY_BOOLEAN_INDEX_SCALE);
        if (i >= 0)
            return i;
        i = length - ~i;
    }
    for (; i < length; i++) {
        if (a[i] != b[i])
            return i;
    }
    return -1;
}
 
Example #16
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 #17
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 #18
Source File: Test7190310_unsafe.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
static boolean verifyGet(long referent_offset, Unsafe unsafe) throws Exception {
    // Access verification
    System.out.println("referent: " + str);
    Object obj = getRef0(ref);
    if (obj != str) {
        System.out.println("FAILED: weakRef.get() " + obj + " != " + str);
        return false;
    }
    obj = getRef1(unsafe, ref, referent_offset);
    if (obj != str) {
        System.out.println("FAILED: unsafe.getObject(weakRef, " + referent_offset + ") " + obj + " != " + str);
        return false;
    }
    obj = getRef2(unsafe, ref, referent_offset);
    if (obj != str) {
        System.out.println("FAILED: unsafe.getObject(abstRef, " + referent_offset + ") " + obj + " != " + str);
        return false;
    }
    obj = getRef3(unsafe, ref, referent_offset);
    if (obj != str) {
        System.out.println("FAILED: unsafe.getObject(Object, " + referent_offset + ") " + obj + " != " + str);
        return false;
    }
    return true;
}
 
Example #19
Source File: LinuxUserDefinedFileAttributeView.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private List<String> asList(long address, int size) {
    List<String> list = new ArrayList<>();
    int start = 0;
    int pos = 0;
    while (pos < size) {
        if (unsafe.getByte(address + pos) == 0) {
            int len = pos - start;
            byte[] value = new byte[len];
            unsafe.copyMemory(null, address+start, value,
                Unsafe.ARRAY_BYTE_BASE_OFFSET, len);
            String s = Util.toString(value);
            if (s.startsWith(USER_NAMESPACE)) {
                s = s.substring(USER_NAMESPACE.length());
                list.add(s);
            }
            start = pos + 1;
        }
        pos++;
    }
    return list;
}
 
Example #20
Source File: HotSpotJVMCIRuntimeProvider.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * The offset from the origin of an array to the first element.
 *
 * @return the offset in bytes
 */
static int getArrayBaseOffset(JavaKind kind) {
    switch (kind) {
        case Boolean:
            return Unsafe.ARRAY_BOOLEAN_BASE_OFFSET;
        case Byte:
            return Unsafe.ARRAY_BYTE_BASE_OFFSET;
        case Char:
            return Unsafe.ARRAY_CHAR_BASE_OFFSET;
        case Short:
            return Unsafe.ARRAY_SHORT_BASE_OFFSET;
        case Int:
            return Unsafe.ARRAY_INT_BASE_OFFSET;
        case Long:
            return Unsafe.ARRAY_LONG_BASE_OFFSET;
        case Float:
            return Unsafe.ARRAY_FLOAT_BASE_OFFSET;
        case Double:
            return Unsafe.ARRAY_DOUBLE_BASE_OFFSET;
        case Object:
            return Unsafe.ARRAY_OBJECT_BASE_OFFSET;
        default:
            throw new JVMCIError("%s", kind);
    }
}
 
Example #21
Source File: ArraysSupport.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
static int mismatch(boolean[] a, int aFromIndex,
                    boolean[] b, int bFromIndex,
                    int length) {
    int i = 0;
    if (length > 7) {
        int aOffset = Unsafe.ARRAY_BOOLEAN_BASE_OFFSET + aFromIndex;
        int bOffset = Unsafe.ARRAY_BOOLEAN_BASE_OFFSET + bFromIndex;
        i = vectorizedMismatch(
                a, aOffset,
                b, bOffset,
                length, LOG2_ARRAY_BOOLEAN_INDEX_SCALE);
        if (i >= 0)
            return i;
        i = length - ~i;
    }
    for (; i < length; i++) {
        if (a[aFromIndex + i] != b[bFromIndex + i])
            return i;
    }
    return -1;
}
 
Example #22
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 #23
Source File: NestedUnsafe2.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();

    // The anonymous class calls defineAnonymousClass creating a nested anonymous class.
    byte klassbuf2[] = InMemoryJavaCompiler.compile("TestClass2",
        "import jdk.internal.misc.Unsafe; " +
        "public class TestClass2 { " +
        "    public static void doit() throws Throwable { " +
        "        Unsafe unsafe = jdk.internal.misc.Unsafe.getUnsafe(); " +
        "        Class klass2 = unsafe.defineAnonymousClass(TestClass2.class, p.NestedUnsafe2.klassbuf, new Object[0]); " +
        "        unsafe.ensureClassInitialized(klass2); " +
        "        Class[] dArgs = new Class[2]; " +
        "        dArgs[0] = String.class; " +
        "        dArgs[1] = String.class; " +
        "        try { " +
        "            klass2.getMethod(\"concat\", dArgs).invoke(null, \"CC\", \"DD\"); " +
        "        } catch (Throwable ex) { " +
        "            throw new RuntimeException(\"Exception: \" + ex.toString()); " +
        "        } " +
        "} } ",
        "--add-exports=java.base/jdk.internal.misc=ALL-UNNAMED");

    Class klass2 = unsafe.defineAnonymousClass(p.NestedUnsafe2.class, klassbuf2, new Object[0]);
    try {
        klass2.getMethod("doit").invoke(null);
        throw new RuntimeException("Expected exception not thrown");
    } catch (Throwable ex) {
        Throwable iae = ex.getCause();
        if (!iae.toString().contains(
            "IllegalArgumentException: Host class p/NestedUnsafe2 and anonymous class q/TestClass")) {
            throw new RuntimeException("Exception: " + iae.toString());
        }
    }
}
 
Example #24
Source File: RenderBuffer.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public RenderBuffer put(int[] x, int offset, int length) {
    // assert (position() % SIZEOF_INT == 0);
    if (length > COPY_FROM_ARRAY_THRESHOLD) {
        long offsetInBytes = offset * SIZEOF_INT + Unsafe.ARRAY_INT_BASE_OFFSET;
        long lengthInBytes = length * SIZEOF_INT;
        unsafe.copyMemory(x, offsetInBytes, null, curAddress, lengthInBytes);
        position(position() + lengthInBytes);
    } else {
        int end = offset + length;
        for (int i = offset; i < end; i++) {
            putInt(x[i]);
        }
    }
    return this;
}
 
Example #25
Source File: RenderBuffer.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public RenderBuffer put(byte[] x, int offset, int length) {
    if (length > COPY_FROM_ARRAY_THRESHOLD) {
        long offsetInBytes = offset * SIZEOF_BYTE + Unsafe.ARRAY_BYTE_BASE_OFFSET;
        long lengthInBytes = length * SIZEOF_BYTE;
        unsafe.copyMemory(x, offsetInBytes, null, curAddress, lengthInBytes);
        position(position() + lengthInBytes);
    } else {
        int end = offset + length;
        for (int i = offset; i < end; i++) {
            putByte(x[i]);
        }
    }
    return this;
}
 
Example #26
Source File: NestedUnsafe.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();

    // The anonymous class calls defineAnonymousClass creating a nested anonymous class.
    byte klassbuf2[] = InMemoryJavaCompiler.compile("p.TestClass2",
        "package p; " +
        "import jdk.internal.misc.Unsafe; " +
        "public class TestClass2 { " +
        "    public static void doit() throws Throwable { " +
        "        Unsafe unsafe = jdk.internal.misc.Unsafe.getUnsafe(); " +
        "        Class klass2 = unsafe.defineAnonymousClass(TestClass2.class, p.NestedUnsafe.klassbuf, new Object[0]); " +
        "        unsafe.ensureClassInitialized(klass2); " +
        "        Class[] dArgs = new Class[2]; " +
        "        dArgs[0] = String.class; " +
        "        dArgs[1] = String.class; " +
        "        try { " +
        "            klass2.getMethod(\"concat\", dArgs).invoke(null, \"CC\", \"DD\"); " +
        "        } catch (Throwable ex) { " +
        "            throw new RuntimeException(\"Exception: \" + ex.toString()); " +
        "        } " +
        "} } ",
        "--add-exports=java.base/jdk.internal.misc=ALL-UNNAMED");

    Class klass2 = unsafe.defineAnonymousClass(p.NestedUnsafe.class, klassbuf2, new Object[0]);
    try {
        klass2.getMethod("doit").invoke(null);
        throw new RuntimeException("Expected exception not thrown");
    } catch (Throwable ex) {
        Throwable iae = ex.getCause();
        if (!iae.toString().contains(
            "IllegalArgumentException: Host class p/NestedUnsafe and anonymous class q/TestClass")) {
            throw new RuntimeException("Exception: " + iae.toString());
        }
    }
}
 
Example #27
Source File: XAbortProvoker.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public String[] getMethodsToCompileNames() {
    return new String[] {
            getMethodWithLockName(),
            Unsafe.class.getName() + "::addressSize"
    };
}
 
Example #28
Source File: TestShrinkAuxiliaryData.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Detects maximum possible size of G1ConcRSLogCacheSize available for
 * current process based on maximum available process memory size
 *
 * @return power of two
 */
private static int getMaxCacheSize() {
    long availableMemory = Runtime.getRuntime().freeMemory()
            - ShrinkAuxiliaryDataTest.getMemoryUsedByTest() - 1l;
    if (availableMemory <= 0) {
        return 0;
    }

    long availablePointersCount = availableMemory / Unsafe.ADDRESS_SIZE;
    return (63 - (int) Long.numberOfLeadingZeros(availablePointersCount));
}
 
Example #29
Source File: ManyNewInstanceAnonTest.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 = Unsafe.getUnsafe().defineAnonymousClass(klass, readClassFile(), null);
    for (int i = 0; i < REPS; ++i) {
        System.out.printf("%d: %s\n", i, c.newInstance());
    }
    System.out.println("Passed.");
}
 
Example #30
Source File: ArraysSupport.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
static int mismatch(float[] a, int aFromIndex,
                    float[] b, int bFromIndex,
                    int length) {
    int i = 0;
    if (length > 1) {
        int aOffset = Unsafe.ARRAY_FLOAT_BASE_OFFSET + (aFromIndex << LOG2_ARRAY_FLOAT_INDEX_SCALE);
        int bOffset = Unsafe.ARRAY_FLOAT_BASE_OFFSET + (bFromIndex << LOG2_ARRAY_FLOAT_INDEX_SCALE);
        i = vectorizedMismatch(
                a, aOffset,
                b, bOffset,
                length, LOG2_ARRAY_FLOAT_INDEX_SCALE);
        // Mismatched
        if (i >= 0) {
            // Check if mismatch is not associated with two NaN values
            if (!Float.isNaN(a[aFromIndex + i]) || !Float.isNaN(b[bFromIndex + i]))
                return i;

            // Mismatch on two different NaN values that are normalized to match
            // Fall back to slow mechanism
            // ISSUE: Consider looping over vectorizedMismatch adjusting ranges
            // However, requires that returned value be relative to input ranges
            i++;
        }
        // Matched
        else {
            i = length - ~i;
        }
    }
    for (; i < length; i++) {
        if (Float.floatToIntBits(a[aFromIndex + i]) != Float.floatToIntBits(b[bFromIndex + i]))
            return i;
    }
    return -1;
}