简介
ResourcePool用于管理资源,负责资源的分配以及回收
结构
BlockGroup:资源池中包含多个BlockGroup,最多65536个
Block:一个BlockGroup中包含多个Block,最多(1<<16)个;1个Block中包含BLOCK_NITEM个类型为T的资源,BLOCK_NITEM由类模板ResourcePoolBlockItemNum中的静态成员value决定
template <typename T>
class ResourcePoolBlockItemNum {
static const size_t N1 = ResourcePoolBlockMaxSize<T>::value / sizeof(T);
static const size_t N2 = (N1 < 1 ? 1 : N1);
public:
static const size_t value = (N2 > ResourcePoolBlockMaxItem<T>::value ?
ResourcePoolBlockMaxItem<T>::value : N2);
};
template <typename T> struct ResourcePoolBlockMaxSize {
static const size_t value = 64 * 1024; // bytes
};
template <typename T> struct ResourcePoolBlockMaxItem {
static const size_t value = 256;
};
分配
空闲资源的定义是通过ResourcePoolFreeChunk
template <typename T, size_t NITEM>
struct ResourcePoolFreeChunk {
size_t nfree;
ResourceId<T> ids[NITEM];
};
// for gcc 3.4.5
template <typename T>
struct ResourcePoolFreeChunk<T, 0> {
size_t nfree;
ResourceId<T> ids[0];
};
分配规则
- 优先从LocalPool空闲资源表中分配
- 如果无法从LocalPool空闲资源表中分配,则从ResourcePool的空闲资源表中分配
- 从LocalPool中的Block分配
- 从ResourcePool中BlockGroup的Block分配