|
77 | 77 | * This allows the allocator to quickly respond to changes in the application workload, |
78 | 78 | * without suffering undue overhead from maintaining its statistics. |
79 | 79 | * <p> |
80 | | - * Since magazines are "relatively thread-local", the allocator has a central chunk cache that allow excess chunks |
| 80 | + * Since magazines are "relatively thread-local", the allocator has a central chunk cache that allows excess chunks |
81 | 81 | * from any magazine to be shared with other magazines. |
82 | 82 | */ |
83 | 83 | @UnstableApi |
@@ -477,11 +477,11 @@ private void free() { |
477 | 477 | } |
478 | 478 | } |
479 | 479 |
|
480 | | - private interface ChunkCache { |
481 | | - Chunk pollChunk(int size); |
482 | | - boolean offerChunk(Chunk chunk); |
483 | | - void freeChunks(); |
484 | | - default void notifyReady(Chunk chunk) { } |
| 480 | + private abstract static class ChunkCache { |
| 481 | + abstract Chunk pollChunk(int size); |
| 482 | + abstract boolean offerChunk(Chunk chunk); |
| 483 | + abstract void freeChunks(); |
| 484 | + void notifyReady(Chunk chunk) { } |
485 | 485 | } |
486 | 486 |
|
487 | 487 | /** |
@@ -498,7 +498,7 @@ default void notifyReady(Chunk chunk) { } |
498 | 498 | * using a sentinel terminal node. Tracks all chunks for ownerThread/finalize cleanup. |
499 | 499 | * Push-once per chunk lifetime, guarded by the {@link SizeClassedChunk#nextFree} link pointer. |
500 | 500 | */ |
501 | | - private static final class ParkingLotChunkCache implements ChunkCache { |
| 501 | + private static final class ParkingLotChunkCache extends ChunkCache { |
502 | 502 | @SuppressWarnings("rawtypes") |
503 | 503 | private static final AtomicReferenceFieldUpdater<ParkingLotChunkCache, SizeClassedChunk> READY_HEAD = |
504 | 504 | AtomicReferenceFieldUpdater.newUpdater( |
@@ -539,6 +539,9 @@ public boolean offerChunk(Chunk chunk) { |
539 | 539 | return true; |
540 | 540 | } |
541 | 541 |
|
| 542 | + // Push-once: the intrusive nextFree link pointer is the insertion guard. |
| 543 | + // No concurrent registerToFree on the same chunk — magazine ownership |
| 544 | + // guarantees single-writer per chunk per lifecycle. |
542 | 545 | private void registerToFree(SizeClassedChunk chunk) { |
543 | 546 | if (chunk.nextFree != null) { |
544 | 547 | return; |
@@ -563,6 +566,11 @@ public void freeChunks() { |
563 | 566 | } |
564 | 567 | } |
565 | 568 |
|
| 569 | + // Called exactly once from the dying owner thread (FastThreadLocal removal). |
| 570 | + // No concurrent registerToFree — only the owner thread calls offerChunk. |
| 571 | + // The first-segment-return vs markToDeallocate race is safe: markToDeallocate |
| 572 | + // uses CAS (handles both AVAILABLE and AVAILABLE_ARMED), and notification CAS |
| 573 | + // on the same state variable ensures only one wins. |
566 | 574 | private void freeChunksThreadLocal() { |
567 | 575 | SizeClassedChunk chunk = cleanupHead; |
568 | 576 | while (chunk != SizeClassedChunk.CLEANUP_SENTINEL) { |
@@ -588,6 +596,9 @@ private SizeClassedChunk drainReadyThreadLocal() { |
588 | 596 | return chunk; |
589 | 597 | } |
590 | 598 |
|
| 599 | + // freeChunksShared is called exactly once, from finalize(). The allocator must be |
| 600 | + // unreachable for finalize to run, so no concurrent offerChunk/registerToFree calls. |
| 601 | + // No null guard needed on the getAndSet result. |
591 | 602 | private void freeChunksShared() { |
592 | 603 | SizeClassedChunk chunk = CLEANUP_HEAD.getAndSet(this, null); |
593 | 604 | while (chunk != SizeClassedChunk.CLEANUP_SENTINEL) { |
@@ -618,7 +629,7 @@ public void notifyReady(Chunk chunk) { |
618 | 629 | } |
619 | 630 | } |
620 | 631 |
|
621 | | - private static final class ConcurrentSkipListChunkCache implements ChunkCache { |
| 632 | + private static final class ConcurrentSkipListChunkCache extends ChunkCache { |
622 | 633 | private final ConcurrentSkipListIntObjMultimap<Chunk> chunks; |
623 | 634 |
|
624 | 635 | private ConcurrentSkipListChunkCache() { |
@@ -1744,6 +1755,7 @@ static final class AdaptiveByteBuf extends AbstractReferenceCountedByteBuf { |
1744 | 1755 | private AbstractByteBuf rootParent; |
1745 | 1756 | Chunk chunk; |
1746 | 1757 | private int length; |
| 1758 | + |
1747 | 1759 | private int maxFastCapacity; |
1748 | 1760 | private ByteBuffer tmpNioBuf; |
1749 | 1761 | private boolean hasArray; |
|