Class AdaptivePoolingAllocator.SizeClassedChunk
java.lang.Object
io.netty.buffer.AdaptivePoolingAllocator.Chunk
io.netty.buffer.AdaptivePoolingAllocator.SizeClassedChunk
- All Implemented Interfaces:
ReferenceCounted
- Enclosing class:
AdaptivePoolingAllocator
private static final class AdaptivePoolingAllocator.SizeClassedChunk
extends AdaptivePoolingAllocator.Chunk
Removes per-allocation retain()/release() atomic ops from the hot path by replacing ref counting
with a segment-count state machine. Atomics are only needed on the cold deallocation path
(
markToDeallocate()), which is rare for long-lived chunks that cycle segments many times.
The tradeoff is a MpscIntQueue.size() call (volatile reads, no RMW) per remaining segment
return after mark — acceptable since it avoids atomic RMWs entirely.
State transitions:
AVAILABLE(-1): chunk is in use, no deallocation tracking needed- 0..N: local free list size at the time
markToDeallocate()was called; used to track when all segments have been returned DEALLOCATED(Integer.MIN_VALUE): all segments returned, chunk deallocated
Ordering: external releaseSegment(int, int) pushes to the MPSC queue (which has an implicit
StoreLoad barrier via its offer()), then reads state — this guarantees
visibility of any preceding markToDeallocate() write.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final intprivate static final intprivate final MpscIntQueueprivate static final intprivate final AdaptivePoolingAllocator.IntStackprivate Threadprivate final intprivate final intprivate intprivate static final AtomicIntegerFieldUpdater<AdaptivePoolingAllocator.SizeClassedChunk> Fields inherited from class AdaptivePoolingAllocator.Chunk
allocatedBytes, delegate, magazine -
Constructor Summary
ConstructorsConstructorDescriptionSizeClassedChunk(AbstractByteBuf delegate, AdaptivePoolingAllocator.Magazine magazine, AdaptivePoolingAllocator.SizeClassChunkController controller) -
Method Summary
Modifier and TypeMethodDescriptionprivate voiddeallocateIfNeeded(int localSize) boolean(package private) voidprivate intbooleanreadInitInto(AdaptivePoolingAllocator.AdaptiveByteBuf buf, int size, int startingCapacity, int maxCapacity) (package private) voidreleaseSegment(int startIndex, int size) Called when a ByteBuf is done using its allocation in this chunk.private voidreleaseSegmentOffsetIntoFreeList(int startIndex) intprivate intupdateRemainingCapacity(int snapshotted) private voidupdateStateOnLocalReleaseSegment(int previousLocalSize, AdaptivePoolingAllocator.IntStack localFreeList) Methods inherited from class AdaptivePoolingAllocator.Chunk
attachToMagazine, capacity, currentMagazine, deallocate, detachFromMagazine, hasUnprocessedFreelistEntries, processFreelistEntries, refCnt, release, release, releaseFromMagazine, retain, retain, touch, touch
-
Field Details
-
FREE_LIST_EMPTY
private static final int FREE_LIST_EMPTY- See Also:
-
AVAILABLE
private static final int AVAILABLE- See Also:
-
DEALLOCATED
private static final int DEALLOCATED- See Also:
-
STATE
-
state
private volatile int state -
segments
private final int segments -
segmentSize
private final int segmentSize -
externalFreeList
-
localFreeList
-
ownerThread
-
-
Constructor Details
-
SizeClassedChunk
SizeClassedChunk(AbstractByteBuf delegate, AdaptivePoolingAllocator.Magazine magazine, AdaptivePoolingAllocator.SizeClassChunkController controller)
-
-
Method Details
-
readInitInto
public boolean readInitInto(AdaptivePoolingAllocator.AdaptiveByteBuf buf, int size, int startingCapacity, int maxCapacity) - Overrides:
readInitIntoin classAdaptivePoolingAllocator.Chunk
-
nextAvailableSegmentOffset
private int nextAvailableSegmentOffset() -
hasRemainingCapacity
public boolean hasRemainingCapacity() -
remainingCapacity
public int remainingCapacity()- Overrides:
remainingCapacityin classAdaptivePoolingAllocator.Chunk
-
updateRemainingCapacity
private int updateRemainingCapacity(int snapshotted) -
releaseSegmentOffsetIntoFreeList
private void releaseSegmentOffsetIntoFreeList(int startIndex) -
releaseSegment
void releaseSegment(int startIndex, int size) Description copied from class:AdaptivePoolingAllocator.ChunkCalled when a ByteBuf is done using its allocation in this chunk.- Overrides:
releaseSegmentin classAdaptivePoolingAllocator.Chunk
-
updateStateOnLocalReleaseSegment
private void updateStateOnLocalReleaseSegment(int previousLocalSize, AdaptivePoolingAllocator.IntStack localFreeList) -
deallocateIfNeeded
private void deallocateIfNeeded(int localSize) -
markToDeallocate
void markToDeallocate()- Overrides:
markToDeallocatein classAdaptivePoolingAllocator.Chunk
-