package org.xnio;
import java.nio.ByteBuffer;
import java.security.AccessController;
import java.util.ArrayDeque;
import java.util.Collections;
import java.util.HashSet;
import java.util.Queue;
import java.util.Set;
import java.util.concurrent.ConcurrentLinkedQueue;
import static org.xnio._private.Messages.msg;
public final class ByteBufferSlicePool implements Pool<ByteBuffer> {
private static final int LOCAL_LENGTH;
static {
String value = AccessController.doPrivileged(new ReadPropertyAction("xnio.bufferpool.threadlocal.size", "12"));
int val;
try {
val = Integer.parseInt(value);
} catch (NumberFormatException ignored) {
val = 12;
}
LOCAL_LENGTH = val;
}
private final Set<Ref> refSet = Collections.synchronizedSet(new HashSet<Ref>());
private final Queue<Slice> sliceQueue;
private final BufferAllocator<ByteBuffer> allocator;
private final int bufferSize;
private final int buffersPerRegion;
private final int threadLocalQueueSize;
private final ThreadLocal<ThreadLocalCache> localQueueHolder = new ThreadLocal<ThreadLocalCache>() {
protected ThreadLocalCache initialValue() {
return new ThreadLocalCache();
}
public void remove() {
final ArrayDeque<Slice> deque = get().queue;
Slice slice = deque.poll();
while (slice != null) {
doFree(slice);
slice = deque.poll();
}
super.remove();
}
};
public ByteBufferSlicePool(final BufferAllocator<ByteBuffer> allocator, final int bufferSize, final int maxRegionSize, final int threadLocalQueueSize) {
if (bufferSize <= 0) {
throw msg.parameterOutOfRange("bufferSize");
}
if (maxRegionSize < bufferSize) {
throw msg.parameterOutOfRange("bufferSize");
}
buffersPerRegion = maxRegionSize / bufferSize;
this.bufferSize = bufferSize;
this.allocator = allocator;
sliceQueue = new ConcurrentLinkedQueue<Slice>();
this.threadLocalQueueSize = threadLocalQueueSize;
}
public ByteBufferSlicePool(final BufferAllocator<ByteBuffer> allocator, final int bufferSize, final int maxRegionSize) {
this(allocator, bufferSize, maxRegionSize, LOCAL_LENGTH);
}
public ByteBufferSlicePool(final int bufferSize, final int maxRegionSize) {
this(BufferAllocator.DIRECT_BYTE_BUFFER_ALLOCATOR, bufferSize, maxRegionSize);
}
public Pooled<ByteBuffer> allocate() {
Slice slice;
if (threadLocalQueueSize > 0) {
ThreadLocalCache localCache = localQueueHolder.get();
if(localCache.outstanding != threadLocalQueueSize) {
localCache.outstanding++;
}
slice = localCache.queue.poll();
if (slice != null) {
return new PooledByteBuffer(slice, slice.slice());
}
}
final Queue<Slice> sliceQueue = this.sliceQueue;
slice = sliceQueue.poll();
if (slice != null) {
return new PooledByteBuffer(slice, slice.slice());
}
synchronized (sliceQueue) {
slice = sliceQueue.poll();
if (slice != null) {
return new PooledByteBuffer(slice, slice.slice());
}
final int bufferSize = this.bufferSize;
final int buffersPerRegion = this.buffersPerRegion;
final ByteBuffer region = allocator.allocate(buffersPerRegion * bufferSize);
int idx = bufferSize;
for (int i = 1; i < buffersPerRegion; i ++) {
sliceQueue.add(new Slice(region, idx, bufferSize));
idx += bufferSize;
}
final Slice newSlice = new Slice(region, 0, bufferSize);
return new PooledByteBuffer(newSlice, newSlice.slice());
}
}
public int getBufferSize() {
return bufferSize;
}
private void doFree(Slice region) {
if (threadLocalQueueSize > 0) {
final ThreadLocalCache localCache = localQueueHolder.get();
boolean cacheOk = false;
if(localCache.outstanding > 0) {
localCache.outstanding--;
cacheOk = true;
}
ArrayDeque<Slice> localQueue = localCache.queue;
if (localQueue.size() == threadLocalQueueSize || !cacheOk) {
sliceQueue.add(region);
} else {
localQueue.add(region);
}
} else {
sliceQueue.add(region);
}
}
private final class PooledByteBuffer implements Pooled<ByteBuffer> {
private final Slice region;
ByteBuffer buffer;
PooledByteBuffer(final Slice region, final ByteBuffer buffer) {
this.region = region;
this.buffer = buffer;
}
public void discard() {
final ByteBuffer buffer = this.buffer;
this.buffer = null;
if (buffer != null) {
refSet.add(new Ref(buffer, region));
}
}
public void free() {
ByteBuffer buffer = this.buffer;
this.buffer = null;
if (buffer != null) {
doFree(region);
}
}
public ByteBuffer getResource() {
final ByteBuffer buffer = this.buffer;
if (buffer == null) {
throw msg.bufferFreed();
}
return buffer;
}
public void close() {
free();
}
public String toString() {
return "Pooled buffer " + buffer;
}
}
private final class Slice {
private final ByteBuffer parent;
private Slice(final ByteBuffer parent, final int start, final int size) {
this.parent = (ByteBuffer)parent.duplicate().position(start).limit(start+size);
}
ByteBuffer slice() {
return parent.slice();
}
}
final class Ref extends AutomaticReference<ByteBuffer> {
private final Slice region;
private Ref(final ByteBuffer referent, final Slice region) {
super(referent, AutomaticReference.PERMIT);
this.region = region;
}
protected void free() {
doFree(region);
refSet.remove(this);
}
}
private final class ThreadLocalCache {
final ArrayDeque<Slice> queue = new ArrayDeque<Slice>(threadLocalQueueSize) {
protected void finalize() {
final ArrayDeque<Slice> deque = queue;
Slice slice = deque.poll();
while (slice != null) {
doFree(slice);
slice = deque.poll();
}
}
};
int outstanding = 0;
ThreadLocalCache() {
}
}
}