V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Aidenboss
V2EX  ›  C++

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

  •  1
     
  •   Aidenboss · 2021-02-11 23:40:38 +08:00 · 2670 次点击
    这是一个创建于 1162 天前的主题,其中的信息可能已经有所发展或是发生改变。

    作为 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 条回复    2021-02-13 18:38:11 +08:00
    QBugHunter
        1
    QBugHunter  
       2021-02-12 14:23:04 +08:00
    单向链表?
    QBugHunter
        2
    QBugHunter  
       2021-02-12 14:24:53 +08:00
    我没太仔细看,你说支持多线程的,但我看 findCanUse 函数就不是线程安全的,多线程编程环境下,那些函数是否线程安全标注下吧
    cxytz01
        3
    cxytz01  
       2021-02-12 15:34:22 +08:00
    你为什么要实现内存池,malloc 就是一个内存池,还不够好,不够快吗?
    dangyuluo
        4
    dangyuluo  
       2021-02-12 16:36:12 +08:00
    @cxytz01 够好,够快,就是很多地方就是不能用它。很多 safety critical 的程序是不能简单的用 malloc/new 的,必须实现自己的内存管理。可以参考下 std::pmr 的目的
    edimetia3d
        5
    edimetia3d  
       2021-02-12 18:47:12 +08:00
    这个应该只能算是 allocator 吧, 内存池的"池"呢.
    而且这个 O(N)的 alloc 和 free 真的大丈夫?

    楼主可以搜搜内存池的应用场景,看看能不能解决应用场景的痛点. 分配器这块可以参考著名的 tcmalloc
    alazysun
        6
    alazysun  
       2021-02-12 19:52:46 +08:00
    这。。。新人我建议你从内存队列开始,很显然你没搞明白池子对概念
    SmallZheng
        7
    SmallZheng  
       2021-02-13 10:03:07 +08:00 via Android
    unique_ptr
    wasd6267016
        8
    wasd6267016  
       2021-02-13 12:12:21 +08:00
    大部分内存池没个三年维护都不如原生的好
    yasaminekldkxf
        9
    yasaminekldkxf  
       2021-02-13 18:38:11 +08:00
    想起 redis 源码中的 zmalloc
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3698 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 00:50 · PVG 08:50 · LAX 17:50 · JFK 20:50
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.