ScopeAlloc

概览

AutoFreeAlloc有较强的局限性,仅仅适用于有限的场合(局部的复杂算法);而ScopeAlloc是通用型的Allocator,基本在任何情况下,你都可通过使用ScopeAlloc来进行内存管理,以获得良好的性能回报。

理解ScopeAlloc的关键,在于理解我们对AutoFreeAlloc的模型所作的修正。详情参考《C++内存管理变革(6):通用型垃圾回收器 - ScopeAlloc》。

规格

typedef BlockPoolT<SingleThreadModel> BlockPoolST;
typedef ProxyAlloc<BlockPoolST> ProxyBlockPoolST;
 
typedef BlockPoolT<MultiThreadModel> BlockPoolMT;
typedef ProxyAlloc<BlockPoolMT> ProxyBlockPoolMT;
 
class ProxyST : public StdAlloc
{
public:
    typedef ProxyBlockPoolST allocator_type;
};
 
class ProxyMT : public StdAlloc
{
public:
    typedef ProxyBlockPoolMT allocator_type;
};
 
typedef AutoFreeAllocT<ProxyST> ScopeAllocST;
typedef AutoFreeAllocT<ProxyMT> ScopeAllocMT;
 
#if defined(_MT)
typedef BlockPoolMT BlockPool;
typedef ProxyBlockPoolMT ProxyBlockPool;
typedef ScopeAllocMT ScopeAlloc;
#else
typedef BlockPoolST BlockPool;
typedef ProxyBlockPoolST ProxyBlockPool;
typedef ScopeAllocST ScopeAlloc;
#endif

BlockPool

  • BlockPool是一个Allocator。其实现就是通常我们所说的内存池(Memory Pool)。但是它比一般的内存池要简单很多,因为它只是管理MemBlock,而不负责对MemBlock进行结点(Node)1的划分(这个工作实际上由AutoFreeAllocT完成了)。
  • 由于ThreadModel的不同,BlockPool又分为BlockPoolST(单线程)、BlockPoolMT(多线程)。详情参考BlockPool

ProxyAlloc

  • Allocator代理。在介绍AutoFreeAlloc时我们说过,传递给AutoFreeAllocT的Allocator,通常要么是一个全局内存Allocator,要么是一个Allocator代理。ScopeAlloc使用后者。
  • 关于该类细节,详情参考ProxyAlloc

AutoFreeAllocT

相关应用

相关参考

使用样例

(todo)

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License

Subscription expired — please renew

Pro account upgrade has expired for this site and the site is now locked. If you are the master administrator for this site, please renew your subscription or delete your outstanding sites or stored files, so that your account fits in the free plan.