privatestaticintlog2(int val){ int INTEGER_SIZE_MINUS_ONE = Integer.SIZE - 1; // compute the (0-based, with lsb = 0) position of highest set bit i.e, log2 return INTEGER_SIZE_MINUS_ONE - Integer.numberOfLeadingZeros(val); }
输出: 16M的对数:14 2的14次方:16384.0
subpageOverflowMask 主要用于位运算的“&”操作判断。
1 2 3 4 5 6 7 8 9
@Test publicvoidtestSubpageOverflowMask(){ int pageSize = 8192; int subpageOverflowMask = ~(pageSize - 1); System.out.println("subpageOverflowMask:" + subpageOverflowMask); }
输出: subpageOverflowMask:-8192
maxSubpageAllocs 用于限制数组的长度
1 2 3 4 5 6 7 8 9 10 11
@Test publicvoidtestMaxSubpageAllocs(){ int maxOrder = 11; int maxSubpageAllocs = 1 << maxOrder; System.out.println("maxSubpageAllocs:" + maxSubpageAllocs); int length = maxSubpageAllocs << 1; System.out.println("memoryMap数组的长度:" + length); } 输出: maxSubpageAllocs:2048 memoryMap数组的长度:4096
publicstaticvoidmain(String [] args){ PoolChunkTest poolChunkTest = new PoolChunkTest(); poolChunkTest.initMemoryMap(); int normCapacity = 8 * 1024; long id01 = poolChunkTest.allocateRun(normCapacity); System.out.println("第一次分配:" + id01);
long id02 = poolChunkTest.allocateRun(normCapacity); System.out.println("第二次分配:" + id02);
long id03 = poolChunkTest.allocateRun(normCapacity); System.out.println("第三次分配:" + id03);
}
privatelongallocateRun(int normCapacity){ int d = 11 - (log2(normCapacity) - 13); int id = allocateNode(d);
return id; }
privateintallocateNode(int d){ int id = 1; int initial = - (1 << d); // has last d bits = 0 and rest all = 1 byte val = value(id); if (val > d) { // unusable return -1; } while (val < d || (id & initial) == 0) { // id & initial == 1 << d for all ids at depth d, for < d it is 0 id <<= 1; val = value(id); if (val > d) { id ^= 1; val = value(id); System.out.println("val:" + val); } } byte value = value(id); assert value == d && (id & initial) == 1 << d : String.format("val = %d, id & initial = %d, d = %d", value, id & initial, d); setValue(id, unusable); // mark as unusable updateParentsAlloc(id); return id; }
privatevoidupdateParentsAlloc(int id){ while (id > 1) { int parentId = id >>> 1; byte val1 = value(id); byte val2 = value(id ^ 1); byte val = val1 < val2 ? val1 : val2; setValue(parentId, val); id = parentId; } }
privatestaticintlog2(int val){ int INTEGER_SIZE_MINUS_ONE = Integer.SIZE - 1; // compute the (0-based, with lsb = 0) position of highest set bit i.e, log2 return INTEGER_SIZE_MINUS_ONE - Integer.numberOfLeadingZeros(val); }
privatebytevalue(int id){ return memoryMap[id]; }
publicvoidinitMemoryMap(){ int maxOrder = 11; int memoryMapIndex = 1; int maxSubpageAllocs = 2048; memoryMap = newbyte[maxSubpageAllocs << 1]; byte[] depthMap = newbyte[memoryMap.length]; for (int d = 0; d <= maxOrder; ++ d) { int depth = 1 << d; for (int p = 0; p < depth; ++ p) { memoryMap[memoryMapIndex] = (byte) d; depthMap[memoryMapIndex] = (byte) d; memoryMapIndex ++; } } }
}
输出:
1 2 3
第一次分配:2048 第二次分配:2049 第三次分配:2050
例子中分配的8KB,根据公式 int d = 11 - (log2(normCapacity) - 13)算出其在11层,所以下文中三次分配时入参d=11。