
作为 c++ 新人,写一些简单的小工具,项目地址: https://github.com/yemingfeng/cmm/
mkdir build cd build cmake -j 6 .. make ./cmm 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 维护一个 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; } 1 QBugHunter 2021-02-12 14:23:04 +08:00 单向链表? |
2 QBugHunter 2021-02-12 14:24:53 +08:00 我没太仔细看,你说支持多线程的,但我看 findCanUse 函数就不是线程安全的,多线程编程环境下,那些函数是否线程安全标注下吧 |
3 cxytz01 2021-02-12 15:34:22 +08:00 你为什么要实现内存池,malloc 就是一个内存池,还不够好,不够快吗? |
4 dangyuluo 2021-02-12 16:36:12 +08:00 @cxytz01 够好,够快,就是很多地方就是不能用它。很多 safety critical 的程序是不能简单的用 malloc/new 的,必须实现自己的内存管理。可以参考下 std::pmr 的目的 |
5 edimetia3d 2021-02-12 18:47:12 +08:00 这个应该只能算是 allocator 吧, 内存池的"池"呢. 而且这个 O(N)的 alloc 和 free 真的大丈夫? 楼主可以搜搜内存池的应用场景,看看能不能解决应用场景的痛点. 分配器这块可以参考著名的 tcmalloc |
6 alazysun 2021-02-12 19:52:46 +08:00 这。。。新人我建议你从内存队列开始,很显然你没搞明白池子对概念 |
7 SmallZheng 2021-02-13 10:03:07 +08:00 via Android unique_ptr |
8 KaynWASD 2021-02-13 12:12:21 +08:00 大部分内存池没个三年维护都不如原生的好 |
9 yasaminekldkxf 2021-02-13 18:38:11 +08:00 想起 redis 源码中的 zmalloc |