Pooling allocator cleanups and checkstyle fixes
This commit is contained in:
parent
12b38234e5
commit
03743fca0d
|
@ -17,7 +17,6 @@ package io.netty.buffer.api.pool;
|
|||
|
||||
import io.netty.buffer.api.AllocatorControl;
|
||||
import io.netty.buffer.api.Buffer;
|
||||
import io.netty.buffer.api.BufferAllocator;
|
||||
import io.netty.buffer.api.MemoryManager;
|
||||
import io.netty.buffer.api.internal.Statics;
|
||||
import io.netty.util.internal.StringUtil;
|
||||
|
@ -110,10 +109,6 @@ class PoolArena extends SizeClasses implements PoolArenaMetric, AllocatorControl
|
|||
return new PoolSubpage[size];
|
||||
}
|
||||
|
||||
boolean isDirect() {
|
||||
return manager.isNative();
|
||||
}
|
||||
|
||||
UntetheredMemory allocate(PooledAllocatorControl control, PoolThreadCache cache, int size) {
|
||||
final int sizeIdx = size2SizeIdx(size);
|
||||
|
||||
|
@ -164,7 +159,8 @@ class PoolArena extends SizeClasses implements PoolArenaMetric, AllocatorControl
|
|||
return memory;
|
||||
}
|
||||
|
||||
private UntetheredMemory tcacheAllocateNormal(PooledAllocatorControl control, PoolThreadCache cache, int size, int sizeIdx) {
|
||||
private UntetheredMemory tcacheAllocateNormal(
|
||||
PooledAllocatorControl control, PoolThreadCache cache, int size, int sizeIdx) {
|
||||
UntetheredMemory memory = cache.allocateNormal(this, control, size, sizeIdx);
|
||||
if (memory != null) {
|
||||
// was able to allocate out of the cache so move on
|
||||
|
@ -178,7 +174,8 @@ class PoolArena extends SizeClasses implements PoolArenaMetric, AllocatorControl
|
|||
}
|
||||
|
||||
// Method must be called inside synchronized(this) { ... } block
|
||||
private UntetheredMemory allocateNormal(int size, int sizeIdx, PoolThreadCache threadCache, PooledAllocatorControl control) {
|
||||
private UntetheredMemory allocateNormal(
|
||||
int size, int sizeIdx, PoolThreadCache threadCache, PooledAllocatorControl control) {
|
||||
UntetheredMemory memory = q050.allocate(size, sizeIdx, threadCache, control);
|
||||
if (memory != null) {
|
||||
return memory;
|
||||
|
@ -215,7 +212,7 @@ class PoolArena extends SizeClasses implements PoolArenaMetric, AllocatorControl
|
|||
private UntetheredMemory allocateHuge(int size) {
|
||||
activeBytesHuge.add(size);
|
||||
allocationsHuge.increment();
|
||||
return new UnpooledUnthetheredMemory(manager, size);
|
||||
return new UnpooledUnthetheredMemory(parent, manager, size);
|
||||
}
|
||||
|
||||
void free(PoolChunk chunk, long handle, int normCapacity, PoolThreadCache cache) {
|
||||
|
@ -234,15 +231,12 @@ class PoolArena extends SizeClasses implements PoolArenaMetric, AllocatorControl
|
|||
void freeChunk(PoolChunk chunk, long handle, int normCapacity, SizeClass sizeClass) {
|
||||
final boolean destroyChunk;
|
||||
synchronized (this) {
|
||||
switch (sizeClass) {
|
||||
case Normal:
|
||||
++deallocationsNormal;
|
||||
break;
|
||||
case Small:
|
||||
++deallocationsSmall;
|
||||
break;
|
||||
default:
|
||||
throw new Error();
|
||||
if (sizeClass == SizeClass.Normal) {
|
||||
++deallocationsNormal;
|
||||
} else if (sizeClass == SizeClass.Small) {
|
||||
++deallocationsSmall;
|
||||
} else {
|
||||
throw new AssertionError("Unexpected size class: " + sizeClass);
|
||||
}
|
||||
destroyChunk = !chunk.parent.free(chunk, handle, normCapacity);
|
||||
}
|
||||
|
|
|
@ -515,14 +515,15 @@ final class PoolChunk implements PoolChunkMetric {
|
|||
| (long) inUsed << IS_USED_SHIFT;
|
||||
}
|
||||
|
||||
UntetheredMemory allocateBuffer(long handle, int size, PoolThreadCache threadCache, PooledAllocatorControl control) {
|
||||
UntetheredMemory allocateBuffer(long handle, int size, PoolThreadCache threadCache,
|
||||
PooledAllocatorControl control) {
|
||||
if (isRun(handle)) {
|
||||
int offset = runOffset(handle) << pageShifts;
|
||||
int maxLength = runSize(pageShifts, handle);
|
||||
PoolThreadCache poolThreadCache = arena.parent.threadCache();
|
||||
initAllocatorControl(control, poolThreadCache, handle, maxLength);
|
||||
return new UntetheredChunkAllocation(
|
||||
memory, control, this, poolThreadCache, handle, maxLength, offset, size);
|
||||
memory, this, poolThreadCache, handle, maxLength, offset, size);
|
||||
} else {
|
||||
return allocateBufferWithSubpage(handle, size, threadCache, control);
|
||||
}
|
||||
|
@ -539,13 +540,12 @@ final class PoolChunk implements PoolChunkMetric {
|
|||
|
||||
int offset = (runOffset << pageShifts) + bitmapIdx * s.elemSize;
|
||||
initAllocatorControl(control, threadCache, handle, s.elemSize);
|
||||
return new UntetheredChunkAllocation(memory, control, this, threadCache, handle, s.elemSize, offset, size);
|
||||
return new UntetheredChunkAllocation(memory, this, threadCache, handle, s.elemSize, offset, size);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private static class UntetheredChunkAllocation implements UntetheredMemory {
|
||||
private static final class UntetheredChunkAllocation implements UntetheredMemory {
|
||||
private final Object memory;
|
||||
private final PooledAllocatorControl control;
|
||||
private final PoolChunk chunk;
|
||||
private final PoolThreadCache threadCache;
|
||||
private final long handle;
|
||||
|
@ -554,10 +554,9 @@ final class PoolChunk implements PoolChunkMetric {
|
|||
private final int size;
|
||||
|
||||
private UntetheredChunkAllocation(
|
||||
Object memory, PooledAllocatorControl control, PoolChunk chunk, PoolThreadCache threadCache,
|
||||
Object memory, PoolChunk chunk, PoolThreadCache threadCache,
|
||||
long handle, int maxLength, int offset, int size) {
|
||||
this.memory = memory;
|
||||
this.control = control;
|
||||
this.chunk = chunk;
|
||||
this.threadCache = threadCache;
|
||||
this.handle = handle;
|
||||
|
@ -573,7 +572,7 @@ final class PoolChunk implements PoolChunkMetric {
|
|||
|
||||
@Override
|
||||
public <BufferType extends Buffer> Drop<BufferType> drop() {
|
||||
return (Drop<BufferType>) new PooledDrop(control, chunk.arena, chunk, threadCache, handle, maxLength);
|
||||
return (Drop<BufferType>) new PooledDrop(chunk.arena, chunk, threadCache, handle, maxLength);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -584,7 +583,6 @@ final class PoolChunk implements PoolChunkMetric {
|
|||
control.threadCache = threadCache;
|
||||
control.handle = handle;
|
||||
control.normSize = normSize;
|
||||
control.updates++;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -288,8 +288,8 @@ final class PoolThreadCache {
|
|||
/**
|
||||
* Allocate a new {@link UntetheredMemory} using the provided chunk and handle with the capacity restrictions.
|
||||
*/
|
||||
protected abstract UntetheredMemory allocBuf(PoolChunk chunk, long handle, int size, PoolThreadCache threadCache,
|
||||
PooledAllocatorControl control);
|
||||
protected abstract UntetheredMemory allocBuf(
|
||||
PoolChunk chunk, long handle, int size, PoolThreadCache threadCache, PooledAllocatorControl control);
|
||||
|
||||
/**
|
||||
* Add to cache if not already full.
|
||||
|
|
|
@ -19,16 +19,16 @@ import io.netty.buffer.api.AllocatorControl;
|
|||
import io.netty.buffer.api.Buffer;
|
||||
|
||||
class PooledAllocatorControl implements AllocatorControl {
|
||||
public PooledBufferAllocator parent;
|
||||
public PoolArena arena;
|
||||
public PoolChunk chunk;
|
||||
public PoolThreadCache threadCache;
|
||||
public long handle;
|
||||
public int normSize;
|
||||
public int updates;
|
||||
|
||||
@Override
|
||||
public UntetheredMemory allocateUntethered(Buffer originator, int size) {
|
||||
return arena.parent.allocate(this, size);
|
||||
return parent.allocate(this, size);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -289,6 +289,7 @@ public class PooledBufferAllocator implements BufferAllocator, BufferAllocatorMe
|
|||
throw new IllegalArgumentException("Allocation size must be positive, but was " + size + '.');
|
||||
}
|
||||
PooledAllocatorControl control = new PooledAllocatorControl();
|
||||
control.parent = this;
|
||||
UntetheredMemory memory = allocate(control, size);
|
||||
Buffer buffer = manager.recoverMemory(control, memory.memory(), memory.drop());
|
||||
return buffer.fill((byte) 0).order(ByteOrder.nativeOrder());
|
||||
|
@ -305,7 +306,7 @@ public class PooledBufferAllocator implements BufferAllocator, BufferAllocatorMe
|
|||
}
|
||||
|
||||
private UntetheredMemory allocateUnpooled(int size) {
|
||||
return new UnpooledUnthetheredMemory(manager, size);
|
||||
return new UnpooledUnthetheredMemory(this, manager, size);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -423,7 +424,7 @@ public class PooledBufferAllocator implements BufferAllocator, BufferAllocatorMe
|
|||
threadCache.free();
|
||||
}
|
||||
|
||||
private PoolArena leastUsedArena(PoolArena[] arenas) {
|
||||
private static PoolArena leastUsedArena(PoolArena[] arenas) {
|
||||
if (arenas == null || arenas.length == 0) {
|
||||
return null;
|
||||
}
|
||||
|
|
|
@ -19,16 +19,13 @@ import io.netty.buffer.api.Buffer;
|
|||
import io.netty.buffer.api.Drop;
|
||||
|
||||
class PooledDrop implements Drop<Buffer> {
|
||||
private final PooledAllocatorControl control;
|
||||
private PoolArena arena;
|
||||
private PoolChunk chunk;
|
||||
private PoolThreadCache threadCache;
|
||||
private long handle;
|
||||
private int normSize;
|
||||
private final PoolArena arena;
|
||||
private final PoolChunk chunk;
|
||||
private final PoolThreadCache threadCache;
|
||||
private final long handle;
|
||||
private final int normSize;
|
||||
|
||||
PooledDrop(PooledAllocatorControl control, PoolArena arena, PoolChunk chunk, PoolThreadCache threadCache,
|
||||
long handle, int normSize) {
|
||||
this.control = control;
|
||||
PooledDrop(PoolArena arena, PoolChunk chunk, PoolThreadCache threadCache, long handle, int normSize) {
|
||||
this.arena = arena;
|
||||
this.chunk = chunk;
|
||||
this.threadCache = threadCache;
|
||||
|
@ -40,16 +37,4 @@ class PooledDrop implements Drop<Buffer> {
|
|||
public void drop(Buffer obj) {
|
||||
arena.free(chunk, handle, normSize, threadCache);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void attach(Buffer obj) {
|
||||
if (control.updates > 0) {
|
||||
arena = control.arena;
|
||||
chunk = control.chunk;
|
||||
threadCache = control.threadCache;
|
||||
handle = control.handle;
|
||||
normSize = control.normSize;
|
||||
control.updates = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -26,9 +26,10 @@ class UnpooledUnthetheredMemory implements AllocatorControl.UntetheredMemory {
|
|||
private final MemoryManager manager;
|
||||
private final Buffer buffer;
|
||||
|
||||
UnpooledUnthetheredMemory(MemoryManager manager, int size) {
|
||||
UnpooledUnthetheredMemory(PooledBufferAllocator allocator, MemoryManager manager, int size) {
|
||||
this.manager = manager;
|
||||
PooledAllocatorControl allocatorControl = new PooledAllocatorControl();
|
||||
allocatorControl.parent = allocator;
|
||||
buffer = manager.allocateShared(allocatorControl, size, manager.drop(), Statics.CLEANER);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue