netty源碼分析(25)- ByteBuf

本節(jié)開始學(xué)習(xí)netty的內(nèi)存分配機制,搜先是ByteBuf。

作為一個容器,源碼中的如下。有三塊區(qū)域

  • discardable bytes:無效空間(已經(jīng)讀取過的空間),可丟棄字節(jié)的區(qū)域,由readerIndex指針控制
  • readable bytes:內(nèi)容空間,可讀字節(jié)的區(qū)域,由readerIndexwriterIndex指針控制控制
  • writable bytes:空閑空間,可寫入字節(jié)的區(qū)域,由writerIndex指針和capacity容量控制
 * <pre>
 *      +-------------------+------------------+------------------+
 *      | discardable bytes |  readable bytes  |  writable bytes  |
 *      |                   |     (CONTENT)    |                  |
 *      +-------------------+------------------+------------------+
 *      |                   |                  |                  |
 *      0      <=      readerIndex   <=   writerIndex    <=    capacity
 * </pre>
  • ByteBuf還定義了抽象方法maxCapacity,用于子類可增加一個最大容量的指針。
    public abstract int maxCapacity();
  • ByteBuf定義了豐富的api,主要是讀寫和設(shè)置以及一些指針和容量的操作,詳情參考《ByteBuf 主要API》
  1. 讀邏輯在AbstractByteBuf骨架類,主要就是操作readerIndex指針,而具體怎么讀著交給具體子類實現(xiàn),暴露_getByte(i);給子類即可。
    @Override
    public byte readByte() {
        checkReadableBytes0(1);
        int i = readerIndex;
        byte b = _getByte(i);
        readerIndex = i + 1;
        return b;
    }
  1. 寫邏輯在AbstractByteBuf骨架類,把當(dāng)前的值寫到ByteBuf里面,擦歐總writerIndex指針,并且暴露_setByte(writerIndex++, value);給子類去具體實現(xiàn)寫邏輯
    @Override
    public ByteBuf writeByte(int value) {
        ensureWritable0(1);
        _setByte(writerIndex++, value);
        return this;
    }
  • ByteBuf分類


    ByteBuf家族
  1. pooled和unpooled

Pooled:每次都從預(yù)先分配好的內(nèi)存中去取出一段連續(xù)內(nèi)存封裝成一個ByteBuf給應(yīng)用程序使用
Unpooled:每次分配內(nèi)存的時候,直接調(diào)用系統(tǒng)api,向操作系統(tǒng)申請一塊內(nèi)存

  1. Unsafe和非Unsafe

jdk中有Unsafe對象可以直接拿到對象的內(nèi)存地址,并且基于這個內(nèi)存地址進(jìn)行讀寫操作。那么對應(yīng)的分類的區(qū)別就是是否可以拿到j(luò)dk底層的Unsafe進(jìn)行讀寫操作了。

  1. 跟進(jìn)PooledUnsafeHeapByteBuf
    @Override
    protected byte _getByte(int index) {
        //memory:內(nèi)存首地址
        //idx(index):偏移量
        return UnsafeByteBufUtil.getByte(memory, idx(index));
    } 

    static byte getByte(byte[] array, int index) {
        return PlatformDependent.getByte(array, index);
    }

    public static byte getByte(byte[] data, int index) {
        return PlatformDependent0.getByte(data, index);
    }

    static byte getByte(byte[] data, int index) {
        //通過UNSAFE去獲取
        return UNSAFE.getByte(data, BYTE_ARRAY_BASE_OFFSET + index);
    }
  1. 跟進(jìn)PooledHeapByteBuf
    @Override
    protected byte _getByte(int index) {
        return HeapByteBufUtil.getByte(memory, idx(index));
    }

    static byte getByte(byte[] memory, int index) {
        //直接拿到一個數(shù)組
        return memory[index];
    }
  1. Head和Direct

Head:是調(diào)用jvm的堆內(nèi)存進(jìn)行分配的,需要被gc進(jìn)行管理
Direct:是調(diào)用jdk的api進(jìn)行內(nèi)存分配,不受jvm控制,不會參與到gc的過程

1.跟進(jìn)UnpooledHeapByteBuf,Head其實就是維持了一個代表內(nèi)存的數(shù)組

    private final ByteBufAllocator alloc;
    //依賴byte數(shù)組,所有內(nèi)存相關(guān)的操作都在這.
    //持有內(nèi)存數(shù)組(堆)
    byte[] array;
    private ByteBuffer tmpNioBuf;

    @Override
    protected byte _getByte(int index) {
        //直接傳入內(nèi)存數(shù)組
        return HeapByteBufUtil.getByte(array, index);
    }

    static byte getByte(byte[] memory, int index) {
        //直接拿到一個數(shù)組
        return memory[index];
    }

  1. 跟進(jìn)UnpooledDirectByteBuf,Direct其實就是調(diào)用jdk的直接內(nèi)存對象DirectByteBuffer
import java.nio.ByteBuffer;

    private final ByteBufAllocator alloc;
    //依賴jdk底層的ByteBuffer
    private ByteBuffer buffer;
    private ByteBuffer tmpNioBuf;
    private int capacity;
    private boolean doNotFree;

    @Override
    public byte getByte(int index) {
        ensureAccessible();
        return _getByte(index);
    }

    @Override
    protected byte _getByte(int index) {
        //使用buffer
        return buffer.get(index);
    }

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容