This class may be removed in the future. Consider the replacements in JEP 193 'Variable Handles': http://openjdk.java.net/jeps/193
static long allocateMemory(long size) { return UNSAFE.allocateMemory(size); }
static long allocateMemory(long size) { return UNSAFE.allocateMemory(size); }
/** * Allocates memory. * * @param size Size. * @return address. */ public static long allocateMemory(long size) { return UNSAFE.allocateMemory(size); }
static long allocateMemory(long size) { return UNSAFE.allocateMemory(size); }
public long allocateMemory(long size) { return this.unsafe.allocateMemory(size); }
static ByteBuffer allocateDirectNoCleaner(int capacity) { // Calling malloc with capacity of 0 may return a null ptr or a memory address that can be used. // Just use 1 to make it safe to use in all cases: // See: http://pubs.opengroup.org/onlinepubs/009695399/functions/malloc.html return newDirectBuffer(UNSAFE.allocateMemory(Math.max(1, capacity)), capacity); }
static ByteBuffer allocateDirectNoCleaner(int capacity) { // Calling malloc with capacity of 0 may return a null ptr or a memory address that can be used. // Just use 1 to make it safe to use in all cases: // See: http://pubs.opengroup.org/onlinepubs/009695399/functions/malloc.html return newDirectBuffer(UNSAFE.allocateMemory(Math.max(1, capacity)), capacity); }
@Override public BasicBytez newInstance(long size) { return new MallocBytez(unsafe.allocateMemory(size),size); }
@Override public Bytez alloc(long len) { MallocBytez mallocBytez = new MallocBytez(MallocBytez.unsafe.allocateMemory(len), len); mallocBytez.clear(); allocated.add(mallocBytez); alloced.getAndAdd(len); return mallocBytez; }
static ByteBuffer allocateDirectNoCleaner(int capacity) { // Calling malloc with capacity of 0 may return a null ptr or a memory address that can be used. // Just use 1 to make it safe to use in all cases: // See: http://pubs.opengroup.org/onlinepubs/009695399/functions/malloc.html return newDirectBuffer(UNSAFE.allocateMemory(Math.max(1, capacity)), capacity); }
@Override public BasicBytez newInstance(long size) { return new MallocBytez(unsafe.allocateMemory(size),size); }
import java.lang.reflect.Constructor; import java.lang.reflect.Field; import sun.misc.Unsafe; public class TestUnsafe { public static void main(String[] args) throws Exception{ Class unsafeClass = Class.forName("sun.misc.Unsafe"); Field f = unsafeClass.getDeclaredField("theUnsafe"); f.setAccessible(true); Unsafe unsafe = (Unsafe) f.get(null); System.out.print("4..3..2..1..."); try { for(;;) unsafe.allocateMemory(1024*1024); } catch(Error e) { System.out.println("Boom :)"); e.printStackTrace(); } } }
@Override public Bytez alloc(long len) { MallocBytez mallocBytez = new MallocBytez(MallocBytez.unsafe.allocateMemory(len), len); mallocBytez.clear(); allocated.add(mallocBytez); alloced.getAndAdd(len); return mallocBytez; }
/** * Allocate a block of memory of the given size in bytes, and return a pointer to that memory. * <p> * The memory is aligned such that it can be used for any data type. * The memory is uninitialised, so it may contain random garbage, or it may not. * * @return a pointer to the allocated memory */ public static long allocateMemory( long bytes ) throws NativeMemoryAllocationRefusedError { final long pointer; try { pointer = unsafe.allocateMemory( bytes ); } catch ( Throwable e ) { throw new NativeMemoryAllocationRefusedError( bytes, GlobalMemoryTracker.INSTANCE.usedDirectMemory(), e ); } if ( DIRTY_MEMORY ) { setMemory( pointer, bytes, (byte) 0xA5 ); } addAllocatedPointer( pointer, bytes ); GlobalMemoryTracker.INSTANCE.allocated( bytes ); return pointer; }
@Test public void testNativeOrder() { BitUtil bitUtil = BitUtil.get(ByteOrder.nativeOrder()); long address = UnsafeDataAccess.UNSAFE.allocateMemory(8); long val = 123123123123L * 123L; byte[] bytes = new byte[8]; bitUtil.fromLong(bytes, val); if (ByteOrder.nativeOrder().equals(ByteOrder.LITTLE_ENDIAN)) { for (int i = 7; i >= 0; i--) { UnsafeDataAccess.UNSAFE.putByte(address + i, bytes[i]); } } else { // not tested: for (int i = 0; i < 8; i++) { UnsafeDataAccess.UNSAFE.putByte(address + i, bytes[i]); } } long tmp = UnsafeDataAccess.UNSAFE.getLong(address); assertEquals(val, tmp); UnsafeDataAccess.UNSAFE.freeMemory(address); } }
static long allocateMemory(long size) { return UNSAFE.allocateMemory(size); }
static ByteBuffer allocateDirectNoCleaner(int capacity) { // Calling malloc with capacity of 0 may return a null ptr or a memory address that can be used. // Just use 1 to make it safe to use in all cases: // See: http://pubs.opengroup.org/onlinepubs/009695399/functions/malloc.html return newDirectBuffer(UNSAFE.allocateMemory(Math.max(1, capacity)), capacity); }
import sun.misc.Unsafe; import java.lang.reflect.Field; public class Main { public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException, InstantiationException { /* too lazy to run with VM args, use Reflection */ Field f = Unsafe.class.getDeclaredField("theUnsafe"); f.setAccessible(true); /* get array address */ Unsafe unsafe = (Unsafe)f.get(null); byte four_bytes[] = {25, 25, 25, 25}; Object trash[] = new Object[] { four_bytes }; long base_offset_bytes = unsafe.arrayBaseOffset(Object[].class); long four_bytes_address = unsafe.getLong(trash, base_offset_bytes); // <- this is it long ints_addr = unsafe.allocateMemory(16); // allocate 4 * 4 bytes, i.e. 4 ints unsafe.copyMemory(four_bytes_address + base_offset_bytes, ints_addr, 4); // copy all four bytes for(int i = 0; i < 4; i++) { System.out.println(unsafe.getInt(ints_addr + i)); //run through entire allocated int[], // get some intestines } System.out.println("*****************************"); for(int i = 0; i < 16; i++) { System.out.println(unsafe.getByte(ints_addr + i)); //run through entire allocated int[], // get some intestines } } }
long ptr = unsafe.allocateMemory(size); long writePtr = ptr;
static ByteBuffer allocateDirectNoCleaner(int capacity) { // Calling malloc with capacity of 0 may return a null ptr or a memory address that can be used. // Just use 1 to make it safe to use in all cases: // See: http://pubs.opengroup.org/onlinepubs/009695399/functions/malloc.html return newDirectBuffer(UNSAFE.allocateMemory(Math.max(1, capacity)), capacity); }