1.说明
- 此类用于计算对象大小的对齐映射规则,包括
- 但不限于上述方法,因为调用此类中的函数并不需要实例化对象出来,可以直接设置为static方法,直接使用
SizeAlignMap::Func()
调用,所以还可以扩展使用
- Thread Cache一次从Central Cache获取多少个对象
- 计算一次性向系统获取几个页
2.接口设计
class SizeAlignMap
{
public:
static inline size_t _RoundUp(size_t size, size_t alignNum)
{
return (size + alignNum - 1) & ~(alignNum - 1);
}
static inline size_t RoundUp(size_t size)
{
if (size <= 128)
{
return _RoundUp(size, 8);
}
else if (size <= 1024)
{
return _RoundUp(size, 16);
}
else if (size <= 8 * 1024)
{
return _RoundUp(size, 128);
}
else if (size <= 64 * 1024)
{
return _RoundUp(size, 1024);
}
else if (size <= 256 * 1024)
{
return _RoundUp(size, 8 * 1024);
}
else
{
return _RoundUp(size, 1 << PAGE_SHIFT);
}
}
static inline size_t _Index(size_t size, size_t align_shift)
{
return ((size + (1 << align_shift) - 1) >> align_shift) - 1;
}
static inline size_t Index(size_t size)
{
assert(size <= MAX_BYTES);
static const size_t FrontNum[4] = {16, 56, 56, 56};
if (size <= 128)
{
return _Index(size, 3);
}
else if (size <= 1024)
{
return _Index(size, 4) + FrontNum[0];
}
else if (size <= 8 * 1024)
{
return _Index(size, 7) + FrontNum[0] + FrontNum[1];
}
else if (size <= 64 * 1024)
{
return _Index(size, 10) + FrontNum[0] + FrontNum[1] + FrontNum[2];
}
else if (size <= 256 * 1024)
{
return _Index(size, 13) + FrontNum[0] + FrontNum[1] + FrontNum[2] + FrontNum[3];
}
else
{
assert(false);
return -1;
}
}
static size_t MoveObjNum(size_t size)
{
assert(size <= MAX_BYTES);
assert(size > 0);
size_t num = MAX_BYTES / size;
if (num < 2)
{
num = 2;
}
if (num > 512)
{
num = 512;
}
return num;
}
static size_t MovePageNum(size_t alignSize)
{
assert(alignSize > 0);
size_t num = MoveObjNum(alignSize);
size_t nPage = num * alignSize;
nPage >>= PAGE_SHIFT;
if (nPage == 0)
{
nPage = 1;
}
return nPage;
}
};