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:
parent
d081851156
commit
787a55874b
@ -26,6 +26,8 @@ import java.util.Collections;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
|
||||||
|
import static java.lang.Math.max;
|
||||||
|
|
||||||
abstract class PoolArena<T> implements PoolArenaMetric {
|
abstract class PoolArena<T> implements PoolArenaMetric {
|
||||||
static final boolean HAS_UNSAFE = PlatformDependent.hasUnsafe();
|
static final boolean HAS_UNSAFE = PlatformDependent.hasUnsafe();
|
||||||
|
|
||||||
@ -506,19 +508,17 @@ abstract class PoolArena<T> implements PoolArenaMetric {
|
|||||||
synchronized (this) {
|
synchronized (this) {
|
||||||
val += allocationsNormal - (deallocationsTiny + deallocationsSmall + deallocationsNormal);
|
val += allocationsNormal - (deallocationsTiny + deallocationsSmall + deallocationsNormal);
|
||||||
}
|
}
|
||||||
return val >= 0 ? val : 0;
|
return max(val, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public long numActiveTinyAllocations() {
|
public long numActiveTinyAllocations() {
|
||||||
long val = numTinyAllocations() - numTinyDeallocations();
|
return max(numTinyAllocations() - numTinyDeallocations(), 0);
|
||||||
return val >= 0 ? val : 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public long numActiveSmallAllocations() {
|
public long numActiveSmallAllocations() {
|
||||||
long val = numSmallAllocations() - numSmallDeallocations();
|
return max(numSmallAllocations() - numSmallDeallocations(), 0);
|
||||||
return val >= 0 ? val : 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -527,13 +527,12 @@ abstract class PoolArena<T> implements PoolArenaMetric {
|
|||||||
synchronized (this) {
|
synchronized (this) {
|
||||||
val = allocationsNormal - deallocationsNormal;
|
val = allocationsNormal - deallocationsNormal;
|
||||||
}
|
}
|
||||||
return val >= 0 ? val : 0;
|
return max(val, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public long numActiveHugeAllocations() {
|
public long numActiveHugeAllocations() {
|
||||||
long val = numHugeAllocations() - numHugeDeallocations();
|
return max(numHugeAllocations() - numHugeDeallocations(), 0);
|
||||||
return val >= 0 ? val : 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected abstract PoolChunk<T> newChunk(int pageSize, int maxOrder, int pageShifts, int chunkSize);
|
protected abstract PoolChunk<T> newChunk(int pageSize, int maxOrder, int pageShifts, int chunkSize);
|
||||||
|
@ -102,6 +102,8 @@ package io.netty.buffer;
|
|||||||
*/
|
*/
|
||||||
final class PoolChunk<T> implements PoolChunkMetric {
|
final class PoolChunk<T> implements PoolChunkMetric {
|
||||||
|
|
||||||
|
private static final int INTEGER_SIZE_MINUS_ONE = Integer.SIZE - 1;
|
||||||
|
|
||||||
final PoolArena<T> arena;
|
final PoolArena<T> arena;
|
||||||
final T memory;
|
final T memory;
|
||||||
final boolean unpooled;
|
final boolean unpooled;
|
||||||
@ -342,8 +344,8 @@ final class PoolChunk<T> implements PoolChunkMetric {
|
|||||||
* @param handle handle to free
|
* @param handle handle to free
|
||||||
*/
|
*/
|
||||||
void free(long handle) {
|
void free(long handle) {
|
||||||
int memoryMapIdx = (int) handle;
|
int memoryMapIdx = memoryMapIdx(handle);
|
||||||
int bitmapIdx = (int) (handle >>> Integer.SIZE);
|
int bitmapIdx = bitmapIdx(handle);
|
||||||
|
|
||||||
if (bitmapIdx != 0) { // free a subpage
|
if (bitmapIdx != 0) { // free a subpage
|
||||||
PoolSubpage<T> subpage = subpages[subpageIdx(memoryMapIdx)];
|
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) {
|
void initBuf(PooledByteBuf<T> buf, long handle, int reqCapacity) {
|
||||||
int memoryMapIdx = (int) handle;
|
int memoryMapIdx = memoryMapIdx(handle);
|
||||||
int bitmapIdx = (int) (handle >>> Integer.SIZE);
|
int bitmapIdx = bitmapIdx(handle);
|
||||||
if (bitmapIdx == 0) {
|
if (bitmapIdx == 0) {
|
||||||
byte val = value(memoryMapIdx);
|
byte val = value(memoryMapIdx);
|
||||||
assert val == unusable : String.valueOf(val);
|
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) {
|
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) {
|
private void initBufWithSubpage(PooledByteBuf<T> buf, long handle, int bitmapIdx, int reqCapacity) {
|
||||||
assert bitmapIdx != 0;
|
assert bitmapIdx != 0;
|
||||||
|
|
||||||
int memoryMapIdx = (int) handle;
|
int memoryMapIdx = memoryMapIdx(handle);
|
||||||
|
|
||||||
PoolSubpage<T> subpage = subpages[subpageIdx(memoryMapIdx)];
|
PoolSubpage<T> subpage = subpages[subpageIdx(memoryMapIdx)];
|
||||||
assert subpage.doNotDestroy;
|
assert subpage.doNotDestroy;
|
||||||
@ -409,7 +411,7 @@ final class PoolChunk<T> implements PoolChunkMetric {
|
|||||||
|
|
||||||
private static int log2(int val) {
|
private static int log2(int val) {
|
||||||
// compute the (0-based, with lsb = 0) position of highest set bit i.e, log2
|
// 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) {
|
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
|
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
|
@Override
|
||||||
public int chunkSize() {
|
public int chunkSize() {
|
||||||
return chunkSize;
|
return chunkSize;
|
||||||
|
Loading…
Reference in New Issue
Block a user