[toc]

原文的项目采用的是链式管理内存的方法

# PoolInfo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
struct FPoolInfo
{
DWORD Bytes; // Bytes allocated for pool.
DWORD OsBytes; // Bytes aligned to page size.
DWORD Taken; // Number of allocated elements in this pool, when counts down to zero can free the entire pool.
BYTE* Mem; // Memory base.
FPoolTable* Table; // Index of pool.
FFreeMem* FirstMem; // Pointer to first free memory in this pool.
FPoolInfo* Next;
FPoolInfo** PrevLink;

void Link( FPoolInfo*& Before )
{
if( Before )
{
Before->PrevLink = &Next;
}
Next = Before;
PrevLink = &Before;
Before = this;
}
void Unlink()
{
if( Next )
{
Next->PrevLink = PrevLink;
}
*PrevLink = Next;
}
};
  • PrevLink : 指向前一个节点的指针
  • Next : 指向后一个节点
  • Owner : 指向哪个链表管理者 (PoolTable)
  • mem : 指向分配空间首地址
  • Taken : 计数器
  • Bytes : 当前链分配的内存大小
  • OsBytes : 页大小对齐

# PoolTable

1
2
3
4
5
6
7
// Pool table.
struct FPoolTable
{
FPoolInfo* FirstPool;
FPoolInfo* ExhaustedPool;
DWORD BlockSize;
};
  • BlockSize : 每次可分配内存大小
  • ExhaustedPool : 管理的 PoolInfo 表头
  • FirstPool : 空闲链表头指针

# 实际分配结构

Tip: 右侧应该是 PoolInfo