GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — AdaptiveByteBufAllocator: Correctly manage used memory strategy in al… · netty/netty@2eb7fce

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • buffer/src/main/java/io/netty/buffer
    • AdaptivePoolingAllocator.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎buffer/src/main/java/io/netty/buffer/AdaptivePoolingAllocator.java‎

Copy file name to clipboardExpand all lines: buffer/src/main/java/io/netty/buffer/AdaptivePoolingAllocator.java
+39-11Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -261,8 +261,9 @@ private AdaptiveByteBuf allocateFallback(int size, int maxCapacity, Thread curre
261261
Magazine magazine;
262262
if (buf != null) {
263263
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+
}
266267
} else {
267268
magazine = getFallbackMagazine(currentThread);
268269
buf = magazine.newBuffer();
@@ -613,6 +614,8 @@ private boolean allocate(int size, int sizeBucket, int maxCapacity, AdaptiveByte
613614
curr = parent.centralQueue.poll();
614615
if (curr == null) {
615616
curr = newChunkAllocation(size);
617+
} else {
618+
curr.attachToMagazine(this);
616619
}
617620
}
618621
current = curr;
@@ -665,10 +668,17 @@ private void restoreMagazineFreed() {
665668
}
666669

667670
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)) {
670678
return;
671679
}
680+
// Attach it again
681+
current.attachToMagazine(this);
672682
Chunk nextChunk = NEXT_IN_LINE.get(this);
673683
if (nextChunk != null && nextChunk != MAGAZINE_FREED
674684
&& current.remainingCapacity() > nextChunk.remainingCapacity()) {
@@ -725,7 +735,8 @@ public AdaptiveByteBuf newBuffer() {
725735
private static final class Chunk implements ReferenceCounted {
726736

727737
private final AbstractByteBuf delegate;
728-
final Magazine magazine;
738+
private Magazine magazine;
739+
private final AdaptivePoolingAllocator allocator;
729740
private final int capacity;
730741
private final boolean pooled;
731742
private int allocatedBytes;
@@ -754,17 +765,35 @@ protected long unsafeOffset() {
754765
// Constructor only used by the MAGAZINE_FREED sentinel.
755766
delegate = null;
756767
magazine = null;
768+
allocator = null;
757769
capacity = 0;
758770
pooled = false;
759771
}
760772

761773
Chunk(AbstractByteBuf delegate, Magazine magazine, boolean pooled) {
762774
this.delegate = delegate;
763-
this.magazine = magazine;
764775
this.pooled = pooled;
765776
capacity = delegate.capacity();
766-
magazine.usedMemory.getAndAdd(capacity);
767777
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);
768797
}
769798

770799
@Override
@@ -818,15 +847,15 @@ private void deallocate() {
818847
if (!pooled || memSize < chunkSize || memSize > chunkSize + (chunkSize >> 1)) {
819848
// Drop the chunk if the parent allocator is closed, or if the chunk is smaller than the
820849
// preferred chunk size, or over 50% larger than the preferred chunk size.
821-
mag.usedMemory.getAndAdd(-capacity());
850+
detachFromMagazine();
822851
delegate.release();
823852
} else {
824853
updater.resetRefCnt(this);
825854
delegate.setIndex(0, 0);
826855
allocatedBytes = 0;
827856
if (!mag.trySetNextInLine(this)) {
828857
// As this Chunk does not belong to the mag anymore we need to decrease the used memory .
829-
mag.usedMemory.getAndAdd(-capacity());
858+
detachFromMagazine();
830859
if (!parent.offerToQueue(this)) {
831860
// The central queue is full. Ensure we release again as we previously did use resetRefCnt()
832861
// which did increase the reference count by 1.
@@ -925,8 +954,7 @@ public ByteBuf capacity(int newCapacity) {
925954
data.clear();
926955
tmpNioBuf = null;
927956
Chunk chunk = this.chunk;
928-
Magazine magazine = chunk.magazine;
929-
AdaptivePoolingAllocator allocator = magazine.parent;
957+
AdaptivePoolingAllocator allocator = chunk.allocator;
930958
int readerIndex = this.readerIndex;
931959
int writerIndex = this.writerIndex;
932960
allocator.allocate(newCapacity, maxCapacity(), this);

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.