跳至主要內容
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?

4563博客

全新的繁體中文 WordPress 網站
  • 首頁
  • 使用 c++ 实现的简单内存池
未分類
13 2 月 2021

使用 c++ 实现的简单内存池

使用 c++ 实现的简单内存池

資深大佬 : Aidenboss 0

作为 c++ 新人,写一些简单的小工具,项目地址: https://github.com/yemingfeng/cmm/

使用 c++ 实现的简单内存池

  • <input checked=”” disabled=”” type=”checkbox”> 简单的内存分配和释放
  • <input disabled=”” type=”checkbox”> 增加内存块申请的最优匹配算法
  • <input disabled=”” type=”checkbox”> 多线程支持
  • <input disabled=”” type=”checkbox”> …

运行

mkdir build cd build cmake -j 6 .. make ./cmm 

原理

Node 节点(用于存储真实的内存空间)
    class Node {     public:         // 真实开辟的空间         char *data;         // 开辟空间的大小         size_t size;         // 指向下一块的 node         Node *next;          Node() {             data = nullptr;             size = 0;             next = nullptr;         }          Node(size_t size) {             this->size = size;             this->data = new char[size];             next = nullptr;         }          ~Node() {             delete data;         }     }; 
Pool 逻辑

pool 维护一个 Node* free,用于表示当前已经申请并且可用的 Node 列表,注意这是一个哨兵 node

申请内存
  • 当申请一块内存空间,首先从 free 列表查找,如果存在 node 满足 size_t >= size,则使用该 node
  • 如果不存在满足条件的 node,则开辟一个新的空间,核心逻辑如下
        Node *findCanUse(size_t size) {             Node *result = nullptr;             // 查找能够满足此次开辟空间的 node             // 注意:这里是判断 it->next 是否满足条件,这样的好处是易于根据 it 节点移除 it->next 节点             Node *it = free;             while (it) {                 if (it->next && it->next->size >= size) {                     // 将这个节点移除                     result = it->next;                     it->next = it->next->next;                     break;                 }                 it = it->next;             }             return result;         }          // 申请一块内存空间         Node *allocate(size_t size) {             // 查找 free 列表可用的 node             Node *result = findCanUse(size);              // 说明 free 列表未找到可用的 node,需要重新开辟这块空间             if (!result) {                 std::cout << "未找到合适的空间,需要重新开辟:" << size << std::endl;                 result = new Node(size);             }              return result;         } 
释放内存

直接将 node 加入到 free 列表中

        // 释放一块内存空间         void deallocate(Node *node) {             // 简单的时间:将 node 加入到 free 列表最后             Node *it = free;             while (it->next) {                 it = it->next;             }             it->next = node;         } 

使用

class Person { public:     int id;     int sex;      friend std::ostream &operator<<(std::ostream &out, Person &p) {         out << "id = " << p.id << "tsex = " << p.sex << "tpointer = " << (&p) << std::endl;         return out;     } };  void mock(Person *person) {     srand((unsigned) time(nullptr));     int id = std::abs(std::rand() % 100);     person->id = id;     person->sex = id; }  int main() {     CMM::Pool pool;      size_t size = sizeof(Person);      auto oneNode = pool.allocate(size);     auto *one = reinterpret_cast<Person *>(oneNode->data);     mock(one);     std::cout << *one;     // 测试释放内存逻辑     pool.deallocate(oneNode);      auto twoNode = pool.allocate(size);     auto *two = reinterpret_cast<Person *>(twoNode->data);     mock(two);     // 这里输出的内存地址和 oneNode 是一样的     std::cout << *two;      auto threeNode = pool.allocate(size);     auto *three = reinterpret_cast<Person *>(threeNode->data);     mock(three);     // 这里输出的内存地址和 oneNode 是不一样的     std::cout << *three;      return 0; } 

大佬有話說 (9)

  • 資深大佬 : QBugHunter

    单向链表?

  • 資深大佬 : QBugHunter

    我没太仔细看,你说支持多线程的,但我看 findCanUse 函数就不是线程安全的,多线程编程环境下,那些函数是否线程安全标注下吧

  • 資深大佬 : cxytz01

    你为什么要实现内存池,malloc 就是一个内存池,还不够好,不够快吗?

  • 資深大佬 : dangyuluo

    @cxytz01 够好,够快,就是很多地方就是不能用它。很多 safety critical 的程序是不能简单的用 malloc/new 的,必须实现自己的内存管理。可以参考下 std::pmr 的目的

  • 資深大佬 : edimetia3d

    这个应该只能算是 allocator 吧, 内存池的”池”呢.
    而且这个 O(N)的 alloc 和 free 真的大丈夫?

    主可以搜搜内存池的应用场景,看看能不能解决应用场景的痛点. 分配器这块可以参考著名的 tcmalloc

  • 資深大佬 : alazysun

    这。。。新人我建议你从内存队列开始,很显然你没搞明白池子对概念

  • 資深大佬 : SmallZheng

    unique_ptr

  • 資深大佬 : wasd6267016

    大部分内存池没个三年维护都不如原生的好

  • 資深大佬 : yasaminekldkxf

    想起 redis 源码中的 zmalloc

文章導覽

上一篇文章
下一篇文章

AD

其他操作

  • 登入
  • 訂閱網站內容的資訊提供
  • 訂閱留言的資訊提供
  • WordPress.org 台灣繁體中文

51la

4563博客

全新的繁體中文 WordPress 網站
返回頂端
本站採用 WordPress 建置 | 佈景主題採用 GretaThemes 所設計的 Memory
4563博客
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?
在這裡新增小工具