内存池的简单实现


当我们频繁的申请内存(new、malloc) 和 释放内存(delete、free) 时,会产生内存碎片,而且申请和释放内存也会增加时间的消耗。内存池就是为了解决该问题,提高效率产生的。

这里使用分段分段连续的内存,来存储多次申请和释放的内存。

  • 内存单元: 定义的内存最小单元
  • 内存块: 一块连续的内存,可以存放多个内存单元
  • 内存池: 用链表的形式管理多个内存块
    MemoryPool
 

1. 内存单元

下面是内存单元部分定义:

  • unitData 表示要存储的单元。
  • pNext 指向下一个要赋值的单元。
 

2. 内存块

内存块首部位置保存内存块的信息,这里仅保存了链表下一个节点指针 m_pNext 和整个的数据指针 m_pData,剩下的数据为要存储内存单元的数据。

  • 函数 createMemory 用于创建内存块,需要指定头内存块的指针,内存单元的大小和内存单元的个数。
  • 函数 destoryMemoryBlock 释放内存块,释放时会释放掉以它为链首的后继链的内存块资源。
  • 函数 data 用于获取内存单元块的首地址。
 

3. 内存池

内存池中 有创建元素的函数 createObject 和 删除元素的函数 destoryObject,这里删除不是释放内存,而是将该块内存的状态标记为未被使用的状态。

  • m_pBlockHead: 存储内存块头指针
  • m_nPerBlockUnitSize: 存储每个内存块中元素的个数
  • m_pFreeUnit: 存储将要添加元素的位置地址。

在创建元素时,为了能够初始化类中的内容,使用了 placement new 关于 new和delete 可参考文章 C++中的new和delete


下面时测试代码:

测试结果打印输出:

Created TestObject_1
Object Info: m_nNumber 1
————————-
Created TestObject_2
Object Info: m_nNumber 2
————————-
Created TestObject_3
Object Info: m_nNumber 3
————————-
Created TestObject_4
Object Info: m_nNumber 4
————————-
Created TestObject_5
Object Info: m_nNumber 5
————————-
Created TestObject_6
Object Info: m_nNumber 6
————————-
Created TestObject_7
Object Info: m_nNumber 7
————————-
Created TestObject_8
Object Info: m_nNumber 8
————————-
Created TestObject_9
Object Info: m_nNumber 9
————————-
Created TestObject_10
Object Info: m_nNumber 10
————————-
Created TestObject_11
Object Info: m_nNumber 11
————————-
Created TestObject_12
Object Info: m_nNumber 12
————————-
Created TestObject_13
Object Info: m_nNumber 13
————————-
Created TestObject_14
Object Info: m_nNumber 14
————————-
Created TestObject_15
Object Info: m_nNumber 15
————————-
Created TestObject_16
Object Info: m_nNumber 16
————————-
Created TestObject_17
Object Info: m_nNumber 17
————————-
Created TestObject_18
Object Info: m_nNumber 18
————————-
Created TestObject_19
Object Info: m_nNumber 19
————————-
Created TestObject_20
Object Info: m_nNumber 20
————————-
Deleted TestObject_1
Deleted TestObject_2
Deleted TestObject_3
Deleted TestObject_4
Deleted TestObject_5
Deleted TestObject_6
Deleted TestObject_7
Deleted TestObject_8
Deleted TestObject_9
Deleted TestObject_10
Deleted TestObject_11
Deleted TestObject_12
Deleted TestObject_13
Deleted TestObject_14
Deleted TestObject_15
Deleted TestObject_16
Deleted TestObject_17
Deleted TestObject_18
Deleted TestObject_19
Deleted TestObject_20

You May Also Like

About the Author: admin

喜欢编程、爱游戏,更爱生活。

发表评论

电子邮件地址不会被公开。