Package io.netty.buffer
Class PoolThreadCache
- java.lang.Object
-
- io.netty.buffer.PoolThreadCache
-
final class PoolThreadCache extends java.lang.Object
Acts a Thread cache for allocations. This implementation is moduled after jemalloc and the descripted technics of Scalable memory allocation using jemalloc.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
PoolThreadCache.FreeOnFinalize
private static class
PoolThreadCache.MemoryRegionCache<T>
private static class
PoolThreadCache.NormalMemoryRegionCache<T>
Cache used for buffers which are backed by NORMAL size.private static class
PoolThreadCache.SubPageMemoryRegionCache<T>
Cache used for buffers which are backed by TINY or SMALL size.
-
Field Summary
Fields Modifier and Type Field Description private int
allocations
(package private) PoolArena<java.nio.ByteBuffer>
directArena
private java.util.concurrent.atomic.AtomicBoolean
freed
private PoolThreadCache.FreeOnFinalize
freeOnFinalize
private int
freeSweepAllocationThreshold
(package private) PoolArena<byte[]>
heapArena
private static int
INTEGER_SIZE_MINUS_ONE
private static InternalLogger
logger
private PoolThreadCache.MemoryRegionCache<java.nio.ByteBuffer>[]
normalDirectCaches
private PoolThreadCache.MemoryRegionCache<byte[]>[]
normalHeapCaches
private PoolThreadCache.MemoryRegionCache<java.nio.ByteBuffer>[]
smallSubPageDirectCaches
private PoolThreadCache.MemoryRegionCache<byte[]>[]
smallSubPageHeapCaches
-
Constructor Summary
Constructors Constructor Description PoolThreadCache(PoolArena<byte[]> heapArena, PoolArena<java.nio.ByteBuffer> directArena, int smallCacheSize, int normalCacheSize, int maxCachedBufferCapacity, int freeSweepAllocationThreshold, boolean useFinalizer)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description (package private) boolean
add(PoolArena<?> area, PoolChunk chunk, java.nio.ByteBuffer nioBuffer, long handle, int normCapacity, PoolArena.SizeClass sizeClass)
AddPoolChunk
andhandle
to the cache if there is enough room.private boolean
allocate(PoolThreadCache.MemoryRegionCache<?> cache, PooledByteBuf buf, int reqCapacity)
(package private) boolean
allocateNormal(PoolArena<?> area, PooledByteBuf<?> buf, int reqCapacity, int sizeIdx)
Try to allocate a normal buffer out of the cache.(package private) boolean
allocateSmall(PoolArena<?> area, PooledByteBuf<?> buf, int reqCapacity, int sizeIdx)
Try to allocate a small buffer out of the cache.private PoolThreadCache.MemoryRegionCache<?>
cache(PoolArena<?> area, int sizeIdx, PoolArena.SizeClass sizeClass)
private static <T> PoolThreadCache.MemoryRegionCache<T>
cache(PoolThreadCache.MemoryRegionCache<T>[] cache, int sizeIdx)
private PoolThreadCache.MemoryRegionCache<?>
cacheForNormal(PoolArena<?> area, int sizeIdx)
private PoolThreadCache.MemoryRegionCache<?>
cacheForSmall(PoolArena<?> area, int sizeIdx)
private static void
checkCacheMayLeak(PoolThreadCache.MemoryRegionCache<?>[] caches, java.lang.String type)
private static <T> PoolThreadCache.MemoryRegionCache<T>[]
createNormalCaches(int cacheSize, int maxCachedBufferCapacity, PoolArena<T> area)
private static <T> PoolThreadCache.MemoryRegionCache<T>[]
createSubPageCaches(int cacheSize, int numCaches)
(package private) void
free(boolean finalizer)
Should be called if the Thread that uses this cache is about to exist to release resources out of the cacheprivate static int
free(PoolThreadCache.MemoryRegionCache<?>[] caches, boolean finalizer)
private static int
free(PoolThreadCache.MemoryRegionCache<?> cache, boolean finalizer)
(package private) static int
log2(int val)
(package private) void
trim()
private static void
trim(PoolThreadCache.MemoryRegionCache<?> cache)
private static void
trim(PoolThreadCache.MemoryRegionCache<?>[] caches)
-
-
-
Field Detail
-
logger
private static final InternalLogger logger
-
INTEGER_SIZE_MINUS_ONE
private static final int INTEGER_SIZE_MINUS_ONE
- See Also:
- Constant Field Values
-
heapArena
final PoolArena<byte[]> heapArena
-
directArena
final PoolArena<java.nio.ByteBuffer> directArena
-
smallSubPageHeapCaches
private final PoolThreadCache.MemoryRegionCache<byte[]>[] smallSubPageHeapCaches
-
smallSubPageDirectCaches
private final PoolThreadCache.MemoryRegionCache<java.nio.ByteBuffer>[] smallSubPageDirectCaches
-
normalHeapCaches
private final PoolThreadCache.MemoryRegionCache<byte[]>[] normalHeapCaches
-
normalDirectCaches
private final PoolThreadCache.MemoryRegionCache<java.nio.ByteBuffer>[] normalDirectCaches
-
freeSweepAllocationThreshold
private final int freeSweepAllocationThreshold
-
freed
private final java.util.concurrent.atomic.AtomicBoolean freed
-
freeOnFinalize
private final PoolThreadCache.FreeOnFinalize freeOnFinalize
-
allocations
private int allocations
-
-
Method Detail
-
createSubPageCaches
private static <T> PoolThreadCache.MemoryRegionCache<T>[] createSubPageCaches(int cacheSize, int numCaches)
-
createNormalCaches
private static <T> PoolThreadCache.MemoryRegionCache<T>[] createNormalCaches(int cacheSize, int maxCachedBufferCapacity, PoolArena<T> area)
-
log2
static int log2(int val)
-
allocateSmall
boolean allocateSmall(PoolArena<?> area, PooledByteBuf<?> buf, int reqCapacity, int sizeIdx)
Try to allocate a small buffer out of the cache. Returnstrue
if successfulfalse
otherwise
-
allocateNormal
boolean allocateNormal(PoolArena<?> area, PooledByteBuf<?> buf, int reqCapacity, int sizeIdx)
Try to allocate a normal buffer out of the cache. Returnstrue
if successfulfalse
otherwise
-
allocate
private boolean allocate(PoolThreadCache.MemoryRegionCache<?> cache, PooledByteBuf buf, int reqCapacity)
-
add
boolean add(PoolArena<?> area, PoolChunk chunk, java.nio.ByteBuffer nioBuffer, long handle, int normCapacity, PoolArena.SizeClass sizeClass)
AddPoolChunk
andhandle
to the cache if there is enough room. Returnstrue
if it fit into the cachefalse
otherwise.
-
cache
private PoolThreadCache.MemoryRegionCache<?> cache(PoolArena<?> area, int sizeIdx, PoolArena.SizeClass sizeClass)
-
free
void free(boolean finalizer)
Should be called if the Thread that uses this cache is about to exist to release resources out of the cache
-
checkCacheMayLeak
private static void checkCacheMayLeak(PoolThreadCache.MemoryRegionCache<?>[] caches, java.lang.String type)
-
free
private static int free(PoolThreadCache.MemoryRegionCache<?>[] caches, boolean finalizer)
-
free
private static int free(PoolThreadCache.MemoryRegionCache<?> cache, boolean finalizer)
-
trim
void trim()
-
trim
private static void trim(PoolThreadCache.MemoryRegionCache<?>[] caches)
-
trim
private static void trim(PoolThreadCache.MemoryRegionCache<?> cache)
-
cacheForSmall
private PoolThreadCache.MemoryRegionCache<?> cacheForSmall(PoolArena<?> area, int sizeIdx)
-
cacheForNormal
private PoolThreadCache.MemoryRegionCache<?> cacheForNormal(PoolArena<?> area, int sizeIdx)
-
cache
private static <T> PoolThreadCache.MemoryRegionCache<T> cache(PoolThreadCache.MemoryRegionCache<T>[] cache, int sizeIdx)
-
-