Bit tricks to check for and calculate power of two.

Motivation:
I was studying the code and thought this was simpler and easier to
understand.

Modifications:
Replaced the for loop and if conditions, with a simple implementation.

Result:
Code is easier to understand.
This commit is contained in:
Jakob Buchgraber 2014-03-17 16:32:39 +01:00 committed by Trustin Lee
parent b79f0cb1d3
commit 1bce46dbb3

View File

@ -174,26 +174,15 @@ public class PooledByteBufAllocator extends AbstractByteBufAllocator {
private static int validateAndCalculatePageShifts(int pageSize) {
if (pageSize < MIN_PAGE_SIZE) {
throw new IllegalArgumentException("pageSize: " + pageSize + " (expected: 4096+)");
throw new IllegalArgumentException("pageSize: " + pageSize + " (expected: " + MIN_PAGE_SIZE + "+)");
}
// Ensure pageSize is power of 2.
boolean found1 = false;
int pageShifts = 0;
for (int i = pageSize; i != 0 ; i >>= 1) {
if ((i & 1) != 0) {
if (!found1) {
found1 = true;
} else {
throw new IllegalArgumentException("pageSize: " + pageSize + " (expected: power of 2");
}
} else {
if (!found1) {
pageShifts ++;
}
}
if ((pageSize & pageSize - 1) != 0) {
throw new IllegalArgumentException("pageSize: " + pageSize + " (expected: power of 2)");
}
return pageShifts;
// Logarithm base 2. At this point we know that pageSize is a power of two.
return Integer.SIZE - 1 - Integer.numberOfLeadingZeros(pageSize);
}
private static int validateAndCalculateChunkSize(int pageSize, int maxOrder) {