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);
}
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();
}
}
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);
}
}
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;
}
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());
}
}
Aggregations