美文网首页程序员
netty源码分析(26)- ByteBufAllocator分

netty源码分析(26)- ByteBufAllocator分

作者: Jorgezhong | 来源:发表于2019-03-05 09:54 被阅读0次

    上一节简单理解了以下ByteBuf的结构。详细的api还需要自己夺取尝试。

    本节学些ByteBufAllocator,内存分配器(管理器)

    内存分配器
    • 查看ByteBufAllocator,作为顶层接口,它根据内存分配的类型定制了一些分配方法,主要还是根据是否是堆内存来进行分配。
        //根据具体的子类实现决定分配内存是direct还是head
        ByteBuf buffer();
        ByteBuf buffer(int var1);
        ByteBuf buffer(int var1, int var2);
        
        //分配一个是适用于IO的直接内存
        ByteBuf ioBuffer();
        ByteBuf ioBuffer(int var1);
        ByteBuf ioBuffer(int var1, int var2);
        
        //分配一块head内存
        ByteBuf heapBuffer();
        ByteBuf heapBuffer(int var1);
        ByteBuf heapBuffer(int var1, int var2);
        
        //分配一块direct内存
        ByteBuf directBuffer();
        ByteBuf directBuffer(int var1);
        ByteBuf directBuffer(int var1, int var2);
        
        //组合缓冲区
        CompositeByteBuf compositeBuffer();
        CompositeByteBuf compositeBuffer(int var1);
        CompositeByteBuf compositeHeapBuffer();
       
        CompositeByteBuf compositeHeapBuffer(int var1);
        CompositeByteBuf compositeDirectBuffer();
        CompositeByteBuf compositeDirectBuffer(int var1);
    
        boolean isDirectBufferPooled();
        int calculateNewCapacity(int var1, int var2);
    
    • AbstractByteBufAllocator是顶层接口ByteBufAllocator一个实现,作为一个抽象的类,骨架。看一下他的buffer()方法。其实在AbstractByteBufAllocator层面是没有定义Pooled和Unpooled以及Safe和Unsafe类别的内存分配,这一层只开放了一个抽象方法,具体的Pooled和Unpooled交由子类实现。
        @Override
        public ByteBuf buffer() {
            //是否是direct内存,调用具体不同的实现
            if (directByDefault) {
                //分配直接内存
                return directBuffer();
            }
            //分配对内存
            return heapBuffer();
        }
    
        @Override
        public ByteBuf directBuffer() {
            //参数:默认的容量256,最大容量Integer.MAX_VALUE;
            return directBuffer(DEFAULT_INITIAL_CAPACITY, DEFAULT_MAX_CAPACITY);
        }
    
        @Override
        public ByteBuf directBuffer(int initialCapacity, int maxCapacity) {
            //检验
            if (initialCapacity == 0 && maxCapacity == 0) {
                return emptyBuf;
            }
            //校验参数设置是否正确
            validate(initialCapacity, maxCapacity);
            //创建DirectBuffer
            return newDirectBuffer(initialCapacity, maxCapacity);
        }
    
        /**
         * Create a direct {@link ByteBuf} with the given initialCapacity and maxCapacity.
         */
        protected abstract ByteBuf newDirectBuffer(int initialCapacity, int maxCapacity);
    
    
    • 关于Safe和Unsafe类别的内存分配,netty是根据jdk底层是否由封装的unsafe来进行分配的,查看UnpooledByteBufAllocator#newHeapBuffer()
        @Override
        protected ByteBuf newHeapBuffer(int initialCapacity, int maxCapacity) {
            //判断是有unsafe来分配
            return PlatformDependent.hasUnsafe() ?
                    new InstrumentedUnpooledUnsafeHeapByteBuf(this, initialCapacity, maxCapacity) :
                    new InstrumentedUnpooledHeapByteBuf(this, initialCapacity, maxCapacity);
        }
    

    相关文章

      网友评论

        本文标题:netty源码分析(26)- ByteBufAllocator分

        本文链接:https://www.haomeiwen.com/subject/mcoguqtx.html