ByteBufAllocator是ByteBuf的分配器,负责创建ByteBuf对象,他主要有三个子类:
- PreferHeapByteBufAllocator ,倾向创建 Heap ByteBuf 的分配器。
- PooledByteBufAllocator ,基于内存池的 ByteBuf 的分配器。
- UnpooledByteBufAllocator ,普通的 ByteBuf 的分配器。
这里先看PreferHeapByteBufAllocator。
buffer() 方法,创建一个 ByteBuf 对象。实现类决定具体创建的是 Heap ByteBuf 还是 Direct ByteBuf 。
/**
* Allocate a {@link ByteBuf}. If it is a direct or heap buffer
* depends on the actual implementation.
*/
ByteBuf buffer();
ByteBuf buffer(int initialCapacity);
ByteBuf buffer(int initialCapacity, int maxCapacity);
ioBuffer() 方法,创建一个用于 IO 操作的 ByteBuf 对象。
对于 IO 操作来说,倾向于 Direct ByteBuf ,性能更优。
/**
* Allocate a {@link ByteBuf}, preferably a direct buffer which is suitable for I/O.
*/
ByteBuf ioBuffer();
ByteBuf ioBuffer(int initialCapacity);
ByteBuf ioBuffer(int initialCapacity, int maxCapacity);
heapBuffer() 方法,创建一个 Heap Buffer 对象:
/**
* Allocate a heap {@link ByteBuf}.
*/
ByteBuf heapBuffer();
ByteBuf heapBuffer(int initialCapacity);
ByteBuf heapBuffer(int initialCapacity, int maxCapacity);
directBuffer() 方法,创建一个 Direct Buffer 对象:
/**
* Allocate a direct {@link ByteBuf} with the given initial capacity.
*/
ByteBuf directBuffer(int initialCapacity);
ByteBuf directBuffer(int initialCapacity, int maxCapacity);
CompositeByteBuf compositeBuffer();
compositeBuffer() 方法,创建一个 Composite ByteBuf 对象。实现类决定具体创建的是 Heap ByteBuf 还是 Direct ByteBuf :
/**
* Allocate a {@link CompositeByteBuf}.
* If it is a direct or heap buffer depends on the actual implementation.
*/
CompositeByteBuf compositeBuffer();
CompositeByteBuf compositeBuffer(int maxNumComponents);
compositeHeapBuffer() 方法,创建一个 Composite Heap ByteBuf 对象:
/**
* Allocate a heap {@link CompositeByteBuf}.
*/
CompositeByteBuf compositeHeapBuffer();
CompositeByteBuf compositeHeapBuffer(int maxNumComponents);
compositeDirectBuffer(…) 方法,创建一个 Composite Direct ByteBuf 对象:
/**
* Allocate a direct {@link CompositeByteBuf}.
*/
CompositeByteBuf compositeDirectBuffer();
CompositeByteBuf compositeDirectBuffer(int maxNumComponents);
isDirectBufferPooled() 方法,是否基于 Direct ByteBuf 对象池:
/**
* Returns {@code true} if direct {@link ByteBuf}'s are pooled
*/
boolean isDirectBufferPooled();
calculateNewCapacity(int minNewCapacity, int maxCapacity) 方法,在 ByteBuf 扩容时,计算新的容量,该容量的值在 [minNewCapacity, maxCapacity] 范围内:
/**
* Calculate the new capacity of a {@link ByteBuf} that is used when a {@link ByteBuf} needs to expand by the
* {@code minNewCapacity} with {@code maxCapacity} as upper-bound.
*/
int calculateNewCapacity(int minNewCapacity, int maxCapacity);
AbstractByteBufAllocator,实现 ByteBufAllocator 接口及ByteBufAllocator 抽象实现类,为 PooledByteBufAllocator 和 UnpooledByteBufAllocator 提供公共的方法。
构造方法:
/**
* 是否倾向创建 Direct ByteBuf
*/
private final boolean directByDefault;
/**
* 空 ByteBuf 缓存
*/
private final ByteBuf emptyBuf;
/**
* Instance use heap buffers by default
*/
protected AbstractByteBufAllocator() {
this(false);
}
/**
* Create new instance
*
* @param preferDirect {@code true} if {@link #buffer(int)} should try to allocate a direct buffer rather than
* a heap buffer
*/
protected AbstractByteBufAllocator(boolean preferDirect) {
directByDefault = preferDirect && PlatformDependent.hasUnsafe(); // 支持 Unsafe
emptyBuf = new EmptyByteBuf(this);
}
directByDefault 属性: 是否倾向创建 Direct ByteBuf 。有一个前提是需要支持 Unsafe 操作。
emptyBuf 属性:空 ByteBuf 缓存对象。用于 buffer() 等方法,创建空 ByteBuf 对象时。
buffer:
根据 directByDefault 的值,调用 directBuffer() 方法,还是调用 heapBuffer() 方法。
@Override
public ByteBuf buffer() {
if (directByDefault) {
return directBuffer();
}
return heapBuffer();
}
@Override
public ByteBuf buffer(int initialCapacity) {
if (directByDefault) {
return directBuffer(initialCapacity);
}
return heapBuffer(initialCapacity);
}
@Override
public ByteBuf buffer(int initialCapacity, int maxCapacity) {
if (directByDefault) {
return directBuffer(initialCapacity, maxCapacity);
}
return heapBuffer(initialCapacity, maxCapacity);
}
ioBuffer:
根据是否支持 Unsafe 操作的情况,调用 directBuffer() 方法,还是调用 heapBuffer() 方法。
/**
* 默认容量大小
*/
static final int DEFAULT_INITIAL_CAPACITY = 256;
@Override
public ByteBuf ioBuffer() {
if (PlatformDependent.hasUnsafe()) {
return directBuffer(DEFAULT_INITIAL_CAPACITY);
}
return heapBuffer(DEFAULT_INITIAL_CAPACITY);
}
@Override
public ByteBuf ioBuffer(int initialCapacity) {
if (PlatformDependent.hasUnsafe()) {
return directBuffer(initialCapacity);
}
return heapBuffer(initialCapacity);
}
@Override
public ByteBuf ioBuffer(int initialCapacity, int maxCapacity) {
if (PlatformDependent.hasUnsafe()) {
return directBuffer(initialCapacity, maxCapacity);
}
return heapBuffer(initialCapacity, maxCapacity);
}
heapBuffer
/**
* 默认最大容量大小,无限。
*/
static final int DEFAULT_MAX_CAPACITY = Integer.MAX_VALUE;
@Override
public ByteBuf heapBuffer() {
return heapBuffer(DEFAULT_INITIAL_CAPACITY, DEFAULT_MAX_CAPACITY);
}
@Override
public ByteBuf heapBuffer(int initialCapacity) {
return heapBuffer(initialCapacity, DEFAULT_MAX_CAPACITY);
}
@Override
public ByteBuf heapBuffer(int initialCapacity, int maxCapacity) {
// 空 ByteBuf 对象
if (initialCapacity == 0 && maxCapacity == 0) {
return emptyBuf;
}
validate(initialCapacity, maxCapacity); // 校验容量的参数
// 创建 Heap ByteBuf 对象
return newHeapBuffer(initialCapacity, maxCapacity);
}
最终调用 newHeapBuffer(int initialCapacity, int maxCapacity) 抽象方法,创建 Heap ByteBuf 对象。因为是否基于对象池的方式,创建 Heap ByteBuf 对象的实现会不同,所以需要抽象。:
/**
* Create a heap {@link ByteBuf} with the given initialCapacity and maxCapacity.
*/
protected abstract ByteBuf newHeapBuffer(int initialCapacity, int maxCapacity);
directBuffer:
@Override
public ByteBuf directBuffer() {
return directBuffer(DEFAULT_INITIAL_CAPACITY, DEFAULT_MAX_CAPACITY);
}
@Override
public ByteBuf directBuffer(int initialCapacity) {
return directBuffer(initialCapacity, DEFAULT_MAX_CAPACITY);
}
@Override
public ByteBuf directBuffer(int initialCapacity, int maxCapacity) {
// 空 ByteBuf 对象
if (initialCapacity == 0 && maxCapacity == 0) {
return emptyBuf;
}
validate(initialCapacity, maxCapacity); // 校验容量的参数
// 创建 Direct ByteBuf 对象
return newDirectBuffer(initialCapacity, maxCapacity);
}
最终调用 newDirectBuffer(int initialCapacity, int maxCapacity) 抽象方法,创建 Direct ByteBuf 对象,因为是否基于对象池的方式,创建 Direct ByteBuf 对象的实现会不同,所以需要抽象。:
/**
* Create a direct {@link ByteBuf} with the given initialCapacity and maxCapacity.
*/
protected abstract ByteBuf newDirectBuffer(int initialCapacity, int maxCapacity);
compositeBuffer,根据 directByDefault 的值,调用compositeDirectBuffer() 方法,还是调用 compositeHeapBuffer() 方法。:
@Override
public CompositeByteBuf compositeBuffer() {
if (directByDefault) {
return compositeDirectBuffer();
}
return compositeHeapBuffer();
}
@Override
public CompositeByteBuf compositeBuffer(int maxNumComponents) {
if (directByDefault) {
return compositeDirectBuffer(maxNumComponents);
}
return compositeHeapBuffer(maxNumComponents);
}
compositeHeapBuffer,创建 CompositeByteBuf 对象,并且方法参数 direct 为 false ,表示 Heap 类型。调用 toLeakAwareBuffer(CompositeByteBuf) 方法,装饰成 LeakAware 的 ByteBuf 对象。:
/**
* Composite ByteBuf 可包含的 ByteBuf 的最大数量
*/
static final int DEFAULT_MAX_COMPONENTS = 16;
@Override
public CompositeByteBuf compositeHeapBuffer() {
return compositeHeapBuffer(DEFAULT_MAX_COMPONENTS);
}
@Override
public CompositeByteBuf compositeHeapBuffer(int maxNumComponents) {
return toLeakAwareBuffer(new CompositeByteBuf(this, false, maxNumComponents));
}
compositeDirectBuffer:
@Override
public CompositeByteBuf compositeDirectBuffer() {
return compositeDirectBuffer(DEFAULT_MAX_COMPONENTS);
}
@Override
public CompositeByteBuf compositeDirectBuffer(int maxNumComponents) {
return toLeakAwareBuffer(new CompositeByteBuf(this, true, maxNumComponents));
}
创建 CompositeByteBuf 对象,并且方法参数 direct 为 true ,表示 Direct 类型。
调用 toLeakAwareBuffer(CompositeByteBuf) 方法,装饰成 LeakAware 的 ByteBuf 对象。
calculateNewCapacity:
/**
* 扩容分界线,4M
*/
static final int CALCULATE_THRESHOLD = 1048576 * 4; // 4 MiB page
@Override
public int calculateNewCapacity(int minNewCapacity, int maxCapacity) {
if (minNewCapacity < 0) {
throw new IllegalArgumentException("minNewCapacity: " + minNewCapacity + " (expected: 0+)");
}
if (minNewCapacity > maxCapacity) {
throw new IllegalArgumentException(String.format(
"minNewCapacity: %d (expected: not greater than maxCapacity(%d)",
minNewCapacity, maxCapacity));
}
final int threshold = CALCULATE_THRESHOLD; // 4 MiB page
// 等于 threshold ,直接返回 threshold 。
if (minNewCapacity == threshold) {
return threshold;
}
// 超过 threshold ,增加 threshold ,不超过 maxCapacity 大小。
// If over threshold, do not double but just increase by threshold.
if (minNewCapacity > threshold) {
int newCapacity = minNewCapacity / threshold * threshold;
if (newCapacity > maxCapacity - threshold) { // 不超过 maxCapacity
newCapacity = maxCapacity;
} else {
newCapacity += threshold;
}
return newCapacity;
}
// 未超过 threshold ,从 64 开始两倍计算,不超过 4M 大小。
// Not over threshold. Double up to 4 MiB, starting from 64.
int newCapacity = 64;
while (newCapacity < minNewCapacity) {
newCapacity <<= 1;
}
return Math.min(newCapacity, maxCapacity);
}
PreferHeapByteBufAllocator,实现 ByteBufAllocator 接口,倾向创建 Heap ByteBuf 的分配器。所以buffer() 和 ioBuffer() 和 compositeBuffer() 方法,创建的都是 Heap ByteBuf 对象:
/**
* 真正的分配器对象
*/
private final ByteBufAllocator allocator;
public PreferHeapByteBufAllocator(ByteBufAllocator allocator) {
this.allocator = ObjectUtil.checkNotNull(allocator, "allocator");
}
@Override
public ByteBuf buffer() {
return allocator.heapBuffer();
}
@Override
public ByteBuf ioBuffer() {
return allocator.heapBuffer();
}
@Override
public CompositeByteBuf compositeBuffer() {
return allocator.compositeHeapBuffer();
}