/** * Reads bytes at the given offset as a long value. * @param buf * @param offset * @return long value at offset */ static long getAsLong(ByteBuffer buf, int offset) { if (buf.isDirect()) { return theUnsafe.getLong(((DirectBuffer) buf).address() + offset); } return theUnsafe.getLong(buf.array(), BYTE_ARRAY_BASE_OFFSET + buf.arrayOffset() + offset); }
public static void main(String[] args) throws IOException { // ByteBuffer bb = ByteBuffer.allocateDirect(1 << 30).order(ByteOrder.nativeOrder()); RandomAccessFile raf = new RandomAccessFile("deleteme", "rw"); ByteBuffer bb = raf.getChannel().map(FileChannel.MapMode.READ_WRITE, 0, 1 << 20).order(ByteOrder.nativeOrder()); long address = ((DirectBuffer) bb).address(); System.out.printf("address: %x%n", address); long l = UnsafeDemo.UNSAFE.getLong(address); System.out.printf("value was : %x%n", l); UnsafeDemo.UNSAFE.putLong(address, l + 0x101); UnsafeDemo.UNSAFE.getLong(0L); System.out.printf("value written: %x%n", bb.getLong(0)); } }
public SharedCASPingPongMain() throws IOException { RandomAccessFile raf = new RandomAccessFile("deleteme", "rw"); MappedByteBuffer map = raf.getChannel().map(FileChannel.MapMode.READ_WRITE, 0, OS.pageSize()); address = ((DirectBuffer) map).address(); UNSAFE.putLong(address + COUNTER_OFFSET, 0L); UNSAFE.putInt(address + TOGGLE_OFFSET, 0); }
/** * Reads bytes at the given offset as an int value. * @param buf * @param offset * @return int value at offset */ static int getAsInt(ByteBuffer buf, int offset) { if (buf.isDirect()) { return theUnsafe.getInt(((DirectBuffer) buf).address() + offset); } return theUnsafe.getInt(buf.array(), BYTE_ARRAY_BASE_OFFSET + buf.arrayOffset() + offset); }
/** * Returns the byte at the given offset * @param buf the buffer to read * @param offset the offset at which the byte has to be read * @return the byte at the given offset */ public static byte toByte(ByteBuffer buf, int offset) { if (buf.isDirect()) { return theUnsafe.getByte(((DirectBuffer) buf).address() + offset); } else { return theUnsafe.getByte(buf.array(), BYTE_ARRAY_BASE_OFFSET + buf.arrayOffset() + offset); } }
/** * Put a byte value out to the specified BB position in big-endian format. * @param buf the byte buffer * @param offset position in the buffer * @param b byte to write out * @return incremented offset */ public static int putByte(ByteBuffer buf, int offset, byte b) { if (buf.isDirect()) { theUnsafe.putByte(((DirectBuffer) buf).address() + offset, b); } else { theUnsafe.putByte(buf.array(), BYTE_ARRAY_BASE_OFFSET + buf.arrayOffset() + offset, b); } return offset + 1; }
/** * Put an int value out to the specified ByteBuffer offset in big-endian format. * @param buf the ByteBuffer to write to * @param offset offset in the ByteBuffer * @param val int to write out * @return incremented offset */ public static int putInt(ByteBuffer buf, int offset, int val) { if (LITTLE_ENDIAN) { val = Integer.reverseBytes(val); } if (buf.isDirect()) { theUnsafe.putInt(((DirectBuffer) buf).address() + offset, val); } else { theUnsafe.putInt(buf.array(), offset + buf.arrayOffset() + BYTE_ARRAY_BASE_OFFSET, val); } return offset + Bytes.SIZEOF_INT; }
/** * Reads bytes at the given offset as a short value. * @param buf * @param offset * @return short value at offset */ static short getAsShort(ByteBuffer buf, int offset) { if (buf.isDirect()) { return theUnsafe.getShort(((DirectBuffer) buf).address() + offset); } return theUnsafe.getShort(buf.array(), BYTE_ARRAY_BASE_OFFSET + buf.arrayOffset() + offset); }
/** * Put a long value out to the specified BB position in big-endian format. * @param buf the byte buffer * @param offset position in the buffer * @param val long to write out * @return incremented offset */ public static int putLong(ByteBuffer buf, int offset, long val) { if (LITTLE_ENDIAN) { val = Long.reverseBytes(val); } if (buf.isDirect()) { theUnsafe.putLong(((DirectBuffer) buf).address() + offset, val); } else { theUnsafe.putLong(buf.array(), BYTE_ARRAY_BASE_OFFSET + buf.arrayOffset() + offset, val); } return offset + Bytes.SIZEOF_LONG; } /**
public int putBytes(final int index, final ByteBuffer srcBuffer, final int srcIndex, final int length) { int count = Math.min(length, capacity - index); count = Math.min(count, srcBuffer.capacity() - srcIndex); boundsCheck(index, count); final byte[] srcByteArray; final long srcBaseOffset; if (srcBuffer.hasArray()) { srcByteArray = srcBuffer.array(); srcBaseOffset = ARRAY_BASE_OFFSET + srcBuffer.arrayOffset() + srcIndex; } else { srcByteArray = null; srcBaseOffset = ((sun.nio.ch.DirectBuffer) srcBuffer).address(); } UNSAFE.copyMemory(srcByteArray, srcBaseOffset + srcIndex, byteArray, addressOffset + index, count); return count; }
/** * Put a short value out to the specified BB position in big-endian format. * @param buf the byte buffer * @param offset position in the buffer * @param val short to write out * @return incremented offset */ public static int putShort(ByteBuffer buf, int offset, short val) { if (LITTLE_ENDIAN) { val = Short.reverseBytes(val); } if (buf.isDirect()) { theUnsafe.putShort(((DirectBuffer) buf).address() + offset, val); } else { theUnsafe.putShort(buf.array(), BYTE_ARRAY_BASE_OFFSET + buf.arrayOffset() + offset, val); } return offset + Bytes.SIZEOF_SHORT; }
@Override public boolean compareAndSwapLong(long offset, long expected, long x) { if (buffer instanceof DirectBuffer) return NativeBytes.UNSAFE.compareAndSwapLong(null, ((DirectBuffer) buffer).address() + offset, expected, x); return NativeBytes.UNSAFE.compareAndSwapLong(buffer.array(), NativeBytes.BYTES_OFFSET + offset, expected, x); }
@Override public boolean compareAndSwapInt(long offset, int expected, int x) { if (buffer instanceof DirectBuffer) return NativeBytes.UNSAFE.compareAndSwapInt(null, ((DirectBuffer) buffer).address() + offset, expected, x); return NativeBytes.UNSAFE.compareAndSwapInt(buffer.array(), NativeBytes.BYTES_OFFSET + offset, expected, x); }
public long address() { return ((DirectBuffer) buffer).address(); }
private static int getOffset(ByteBuffer slice) { final sun.nio.ch.DirectBuffer _slice = (sun.nio.ch.DirectBuffer) slice; final sun.nio.ch.DirectBuffer parent = (sun.nio.ch.DirectBuffer) _slice.attachment(); // java<7: viewedBuffer(); return (int) (_slice.address() - parent.address()); }
private static Object getViewed(ByteBuffer buffer) { return ((sun.nio.ch.DirectBuffer) buffer).attachment(); // java<7: viewedBuffer(); }
/** * Reads bytes at the given offset as an int value. * @param buf * @param offset * @return int value at offset */ static int getAsInt(ByteBuffer buf, int offset) { if (buf.isDirect()) { return theUnsafe.getInt(((DirectBuffer) buf).address() + offset); } return theUnsafe.getInt(buf.array(), BYTE_ARRAY_BASE_OFFSET + buf.arrayOffset() + offset); }
/** * Returns the byte at the given offset * @param buf the buffer to read * @param offset the offset at which the byte has to be read * @return the byte at the given offset */ public static byte toByte(ByteBuffer buf, int offset) { if (buf.isDirect()) { return theUnsafe.getByte(((DirectBuffer) buf).address() + offset); } else { return theUnsafe.getByte(buf.array(), BYTE_ARRAY_BASE_OFFSET + buf.arrayOffset() + offset); } }
/** * Put a byte value out to the specified BB position in big-endian format. * @param buf the byte buffer * @param offset position in the buffer * @param b byte to write out * @return incremented offset */ public static int putByte(ByteBuffer buf, int offset, byte b) { if (buf.isDirect()) { theUnsafe.putByte(((DirectBuffer) buf).address() + offset, b); } else { theUnsafe.putByte(buf.array(), BYTE_ARRAY_BASE_OFFSET + buf.arrayOffset() + offset, b); } return offset + 1; }