Allocator
2016-11-18 11:22:34 0 举报
Allocator,也被称为内存分配器,是一个在计算机编程中用于管理内存资源的工具。它的主要任务是为程序中的变量、数据结构等动态分配和释放内存空间。Allocator通过跟踪哪些内存块已经被分配以及它们的使用情况,来有效地管理内存资源。此外,它还负责处理内存碎片问题,即当一块连续的内存被分割成多个小块时,如何将这些小块重新组合成一个大的连续内存块。Allocator的性能直接影响到程序的运行效率,因此选择合适的内存分配策略对于优化程序性能至关重要。常见的内存分配器有静态内存分配器、栈内存分配器和堆内存分配器等。
作者其他创作
大纲/内容
Allocator_SingletonSupport
+ attribute1:type = defaultValue+ attribute2:type- attribute3:type
+ operation1(params):returnType- operation2(params)- operation3()
static D* InitSystemSingleton(){ static AllocContainer Container; Allocator_SingletonSupport *presult = Construct((void*)Container.Data); presult-pContainer = &Container; Container.Initialized = true; return (D*)presult;}
public
这种设计, 保证了每个类似于 DefaultAllocator 的具体 Allocator 都将只有个实例。实现了利用static 空间来做分配器?每用一个 class D 泛化出一个 Allocator_SingletonSupport, 对应的 InitSystemSingleton 也是独一无二的,然后会利用静态成员函数的静态成员完成对象的初始化
template Allocator_SingletonSupport
- AllocContainer* pContainer;
+ static D* InitSystemSingleton()- virtual void onSystemShutdown()
DefaultAllocator
struct AllocContainer{ UPInt Data[(sizeof(D) + sizeof(UPInt)-1) / sizeof(UPInt)];};保证 Data 一定满足 class D, 并且是 UPInt 对齐, UPInt 是 size_t。Data 会被用于 placement new 操作符来构造一个 class D 实例。
System
virtual void onSystemShutdown() { Allocator::onSystemShutdown(); if (pContainer) { pContainer-Initialized = false; Destruct((D*)this); pContainer = 0; } }
As 泛化参数 D
Allocator
0 条评论
下一页