diff --git a/buffer/src/main/java/io/netty/buffer/PoolArena.java b/buffer/src/main/java/io/netty/buffer/PoolArena.java index 0c0124d2c6a8..e613a9aa383d 100644 --- a/buffer/src/main/java/io/netty/buffer/PoolArena.java +++ b/buffer/src/main/java/io/netty/buffer/PoolArena.java @@ -30,7 +30,7 @@ import static io.netty.buffer.PoolChunk.isSubpage; import static java.lang.Math.max; -abstract class PoolArena extends SizeClasses implements PoolArenaMetric { +abstract class PoolArena implements PoolArenaMetric { private static final boolean HAS_UNSAFE = PlatformDependent.hasUnsafe(); enum SizeClass { @@ -40,8 +40,6 @@ enum SizeClass { final PooledByteBufAllocator parent; - final int numSmallSubpagePools; - final int directMemoryCacheAlignment; final PoolSubpage[] smallSubpagePools; private final PoolChunkList q050; @@ -74,24 +72,23 @@ enum SizeClass { private final ReentrantLock lock = new ReentrantLock(); - protected PoolArena(PooledByteBufAllocator parent, int pageSize, - int pageShifts, int chunkSize, int cacheAlignment) { - super(pageSize, pageShifts, chunkSize, cacheAlignment); - this.parent = parent; - directMemoryCacheAlignment = cacheAlignment; + final SizeClasses sizeClass; - numSmallSubpagePools = nSubpages; - smallSubpagePools = newSubpagePoolArray(numSmallSubpagePools); + protected PoolArena(PooledByteBufAllocator parent, SizeClasses sizeClass) { + assert null != sizeClass; + this.parent = parent; + this.sizeClass = sizeClass; + smallSubpagePools = newSubpagePoolArray(sizeClass.nSubpages); for (int i = 0; i < smallSubpagePools.length; i ++) { smallSubpagePools[i] = newSubpagePoolHead(i); } - q100 = new PoolChunkList(this, null, 100, Integer.MAX_VALUE, chunkSize); - q075 = new PoolChunkList(this, q100, 75, 100, chunkSize); - q050 = new PoolChunkList(this, q075, 50, 100, chunkSize); - q025 = new PoolChunkList(this, q050, 25, 75, chunkSize); - q000 = new PoolChunkList(this, q025, 1, 50, chunkSize); - qInit = new PoolChunkList(this, q000, Integer.MIN_VALUE, 25, chunkSize); + q100 = new PoolChunkList(this, null, 100, Integer.MAX_VALUE, sizeClass.chunkSize); + q075 = new PoolChunkList(this, q100, 75, 100, sizeClass.chunkSize); + q050 = new PoolChunkList(this, q075, 50, 100, sizeClass.chunkSize); + q025 = new PoolChunkList(this, q050, 25, 75, sizeClass.chunkSize); + q000 = new PoolChunkList(this, q025, 1, 50, sizeClass.chunkSize); + qInit = new PoolChunkList(this, q000, Integer.MIN_VALUE, 25, sizeClass.chunkSize); q100.prevList(q075); q075.prevList(q050); @@ -131,15 +128,15 @@ PooledByteBuf allocate(PoolThreadCache cache, int reqCapacity, int maxCapacit } private void allocate(PoolThreadCache cache, PooledByteBuf buf, final int reqCapacity) { - final int sizeIdx = size2SizeIdx(reqCapacity); + final int sizeIdx = sizeClass.size2SizeIdx(reqCapacity); - if (sizeIdx <= smallMaxSizeIdx) { + if (sizeIdx <= sizeClass.smallMaxSizeIdx) { tcacheAllocateSmall(cache, buf, reqCapacity, sizeIdx); - } else if (sizeIdx < nSizes) { + } else if (sizeIdx < sizeClass.nSizes) { tcacheAllocateNormal(cache, buf, reqCapacity, sizeIdx); } else { - int normCapacity = directMemoryCacheAlignment > 0 - ? normalizeSize(reqCapacity) : reqCapacity; + int normCapacity = sizeClass.directMemoryCacheAlignment > 0 + ? sizeClass.normalizeSize(reqCapacity) : reqCapacity; // Huge allocations are never served via the cache so just call allocateHuge allocateHuge(buf, normCapacity); } @@ -164,7 +161,7 @@ private void tcacheAllocateSmall(PoolThreadCache cache, PooledByteBuf buf, fi final PoolSubpage s = head.next; needsNormalAllocation = s == head; if (!needsNormalAllocation) { - assert s.doNotDestroy && s.elemSize == sizeIdx2size(sizeIdx) : "doNotDestroy=" + + assert s.doNotDestroy && s.elemSize == sizeClass.sizeIdx2size(sizeIdx) : "doNotDestroy=" + s.doNotDestroy + ", elemSize=" + s.elemSize + ", sizeIdx=" + sizeIdx; long handle = s.allocate(); assert handle >= 0; @@ -212,7 +209,7 @@ private void allocateNormal(PooledByteBuf buf, int reqCapacity, int sizeIdx, } // Add a new chunk. - PoolChunk c = newChunk(pageSize, nPSizes, pageShifts, chunkSize); + PoolChunk c = newChunk(sizeClass.pageSize, sizeClass.nPSizes, sizeClass.pageShifts, sizeClass.chunkSize); boolean success = c.allocate(buf, reqCapacity, sizeIdx, threadCache); assert success; qInit.add(c); @@ -625,10 +622,8 @@ private void destroyPoolChunkLists(PoolChunkList... chunkLists) { static final class HeapArena extends PoolArena { - HeapArena(PooledByteBufAllocator parent, int pageSize, int pageShifts, - int chunkSize) { - super(parent, pageSize, pageShifts, chunkSize, - 0); + HeapArena(PooledByteBufAllocator parent, SizeClasses sizeClass) { + super(parent, sizeClass); } private static byte[] newByteArray(int size) { @@ -674,10 +669,8 @@ protected void memoryCopy(byte[] src, int srcOffset, PooledByteBuf dst, static final class DirectArena extends PoolArena { - DirectArena(PooledByteBufAllocator parent, int pageSize, int pageShifts, - int chunkSize, int directMemoryCacheAlignment) { - super(parent, pageSize, pageShifts, chunkSize, - directMemoryCacheAlignment); + DirectArena(PooledByteBufAllocator parent, SizeClasses sizeClass) { + super(parent, sizeClass); } @Override @@ -688,27 +681,27 @@ boolean isDirect() { @Override protected PoolChunk newChunk(int pageSize, int maxPageIdx, int pageShifts, int chunkSize) { - if (directMemoryCacheAlignment == 0) { + if (sizeClass.directMemoryCacheAlignment == 0) { ByteBuffer memory = allocateDirect(chunkSize); return new PoolChunk(this, memory, memory, pageSize, pageShifts, chunkSize, maxPageIdx); } - final ByteBuffer base = allocateDirect(chunkSize + directMemoryCacheAlignment); - final ByteBuffer memory = PlatformDependent.alignDirectBuffer(base, directMemoryCacheAlignment); + final ByteBuffer base = allocateDirect(chunkSize + sizeClass.directMemoryCacheAlignment); + final ByteBuffer memory = PlatformDependent.alignDirectBuffer(base, sizeClass.directMemoryCacheAlignment); return new PoolChunk(this, base, memory, pageSize, pageShifts, chunkSize, maxPageIdx); } @Override protected PoolChunk newUnpooledChunk(int capacity) { - if (directMemoryCacheAlignment == 0) { + if (sizeClass.directMemoryCacheAlignment == 0) { ByteBuffer memory = allocateDirect(capacity); return new PoolChunk(this, memory, memory, capacity); } - final ByteBuffer base = allocateDirect(capacity + directMemoryCacheAlignment); - final ByteBuffer memory = PlatformDependent.alignDirectBuffer(base, directMemoryCacheAlignment); + final ByteBuffer base = allocateDirect(capacity + sizeClass.directMemoryCacheAlignment); + final ByteBuffer memory = PlatformDependent.alignDirectBuffer(base, sizeClass.directMemoryCacheAlignment); return new PoolChunk(this, base, memory, capacity); } @@ -763,4 +756,44 @@ void lock() { void unlock() { lock.unlock(); } + + @Override + public int sizeIdx2size(int sizeIdx) { + return sizeClass.sizeIdx2size(sizeIdx); + } + + @Override + public int sizeIdx2sizeCompute(int sizeIdx) { + return sizeClass.sizeIdx2sizeCompute(sizeIdx); + } + + @Override + public long pageIdx2size(int pageIdx) { + return sizeClass.pageIdx2size(pageIdx); + } + + @Override + public long pageIdx2sizeCompute(int pageIdx) { + return sizeClass.pageIdx2sizeCompute(pageIdx); + } + + @Override + public int size2SizeIdx(int size) { + return sizeClass.size2SizeIdx(size); + } + + @Override + public int pages2pageIdx(int pages) { + return sizeClass.pages2pageIdx(pages); + } + + @Override + public int pages2pageIdxFloor(int pages) { + return sizeClass.pages2pageIdxFloor(pages); + } + + @Override + public int normalizeSize(int size) { + return sizeClass.normalizeSize(size); + } } diff --git a/buffer/src/main/java/io/netty/buffer/PoolChunk.java b/buffer/src/main/java/io/netty/buffer/PoolChunk.java index 7567160555e7..2d53374f3840 100644 --- a/buffer/src/main/java/io/netty/buffer/PoolChunk.java +++ b/buffer/src/main/java/io/netty/buffer/PoolChunk.java @@ -39,7 +39,7 @@ * return a (long) handle that encodes this offset information, (this memory segment is then * marked as reserved so it is always used by exactly one ByteBuf and no more) * - * For simplicity all sizes are normalized according to {@link PoolArena#size2SizeIdx(int)} method. + * For simplicity all sizes are normalized according to {@link PoolArena#sizeClass#size2SizeIdx(int)} method. * This ensures that when we request for memory segments of size > pageSize the normalizedCapacity * equals the next nearest size in {@link SizeClasses}. * @@ -240,7 +240,7 @@ private static IntPriorityQueue[] newRunsAvailqueueArray(int size) { } private void insertAvailRun(int runOffset, int pages, long handle) { - int pageIdxFloor = arena.pages2pageIdxFloor(pages); + int pageIdxFloor = arena.sizeClass.pages2pageIdxFloor(pages); IntPriorityQueue queue = runsAvail[pageIdxFloor]; assert isRun(handle); queue.offer((int) (handle >> BITMAP_IDX_BIT_LENGTH)); @@ -259,7 +259,7 @@ private void insertAvailRun0(int runOffset, long handle) { } private void removeAvailRun(long handle) { - int pageIdxFloor = arena.pages2pageIdxFloor(runPages(handle)); + int pageIdxFloor = arena.sizeClass.pages2pageIdxFloor(runPages(handle)); runsAvail[pageIdxFloor].remove((int) (handle >> BITMAP_IDX_BIT_LENGTH)); removeAvailRun0(handle); } @@ -313,7 +313,7 @@ private int usage(int freeBytes) { boolean allocate(PooledByteBuf buf, int reqCapacity, int sizeIdx, PoolThreadCache cache) { final long handle; - if (sizeIdx <= arena.smallMaxSizeIdx) { + if (sizeIdx <= arena.sizeClass.smallMaxSizeIdx) { final PoolSubpage nextSub; // small // Obtain the head of the PoolSubPage pool that is owned by the PoolArena and synchronize on it. @@ -323,8 +323,9 @@ boolean allocate(PooledByteBuf buf, int reqCapacity, int sizeIdx, PoolThreadC try { nextSub = head.next; if (nextSub != head) { - assert nextSub.doNotDestroy && nextSub.elemSize == arena.sizeIdx2size(sizeIdx) : "doNotDestroy=" + - nextSub.doNotDestroy + ", elemSize=" + nextSub.elemSize + ", sizeIdx=" + sizeIdx; + assert nextSub.doNotDestroy && nextSub.elemSize == arena.sizeClass.sizeIdx2size(sizeIdx) : + "doNotDestroy=" + nextSub.doNotDestroy + ", elemSize=" + nextSub.elemSize + ", sizeIdx=" + + sizeIdx; handle = nextSub.allocate(); assert handle >= 0; assert isSubpage(handle); @@ -342,7 +343,7 @@ boolean allocate(PooledByteBuf buf, int reqCapacity, int sizeIdx, PoolThreadC } else { // normal // runSize must be multiple of pageSize - int runSize = arena.sizeIdx2size(sizeIdx); + int runSize = arena.sizeClass.sizeIdx2size(sizeIdx); handle = allocateRun(runSize); if (handle < 0) { return false; @@ -357,7 +358,7 @@ boolean allocate(PooledByteBuf buf, int reqCapacity, int sizeIdx, PoolThreadC private long allocateRun(int runSize) { int pages = runSize >> pageShifts; - int pageIdx = arena.pages2pageIdx(pages); + int pageIdx = arena.sizeClass.pages2pageIdx(pages); runsAvailLock.lock(); try { @@ -391,7 +392,7 @@ private int calculateRunSize(int sizeIdx) { int runSize = 0; int nElements; - final int elemSize = arena.sizeIdx2size(sizeIdx); + final int elemSize = arena.sizeClass.sizeIdx2size(sizeIdx); //find lowest common multiple of pageSize and elemSize do { @@ -413,9 +414,9 @@ private int calculateRunSize(int sizeIdx) { private int runFirstBestFit(int pageIdx) { if (freeBytes == chunkSize) { - return arena.nPSizes - 1; + return arena.sizeClass.nPSizes - 1; } - for (int i = pageIdx; i < arena.nPSizes; i++) { + for (int i = pageIdx; i < arena.sizeClass.nPSizes; i++) { IntPriorityQueue queue = runsAvail[i]; if (queue != null && !queue.isEmpty()) { return i; @@ -469,7 +470,7 @@ private long allocateSubpage(int sizeIdx, PoolSubpage head) { int runOffset = runOffset(runHandle); assert subpages[runOffset] == null; - int elemSize = arena.sizeIdx2size(sizeIdx); + int elemSize = arena.sizeClass.sizeIdx2size(sizeIdx); PoolSubpage subpage = new PoolSubpage(head, this, pageShifts, runOffset, runSize(pageShifts, runHandle), elemSize); diff --git a/buffer/src/main/java/io/netty/buffer/PoolChunkList.java b/buffer/src/main/java/io/netty/buffer/PoolChunkList.java index aa28448d647a..19daec7919a8 100644 --- a/buffer/src/main/java/io/netty/buffer/PoolChunkList.java +++ b/buffer/src/main/java/io/netty/buffer/PoolChunkList.java @@ -97,7 +97,7 @@ void prevList(PoolChunkList prevList) { } boolean allocate(PooledByteBuf buf, int reqCapacity, int sizeIdx, PoolThreadCache threadCache) { - int normCapacity = arena.sizeIdx2size(sizeIdx); + int normCapacity = arena.sizeClass.sizeIdx2size(sizeIdx); if (normCapacity > maxCapacity) { // Either this PoolChunkList is empty or the requested capacity is larger then the capacity which can // be handled by the PoolChunks that are contained in this PoolChunkList. diff --git a/buffer/src/main/java/io/netty/buffer/PoolThreadCache.java b/buffer/src/main/java/io/netty/buffer/PoolThreadCache.java index 1b252d67666b..3c10b5af7ca1 100644 --- a/buffer/src/main/java/io/netty/buffer/PoolThreadCache.java +++ b/buffer/src/main/java/io/netty/buffer/PoolThreadCache.java @@ -74,12 +74,8 @@ final class PoolThreadCache { this.heapArena = heapArena; this.directArena = directArena; if (directArena != null) { - smallSubPageDirectCaches = createSubPageCaches( - smallCacheSize, directArena.numSmallSubpagePools); - - normalDirectCaches = createNormalCaches( - normalCacheSize, maxCachedBufferCapacity, directArena); - + smallSubPageDirectCaches = createSubPageCaches(smallCacheSize, directArena.sizeClass.nSubpages); + normalDirectCaches = createNormalCaches(normalCacheSize, maxCachedBufferCapacity, directArena); directArena.numThreadCaches.getAndIncrement(); } else { // No directArea is configured so just null out all caches @@ -88,12 +84,8 @@ final class PoolThreadCache { } if (heapArena != null) { // Create the caches for the heap allocations - smallSubPageHeapCaches = createSubPageCaches( - smallCacheSize, heapArena.numSmallSubpagePools); - - normalHeapCaches = createNormalCaches( - normalCacheSize, maxCachedBufferCapacity, heapArena); - + smallSubPageHeapCaches = createSubPageCaches(smallCacheSize, heapArena.sizeClass.nSubpages); + normalHeapCaches = createNormalCaches(normalCacheSize, maxCachedBufferCapacity, heapArena); heapArena.numThreadCaches.getAndIncrement(); } else { // No heapArea is configured so just null out all caches @@ -130,11 +122,12 @@ private static MemoryRegionCache[] createSubPageCaches( private static MemoryRegionCache[] createNormalCaches( int cacheSize, int maxCachedBufferCapacity, PoolArena area) { if (cacheSize > 0 && maxCachedBufferCapacity > 0) { - int max = Math.min(area.chunkSize, maxCachedBufferCapacity); + int max = Math.min(area.sizeClass.chunkSize, maxCachedBufferCapacity); // Create as many normal caches as we support based on how many sizeIdx we have and what the upper // bound is that we want to cache in general. List> cache = new ArrayList>() ; - for (int idx = area.numSmallSubpagePools; idx < area.nSizes && area.sizeIdx2size(idx) <= max ; idx++) { + for (int idx = area.sizeClass.nSubpages; idx < area.sizeClass.nSizes && + area.sizeClass.sizeIdx2size(idx) <= max; idx++) { cache.add(new NormalMemoryRegionCache(cacheSize)); } return cache.toArray(new MemoryRegionCache[0]); @@ -183,7 +176,7 @@ private boolean allocate(MemoryRegionCache cache, PooledByteBuf buf, int reqC @SuppressWarnings({ "unchecked", "rawtypes" }) boolean add(PoolArena area, PoolChunk chunk, ByteBuffer nioBuffer, long handle, int normCapacity, SizeClass sizeClass) { - int sizeIdx = area.size2SizeIdx(normCapacity); + int sizeIdx = area.sizeClass.size2SizeIdx(normCapacity); MemoryRegionCache cache = cache(area, sizeIdx, sizeClass); if (cache == null) { return false; @@ -297,8 +290,8 @@ private MemoryRegionCache cacheForSmall(PoolArena area, int sizeIdx) { } private MemoryRegionCache cacheForNormal(PoolArena area, int sizeIdx) { - // We need to substract area.numSmallSubpagePools as sizeIdx is the overall index for all sizes. - int idx = sizeIdx - area.numSmallSubpagePools; + // We need to subtract area.sizeClass.nSubpages as sizeIdx is the overall index for all sizes. + int idx = sizeIdx - area.sizeClass.nSubpages; if (area.isDirect()) { return cache(normalDirectCaches, idx); } diff --git a/buffer/src/main/java/io/netty/buffer/PooledByteBuf.java b/buffer/src/main/java/io/netty/buffer/PooledByteBuf.java index c21b76b7281b..8e404c92e7b5 100644 --- a/buffer/src/main/java/io/netty/buffer/PooledByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/PooledByteBuf.java @@ -60,7 +60,8 @@ private void init0(PoolChunk chunk, ByteBuffer nioBuffer, long handle, int offset, int length, int maxLength, PoolThreadCache cache) { assert handle >= 0; assert chunk != null; - assert !PoolChunk.isSubpage(handle) || chunk.arena.size2SizeIdx(maxLength) <= chunk.arena.smallMaxSizeIdx: + assert !PoolChunk.isSubpage(handle) || + chunk.arena.sizeClass.size2SizeIdx(maxLength) <= chunk.arena.sizeClass.smallMaxSizeIdx: "Allocated small sub-page handle for a buffer size that isn't \"small.\""; chunk.incrementPinnedMemory(maxLength); diff --git a/buffer/src/main/java/io/netty/buffer/PooledByteBufAllocator.java b/buffer/src/main/java/io/netty/buffer/PooledByteBufAllocator.java index 8c922de7b521..65d14c4b30af 100644 --- a/buffer/src/main/java/io/netty/buffer/PooledByteBufAllocator.java +++ b/buffer/src/main/java/io/netty/buffer/PooledByteBufAllocator.java @@ -300,8 +300,9 @@ public PooledByteBufAllocator(boolean preferDirect, int nHeapArena, int nDirectA if (nHeapArena > 0) { heapArenas = newArenaArray(nHeapArena); List metrics = new ArrayList(heapArenas.length); + final SizeClasses sizeClasses = new SizeClasses(pageSize, pageShifts, chunkSize, 0); for (int i = 0; i < heapArenas.length; i ++) { - PoolArena.HeapArena arena = new PoolArena.HeapArena(this, pageSize, pageShifts, chunkSize); + PoolArena.HeapArena arena = new PoolArena.HeapArena(this, sizeClasses); heapArenas[i] = arena; metrics.add(arena); } @@ -314,9 +315,10 @@ public PooledByteBufAllocator(boolean preferDirect, int nHeapArena, int nDirectA if (nDirectArena > 0) { directArenas = newArenaArray(nDirectArena); List metrics = new ArrayList(directArenas.length); + final SizeClasses sizeClasses = new SizeClasses(pageSize, pageShifts, chunkSize, + directMemoryCacheAlignment); for (int i = 0; i < directArenas.length; i ++) { - PoolArena.DirectArena arena = new PoolArena.DirectArena( - this, pageSize, pageShifts, chunkSize, directMemoryCacheAlignment); + PoolArena.DirectArena arena = new PoolArena.DirectArena(this, sizeClasses); directArenas[i] = arena; metrics.add(arena); } diff --git a/buffer/src/main/java/io/netty/buffer/SizeClasses.java b/buffer/src/main/java/io/netty/buffer/SizeClasses.java index c9c54caa4d9d..327387d5c75f 100644 --- a/buffer/src/main/java/io/netty/buffer/SizeClasses.java +++ b/buffer/src/main/java/io/netty/buffer/SizeClasses.java @@ -78,14 +78,13 @@ *

* ( 76, 24, 22, 1, yes, no, no) */ -abstract class SizeClasses implements SizeClassesMetric { +class SizeClasses implements SizeClassesMetric { static final int LOG2_QUANTUM = 4; private static final int LOG2_SIZE_CLASS_GROUP = 2; private static final int LOG2_MAX_LOOKUP_SIZE = 12; - private static final int INDEX_IDX = 0; private static final int LOG2GROUP_IDX = 1; private static final int LOG2DELTA_IDX = 2; private static final int NDELTA_IDX = 3; @@ -95,10 +94,10 @@ abstract class SizeClasses implements SizeClassesMetric { private static final byte no = 0, yes = 1; - protected final int pageSize; - protected final int pageShifts; - protected final int chunkSize; - protected final int directMemoryCacheAlignment; + final int pageSize; + final int pageShifts; + final int chunkSize; + final int directMemoryCacheAlignment; final int nSizes; final int nSubpages; @@ -115,7 +114,7 @@ abstract class SizeClasses implements SizeClassesMetric { // spacing is 1 << LOG2_QUANTUM, so the size of array is lookupMaxClass >> LOG2_QUANTUM private final int[] size2idxTab; - protected SizeClasses(int pageSize, int pageShifts, int chunkSize, int directMemoryCacheAlignment) { + SizeClasses(int pageSize, int pageShifts, int chunkSize, int directMemoryCacheAlignment) { int group = log2(chunkSize) - LOG2_QUANTUM - LOG2_SIZE_CLASS_GROUP + 1; //generate size classes @@ -181,9 +180,9 @@ protected SizeClasses(int pageSize, int pageShifts, int chunkSize, int directMem this.directMemoryCacheAlignment = directMemoryCacheAlignment; //generate lookup tables - sizeIdx2sizeTab = newIdx2SizeTab(sizeClasses, nSizes, directMemoryCacheAlignment); - pageIdx2sizeTab = newPageIdx2sizeTab(sizeClasses, nSizes, nPSizes, directMemoryCacheAlignment); - size2idxTab = newSize2idxTab(lookupMaxSize, sizeClasses); + this.sizeIdx2sizeTab = newIdx2SizeTab(sizeClasses, nSizes, directMemoryCacheAlignment); + this.pageIdx2sizeTab = newPageIdx2sizeTab(sizeClasses, nSizes, nPSizes, directMemoryCacheAlignment); + this.size2idxTab = newSize2idxTab(lookupMaxSize, sizeClasses); } //calculate size class diff --git a/buffer/src/test/java/io/netty/buffer/PoolArenaTest.java b/buffer/src/test/java/io/netty/buffer/PoolArenaTest.java index 32f9189d7d77..7da96da1087d 100644 --- a/buffer/src/test/java/io/netty/buffer/PoolArenaTest.java +++ b/buffer/src/test/java/io/netty/buffer/PoolArenaTest.java @@ -33,33 +33,38 @@ public class PoolArenaTest { @Test public void testNormalizeCapacity() { - PoolArena arena = new PoolArena.DirectArena(null, PAGE_SIZE, PAGE_SHIFTS, CHUNK_SIZE, 0); + SizeClasses sc = new SizeClasses(PAGE_SIZE, PAGE_SHIFTS, CHUNK_SIZE, 0); + PoolArena arena = new PoolArena.DirectArena(null, sc); int[] reqCapacities = {0, 15, 510, 1024, 1023, 1025}; int[] expectedResult = {16, 16, 512, 1024, 1024, 1280}; for (int i = 0; i < reqCapacities.length; i ++) { - assertEquals(expectedResult[i], arena.sizeIdx2size(arena.size2SizeIdx(reqCapacities[i]))); + assertEquals(expectedResult[i], + arena.sizeClass.sizeIdx2size(arena.sizeClass.size2SizeIdx(reqCapacities[i]))); } } @Test public void testNormalizeAlignedCapacity() { - PoolArena arena = new PoolArena.DirectArena(null, PAGE_SIZE, PAGE_SHIFTS, CHUNK_SIZE, 64); + SizeClasses sc = new SizeClasses(PAGE_SIZE, PAGE_SHIFTS, CHUNK_SIZE, 64); + PoolArena arena = new PoolArena.DirectArena(null, sc); int[] reqCapacities = {0, 15, 510, 1024, 1023, 1025}; int[] expectedResult = {64, 64, 512, 1024, 1024, 1280}; for (int i = 0; i < reqCapacities.length; i ++) { - assertEquals(expectedResult[i], arena.sizeIdx2size(arena.size2SizeIdx(reqCapacities[i]))); + assertEquals(expectedResult[i], + arena.sizeClass.sizeIdx2size(arena.sizeClass.size2SizeIdx(reqCapacities[i]))); } } @Test public void testSize2SizeIdx() { - PoolArena arena = new PoolArena.DirectArena(null, PAGE_SIZE, PAGE_SHIFTS, CHUNK_SIZE, 0); + SizeClasses sc = new SizeClasses(PAGE_SIZE, PAGE_SHIFTS, CHUNK_SIZE, 0); + PoolArena arena = new PoolArena.DirectArena(null, sc); for (int sz = 0; sz <= CHUNK_SIZE; sz++) { - int sizeIdx = arena.size2SizeIdx(sz); - assertTrue(sz <= arena.sizeIdx2size(sizeIdx)); + int sizeIdx = arena.sizeClass.size2SizeIdx(sz); + assertTrue(sz <= arena.sizeClass.sizeIdx2size(sizeIdx)); if (sizeIdx > 0) { - assertTrue(sz > arena.sizeIdx2size(sizeIdx - 1)); + assertTrue(sz > arena.sizeClass.sizeIdx2size(sizeIdx - 1)); } } } @@ -67,38 +72,40 @@ public void testSize2SizeIdx() { @Test public void testPages2PageIdx() { int pageShifts = PAGE_SHIFTS; - - PoolArena arena = new PoolArena.DirectArena(null, PAGE_SIZE, PAGE_SHIFTS, CHUNK_SIZE, 0); + SizeClasses sc = new SizeClasses(PAGE_SIZE, PAGE_SHIFTS, CHUNK_SIZE, 0); + PoolArena arena = new PoolArena.DirectArena(null, sc); int maxPages = CHUNK_SIZE >> pageShifts; for (int pages = 1; pages <= maxPages; pages++) { - int pageIdxFloor = arena.pages2pageIdxFloor(pages); - assertTrue(pages << pageShifts >= arena.pageIdx2size(pageIdxFloor)); + int pageIdxFloor = arena.sizeClass.pages2pageIdxFloor(pages); + assertTrue(pages << pageShifts >= arena.sizeClass.pageIdx2size(pageIdxFloor)); if (pageIdxFloor > 0 && pages < maxPages) { - assertTrue(pages << pageShifts < arena.pageIdx2size(pageIdxFloor + 1)); + assertTrue(pages << pageShifts < arena.sizeClass.pageIdx2size(pageIdxFloor + 1)); } - int pageIdxCeiling = arena.pages2pageIdx(pages); - assertTrue(pages << pageShifts <= arena.pageIdx2size(pageIdxCeiling)); + int pageIdxCeiling = arena.sizeClass.pages2pageIdx(pages); + assertTrue(pages << pageShifts <= arena.sizeClass.pageIdx2size(pageIdxCeiling)); if (pageIdxCeiling > 0) { - assertTrue(pages << pageShifts > arena.pageIdx2size(pageIdxCeiling - 1)); + assertTrue(pages << pageShifts > arena.sizeClass.pageIdx2size(pageIdxCeiling - 1)); } } } @Test public void testSizeIdx2size() { - PoolArena arena = new PoolArena.DirectArena(null, PAGE_SIZE, PAGE_SHIFTS, CHUNK_SIZE, 0); - for (int i = 0; i < arena.nSizes; i++) { - assertEquals(arena.sizeIdx2sizeCompute(i), arena.sizeIdx2size(i)); + SizeClasses sc = new SizeClasses(PAGE_SIZE, PAGE_SHIFTS, CHUNK_SIZE, 0); + PoolArena arena = new PoolArena.DirectArena(null, sc); + for (int i = 0; i < arena.sizeClass.nSizes; i++) { + assertEquals(arena.sizeClass.sizeIdx2sizeCompute(i), arena.sizeClass.sizeIdx2size(i)); } } @Test public void testPageIdx2size() { - PoolArena arena = new PoolArena.DirectArena(null, PAGE_SIZE, PAGE_SHIFTS, CHUNK_SIZE, 0); - for (int i = 0; i < arena.nPSizes; i++) { - assertEquals(arena.pageIdx2sizeCompute(i), arena.pageIdx2size(i)); + SizeClasses sc = new SizeClasses(PAGE_SIZE, PAGE_SHIFTS, CHUNK_SIZE, 0); + PoolArena arena = new PoolArena.DirectArena(null, sc); + for (int i = 0; i < arena.sizeClass.nPSizes; i++) { + assertEquals(arena.sizeClass.pageIdx2sizeCompute(i), arena.sizeClass.pageIdx2size(i)); } }