Add Recycler.Handle.recycle() so that it's possible to recycle an object without an explicit reference to Recycler
This commit is contained in:
parent
df346a023b
commit
2b84314fdd
@ -17,13 +17,14 @@
|
||||
package io.netty.buffer;
|
||||
|
||||
import io.netty.util.Recycler;
|
||||
import io.netty.util.Recycler.Handle;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
|
||||
abstract class PooledByteBuf<T> extends AbstractReferenceCountedByteBuf {
|
||||
|
||||
private final Recycler.Handle recyclerHandle;
|
||||
private final Recycler.Handle<PooledByteBuf<T>> recyclerHandle;
|
||||
|
||||
protected PoolChunk<T> chunk;
|
||||
protected long handle;
|
||||
@ -34,9 +35,10 @@ abstract class PooledByteBuf<T> extends AbstractReferenceCountedByteBuf {
|
||||
|
||||
private ByteBuffer tmpNioBuf;
|
||||
|
||||
protected PooledByteBuf(Recycler.Handle recyclerHandle, int maxCapacity) {
|
||||
@SuppressWarnings("unchecked")
|
||||
protected PooledByteBuf(Recycler.Handle<? extends PooledByteBuf<T>> recyclerHandle, int maxCapacity) {
|
||||
super(maxCapacity);
|
||||
this.recyclerHandle = recyclerHandle;
|
||||
this.recyclerHandle = (Handle<PooledByteBuf<T>>) recyclerHandle;
|
||||
}
|
||||
|
||||
void init(PoolChunk<T> chunk, long handle, int offset, int length, int maxLength) {
|
||||
@ -147,14 +149,9 @@ abstract class PooledByteBuf<T> extends AbstractReferenceCountedByteBuf {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private void recycle() {
|
||||
Recycler.Handle recyclerHandle = this.recyclerHandle;
|
||||
if (recyclerHandle != null) {
|
||||
((Recycler<Object>) recycler()).recycle(this, recyclerHandle);
|
||||
}
|
||||
recyclerHandle.recycle(this);
|
||||
}
|
||||
|
||||
protected abstract Recycler<?> recycler();
|
||||
|
||||
protected final int idx(int index) {
|
||||
return offset + index;
|
||||
}
|
||||
|
@ -30,7 +30,7 @@ final class PooledDirectByteBuf extends PooledByteBuf<ByteBuffer> {
|
||||
|
||||
private static final Recycler<PooledDirectByteBuf> RECYCLER = new Recycler<PooledDirectByteBuf>() {
|
||||
@Override
|
||||
protected PooledDirectByteBuf newObject(Handle handle) {
|
||||
protected PooledDirectByteBuf newObject(Handle<PooledDirectByteBuf> handle) {
|
||||
return new PooledDirectByteBuf(handle, 0);
|
||||
}
|
||||
};
|
||||
@ -42,7 +42,7 @@ final class PooledDirectByteBuf extends PooledByteBuf<ByteBuffer> {
|
||||
return buf;
|
||||
}
|
||||
|
||||
private PooledDirectByteBuf(Recycler.Handle recyclerHandle, int maxCapacity) {
|
||||
private PooledDirectByteBuf(Recycler.Handle<PooledDirectByteBuf> recyclerHandle, int maxCapacity) {
|
||||
super(recyclerHandle, maxCapacity);
|
||||
}
|
||||
|
||||
@ -369,9 +369,4 @@ final class PooledDirectByteBuf extends PooledByteBuf<ByteBuffer> {
|
||||
public long memoryAddress() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Recycler<?> recycler() {
|
||||
return RECYCLER;
|
||||
}
|
||||
}
|
||||
|
@ -29,7 +29,7 @@ final class PooledHeapByteBuf extends PooledByteBuf<byte[]> {
|
||||
|
||||
private static final Recycler<PooledHeapByteBuf> RECYCLER = new Recycler<PooledHeapByteBuf>() {
|
||||
@Override
|
||||
protected PooledHeapByteBuf newObject(Handle handle) {
|
||||
protected PooledHeapByteBuf newObject(Handle<PooledHeapByteBuf> handle) {
|
||||
return new PooledHeapByteBuf(handle, 0);
|
||||
}
|
||||
};
|
||||
@ -41,7 +41,7 @@ final class PooledHeapByteBuf extends PooledByteBuf<byte[]> {
|
||||
return buf;
|
||||
}
|
||||
|
||||
private PooledHeapByteBuf(Recycler.Handle recyclerHandle, int maxCapacity) {
|
||||
private PooledHeapByteBuf(Recycler.Handle<PooledHeapByteBuf> recyclerHandle, int maxCapacity) {
|
||||
super(recyclerHandle, maxCapacity);
|
||||
}
|
||||
|
||||
@ -299,9 +299,4 @@ final class PooledHeapByteBuf extends PooledByteBuf<byte[]> {
|
||||
protected ByteBuffer newInternalNioBuffer(byte[] memory) {
|
||||
return ByteBuffer.wrap(memory);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Recycler<?> recycler() {
|
||||
return RECYCLER;
|
||||
}
|
||||
}
|
||||
|
@ -34,7 +34,7 @@ final class PooledUnsafeDirectByteBuf extends PooledByteBuf<ByteBuffer> {
|
||||
|
||||
private static final Recycler<PooledUnsafeDirectByteBuf> RECYCLER = new Recycler<PooledUnsafeDirectByteBuf>() {
|
||||
@Override
|
||||
protected PooledUnsafeDirectByteBuf newObject(Handle handle) {
|
||||
protected PooledUnsafeDirectByteBuf newObject(Handle<PooledUnsafeDirectByteBuf> handle) {
|
||||
return new PooledUnsafeDirectByteBuf(handle, 0);
|
||||
}
|
||||
};
|
||||
@ -48,7 +48,7 @@ final class PooledUnsafeDirectByteBuf extends PooledByteBuf<ByteBuffer> {
|
||||
|
||||
private long memoryAddress;
|
||||
|
||||
private PooledUnsafeDirectByteBuf(Recycler.Handle recyclerHandle, int maxCapacity) {
|
||||
private PooledUnsafeDirectByteBuf(Recycler.Handle<PooledUnsafeDirectByteBuf> recyclerHandle, int maxCapacity) {
|
||||
super(recyclerHandle, maxCapacity);
|
||||
}
|
||||
|
||||
@ -381,9 +381,4 @@ final class PooledUnsafeDirectByteBuf extends PooledByteBuf<ByteBuffer> {
|
||||
private long addr(int index) {
|
||||
return memoryAddress + index;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Recycler<?> recycler() {
|
||||
return RECYCLER;
|
||||
}
|
||||
}
|
||||
|
@ -42,7 +42,7 @@ public abstract class Recycler<T> {
|
||||
return o;
|
||||
}
|
||||
|
||||
public final boolean recycle(T o, Handle handle) {
|
||||
public final boolean recycle(T o, Handle<T> handle) {
|
||||
@SuppressWarnings("unchecked")
|
||||
Stack<T> stack = (Stack<T>) handle;
|
||||
if (stack.parent != this) {
|
||||
@ -57,11 +57,13 @@ public abstract class Recycler<T> {
|
||||
return true;
|
||||
}
|
||||
|
||||
protected abstract T newObject(Handle handle);
|
||||
protected abstract T newObject(Handle<T> handle);
|
||||
|
||||
public interface Handle { }
|
||||
public interface Handle<T> {
|
||||
void recycle(T object);
|
||||
}
|
||||
|
||||
static final class Stack<T> implements Handle {
|
||||
static final class Stack<T> implements Handle<T> {
|
||||
|
||||
private static final int INITIAL_CAPACITY = 256;
|
||||
|
||||
@ -78,6 +80,11 @@ public abstract class Recycler<T> {
|
||||
elements = newArray(INITIAL_CAPACITY);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void recycle(T object) {
|
||||
parent.recycle(object, this);
|
||||
}
|
||||
|
||||
T pop() {
|
||||
int size = this.size;
|
||||
if (size == 0) {
|
||||
|
@ -25,7 +25,7 @@ import io.netty.util.concurrent.Promise;
|
||||
public final class PendingWrite {
|
||||
private static final Recycler<PendingWrite> RECYCLER = new Recycler<PendingWrite>() {
|
||||
@Override
|
||||
protected PendingWrite newObject(Handle handle) {
|
||||
protected PendingWrite newObject(Handle<PendingWrite> handle) {
|
||||
return new PendingWrite(handle);
|
||||
}
|
||||
};
|
||||
@ -40,11 +40,11 @@ public final class PendingWrite {
|
||||
return pending;
|
||||
}
|
||||
|
||||
private final Recycler.Handle handle;
|
||||
private final Recycler.Handle<PendingWrite> handle;
|
||||
private Object msg;
|
||||
private Promise<Void> promise;
|
||||
|
||||
private PendingWrite(Recycler.Handle handle) {
|
||||
private PendingWrite(Recycler.Handle<PendingWrite> handle) {
|
||||
this.handle = handle;
|
||||
}
|
||||
|
||||
|
@ -35,7 +35,7 @@ public final class RecyclableArrayList extends ArrayList<Object> {
|
||||
|
||||
private static final Recycler<RecyclableArrayList> RECYCLER = new Recycler<RecyclableArrayList>() {
|
||||
@Override
|
||||
protected RecyclableArrayList newObject(Handle handle) {
|
||||
protected RecyclableArrayList newObject(Handle<RecyclableArrayList> handle) {
|
||||
return new RecyclableArrayList(handle);
|
||||
}
|
||||
};
|
||||
@ -56,13 +56,13 @@ public final class RecyclableArrayList extends ArrayList<Object> {
|
||||
return ret;
|
||||
}
|
||||
|
||||
private final Handle handle;
|
||||
private final Handle<RecyclableArrayList> handle;
|
||||
|
||||
private RecyclableArrayList(Handle handle) {
|
||||
private RecyclableArrayList(Handle<RecyclableArrayList> handle) {
|
||||
this(handle, DEFAULT_INITIAL_CAPACITY);
|
||||
}
|
||||
|
||||
private RecyclableArrayList(Handle handle, int initialCapacity) {
|
||||
private RecyclableArrayList(Handle<RecyclableArrayList> handle, int initialCapacity) {
|
||||
super(initialCapacity);
|
||||
this.handle = handle;
|
||||
}
|
||||
|
@ -49,7 +49,7 @@ public final class ChannelOutboundBuffer {
|
||||
|
||||
private static final Recycler<ChannelOutboundBuffer> RECYCLER = new Recycler<ChannelOutboundBuffer>() {
|
||||
@Override
|
||||
protected ChannelOutboundBuffer newObject(Handle handle) {
|
||||
protected ChannelOutboundBuffer newObject(Handle<ChannelOutboundBuffer> handle) {
|
||||
return new ChannelOutboundBuffer(handle);
|
||||
}
|
||||
};
|
||||
@ -62,7 +62,7 @@ public final class ChannelOutboundBuffer {
|
||||
return buffer;
|
||||
}
|
||||
|
||||
private final Handle handle;
|
||||
private final Handle<ChannelOutboundBuffer> handle;
|
||||
|
||||
private AbstractChannel channel;
|
||||
|
||||
@ -104,7 +104,7 @@ public final class ChannelOutboundBuffer {
|
||||
|
||||
private volatile int writable = 1;
|
||||
|
||||
private ChannelOutboundBuffer(Handle handle) {
|
||||
private ChannelOutboundBuffer(Handle<ChannelOutboundBuffer> handle) {
|
||||
this.handle = handle;
|
||||
|
||||
buffer = new Entry[INITIAL_CAPACITY];
|
||||
@ -638,5 +638,4 @@ public final class ChannelOutboundBuffer {
|
||||
cancelled = false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user