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: Cleanup internals and make code easier to r… · netty/netty@f13b165

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
+36-14Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,8 @@ private boolean allocate(int size, int maxCapacity, Thread currentThread, Adapti
217217
if (threadLocalMagazine != null && currentThread instanceof FastThreadLocalThread) {
218218
Object mag = threadLocalMagazine.get();
219219
if (mag != NO_MAGAZINE) {
220-
((Magazine) mag).allocate(size, sizeBucket, maxCapacity, buf);
220+
boolean allocated = ((Magazine) mag).tryAllocate(size, sizeBucket, maxCapacity, buf);
221+
assert allocated : "Allocation of threadLocalMagazine must always succeed";
221222
return true;
222223
}
223224
}
@@ -230,14 +231,9 @@ private boolean allocate(int size, int maxCapacity, Thread currentThread, Adapti
230231
int index = (int) (threadId & mask);
231232
for (int i = 0, m = Integer.numberOfTrailingZeros(~mask); i < m; i++) {
232233
Magazine mag = mags[index + i & mask];
233-
long writeLock = mag.tryWriteLock();
234-
if (writeLock != 0) {
235-
try {
236-
mag.allocate(size, sizeBucket, maxCapacity, buf);
237-
return true;
238-
} finally {
239-
mag.unlockWrite(writeLock);
240-
}
234+
if (mag.tryAllocate(size, sizeBucket, maxCapacity, buf)) {
235+
// Was able to allocate.
236+
return true;
241237
}
242238
}
243239
expansions++;
@@ -339,8 +335,7 @@ static int sizeBucket(int size) {
339335
}
340336

341337
@SuppressWarnings("checkstyle:finalclass") // Checkstyle mistakenly believes this class should be final.
342-
private static class AllocationStatistics extends StampedLock {
343-
private static final long serialVersionUID = -8319929980932269688L;
338+
private static class AllocationStatistics {
344339
private static final int MIN_DATUM_TARGET = 1024;
345340
private static final int MAX_DATUM_TARGET = 65534;
346341
private static final int INIT_DATUM_TARGET = 9;
@@ -442,28 +437,55 @@ protected int preferredChunkSize() {
442437
}
443438
}
444439

440+
@SuppressJava6Requirement(reason = "Guarded by version check")
445441
private static final class Magazine extends AllocationStatistics {
446-
private static final long serialVersionUID = -4068223712022528165L;
447442
private static final AtomicReferenceFieldUpdater<Magazine, Chunk> NEXT_IN_LINE;
448-
449443
static {
450444
NEXT_IN_LINE = AtomicReferenceFieldUpdater.newUpdater(Magazine.class, Chunk.class, "nextInLine");
451445
}
452446
private Chunk current;
453447
@SuppressWarnings("unused") // updated via NEXT_IN_LINE
454448
private volatile Chunk nextInLine;
455449
private final AtomicLong usedMemory;
450+
private final StampedLock allocationLock;
456451

457452
Magazine(AdaptivePoolingAllocator parent) {
458453
this(parent, true);
459454
}
460455

461456
Magazine(AdaptivePoolingAllocator parent, boolean shareable) {
462457
super(parent, shareable);
458+
459+
// We only need the StampedLock if this Magazine will be shared across threads.
460+
if (shareable) {
461+
allocationLock = new StampedLock();
462+
} else {
463+
allocationLock = null;
464+
}
463465
usedMemory = new AtomicLong();
464466
}
465467

466-
public void allocate(int size, int sizeBucket, int maxCapacity, AdaptiveByteBuf buf) {
468+
public boolean tryAllocate(int size, int sizeBucket, int maxCapacity, AdaptiveByteBuf buf) {
469+
if (allocationLock == null) {
470+
// This magazine is not shared across threads, just allocate directly.
471+
allocate(size, sizeBucket, maxCapacity, buf);
472+
return true;
473+
}
474+
475+
// Try to retrieve the lock and if successful allocate.
476+
long writeLock = allocationLock.tryWriteLock();
477+
if (writeLock != 0) {
478+
try {
479+
allocate(size, sizeBucket, maxCapacity, buf);
480+
return true;
481+
} finally {
482+
allocationLock.unlockWrite(writeLock);
483+
}
484+
}
485+
return false;
486+
}
487+
488+
private void allocate(int size, int sizeBucket, int maxCapacity, AdaptiveByteBuf buf) {
467489
recordAllocationSize(sizeBucket);
468490
Chunk curr = current;
469491
if (curr != null && curr.remainingCapacity() >= size) {

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.