public long address() { return ((DirectBuffer) buffer).address(); }
public SingleByteBuff(ByteBuffer buf) { this.buf = buf; if (buf.hasArray()) { this.unsafeOffset = UnsafeAccess.BYTE_ARRAY_BASE_OFFSET + buf.arrayOffset(); this.unsafeRef = buf.array(); } else { this.unsafeOffset = ((DirectBuffer) buf).address(); } }
/** * 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); }
/** * 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); }
/** * 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; }
@Override public int compareTo(byte[] buf1, int o1, int l1, ByteBuffer buf2, int o2, int l2) { long offset2Adj; Object refObj2 = null; if (buf2.isDirect()) { offset2Adj = o2 + ((DirectBuffer)buf2).address(); } else { offset2Adj = o2 + buf2.arrayOffset() + UnsafeAccess.BYTE_ARRAY_BASE_OFFSET; refObj2 = buf2.array(); } return compareToUnsafe(buf1, o1 + UnsafeAccess.BYTE_ARRAY_BASE_OFFSET, l1, refObj2, offset2Adj, l2); }
/** * 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; }
/** * 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; } /**
@Override public int compareTo(ByteBuffer buf1, int o1, int l1, ByteBuffer buf2, int o2, int l2) { long offset1Adj, offset2Adj; Object refObj1 = null, refObj2 = null; if (buf1.isDirect()) { offset1Adj = o1 + ((DirectBuffer) buf1).address(); } else { offset1Adj = o1 + buf1.arrayOffset() + UnsafeAccess.BYTE_ARRAY_BASE_OFFSET; refObj1 = buf1.array(); } if (buf2.isDirect()) { offset2Adj = o2 + ((DirectBuffer) buf2).address(); } else { offset2Adj = o2 + buf2.arrayOffset() + UnsafeAccess.BYTE_ARRAY_BASE_OFFSET; refObj2 = buf2.array(); } return compareToUnsafe(refObj1, offset1Adj, l1, refObj2, offset2Adj, l2); } }
/** * 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; }
/** * Copies the bytes from given array's offset to length part into the given buffer. * @param src * @param srcOffset * @param dest * @param destOffset * @param length */ public static void copy(byte[] src, int srcOffset, ByteBuffer dest, int destOffset, int length) { long destAddress = destOffset; Object destBase = null; if (dest.isDirect()) { destAddress = destAddress + ((DirectBuffer) dest).address(); } else { destAddress = destAddress + BYTE_ARRAY_BASE_OFFSET + dest.arrayOffset(); destBase = dest.array(); } long srcAddress = srcOffset + BYTE_ARRAY_BASE_OFFSET; unsafeCopy(src, srcAddress, destBase, destAddress, length); }
/** * Copies specified number of bytes from given offset of {@code src} buffer into the {@code dest} * buffer. * * @param src * @param srcOffset * @param dest * @param destOffset * @param length */ public static void copy(ByteBuffer src, int srcOffset, ByteBuffer dest, int destOffset, int length) { long srcAddress, destAddress; Object srcBase = null, destBase = null; if (src.isDirect()) { srcAddress = srcOffset + ((DirectBuffer) src).address(); } else { srcAddress = (long) srcOffset + src.arrayOffset() + BYTE_ARRAY_BASE_OFFSET; srcBase = src.array(); } if (dest.isDirect()) { destAddress = destOffset + ((DirectBuffer) dest).address(); } else { destAddress = destOffset + BYTE_ARRAY_BASE_OFFSET + dest.arrayOffset(); destBase = dest.array(); } unsafeCopy(srcBase, srcAddress, destBase, destAddress, length); }
/** * Copies specified number of bytes from given offset of {@code src} ByteBuffer to the * {@code dest} array. * * @param src * @param srcOffset * @param dest * @param destOffset * @param length */ public static void copy(ByteBuffer src, int srcOffset, byte[] dest, int destOffset, int length) { long srcAddress = srcOffset; Object srcBase = null; if (src.isDirect()) { srcAddress = srcAddress + ((DirectBuffer) src).address(); } else { srcAddress = srcAddress + BYTE_ARRAY_BASE_OFFSET + src.arrayOffset(); srcBase = src.array(); } long destAddress = destOffset + BYTE_ARRAY_BASE_OFFSET; unsafeCopy(srcBase, srcAddress, dest, destAddress, length); }
public void munlock() { final long beginTime = System.currentTimeMillis(); final long address = ((DirectBuffer) (this.mappedByteBuffer)).address(); Pointer pointer = new Pointer(address); int ret = LibC.INSTANCE.munlock(pointer, new NativeLong(this.fileSize)); log.info("munlock {} {} {} ret = {} time consuming = {}", address, this.fileName, this.fileSize, ret, System.currentTimeMillis() - beginTime); }
/** * It's a heavy init method. */ public void init() { for (int i = 0; i < poolSize; i++) { ByteBuffer byteBuffer = ByteBuffer.allocateDirect(fileSize); final long address = ((DirectBuffer) byteBuffer).address(); Pointer pointer = new Pointer(address); LibC.INSTANCE.mlock(pointer, new NativeLong(fileSize)); availableBuffers.offer(byteBuffer); } }
public void destroy() { for (ByteBuffer byteBuffer : availableBuffers) { final long address = ((DirectBuffer) byteBuffer).address(); Pointer pointer = new Pointer(address); LibC.INSTANCE.munlock(pointer, new NativeLong(fileSize)); } }
public void mlock() { final long beginTime = System.currentTimeMillis(); final long address = ((DirectBuffer) (this.mappedByteBuffer)).address(); Pointer pointer = new Pointer(address); { int ret = LibC.INSTANCE.mlock(pointer, new NativeLong(this.fileSize)); log.info("mlock {} {} {} ret = {} time consuming = {}", address, this.fileName, this.fileSize, ret, System.currentTimeMillis() - beginTime); } { int ret = LibC.INSTANCE.madvise(pointer, new NativeLong(this.fileSize), LibC.MADV_WILLNEED); log.info("madvise {} {} {} ret = {} time consuming = {}", address, this.fileName, this.fileSize, ret, System.currentTimeMillis() - beginTime); } }
protected void index0(long index, long startPosition, long endPosition) { this.index = index; this.startPosition = startPosition; MappedMemory.release(mappedMemory); mappedMemory = chronicle.acquireDataBuffer(startPosition); buffer = mappedMemory.buffer(); long address = ((DirectBuffer) buffer).address(); start = position = address + chronicle.positionInBuffer(startPosition); limit = address + chronicle.positionInBuffer(endPosition - 1) + 1; assert limit > start && position < limit && endPosition > startPosition; }