Java 8+ direct memory api. It is approximattely the same as sun.misc.Unsafe, but when unsafe methods will start to dissapear in new JVMs, this library will continue to work just fine.
So basically you can just use it instead of Unsafe to ensure that your application won't break in newer JVMs.
Why do I repeat myself
Whatever
This library is unfinished due to lack of multiplatfrom support in JNIDirect (which is unlickely to ever come out), but it still might be useful if you know you target JVM (see conditions in releases) or just want to test something on old java versions.
public class MyVeryTrustedYetExplosiveClass {
private static final MemAccessor m = Mem.memoryAccessor();
public static long newLongArray(long len) {
long byteSize = len * 8;
try {
long addr = m.allocateMemory(byteSize, 8);
m.setMemory(addr, 0, byteSize);
return addr;
} catch (Unchecked _) {
// this is never thrown, it's just a marker that this code is unsafe
return 0;
}
}
public static long getLong(long _Array) throws Unchecked /* more logical way to mark it */ {
return m.getLongAligned(_Array);
}
public static void setLongs(long _Array, long... longs) throws Unchecked {
m.putLongs(_Array, longs, 0, longs.lenght);
}
public static void deleteLongArray(long _Addr) {
try {
m.deallocateMemory(_Addr);
} catch (Unchecked _) {
// assertion error or whatever
// if something goes wrong you probably won't even get that famous JVM crash, just some random exit code, idk why but JVM seems to be unable to handle wrong free() calls
}
}
}
// wait i just realized that allocateMemory might not work in java8 (excluding windows x64) even with unsafe since it uses jnidirect for alignment allocations support
// ._.