Search in sources :

Example 71 with Unsafe

use of sun.misc.Unsafe in project ignite by apache.

the class GridUnsafe method unsafe.

/**
 * @return Instance of Unsafe class.
 */
private static Unsafe unsafe() {
    try {
        return Unsafe.getUnsafe();
    } catch (SecurityException ignored) {
        try {
            return AccessController.doPrivileged(new PrivilegedExceptionAction<Unsafe>() {

                @Override
                public Unsafe run() throws Exception {
                    Field f = Unsafe.class.getDeclaredField("theUnsafe");
                    f.setAccessible(true);
                    return (Unsafe) f.get(null);
                }
            });
        } catch (PrivilegedActionException e) {
            throw new RuntimeException("Could not initialize intrinsics.", e.getCause());
        }
    }
}
Also used : Field(java.lang.reflect.Field) PrivilegedActionException(java.security.PrivilegedActionException) Unsafe(sun.misc.Unsafe) PrivilegedExceptionAction(java.security.PrivilegedExceptionAction)

Example 72 with Unsafe

use of sun.misc.Unsafe in project lwjgl by LWJGL.

the class MappedObjectUnsafe method getUnsafeInstance.

private static Unsafe getUnsafeInstance() {
    final Field[] fields = Unsafe.class.getDeclaredFields();
    /*
		Different runtimes use different names for the Unsafe singleton,
		so we cannot use .getDeclaredField and we scan instead. For example:

		Oracle: theUnsafe
		PERC : m_unsafe_instance
		Android: THE_ONE
		*/
    for (Field field : fields) {
        if (!field.getType().equals(Unsafe.class))
            continue;
        final int modifiers = field.getModifiers();
        if (!(Modifier.isStatic(modifiers) && Modifier.isFinal(modifiers)))
            continue;
        field.setAccessible(true);
        try {
            return (Unsafe) field.get(null);
        } catch (IllegalAccessException e) {
        // ignore
        }
        break;
    }
    throw new UnsupportedOperationException();
}
Also used : Field(java.lang.reflect.Field) Unsafe(sun.misc.Unsafe)

Example 73 with Unsafe

use of sun.misc.Unsafe in project powermock by powermock.

the class WhiteboxImpl method getUnsafe.

private static Unsafe getUnsafe() throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException {
    Field field1 = Unsafe.class.getDeclaredField("theUnsafe");
    field1.setAccessible(true);
    Unsafe unsafe = (Unsafe) field1.get(null);
    return unsafe;
}
Also used : Field(java.lang.reflect.Field) Unsafe(sun.misc.Unsafe)

Example 74 with Unsafe

use of sun.misc.Unsafe in project j2objc by google.

the class MappedByteBuffer method load.

/**
 * Loads this buffer's content into physical memory.
 *
 * <p> This method makes a best effort to ensure that, when it returns,
 * this buffer's content is resident in physical memory.  Invoking this
 * method may cause some number of page faults and I/O operations to
 * occur. </p>
 *
 * @return  This buffer
 */
public final MappedByteBuffer load() {
    checkMapped();
    if ((address == 0) || (capacity() == 0))
        return this;
    long offset = mappingOffset();
    long length = mappingLength(offset);
    load0(mappingAddress(offset), length);
    // Read a byte from each page to bring it into memory. A checksum
    // is computed as we go along to prevent the compiler from otherwise
    // considering the loop as dead code.
    Unsafe unsafe = Unsafe.getUnsafe();
    int ps = Bits.pageSize();
    int count = Bits.pageCount(length);
    long a = mappingAddress(offset);
    byte x = 0;
    for (int i = 0; i < count; i++) {
        x ^= unsafe.getByte(a);
        a += ps;
    }
    if (unused != 0)
        unused = x;
    return this;
}
Also used : Unsafe(sun.misc.Unsafe)

Example 75 with Unsafe

use of sun.misc.Unsafe in project hazelcast by hazelcast.

the class UnsafeUtil method findUnsafe.

private static Unsafe findUnsafe() {
    try {
        return Unsafe.getUnsafe();
    } catch (SecurityException se) {
        return AccessController.doPrivileged(new PrivilegedAction<Unsafe>() {

            @Override
            public Unsafe run() {
                try {
                    Class<Unsafe> type = Unsafe.class;
                    try {
                        Field field = type.getDeclaredField("theUnsafe");
                        field.setAccessible(true);
                        return type.cast(field.get(type));
                    } catch (Exception e) {
                        for (Field field : type.getDeclaredFields()) {
                            if (type.isAssignableFrom(field.getType())) {
                                field.setAccessible(true);
                                return type.cast(field.get(type));
                            }
                        }
                    }
                } catch (Throwable t) {
                    throw rethrow(t);
                }
                throw new RuntimeException("Unsafe unavailable");
            }
        });
    }
}
Also used : Field(java.lang.reflect.Field) PrivilegedAction(java.security.PrivilegedAction) Unsafe(sun.misc.Unsafe)

Aggregations

Unsafe (sun.misc.Unsafe)90 Field (java.lang.reflect.Field)62 PrivilegedExceptionAction (java.security.PrivilegedExceptionAction)7 Test (org.testng.annotations.Test)7 PrivilegedActionException (java.security.PrivilegedActionException)5 PrivilegedAction (java.security.PrivilegedAction)4 IOException (java.io.IOException)3 JavaKind (jdk.vm.ci.meta.JavaKind)3 ResolvedJavaMethod (jdk.vm.ci.meta.ResolvedJavaMethod)3 ValueNode (org.graalvm.compiler.nodes.ValueNode)3 GraphBuilderContext (org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderContext)3 Registration (org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins.Registration)3 Test (org.junit.Test)3 LazyArrayIntTags (edu.columbia.cs.psl.phosphor.struct.LazyArrayIntTags)2 LazyArrayObjTags (edu.columbia.cs.psl.phosphor.struct.LazyArrayObjTags)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 InvocationPlugin (org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin)2 Receiver (org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin.Receiver)2 FrameSlotTypeException (com.oracle.truffle.api.frame.FrameSlotTypeException)1 DataSize (io.airlift.units.DataSize)1