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

2021-02-11 23:40:38 +08:00
 Aidenboss

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

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

运行

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

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

楼主可以搜搜内存池的应用场景,看看能不能解决应用场景的痛点. 分配器这块可以参考著名的 tcmalloc
alazysun
2021-02-12 19:52:46 +08:00
这。。。新人我建议你从内存队列开始,很显然你没搞明白池子对概念
SmallZheng
2021-02-13 10:03:07 +08:00
unique_ptr
wasd6267016
2021-02-13 12:12:21 +08:00
大部分内存池没个三年维护都不如原生的好
yasaminekldkxf
2021-02-13 18:38:11 +08:00
想起 redis 源码中的 zmalloc

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/752996

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX