Merge branch 'master' of github.com:netty/netty

This commit is contained in:
Norman Maurer 2013-06-10 14:06:53 +02:00
commit 383bb80d1e
5 changed files with 95 additions and 11 deletions

View File

@ -345,7 +345,7 @@ abstract class PoolArena<T> {
@Override
protected PooledByteBuf<byte[]> newByteBuf(int maxCapacity) {
return new PooledHeapByteBuf(maxCapacity);
return PooledHeapByteBuf.newInstance(maxCapacity);
}
@Override
@ -385,9 +385,9 @@ abstract class PoolArena<T> {
@Override
protected PooledByteBuf<ByteBuffer> newByteBuf(int maxCapacity) {
if (HAS_UNSAFE) {
return new PooledUnsafeDirectByteBuf(maxCapacity);
return PooledUnsafeDirectByteBuf.newInstance(maxCapacity);
} else {
return new PooledDirectByteBuf(maxCapacity);
return PooledDirectByteBuf.newInstance(maxCapacity);
}
}

View File

@ -16,7 +16,9 @@
package io.netty.buffer;
import io.netty.util.Recycler;
import io.netty.util.ResourceLeak;
import io.netty.util.ResourceLeakDetector;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
@ -24,6 +26,7 @@ import java.nio.ByteOrder;
abstract class PooledByteBuf<T> extends AbstractReferenceCountedByteBuf {
private final ResourceLeak leak;
private final Recycler.Handle recyclerHandle;
protected PoolChunk<T> chunk;
protected long handle;
@ -34,9 +37,10 @@ abstract class PooledByteBuf<T> extends AbstractReferenceCountedByteBuf {
private ByteBuffer tmpNioBuf;
protected PooledByteBuf(int maxCapacity) {
protected PooledByteBuf(Recycler.Handle recyclerHandle, int maxCapacity) {
super(maxCapacity);
leak = leakDetector.open(this);
this.recyclerHandle = recyclerHandle;
}
void init(PoolChunk<T> chunk, long handle, int offset, int length, int maxLength) {
@ -141,9 +145,24 @@ abstract class PooledByteBuf<T> extends AbstractReferenceCountedByteBuf {
this.handle = -1;
memory = null;
chunk.arena.free(chunk, handle);
if (ResourceLeakDetector.ENABLED) {
leak.close();
} else {
recycle();
}
}
}
@SuppressWarnings("unchecked")
private void recycle() {
Recycler.Handle recyclerHandle = this.recyclerHandle;
if (recyclerHandle != null) {
setRefCnt(1);
((Recycler<Object>) recycler()).recycle(this, recyclerHandle);
}
}
protected abstract Recycler<?> recycler();
protected final int idx(int index) {
return offset + index;

View File

@ -16,6 +16,8 @@
package io.netty.buffer;
import io.netty.util.Recycler;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
@ -26,8 +28,23 @@ import java.nio.channels.ScatteringByteChannel;
final class PooledDirectByteBuf extends PooledByteBuf<ByteBuffer> {
PooledDirectByteBuf(int maxCapacity) {
super(maxCapacity);
private static final Recycler<PooledDirectByteBuf> RECYCLER = new Recycler<PooledDirectByteBuf>() {
@Override
protected PooledDirectByteBuf newObject(Handle handle) {
return new PooledDirectByteBuf(handle, Integer.MAX_VALUE);
}
};
static PooledDirectByteBuf newInstance(int maxCapacity) {
if (maxCapacity == Integer.MAX_VALUE) {
return RECYCLER.get();
} else {
return new PooledDirectByteBuf(null, maxCapacity);
}
}
private PooledDirectByteBuf(Recycler.Handle recyclerHandle, int maxCapacity) {
super(recyclerHandle, maxCapacity);
}
@Override
@ -277,4 +294,9 @@ final class PooledDirectByteBuf extends PooledByteBuf<ByteBuffer> {
public long memoryAddress() {
throw new UnsupportedOperationException();
}
@Override
protected Recycler<?> recycler() {
return RECYCLER;
}
}

View File

@ -14,6 +14,7 @@
package io.netty.buffer;
import io.netty.util.Recycler;
import io.netty.util.internal.PlatformDependent;
import java.io.IOException;
@ -26,8 +27,23 @@ import java.nio.channels.ScatteringByteChannel;
final class PooledHeapByteBuf extends PooledByteBuf<byte[]> {
PooledHeapByteBuf(int maxCapacity) {
super(maxCapacity);
private static final Recycler<PooledHeapByteBuf> RECYCLER = new Recycler<PooledHeapByteBuf>() {
@Override
protected PooledHeapByteBuf newObject(Handle handle) {
return new PooledHeapByteBuf(handle, Integer.MAX_VALUE);
}
};
static PooledHeapByteBuf newInstance(int maxCapacity) {
if (maxCapacity == Integer.MAX_VALUE) {
return RECYCLER.get();
} else {
return new PooledHeapByteBuf(null, maxCapacity);
}
}
private PooledHeapByteBuf(Recycler.Handle recyclerHandle, int maxCapacity) {
super(recyclerHandle, maxCapacity);
}
@Override
@ -258,4 +274,9 @@ final class PooledHeapByteBuf extends PooledByteBuf<byte[]> {
protected ByteBuffer newInternalNioBuffer(byte[] memory) {
return ByteBuffer.wrap(memory);
}
@Override
protected Recycler<?> recycler() {
return RECYCLER;
}
}

View File

@ -16,6 +16,7 @@
package io.netty.buffer;
import io.netty.util.Recycler;
import io.netty.util.internal.PlatformDependent;
import java.io.IOException;
@ -30,10 +31,26 @@ import java.nio.channels.ScatteringByteChannel;
final class PooledUnsafeDirectByteBuf extends PooledByteBuf<ByteBuffer> {
private static final boolean NATIVE_ORDER = ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN;
private static final Recycler<PooledUnsafeDirectByteBuf> RECYCLER = new Recycler<PooledUnsafeDirectByteBuf>() {
@Override
protected PooledUnsafeDirectByteBuf newObject(Handle handle) {
return new PooledUnsafeDirectByteBuf(handle, Integer.MAX_VALUE);
}
};
static PooledUnsafeDirectByteBuf newInstance(int maxCapacity) {
if (maxCapacity == Integer.MAX_VALUE) {
return RECYCLER.get();
} else {
return new PooledUnsafeDirectByteBuf(null, maxCapacity);
}
}
private long memoryAddress;
PooledUnsafeDirectByteBuf(int maxCapacity) {
super(maxCapacity);
private PooledUnsafeDirectByteBuf(Recycler.Handle recyclerHandle, int maxCapacity) {
super(recyclerHandle, maxCapacity);
}
@Override
@ -318,4 +335,9 @@ final class PooledUnsafeDirectByteBuf extends PooledByteBuf<ByteBuffer> {
private long addr(int index) {
return memoryAddress + index;
}
@Override
protected Recycler<?> recycler() {
return RECYCLER;
}
}