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:
parent
3030541844
commit
c2d34649ff
@ -174,26 +174,15 @@ public class PooledByteBufAllocator extends AbstractByteBufAllocator {
|
|||||||
|
|
||||||
private static int validateAndCalculatePageShifts(int pageSize) {
|
private static int validateAndCalculatePageShifts(int pageSize) {
|
||||||
if (pageSize < MIN_PAGE_SIZE) {
|
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.
|
if ((pageSize & pageSize - 1) != 0) {
|
||||||
boolean found1 = false;
|
throw new IllegalArgumentException("pageSize: " + pageSize + " (expected: power of 2)");
|
||||||
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 ++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
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) {
|
private static int validateAndCalculateChunkSize(int pageSize, int maxOrder) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user