Cleanup PoolChunk and PoolArena

Motivation:

To make it easier to understand PoolChunk and PoolArena we should cleanup duplicated code.

Modifications:

- Move reused code into methods
- Use Math.max(...)

Result:

Cleaner code and easier to understand.
This commit is contained in:
Norman Maurer 2016-04-08 11:56:32 +02:00
parent d081851156
commit 787a55874b
2 changed files with 24 additions and 15 deletions

View File

@ -26,6 +26,8 @@ import java.util.Collections;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import static java.lang.Math.max;
abstract class PoolArena<T> implements PoolArenaMetric {
static final boolean HAS_UNSAFE = PlatformDependent.hasUnsafe();
@ -506,19 +508,17 @@ abstract class PoolArena<T> implements PoolArenaMetric {
synchronized (this) {
val += allocationsNormal - (deallocationsTiny + deallocationsSmall + deallocationsNormal);
}
return val >= 0 ? val : 0;
return max(val, 0);
}
@Override
public long numActiveTinyAllocations() {
long val = numTinyAllocations() - numTinyDeallocations();
return val >= 0 ? val : 0;
return max(numTinyAllocations() - numTinyDeallocations(), 0);
}
@Override
public long numActiveSmallAllocations() {
long val = numSmallAllocations() - numSmallDeallocations();
return val >= 0 ? val : 0;
return max(numSmallAllocations() - numSmallDeallocations(), 0);
}
@Override
@ -527,13 +527,12 @@ abstract class PoolArena<T> implements PoolArenaMetric {
synchronized (this) {
val = allocationsNormal - deallocationsNormal;
}
return val >= 0 ? val : 0;
return max(val, 0);
}
@Override
public long numActiveHugeAllocations() {
long val = numHugeAllocations() - numHugeDeallocations();
return val >= 0 ? val : 0;
return max(numHugeAllocations() - numHugeDeallocations(), 0);
}
protected abstract PoolChunk<T> newChunk(int pageSize, int maxOrder, int pageShifts, int chunkSize);

View File

@ -102,6 +102,8 @@ package io.netty.buffer;
*/
final class PoolChunk<T> implements PoolChunkMetric {
private static final int INTEGER_SIZE_MINUS_ONE = Integer.SIZE - 1;
final PoolArena<T> arena;
final T memory;
final boolean unpooled;
@ -342,8 +344,8 @@ final class PoolChunk<T> implements PoolChunkMetric {
* @param handle handle to free
*/
void free(long handle) {
int memoryMapIdx = (int) handle;
int bitmapIdx = (int) (handle >>> Integer.SIZE);
int memoryMapIdx = memoryMapIdx(handle);
int bitmapIdx = bitmapIdx(handle);
if (bitmapIdx != 0) { // free a subpage
PoolSubpage<T> subpage = subpages[subpageIdx(memoryMapIdx)];
@ -364,8 +366,8 @@ final class PoolChunk<T> implements PoolChunkMetric {
}
void initBuf(PooledByteBuf<T> buf, long handle, int reqCapacity) {
int memoryMapIdx = (int) handle;
int bitmapIdx = (int) (handle >>> Integer.SIZE);
int memoryMapIdx = memoryMapIdx(handle);
int bitmapIdx = bitmapIdx(handle);
if (bitmapIdx == 0) {
byte val = value(memoryMapIdx);
assert val == unusable : String.valueOf(val);
@ -377,13 +379,13 @@ final class PoolChunk<T> implements PoolChunkMetric {
}
void initBufWithSubpage(PooledByteBuf<T> buf, long handle, int reqCapacity) {
initBufWithSubpage(buf, handle, (int) (handle >>> Integer.SIZE), reqCapacity);
initBufWithSubpage(buf, handle, bitmapIdx(handle), reqCapacity);
}
private void initBufWithSubpage(PooledByteBuf<T> buf, long handle, int bitmapIdx, int reqCapacity) {
assert bitmapIdx != 0;
int memoryMapIdx = (int) handle;
int memoryMapIdx = memoryMapIdx(handle);
PoolSubpage<T> subpage = subpages[subpageIdx(memoryMapIdx)];
assert subpage.doNotDestroy;
@ -409,7 +411,7 @@ final class PoolChunk<T> implements PoolChunkMetric {
private static int log2(int val) {
// compute the (0-based, with lsb = 0) position of highest set bit i.e, log2
return Integer.SIZE - 1 - Integer.numberOfLeadingZeros(val);
return INTEGER_SIZE_MINUS_ONE - Integer.numberOfLeadingZeros(val);
}
private int runLength(int id) {
@ -427,6 +429,14 @@ final class PoolChunk<T> implements PoolChunkMetric {
return memoryMapIdx ^ maxSubpageAllocs; // remove highest set bit, to get offset
}
private static int memoryMapIdx(long handle) {
return (int) handle;
}
private static int bitmapIdx(long handle) {
return (int) (handle >>> Integer.SIZE);
}
@Override
public int chunkSize() {
return chunkSize;