Search in sources :

Example 81 with Unsafe

use of sun.misc.Unsafe in project Chronicle-Bytes by OpenHFT.

the class NativeBytesStore method appendUtf8.

public long appendUtf8(long pos, char[] chars, int offset, int length) throws BufferOverflowException {
    if (pos + length > realCapacity())
        throw new BufferOverflowException();
    long address = this.address + translate(0);
    @Nullable Memory memory = this.memory;
    if (memory == null)
        throw new NullPointerException();
    Unsafe unsafe = UnsafeMemory.UNSAFE;
    int i;
    ascii: {
        for (i = 0; i < length - 3; i += 4) {
            final int i2 = offset + i;
            char c0 = chars[i2];
            char c1 = chars[i2 + 1];
            char c2 = chars[i2 + 2];
            char c3 = chars[i2 + 3];
            if ((c0 | c1 | c2 | c3) > 0x007F)
                break ascii;
            final int value = (c0) | (c1 << 8) | (c2 << 16) | (c3 << 24);
            unsafe.putInt(address + pos, value);
            pos += 4;
        }
        for (; i < length; i++) {
            char c = chars[offset + i];
            if (c > 0x007F)
                break ascii;
            unsafe.putByte(address + pos++, (byte) c);
        }
        return pos;
    }
    return appendUtf8a(pos, chars, offset, length, i);
}
Also used : UnsafeMemory(net.openhft.chronicle.core.UnsafeMemory) Memory(net.openhft.chronicle.core.Memory) Unsafe(sun.misc.Unsafe) BufferOverflowException(java.nio.BufferOverflowException) Nullable(org.jetbrains.annotations.Nullable)

Example 82 with Unsafe

use of sun.misc.Unsafe in project sulong by graalvm.

the class UnsafeArrayAccess method getUnsafe.

private static Unsafe getUnsafe() {
    CompilerAsserts.neverPartOfCompilation();
    try {
        Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
        theUnsafe.setAccessible(true);
        return (Unsafe) theUnsafe.get(null);
    } catch (Exception e) {
        throw new AssertionError();
    }
}
Also used : Field(java.lang.reflect.Field) Unsafe(sun.misc.Unsafe)

Example 83 with Unsafe

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

the class UnsafeAccessor method getUnsafe.

static Unsafe getUnsafe() {
    try {
        PrivilegedExceptionAction<Unsafe> getUnsafe = () -> {
            try {
                return Unsafe.getUnsafe();
            } catch (Exception e) {
                Class<Unsafe> type = Unsafe.class;
                Field[] fields = type.getDeclaredFields();
                for (Field field : fields) {
                    if (Modifier.isStatic(field.getModifiers()) && type.isAssignableFrom(field.getType())) {
                        field.setAccessible(true);
                        return type.cast(field.get(null));
                    }
                }
                LinkageError error = new LinkageError("No static field of type sun.misc.Unsafe");
                error.addSuppressed(e);
                throw error;
            }
        };
        return AccessController.doPrivileged(getUnsafe);
    } catch (Exception e) {
        throw new LinkageError("Cannot access sun.misc.Unsafe", e);
    }
}
Also used : Field(java.lang.reflect.Field) Unsafe(sun.misc.Unsafe)

Example 84 with Unsafe

use of sun.misc.Unsafe in project MyPerf4J by ThinkpadNC5.

the class UnsafeUtils method generateUnsafe.

private static Unsafe generateUnsafe() {
    sun.misc.Unsafe unsafe = null;
    try {
        unsafe = AccessController.doPrivileged(new PrivilegedExceptionAction<Unsafe>() {

            @Override
            public Unsafe run() throws Exception {
                Class<Unsafe> k = Unsafe.class;
                for (Field f : k.getDeclaredFields()) {
                    f.setAccessible(true);
                    Object x = f.get(null);
                    if (k.isInstance(x)) {
                        return k.cast(x);
                    }
                }
                // The sun.misc.Unsafe field does not exist.
                return null;
            }
        });
    } catch (Throwable e) {
    // Catching Throwable here due to the fact that Google AppEngine raises NoClassDefFoundError
    // for Unsafe.
    }
    return unsafe;
}
Also used : Unsafe(sun.misc.Unsafe) Field(java.lang.reflect.Field) Unsafe(sun.misc.Unsafe) PrivilegedExceptionAction(java.security.PrivilegedExceptionAction)

Example 85 with Unsafe

use of sun.misc.Unsafe in project openj9 by eclipse.

the class Test_UnsafeFence method testIntFence.

/**
 * @tests sun.misc.Unsafe.getAndAddInt()
 * @tests sun.misc.Unsafe.loadFence()
 * @tests sun.misc.Unsafe.storeFence()
 * @tests sun.misc.Unsafe.fullFence()
 */
@Test
public void testIntFence() {
    Unsafe unsafe = getUnsafe();
    Field intFieldReflect;
    try {
        intFieldReflect = getClass().getDeclaredField("intField");
        long intFieldOffset = unsafe.objectFieldOffset(intFieldReflect);
        AssertJUnit.assertEquals(unsafe.getAndAddInt(this, intFieldOffset, 2), 0);
        AssertJUnit.assertEquals(unsafe.getAndAddInt(this, intFieldOffset, 3), 2);
        AssertJUnit.assertEquals(unsafe.getAndSetInt(this, intFieldOffset, 7), 5);
        AssertJUnit.assertEquals(unsafe.getAndAddInt(this, intFieldOffset, -3), 7);
        AssertJUnit.assertEquals(unsafe.getAndSetInt(this, intFieldOffset, -9), 4);
        unsafe.loadFence();
        AssertJUnit.assertEquals(intField, -9);
        unsafe.storeFence();
        AssertJUnit.assertEquals(intField, -9);
        unsafe.fullFence();
        AssertJUnit.assertEquals(intField, -9);
    } catch (NoSuchFieldException e) {
        Assert.fail("Missing field");
    } catch (SecurityException e) {
        Assert.fail("SecurityException: " + e.getMessage());
    }
}
Also used : Unsafe(sun.misc.Unsafe) Test(org.testng.annotations.Test)

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