ReadOnlyUnsafeDirectByteBuf(ByteBufAllocator allocator, ByteBuffer byteBuffer) { super(allocator, byteBuffer); // Use buffer as the super class will slice the passed in ByteBuffer which means the memoryAddress // may be different if the position != 0. memoryAddress = PlatformDependent.directBufferAddress(buffer); }
int offsetCacheLine(ByteBuffer memory) { // We can only calculate the offset if Unsafe is present as otherwise directBufferAddress(...) will // throw an NPE. int remainder = HAS_UNSAFE ? (int) (PlatformDependent.directBufferAddress(memory) & directMemoryCacheAlignmentMask) : 0; // offset = alignment - address & (alignment - 1) return directMemoryCacheAlignment - remainder; }
private void initMemoryAddress() { memoryAddress = PlatformDependent.directBufferAddress(memory) + offset; }
int offsetCacheLine(ByteBuffer memory) { // We can only calculate the offset if Unsafe is present as otherwise directBufferAddress(...) will // throw an NPE. int remainder = HAS_UNSAFE ? (int) (PlatformDependent.directBufferAddress(memory) & directMemoryCacheAlignmentMask) : 0; // offset = alignment - address & (alignment - 1) return directMemoryCacheAlignment - remainder; }
ReadOnlyUnsafeDirectByteBuf(ByteBufAllocator allocator, ByteBuffer byteBuffer) { super(allocator, byteBuffer); // Use buffer as the super class will slice the passed in ByteBuffer which means the memoryAddress // may be different if the position != 0. memoryAddress = PlatformDependent.directBufferAddress(buffer); }
private void initMemoryAddress() { memoryAddress = PlatformDependent.directBufferAddress(memory) + offset; }
ReadOnlyUnsafeDirectByteBuf(ByteBufAllocator allocator, ByteBuffer byteBuffer) { super(allocator, byteBuffer); // Use buffer as the super class will slice the passed in ByteBuffer which means the memoryAddress // may be different if the position != 0. memoryAddress = PlatformDependent.directBufferAddress(buffer); }
private int offsetCacheLine(ByteBuffer memory) { // We can only calculate the offset if Unsafe is present as otherwise directBufferAddress(...) will // throw an NPE. return HAS_UNSAFE ? (int) (PlatformDependent.directBufferAddress(memory) & directMemoryCacheAlignmentMask) : 0; }
private void initMemoryAddress() { memoryAddress = PlatformDependent.directBufferAddress(memory) + offset; }
@Override protected void memoryCopy(ByteBuffer src, int srcOffset, ByteBuffer dst, int dstOffset, int length) { if (length == 0) { return; } if (HAS_UNSAFE) { PlatformDependent.copyMemory( PlatformDependent.directBufferAddress(src) + srcOffset, PlatformDependent.directBufferAddress(dst) + dstOffset, length); } else { // We must duplicate the NIO buffers because they may be accessed by other Netty buffers. src = src.duplicate(); dst = dst.duplicate(); src.position(srcOffset).limit(srcOffset + length); dst.position(dstOffset); dst.put(src); } } }
final void setByteBuffer(ByteBuffer buffer, boolean tryFree) { if (tryFree) { ByteBuffer oldBuffer = this.buffer; if (oldBuffer != null) { if (doNotFree) { doNotFree = false; } else { freeDirect(oldBuffer); } } } this.buffer = buffer; memoryAddress = PlatformDependent.directBufferAddress(buffer); tmpNioBuf = null; capacity = buffer.remaining(); }
@Override protected void memoryCopy(ByteBuffer src, int srcOffset, ByteBuffer dst, int dstOffset, int length) { if (length == 0) { return; } if (HAS_UNSAFE) { PlatformDependent.copyMemory( PlatformDependent.directBufferAddress(src) + srcOffset, PlatformDependent.directBufferAddress(dst) + dstOffset, length); } else { // We must duplicate the NIO buffers because they may be accessed by other Netty buffers. src = src.duplicate(); dst = dst.duplicate(); src.position(srcOffset).limit(srcOffset + length); dst.position(dstOffset); dst.put(src); } } }
final void setByteBuffer(ByteBuffer buffer, boolean tryFree) { if (tryFree) { ByteBuffer oldBuffer = this.buffer; if (oldBuffer != null) { if (doNotFree) { doNotFree = false; } else { freeDirect(oldBuffer); } } } this.buffer = buffer; memoryAddress = PlatformDependent.directBufferAddress(buffer); tmpNioBuf = null; capacity = buffer.remaining(); }
private static long bufferAddress(ByteBuffer b) { assert b.isDirect(); if (PlatformDependent.hasUnsafe()) { return PlatformDependent.directBufferAddress(b); } return Buffer.address(b); }
/** * Returns the memory address of the given direct {@link ByteBuffer}. */ public static long memoryAddress(ByteBuffer buffer) { assert buffer.isDirect(); if (PlatformDependent.hasUnsafe()) { return PlatformDependent.directBufferAddress(buffer); } return memoryAddress0(buffer); }
static void getBytes(AbstractByteBuf buf, long addr, int index, ByteBuffer dst) { buf.checkIndex(index, dst.remaining()); if (dst.remaining() == 0) { return; } if (dst.isDirect()) { if (dst.isReadOnly()) { // We need to check if dst is ready-only so we not write something in it by using Unsafe. throw new ReadOnlyBufferException(); } // Copy to direct memory long dstAddress = PlatformDependent.directBufferAddress(dst); PlatformDependent.copyMemory(addr, dstAddress + dst.position(), dst.remaining()); dst.position(dst.position() + dst.remaining()); } else if (dst.hasArray()) { // Copy to array PlatformDependent.copyMemory(addr, dst.array(), dst.arrayOffset() + dst.position(), dst.remaining()); dst.position(dst.position() + dst.remaining()); } else { dst.put(buf.nioBuffer()); } }
static void getBytes(AbstractByteBuf buf, long addr, int index, ByteBuffer dst) { buf.checkIndex(index, dst.remaining()); if (dst.remaining() == 0) { return; } if (dst.isDirect()) { if (dst.isReadOnly()) { // We need to check if dst is ready-only so we not write something in it by using Unsafe. throw new ReadOnlyBufferException(); } // Copy to direct memory long dstAddress = PlatformDependent.directBufferAddress(dst); PlatformDependent.copyMemory(addr, dstAddress + dst.position(), dst.remaining()); dst.position(dst.position() + dst.remaining()); } else if (dst.hasArray()) { // Copy to array PlatformDependent.copyMemory(addr, dst.array(), dst.arrayOffset() + dst.position(), dst.remaining()); dst.position(dst.position() + dst.remaining()); } else { dst.put(buf.nioBuffer()); } }
private static long bufferAddress(ByteBuffer b) { assert b.isDirect(); if (PlatformDependent.hasUnsafe()) { return PlatformDependent.directBufferAddress(b); } return Buffer.address(b); }
static void setBytes(AbstractByteBuf buf, long addr, int index, ByteBuffer src) { final int length = src.remaining(); if (length == 0) { return; } if (src.isDirect()) { buf.checkIndex(index, length); // Copy from direct memory long srcAddress = PlatformDependent.directBufferAddress(src); PlatformDependent.copyMemory(srcAddress + src.position(), addr, length); src.position(src.position() + length); } else if (src.hasArray()) { buf.checkIndex(index, length); // Copy from array PlatformDependent.copyMemory(src.array(), src.arrayOffset() + src.position(), addr, length); src.position(src.position() + length); } else { if (length < 8) { setSingleBytes(buf, addr, index, src, length); } else { //no need to checkIndex: internalNioBuffer is already taking care of it assert buf.nioBufferCount() == 1; final ByteBuffer internalBuffer = buf.internalNioBuffer(index, length); internalBuffer.put(src); } } }
static void setBytes(AbstractByteBuf buf, long addr, int index, ByteBuffer src) { final int length = src.remaining(); if (length == 0) { return; } if (src.isDirect()) { buf.checkIndex(index, length); // Copy from direct memory long srcAddress = PlatformDependent.directBufferAddress(src); PlatformDependent.copyMemory(srcAddress + src.position(), addr, length); src.position(src.position() + length); } else if (src.hasArray()) { buf.checkIndex(index, length); // Copy from array PlatformDependent.copyMemory(src.array(), src.arrayOffset() + src.position(), addr, length); src.position(src.position() + length); } else { if (length < 8) { setSingleBytes(buf, addr, index, src, length); } else { //no need to checkIndex: internalNioBuffer is already taking care of it assert buf.nioBufferCount() == 1; final ByteBuffer internalBuffer = buf.internalNioBuffer(index, length); internalBuffer.put(src); } } }