/** * {@inheritDoc} */ @Override public ByteBufferWrapper allocate(final int size) { if (size <= maxSmallBufferSize) { final SmallByteBufferWrapper buffer = createSmallBuffer(); buffer.limit(size); return buffer; } return wrap(allocateByteBuffer(size)); }
/** * {@inheritDoc} */ @Override public ByteBufferWrapper allocateAtLeast(int size) { if (size <= maxSmallBufferSize) { final SmallByteBufferWrapper buffer = createSmallBuffer(); buffer.limit(size); return buffer; } return wrap(allocateByteBufferAtLeast(size)); }
/** * {@inheritDoc} */ @Override public ByteBufferWrapper wrap(final ByteBuffer byteBuffer) { return createTrimAwareBuffer(byteBuffer); }
/** * Allocates {@link ByteBuffer} of required size. * * @param size {@link ByteBuffer} size. * @return allocated {@link ByteBuffer}. */ @Override @SuppressWarnings("unchecked") public ByteBuffer allocateByteBuffer(final int size) { if (size > maxBufferSize) { // Don't use pool return allocateByteBuffer0(size); } final ThreadLocalPool<ByteBuffer> threadLocalCache = getByteBufferThreadLocalPool(); if (threadLocalCache != null) { final int remaining = threadLocalCache.remaining(); if (remaining == 0 || remaining < size) { reallocatePoolBuffer(); } return (ByteBuffer) allocateFromPool(threadLocalCache, size); } else { return allocateByteBuffer0(size); } }
/** * {@inheritDoc} */ @Override public ByteBufferWrapper reallocate(ByteBufferWrapper oldBuffer, int newSize) { return wrap(reallocateByteBuffer(oldBuffer.underlying(), newSize)); }
@SuppressWarnings({"unchecked"}) private void reallocatePoolBuffer() { final ByteBuffer byteBuffer = allocateByteBuffer0(maxBufferSize); final ThreadLocalPool<ByteBuffer> threadLocalCache = getByteBufferThreadLocalPool(); if (threadLocalCache != null) { threadLocalCache.reset(byteBuffer); } }
@SuppressWarnings("unchecked") private static ByteBufferThreadLocalPool getByteBufferThreadLocalPool() { final ThreadLocalPool pool = getThreadLocalPool(); return ((pool instanceof ByteBufferThreadLocalPool) ? (ByteBufferThreadLocalPool) pool : null); }
@Override @SuppressWarnings("unchecked") public ByteBuffer reallocateByteBuffer(ByteBuffer oldByteBuffer, int newSize) { if (oldByteBuffer.capacity() >= newSize) return oldByteBuffer; final ThreadLocalPool<ByteBuffer> memoryPool = getByteBufferThreadLocalPool(); if (memoryPool != null) { final ByteBuffer newBuffer = memoryPool.reallocate(oldByteBuffer, newSize); if (newBuffer != null) { ProbeNotifier.notifyBufferAllocatedFromPool(monitoringConfig, newSize - oldByteBuffer.capacity()); return newBuffer; } } ByteBuffer newByteBuffer = allocateByteBuffer(newSize); oldByteBuffer.flip(); return newByteBuffer.put(oldByteBuffer); }
protected SmallByteBufferWrapper createSmallBuffer() { final SmallByteBufferWrapper buffer = ThreadCache.takeFromCache(SMALL_BUFFER_CACHE_IDX); if (buffer != null) { ProbeNotifier.notifyBufferAllocatedFromPool(monitoringConfig, maxSmallBufferSize); return buffer; } return new SmallByteBufferWrapper(allocateByteBuffer0(maxSmallBufferSize)); }
@Override @SuppressWarnings("unchecked") public void releaseByteBuffer(ByteBuffer byteBuffer) { ThreadLocalPool<ByteBuffer> memoryPool = getByteBufferThreadLocalPool(); if (memoryPool != null) { if (memoryPool.release((ByteBuffer) byteBuffer.clear())) { ProbeNotifier.notifyBufferReleasedToPool(monitoringConfig, byteBuffer.capacity()); } } }
/** * Allocates {@link ByteBuffer} of required size. * * @param size {@link ByteBuffer} size. * @return allocated {@link ByteBuffer}. */ @Override @SuppressWarnings("unchecked") public ByteBuffer allocateByteBuffer(final int size) { if (size > maxBufferSize) { // Don't use pool return allocateByteBuffer0(size); } final ThreadLocalPool<ByteBuffer> threadLocalCache = getByteBufferThreadLocalPool(); if (threadLocalCache != null) { final int remaining = threadLocalCache.remaining(); if (remaining == 0 || remaining < size) { reallocatePoolBuffer(); } return (ByteBuffer) allocateFromPool(threadLocalCache, size); } else { return allocateByteBuffer0(size); } }
/** * {@inheritDoc} */ @Override public ByteBufferWrapper reallocate(ByteBufferWrapper oldBuffer, int newSize) { return wrap(reallocateByteBuffer(oldBuffer.underlying(), newSize)); }
@SuppressWarnings({"unchecked"}) private void reallocatePoolBuffer() { final ByteBuffer byteBuffer = allocateByteBuffer0(maxBufferSize); final ThreadLocalPool<ByteBuffer> threadLocalCache = getByteBufferThreadLocalPool(); if (threadLocalCache != null) { threadLocalCache.reset(byteBuffer); } }
@SuppressWarnings("unchecked") private static ByteBufferThreadLocalPool getByteBufferThreadLocalPool() { final ThreadLocalPool pool = getThreadLocalPool(); return ((pool instanceof ByteBufferThreadLocalPool) ? (ByteBufferThreadLocalPool) pool : null); }
@Override @SuppressWarnings("unchecked") public ByteBuffer reallocateByteBuffer(ByteBuffer oldByteBuffer, int newSize) { if (oldByteBuffer.capacity() >= newSize) return oldByteBuffer; final ThreadLocalPool<ByteBuffer> memoryPool = getByteBufferThreadLocalPool(); if (memoryPool != null) { final ByteBuffer newBuffer = memoryPool.reallocate(oldByteBuffer, newSize); if (newBuffer != null) { ProbeNotifier.notifyBufferAllocatedFromPool(monitoringConfig, newSize - oldByteBuffer.capacity()); return newBuffer; } } ByteBuffer newByteBuffer = allocateByteBuffer(newSize); oldByteBuffer.flip(); return newByteBuffer.put(oldByteBuffer); }
protected SmallByteBufferWrapper createSmallBuffer() { final SmallByteBufferWrapper buffer = ThreadCache.takeFromCache(SMALL_BUFFER_CACHE_IDX); if (buffer != null) { ProbeNotifier.notifyBufferAllocatedFromPool(monitoringConfig, maxSmallBufferSize); return buffer; } return new SmallByteBufferWrapper(allocateByteBuffer0(maxSmallBufferSize)); }
@Override @SuppressWarnings("unchecked") public void releaseByteBuffer(ByteBuffer byteBuffer) { ThreadLocalPool<ByteBuffer> memoryPool = getByteBufferThreadLocalPool(); if (memoryPool != null) { if (memoryPool.release((ByteBuffer) byteBuffer.clear())) { ProbeNotifier.notifyBufferReleasedToPool(monitoringConfig, byteBuffer.capacity()); } } }
/** * Allocates {@link ByteBuffer} of required size. * * @param size {@link ByteBuffer} size. * @return allocated {@link ByteBuffer}. */ @Override @SuppressWarnings("unchecked") public ByteBuffer allocateByteBuffer(final int size) { if (size > maxBufferSize) { // Don't use pool return allocateByteBuffer0(size); } final ThreadLocalPool<ByteBuffer> threadLocalCache = getByteBufferThreadLocalPool(); if (threadLocalCache != null) { final int remaining = threadLocalCache.remaining(); if (remaining == 0 || remaining < size) { reallocatePoolBuffer(); } return (ByteBuffer) allocateFromPool(threadLocalCache, size); } else { return allocateByteBuffer0(size); } }
/** * {@inheritDoc} */ @Override public ByteBufferWrapper allocate(final int size) { if (size <= maxSmallBufferSize) { final SmallByteBufferWrapper buffer = createSmallBuffer(); buffer.limit(size); return buffer; } return wrap(allocateByteBuffer(size)); }
/** * {@inheritDoc} */ @Override public ByteBufferWrapper allocateAtLeast(int size) { if (size <= maxSmallBufferSize) { final SmallByteBufferWrapper buffer = createSmallBuffer(); buffer.limit(size); return buffer; } return wrap(allocateByteBufferAtLeast(size)); }