Skip to content

Commit

Permalink
[#4017] Implement proper resource leak detection for CompositeByteBuf
Browse files Browse the repository at this point in the history
Motivation:

CompositeByteBuf only implemented simple resource leak detection and how it was implemented was completly different to the way it was for ByteBuf. The other problem was that slice(), duplicate() and others would not return a resource leak enabled buffer.

Modifications:

- Proper implementation for all level of resource leak detection for CompositeByteBuf

Result:

Proper resource leak detection for CompositeByteBuf.
  • Loading branch information
normanmaurer committed Jan 18, 2016
1 parent d7d6413 commit 18765a3
Show file tree
Hide file tree
Showing 8 changed files with 2,038 additions and 129 deletions.
28 changes: 25 additions & 3 deletions buffer/src/main/java/io/netty/buffer/AbstractByteBufAllocator.java
Expand Up @@ -26,7 +26,7 @@
*/
public abstract class AbstractByteBufAllocator implements ByteBufAllocator {
private static final int DEFAULT_INITIAL_CAPACITY = 256;
private static final int DEFAULT_MAX_COMPONENTS = 16;
static final int DEFAULT_MAX_COMPONENTS = 16;

protected static ByteBuf toLeakAwareBuffer(ByteBuf buf) {
ResourceLeak leak;
Expand All @@ -48,6 +48,28 @@ protected static ByteBuf toLeakAwareBuffer(ByteBuf buf) {
return buf;
}

protected static CompositeByteBuf toLeakAwareBuffer(CompositeByteBuf buf) {
ResourceLeak leak;
switch (ResourceLeakDetector.getLevel()) {
case SIMPLE:
leak = AbstractByteBuf.leakDetector.open(buf);
if (leak != null) {
buf = new SimpleLeakAwareCompositeByteBuf(buf, leak);
}
break;
case ADVANCED:
case PARANOID:
leak = AbstractByteBuf.leakDetector.open(buf);
if (leak != null) {
buf = new AdvancedLeakAwareCompositeByteBuf(buf, leak);
}
break;
default:
break;
}
return buf;
}

private final boolean directByDefault;
private final ByteBuf emptyBuf;

Expand Down Expand Up @@ -178,7 +200,7 @@ public CompositeByteBuf compositeHeapBuffer() {

@Override
public CompositeByteBuf compositeHeapBuffer(int maxNumComponents) {
return new CompositeByteBuf(this, false, maxNumComponents);
return toLeakAwareBuffer(new CompositeByteBuf(this, false, maxNumComponents));
}

@Override
Expand All @@ -188,7 +210,7 @@ public CompositeByteBuf compositeDirectBuffer() {

@Override
public CompositeByteBuf compositeDirectBuffer(int maxNumComponents) {
return new CompositeByteBuf(this, true, maxNumComponents);
return toLeakAwareBuffer(new CompositeByteBuf(this, true, maxNumComponents));
}

private static void validate(int initialCapacity, int maxCapacity) {
Expand Down
Expand Up @@ -44,7 +44,7 @@ protected AbstractReferenceCountedByteBuf(int maxCapacity) {
}

@Override
public final int refCnt() {
public int refCnt() {
return refCnt;
}

Expand Down Expand Up @@ -94,7 +94,7 @@ public ByteBuf retain(int increment) {
}

@Override
public final boolean release() {
public boolean release() {
for (;;) {
int refCnt = this.refCnt;
if (refCnt == 0) {
Expand All @@ -112,7 +112,7 @@ public final boolean release() {
}

@Override
public final boolean release(int decrement) {
public boolean release(int decrement) {
if (decrement <= 0) {
throw new IllegalArgumentException("decrement: " + decrement + " (expected: > 0)");
}
Expand Down

0 comments on commit 18765a3

Please sign in to comment.