@@ -261,8 +261,9 @@ private AdaptiveByteBuf allocateFallback(int size, int maxCapacity, Thread curre |
261 | 261 | Magazine magazine; |
262 | 262 | if (buf != null) { |
263 | 263 | Chunk chunk = buf.chunk; |
264 | | - magazine = chunk != null && chunk != Magazine.MAGAZINE_FREED? |
265 | | - chunk.magazine : getFallbackMagazine(currentThread); |
| 264 | + if (chunk == null || chunk == Magazine.MAGAZINE_FREED || (magazine = chunk.currentMagazine()) == null) { |
| 265 | + magazine = getFallbackMagazine(currentThread); |
| 266 | + } |
266 | 267 | } else { |
267 | 268 | magazine = getFallbackMagazine(currentThread); |
268 | 269 | buf = magazine.newBuffer(); |
@@ -613,6 +614,8 @@ private boolean allocate(int size, int sizeBucket, int maxCapacity, AdaptiveByte |
613 | 614 | curr = parent.centralQueue.poll(); |
614 | 615 | if (curr == null) { |
615 | 616 | curr = newChunkAllocation(size); |
| 617 | + } else { |
| 618 | + curr.attachToMagazine(this); |
616 | 619 | } |
617 | 620 | } |
618 | 621 | current = curr; |
@@ -665,10 +668,17 @@ private void restoreMagazineFreed() { |
665 | 668 | } |
666 | 669 |
|
667 | 670 | private void transferChunk(Chunk current) { |
668 | | - if (NEXT_IN_LINE.compareAndSet(this, null, current) |
669 | | - || parent.offerToQueue(current)) { |
| 671 | + if (NEXT_IN_LINE.compareAndSet(this, null, current)) { |
| 672 | + return; |
| 673 | + } |
| 674 | + |
| 675 | + // Detach the chunk as we are about to offer it back for reuse. |
| 676 | + current.detachFromMagazine(); |
| 677 | + if (parent.offerToQueue(current)) { |
670 | 678 | return; |
671 | 679 | } |
| 680 | + // Attach it again |
| 681 | + current.attachToMagazine(this); |
672 | 682 | Chunk nextChunk = NEXT_IN_LINE.get(this); |
673 | 683 | if (nextChunk != null && nextChunk != MAGAZINE_FREED |
674 | 684 | && current.remainingCapacity() > nextChunk.remainingCapacity()) { |
@@ -725,7 +735,8 @@ public AdaptiveByteBuf newBuffer() { |
725 | 735 | private static final class Chunk implements ReferenceCounted { |
726 | 736 |
|
727 | 737 | private final AbstractByteBuf delegate; |
728 | | - final Magazine magazine; |
| 738 | + private Magazine magazine; |
| 739 | + private final AdaptivePoolingAllocator allocator; |
729 | 740 | private final int capacity; |
730 | 741 | private final boolean pooled; |
731 | 742 | private int allocatedBytes; |
@@ -754,17 +765,35 @@ protected long unsafeOffset() { |
754 | 765 | // Constructor only used by the MAGAZINE_FREED sentinel. |
755 | 766 | delegate = null; |
756 | 767 | magazine = null; |
| 768 | + allocator = null; |
757 | 769 | capacity = 0; |
758 | 770 | pooled = false; |
759 | 771 | } |
760 | 772 |
|
761 | 773 | Chunk(AbstractByteBuf delegate, Magazine magazine, boolean pooled) { |
762 | 774 | this.delegate = delegate; |
763 | | - this.magazine = magazine; |
764 | 775 | this.pooled = pooled; |
765 | 776 | capacity = delegate.capacity(); |
766 | | - magazine.usedMemory.getAndAdd(capacity); |
767 | 777 | updater.setInitialValue(this); |
| 778 | + allocator = magazine.parent; |
| 779 | + attachToMagazine(magazine); |
| 780 | + } |
| 781 | + |
| 782 | + Magazine currentMagazine() { |
| 783 | + return magazine; |
| 784 | + } |
| 785 | + |
| 786 | + void detachFromMagazine() { |
| 787 | + if (magazine != null) { |
| 788 | + magazine.usedMemory.getAndAdd(-capacity); |
| 789 | + magazine = null; |
| 790 | + } |
| 791 | + } |
| 792 | + |
| 793 | + void attachToMagazine(Magazine magazine) { |
| 794 | + assert this.magazine == null; |
| 795 | + this.magazine = magazine; |
| 796 | + magazine.usedMemory.getAndAdd(capacity); |
768 | 797 | } |
769 | 798 |
|
770 | 799 | @Override |
@@ -818,15 +847,15 @@ private void deallocate() { |
818 | 847 | if (!pooled || memSize < chunkSize || memSize > chunkSize + (chunkSize >> 1)) { |
819 | 848 | // Drop the chunk if the parent allocator is closed, or if the chunk is smaller than the |
820 | 849 | // preferred chunk size, or over 50% larger than the preferred chunk size. |
821 | | - mag.usedMemory.getAndAdd(-capacity()); |
| 850 | + detachFromMagazine(); |
822 | 851 | delegate.release(); |
823 | 852 | } else { |
824 | 853 | updater.resetRefCnt(this); |
825 | 854 | delegate.setIndex(0, 0); |
826 | 855 | allocatedBytes = 0; |
827 | 856 | if (!mag.trySetNextInLine(this)) { |
828 | 857 | // As this Chunk does not belong to the mag anymore we need to decrease the used memory . |
829 | | - mag.usedMemory.getAndAdd(-capacity()); |
| 858 | + detachFromMagazine(); |
830 | 859 | if (!parent.offerToQueue(this)) { |
831 | 860 | // The central queue is full. Ensure we release again as we previously did use resetRefCnt() |
832 | 861 | // which did increase the reference count by 1. |
@@ -925,8 +954,7 @@ public ByteBuf capacity(int newCapacity) { |
925 | 954 | data.clear(); |
926 | 955 | tmpNioBuf = null; |
927 | 956 | Chunk chunk = this.chunk; |
928 | | - Magazine magazine = chunk.magazine; |
929 | | - AdaptivePoolingAllocator allocator = magazine.parent; |
| 957 | + AdaptivePoolingAllocator allocator = chunk.allocator; |
930 | 958 | int readerIndex = this.readerIndex; |
931 | 959 | int writerIndex = this.writerIndex; |
932 | 960 | allocator.allocate(newCapacity, maxCapacity(), this); |
|