Java Code Examples for io.netty.util.internal.PlatformDependent#directBufferAddress()

The following examples show how to use io.netty.util.internal.PlatformDependent#directBufferAddress() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UnpooledUnsafeDirectByteBuf.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
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();
    }
 
Example 2
Source File: UnsafeByteBufUtil.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
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.我们需要检查dst是否已经就绪,这样我们就不会使用不安全的方法在其中写入内容。
            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());
    }
}
 
Example 3
Source File: IovArray.java    From netty4.0.27Learn with Apache License 2.0 6 votes vote down vote up
/**
 * Try to add the given {@link CompositeByteBuf}. Returns {@code true} on success,
 * {@code false} otherwise.
 */
boolean add(CompositeByteBuf buf) {
    ByteBuffer[] buffers = buf.nioBuffers();
    if (count + buffers.length >= Native.IOV_MAX) {
        // No more room!
        return false;
    }
    for (int i = 0; i < buffers.length; i++) {
        ByteBuffer nioBuffer = buffers[i];
        int offset = nioBuffer.position();
        int len = nioBuffer.limit() - nioBuffer.position();
        if (len == 0) {
            // No need to add an empty buffer so just continue
            continue;
        }
        long addr = PlatformDependent.directBufferAddress(nioBuffer);

        add(addr, offset, len);
    }
    return true;
}
 
Example 4
Source File: ReadOnlyUnsafeDirectByteBuf.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
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.//使用buffer作为超类将分割在ByteBuffer中传递的内存地址
//        如果位置!= 0,则可能不同。
        memoryAddress = PlatformDependent.directBufferAddress(buffer);
    }
 
Example 5
Source File: PoolArena.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
private int offsetCacheLine(ByteBuffer memory) {
            // We can only calculate the offset if Unsafe is present as otherwise directBufferAddress(...) will
            // throw an NPE.//我们只能计算偏移,如果不安全的存在,否则directBufferAddress(…)将
//扔一个NPE。
            return HAS_UNSAFE ?
                    (int) (PlatformDependent.directBufferAddress(memory) & directMemoryCacheAlignmentMask) : 0;
        }
 
Example 6
Source File: UnsafeByteBufUtil.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
static void setBytes(AbstractByteBuf buf, long addr, int index, ByteBuffer src) {
    buf.checkIndex(index, src.remaining());

    int length = src.remaining();
    if (length == 0) {
        return;
    }

    if (src.isDirect()) {
        // Copy from direct memory
        long srcAddress = PlatformDependent.directBufferAddress(src);
        PlatformDependent.copyMemory(srcAddress + src.position(), addr, src.remaining());
        src.position(src.position() + length);
    } else if (src.hasArray()) {
        // Copy from array
        PlatformDependent.copyMemory(src.array(), src.arrayOffset() + src.position(), addr, length);
        src.position(src.position() + length);
    } else {
        ByteBuf tmpBuf = buf.alloc().heapBuffer(length);
        try {
            byte[] tmp = tmpBuf.array();
            src.get(tmp, tmpBuf.arrayOffset(), length); // moves the src position too
            PlatformDependent.copyMemory(tmp, tmpBuf.arrayOffset(), addr, length);
        } finally {
            tmpBuf.release();
        }
    }
}
 
Example 7
Source File: UnpooledUnsafeDirectByteBuf.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
private void setByteBuffer(ByteBuffer buffer) {
    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();
}
 
Example 8
Source File: MappedFile.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
private MappedFile(FileChannel channel, MappedByteBuffer byteBuffer, int position, int length) throws IOException {
   this.channel = channel;
   this.buffer = byteBuffer;
   this.position = position;
   this.length = length;
   this.byteBufWrapper = Unpooled.wrappedBuffer(buffer);
   this.channelBufferWrapper = new ChannelBufferWrapper(this.byteBufWrapper, false);
   this.address = PlatformDependent.directBufferAddress(buffer);
}
 
Example 9
Source File: MappedFile.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
/**
 * Writes a sequence of bytes to this file from the given buffer.
 * <p>
 * <p> Bytes are written starting at this file's current position,
 */
public void write(ByteBuffer src, int srcStart, int srcLength) throws IOException {
   final int nextPosition = this.position + srcLength;
   checkCapacity(nextPosition);
   final long destAddress = this.address + this.position;
   if (src.isDirect()) {
      final long srcAddress = PlatformDependent.directBufferAddress(src) + srcStart;
      PlatformDependent.copyMemory(srcAddress, destAddress, srcLength);
   } else {
      final byte[] srcArray = src.array();
      PlatformDependent.copyMemory(srcArray, srcStart, destAddress, srcLength);
   }
   rawMovePositionAndLength(nextPosition);
}
 
Example 10
Source File: PooledUnsafeDirectByteBuf.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
private void initMemoryAddress() {
    memoryAddress = PlatformDependent.directBufferAddress(memory) + offset;
}
 
Example 11
Source File: ReadOnlyUnsafeDirectByteBuf.java    From netty4.0.27Learn with Apache License 2.0 4 votes vote down vote up
ReadOnlyUnsafeDirectByteBuf(ByteBufAllocator allocator, ByteBuffer buffer) {
    super(allocator, buffer);
    memoryAddress = PlatformDependent.directBufferAddress(buffer);
}
 
Example 12
Source File: PooledUnsafeDirectByteBuf.java    From netty4.0.27Learn with Apache License 2.0 4 votes vote down vote up
private void initMemoryAddress() {
    memoryAddress = PlatformDependent.directBufferAddress(memory) + offset;
}