├── PC的实现和原理 ├── Common.h ├── PageCache.h ├── CentralCache.h ├── ObjectPool.h ├── PageCache.cc ├── ThreadCache.cc ├── ThreadCache.h ├── CentralCache.cc ├── ConcurrentAlloc.h ├── ObjectPool.cc └── TestObj.cc ├── 内存池的回收机制 ├── Common.h ├── PageCache.h ├── CentralCache.h ├── ObjectPool.h ├── PageCache.cc ├── ThreadCache.cc ├── ThreadCache.h ├── CentralCache.cc ├── ConcurrentAlloc.h ├── ObjectPool.cc └── TestObj.cc ├── 定长内存池 ├── ObjectPool.h ├── ObjectPool.cc └── TestObj.cc ├── 高并发内存池(完整版) ├── Common.h ├── PageCache.h ├── Benchmark.cc ├── ObjectPool.h ├── PageCache.cc ├── ThreadCache.h ├── CentralCache.cc ├── CentralCache.h ├── ThreadCache.cc ├── ConcurrentAlloc.h ├── ObjectPool.cc ├── TestObj.cc └── PageMap.h ├── 内存池的申请释放调试--已完成 ├── Common.h ├── Benchmark.cc ├── ObjectPool.h ├── PageCache.cc ├── PageCache.h ├── CentralCache.h ├── ThreadCache.cc ├── ThreadCache.h ├── CentralCache.cc ├── ConcurrentAlloc.h ├── ObjectPool.cc └── TestObj.cc ├── 高并发内存池(1)TC和CC的原理 ├── Common.h ├── ObjectPool.h ├── CentralCache.cc ├── CentralCache.h ├── ThreadCache.cc ├── ThreadCache.h ├── ConcurrentAlloc.h ├── ObjectPool.cc └── TestObj.cc └── README.md /PC的实现和原理/Common.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/peaceMan1999/high-concurrency-memory-pool/HEAD/PC的实现和原理/Common.h -------------------------------------------------------------------------------- /内存池的回收机制/Common.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/peaceMan1999/high-concurrency-memory-pool/HEAD/内存池的回收机制/Common.h -------------------------------------------------------------------------------- /定长内存池/ObjectPool.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/peaceMan1999/high-concurrency-memory-pool/HEAD/定长内存池/ObjectPool.h -------------------------------------------------------------------------------- /PC的实现和原理/PageCache.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/peaceMan1999/high-concurrency-memory-pool/HEAD/PC的实现和原理/PageCache.h -------------------------------------------------------------------------------- /内存池的回收机制/PageCache.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/peaceMan1999/high-concurrency-memory-pool/HEAD/内存池的回收机制/PageCache.h -------------------------------------------------------------------------------- /高并发内存池(完整版)/Common.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/peaceMan1999/high-concurrency-memory-pool/HEAD/高并发内存池(完整版)/Common.h -------------------------------------------------------------------------------- /PC的实现和原理/CentralCache.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/peaceMan1999/high-concurrency-memory-pool/HEAD/PC的实现和原理/CentralCache.h -------------------------------------------------------------------------------- /PC的实现和原理/ObjectPool.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/peaceMan1999/high-concurrency-memory-pool/HEAD/PC的实现和原理/ObjectPool.h -------------------------------------------------------------------------------- /PC的实现和原理/PageCache.cc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/peaceMan1999/high-concurrency-memory-pool/HEAD/PC的实现和原理/PageCache.cc -------------------------------------------------------------------------------- /PC的实现和原理/ThreadCache.cc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/peaceMan1999/high-concurrency-memory-pool/HEAD/PC的实现和原理/ThreadCache.cc -------------------------------------------------------------------------------- /PC的实现和原理/ThreadCache.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/peaceMan1999/high-concurrency-memory-pool/HEAD/PC的实现和原理/ThreadCache.h -------------------------------------------------------------------------------- /内存池的回收机制/CentralCache.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/peaceMan1999/high-concurrency-memory-pool/HEAD/内存池的回收机制/CentralCache.h -------------------------------------------------------------------------------- /内存池的回收机制/ObjectPool.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/peaceMan1999/high-concurrency-memory-pool/HEAD/内存池的回收机制/ObjectPool.h -------------------------------------------------------------------------------- /内存池的回收机制/PageCache.cc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/peaceMan1999/high-concurrency-memory-pool/HEAD/内存池的回收机制/PageCache.cc -------------------------------------------------------------------------------- /内存池的回收机制/ThreadCache.cc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/peaceMan1999/high-concurrency-memory-pool/HEAD/内存池的回收机制/ThreadCache.cc -------------------------------------------------------------------------------- /内存池的回收机制/ThreadCache.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/peaceMan1999/high-concurrency-memory-pool/HEAD/内存池的回收机制/ThreadCache.h -------------------------------------------------------------------------------- /高并发内存池(完整版)/PageCache.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/peaceMan1999/high-concurrency-memory-pool/HEAD/高并发内存池(完整版)/PageCache.h -------------------------------------------------------------------------------- /PC的实现和原理/CentralCache.cc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/peaceMan1999/high-concurrency-memory-pool/HEAD/PC的实现和原理/CentralCache.cc -------------------------------------------------------------------------------- /内存池的回收机制/CentralCache.cc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/peaceMan1999/high-concurrency-memory-pool/HEAD/内存池的回收机制/CentralCache.cc -------------------------------------------------------------------------------- /内存池的申请释放调试--已完成/Common.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/peaceMan1999/high-concurrency-memory-pool/HEAD/内存池的申请释放调试--已完成/Common.h -------------------------------------------------------------------------------- /高并发内存池(完整版)/Benchmark.cc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/peaceMan1999/high-concurrency-memory-pool/HEAD/高并发内存池(完整版)/Benchmark.cc -------------------------------------------------------------------------------- /高并发内存池(完整版)/ObjectPool.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/peaceMan1999/high-concurrency-memory-pool/HEAD/高并发内存池(完整版)/ObjectPool.h -------------------------------------------------------------------------------- /高并发内存池(完整版)/PageCache.cc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/peaceMan1999/high-concurrency-memory-pool/HEAD/高并发内存池(完整版)/PageCache.cc -------------------------------------------------------------------------------- /高并发内存池(完整版)/ThreadCache.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/peaceMan1999/high-concurrency-memory-pool/HEAD/高并发内存池(完整版)/ThreadCache.h -------------------------------------------------------------------------------- /PC的实现和原理/ConcurrentAlloc.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/peaceMan1999/high-concurrency-memory-pool/HEAD/PC的实现和原理/ConcurrentAlloc.h -------------------------------------------------------------------------------- /内存池的回收机制/ConcurrentAlloc.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/peaceMan1999/high-concurrency-memory-pool/HEAD/内存池的回收机制/ConcurrentAlloc.h -------------------------------------------------------------------------------- /内存池的申请释放调试--已完成/Benchmark.cc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/peaceMan1999/high-concurrency-memory-pool/HEAD/内存池的申请释放调试--已完成/Benchmark.cc -------------------------------------------------------------------------------- /内存池的申请释放调试--已完成/ObjectPool.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/peaceMan1999/high-concurrency-memory-pool/HEAD/内存池的申请释放调试--已完成/ObjectPool.h -------------------------------------------------------------------------------- /内存池的申请释放调试--已完成/PageCache.cc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/peaceMan1999/high-concurrency-memory-pool/HEAD/内存池的申请释放调试--已完成/PageCache.cc -------------------------------------------------------------------------------- /内存池的申请释放调试--已完成/PageCache.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/peaceMan1999/high-concurrency-memory-pool/HEAD/内存池的申请释放调试--已完成/PageCache.h -------------------------------------------------------------------------------- /高并发内存池(1)TC和CC的原理/Common.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/peaceMan1999/high-concurrency-memory-pool/HEAD/高并发内存池(1)TC和CC的原理/Common.h -------------------------------------------------------------------------------- /高并发内存池(完整版)/CentralCache.cc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/peaceMan1999/high-concurrency-memory-pool/HEAD/高并发内存池(完整版)/CentralCache.cc -------------------------------------------------------------------------------- /高并发内存池(完整版)/CentralCache.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/peaceMan1999/high-concurrency-memory-pool/HEAD/高并发内存池(完整版)/CentralCache.h -------------------------------------------------------------------------------- /高并发内存池(完整版)/ThreadCache.cc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/peaceMan1999/high-concurrency-memory-pool/HEAD/高并发内存池(完整版)/ThreadCache.cc -------------------------------------------------------------------------------- /内存池的申请释放调试--已完成/CentralCache.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/peaceMan1999/high-concurrency-memory-pool/HEAD/内存池的申请释放调试--已完成/CentralCache.h -------------------------------------------------------------------------------- /内存池的申请释放调试--已完成/ThreadCache.cc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/peaceMan1999/high-concurrency-memory-pool/HEAD/内存池的申请释放调试--已完成/ThreadCache.cc -------------------------------------------------------------------------------- /内存池的申请释放调试--已完成/ThreadCache.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/peaceMan1999/high-concurrency-memory-pool/HEAD/内存池的申请释放调试--已完成/ThreadCache.h -------------------------------------------------------------------------------- /高并发内存池(1)TC和CC的原理/ObjectPool.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/peaceMan1999/high-concurrency-memory-pool/HEAD/高并发内存池(1)TC和CC的原理/ObjectPool.h -------------------------------------------------------------------------------- /高并发内存池(完整版)/ConcurrentAlloc.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/peaceMan1999/high-concurrency-memory-pool/HEAD/高并发内存池(完整版)/ConcurrentAlloc.h -------------------------------------------------------------------------------- /内存池的申请释放调试--已完成/CentralCache.cc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/peaceMan1999/high-concurrency-memory-pool/HEAD/内存池的申请释放调试--已完成/CentralCache.cc -------------------------------------------------------------------------------- /内存池的申请释放调试--已完成/ConcurrentAlloc.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/peaceMan1999/high-concurrency-memory-pool/HEAD/内存池的申请释放调试--已完成/ConcurrentAlloc.h -------------------------------------------------------------------------------- /高并发内存池(1)TC和CC的原理/CentralCache.cc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/peaceMan1999/high-concurrency-memory-pool/HEAD/高并发内存池(1)TC和CC的原理/CentralCache.cc -------------------------------------------------------------------------------- /高并发内存池(1)TC和CC的原理/CentralCache.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/peaceMan1999/high-concurrency-memory-pool/HEAD/高并发内存池(1)TC和CC的原理/CentralCache.h -------------------------------------------------------------------------------- /高并发内存池(1)TC和CC的原理/ThreadCache.cc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/peaceMan1999/high-concurrency-memory-pool/HEAD/高并发内存池(1)TC和CC的原理/ThreadCache.cc -------------------------------------------------------------------------------- /高并发内存池(1)TC和CC的原理/ThreadCache.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/peaceMan1999/high-concurrency-memory-pool/HEAD/高并发内存池(1)TC和CC的原理/ThreadCache.h -------------------------------------------------------------------------------- /高并发内存池(1)TC和CC的原理/ConcurrentAlloc.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/peaceMan1999/high-concurrency-memory-pool/HEAD/高并发内存池(1)TC和CC的原理/ConcurrentAlloc.h -------------------------------------------------------------------------------- /PC的实现和原理/ObjectPool.cc: -------------------------------------------------------------------------------- 1 | //#define _CRT_SECURE_NO_WARNINGS 1 2 | // 3 | //#include "ObjectPool.h" 4 | // 5 | //int main() 6 | //{ 7 | // TestObjectPool(); 8 | // return 0; 9 | //} -------------------------------------------------------------------------------- /内存池的回收机制/ObjectPool.cc: -------------------------------------------------------------------------------- 1 | //#define _CRT_SECURE_NO_WARNINGS 1 2 | // 3 | //#include "ObjectPool.h" 4 | // 5 | //int main() 6 | //{ 7 | // TestObjectPool(); 8 | // return 0; 9 | //} -------------------------------------------------------------------------------- /定长内存池/ObjectPool.cc: -------------------------------------------------------------------------------- 1 | //#define _CRT_SECURE_NO_WARNINGS 1 2 | // 3 | //#include "ObjectPool.h" 4 | // 5 | //int main() 6 | //{ 7 | // TestObjectPool(); 8 | // return 0; 9 | //} -------------------------------------------------------------------------------- /高并发内存池(完整版)/ObjectPool.cc: -------------------------------------------------------------------------------- 1 | //#define _CRT_SECURE_NO_WARNINGS 1 2 | // 3 | //#include "ObjectPool.h" 4 | // 5 | //int main() 6 | //{ 7 | // TestObjectPool(); 8 | // return 0; 9 | //} -------------------------------------------------------------------------------- /内存池的申请释放调试--已完成/ObjectPool.cc: -------------------------------------------------------------------------------- 1 | //#define _CRT_SECURE_NO_WARNINGS 1 2 | // 3 | //#include "ObjectPool.h" 4 | // 5 | //int main() 6 | //{ 7 | // TestObjectPool(); 8 | // return 0; 9 | //} -------------------------------------------------------------------------------- /高并发内存池(1)TC和CC的原理/ObjectPool.cc: -------------------------------------------------------------------------------- 1 | //#define _CRT_SECURE_NO_WARNINGS 1 2 | // 3 | //#include "ObjectPool.h" 4 | // 5 | //int main() 6 | //{ 7 | // TestObjectPool(); 8 | // return 0; 9 | //} -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 高并发内存池 2 | 3 | ##实现环境: 4 | Windows 32 位系统下 VS2019 5 | 6 | ##项目简介: 7 | 内存池的框架由 3 个主要部分组成,分别为:ThreadCache、CentralCache 和 8 | PageCache,它们形成了便捷的供给需求,当每一个线程需要空间时,上层会提前申请并切分 9 | 给各个线程,且利用映射的回收机制最大地解决内存碎片的问题,提高了内存利用率和使用效 10 | 率。 11 | 12 | ##使用技术: 13 | C++、单例模式、数据结构、TLS。 14 | -------------------------------------------------------------------------------- /定长内存池/TestObj.cc: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 1 2 | #include "ConcurrentAlloc.h" 3 | //#include "ObjectPool.h" 4 | 5 | void Alloc1() 6 | { 7 | for (int i = 0; i < 5; i++) 8 | { 9 | void* ptr = ConcurrentAlloc(7); 10 | } 11 | } 12 | 13 | void Alloc2() 14 | { 15 | for (int i = 0; i < 5; i++) 16 | { 17 | void* ptr = ConcurrentAlloc(6); 18 | } 19 | } 20 | 21 | void Test() 22 | { 23 | std::thread t1(Alloc1); 24 | t1.join(); // wait 25 | 26 | std::thread t2(Alloc2); 27 | t2.join(); 28 | } 29 | 30 | int main() 31 | { 32 | Test(); 33 | 34 | return 0; 35 | } -------------------------------------------------------------------------------- /高并发内存池(1)TC和CC的原理/TestObj.cc: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 1 2 | #include "ConcurrentAlloc.h" 3 | //#include "ObjectPool.h" 4 | 5 | void Alloc1() 6 | { 7 | for (int i = 0; i < 5; i++) 8 | { 9 | void* ptr = ConcurrentAlloc(7); 10 | } 11 | } 12 | 13 | void Alloc2() 14 | { 15 | for (int i = 0; i < 5; i++) 16 | { 17 | void* ptr = ConcurrentAlloc(6); 18 | } 19 | } 20 | 21 | void Test() 22 | { 23 | std::thread t1(Alloc1); 24 | t1.join(); // wait 25 | 26 | std::thread t2(Alloc2); 27 | t2.join(); 28 | } 29 | 30 | int main() 31 | { 32 | Test(); 33 | 34 | return 0; 35 | } -------------------------------------------------------------------------------- /PC的实现和原理/TestObj.cc: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 1 2 | #include "ConcurrentAlloc.h" 3 | //#include "ObjectPool.h" 4 | 5 | //void Alloc1() 6 | //{ 7 | // for (int i = 0; i < 5; i++) 8 | // { 9 | // void* ptr = ConcurrentAlloc(7); 10 | // } 11 | //} 12 | // 13 | //void Alloc2() 14 | //{ 15 | // for (int i = 0; i < 5; i++) 16 | // { 17 | // void* ptr = ConcurrentAlloc(6); 18 | // } 19 | //} 20 | // 21 | //void Test() 22 | //{ 23 | // std::thread t1(Alloc1); 24 | // t1.join(); // wait 25 | // 26 | // std::thread t2(Alloc2); 27 | // t2.join(); 28 | //} 29 | 30 | //void Test2() 31 | //{ 32 | // void* p1 = ConcurrentAlloc(6); 33 | // void* p2 = ConcurrentAlloc(1); 34 | // void* p3 = ConcurrentAlloc(8); 35 | // void* p4 = ConcurrentAlloc(3); 36 | // void* p5 = ConcurrentAlloc(5); 37 | //} 38 | 39 | void Test3() 40 | { 41 | for (int i = 0; i < 1023; i++) 42 | { 43 | void* p1 = ConcurrentAlloc(6); 44 | cout << p1 << endl; 45 | } 46 | 47 | void* p1 = ConcurrentAlloc(8); 48 | cout << p1 << endl; 49 | 50 | void* p2 = ConcurrentAlloc(8); 51 | cout << p2 << endl; 52 | 53 | } 54 | 55 | int main() 56 | { 57 | //Test(); 58 | //Test2(); 59 | Test3(); 60 | 61 | return 0; 62 | } -------------------------------------------------------------------------------- /内存池的回收机制/TestObj.cc: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 1 2 | #include "ConcurrentAlloc.h" 3 | //#include "ObjectPool.h" 4 | 5 | //void Alloc1() 6 | //{ 7 | // for (int i = 0; i < 5; i++) 8 | // { 9 | // void* ptr = ConcurrentAlloc(7); 10 | // } 11 | //} 12 | // 13 | //void Alloc2() 14 | //{ 15 | // for (int i = 0; i < 5; i++) 16 | // { 17 | // void* ptr = ConcurrentAlloc(6); 18 | // } 19 | //} 20 | 21 | void Alloc3() 22 | { 23 | std::vector v; 24 | for (size_t i = 0; i < 7; ++i) 25 | { 26 | void* ptr = ConcurrentAlloc(6); 27 | v.push_back(ptr); 28 | } 29 | 30 | for (auto e : v) 31 | { 32 | ConcurrentFree(e, 6); 33 | } 34 | } 35 | 36 | void Alloc4() 37 | { 38 | std::vector v; 39 | for (size_t i = 0; i < 7; ++i) 40 | { 41 | void* ptr = ConcurrentAlloc(8); 42 | v.push_back(ptr); 43 | } 44 | 45 | for (auto e : v) 46 | { 47 | ConcurrentFree(e, 8); 48 | } 49 | } 50 | 51 | void Test4() 52 | { 53 | std::thread t1(Alloc3); 54 | t1.join(); // wait 55 | 56 | std::thread t2(Alloc4); 57 | t2.join(); 58 | } 59 | 60 | // 61 | //void Test() 62 | //{ 63 | // std::thread t1(Alloc1); 64 | // t1.join(); // wait 65 | // 66 | // std::thread t2(Alloc2); 67 | // t2.join(); 68 | //} 69 | 70 | //void Test2() 71 | //{ 72 | // void* p1 = ConcurrentAlloc(6); 73 | // void* p2 = ConcurrentAlloc(1); 74 | // void* p3 = ConcurrentAlloc(8); 75 | // void* p4 = ConcurrentAlloc(3); 76 | // void* p5 = ConcurrentAlloc(5); 77 | //} 78 | 79 | //void Test3() 80 | //{ 81 | // for (int i = 0; i < 1023; i++) 82 | // { 83 | // void* p1 = ConcurrentAlloc(6); 84 | // cout << p1 << endl; 85 | // } 86 | // 87 | // void* p1 = ConcurrentAlloc(8); 88 | // cout << p1 << endl; 89 | // 90 | // void* p2 = ConcurrentAlloc(8); 91 | // cout << p2 << endl; 92 | // 93 | //} 94 | 95 | int main() 96 | { 97 | //Test(); 98 | //Test2(); 99 | //Test3(); 100 | Test4(); 101 | 102 | return 0; 103 | } -------------------------------------------------------------------------------- /高并发内存池(完整版)/TestObj.cc: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 1 2 | //#include "ConcurrentAlloc.h" 3 | //#include "ObjectPool.h" 4 | 5 | //void Alloc1() 6 | //{ 7 | // for (int i = 0; i < 5; i++) 8 | // { 9 | // void* ptr = ConcurrentAlloc(7); 10 | // } 11 | //} 12 | // 13 | //void Alloc2() 14 | //{ 15 | // for (int i = 0; i < 5; i++) 16 | // { 17 | // void* ptr = ConcurrentAlloc(6); 18 | // } 19 | //} 20 | // 21 | //void Alloc3() 22 | //{ 23 | // std::vector v; 24 | // for (size_t i = 0; i < 7; ++i) 25 | // { 26 | // void* ptr = ConcurrentAlloc(6); 27 | // v.push_back(ptr); 28 | // } 29 | // 30 | // for (auto e : v) 31 | // { 32 | // ConcurrentFree(e); 33 | // } 34 | //} 35 | // 36 | //void Alloc4() 37 | //{ 38 | // std::vector v; 39 | // for (size_t i = 0; i < 7; ++i) 40 | // { 41 | // void* ptr = ConcurrentAlloc(8); 42 | // v.push_back(ptr); 43 | // } 44 | // 45 | // for (auto e : v) 46 | // { 47 | // ConcurrentFree(e); 48 | // } 49 | //} 50 | // 51 | //void Test4() 52 | //{ 53 | // std::thread t1(Alloc3); 54 | // t1.join(); // wait 55 | // 56 | // std::thread t2(Alloc4); 57 | // t2.join(); 58 | //} 59 | 60 | // 61 | //void Test() 62 | //{ 63 | // std::thread t1(Alloc1); 64 | // t1.join(); // wait 65 | // 66 | // std::thread t2(Alloc2); 67 | // t2.join(); 68 | //} 69 | 70 | //void Test2() 71 | //{ 72 | // void* p1 = ConcurrentAlloc(6); 73 | // void* p2 = ConcurrentAlloc(1); 74 | // void* p3 = ConcurrentAlloc(8); 75 | // void* p4 = ConcurrentAlloc(3); 76 | // void* p5 = ConcurrentAlloc(5); 77 | //} 78 | 79 | //void Test3() 80 | //{ 81 | // for (int i = 0; i < 1023; i++) 82 | // { 83 | // void* p1 = ConcurrentAlloc(6); 84 | // cout << p1 << endl; 85 | // } 86 | // 87 | // void* p1 = ConcurrentAlloc(8); 88 | // cout << p1 << endl; 89 | // 90 | // void* p2 = ConcurrentAlloc(8); 91 | // cout << p2 << endl; 92 | // 93 | //} 94 | 95 | //int main() 96 | //{ 97 | // //Test(); 98 | // //Test2(); 99 | // //Test3(); 100 | // //Test4(); 101 | // 102 | // return 0; 103 | //} -------------------------------------------------------------------------------- /内存池的申请释放调试--已完成/TestObj.cc: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 1 2 | //#include "ConcurrentAlloc.h" 3 | //#include "ObjectPool.h" 4 | 5 | //void Alloc1() 6 | //{ 7 | // for (int i = 0; i < 5; i++) 8 | // { 9 | // void* ptr = ConcurrentAlloc(7); 10 | // } 11 | //} 12 | // 13 | //void Alloc2() 14 | //{ 15 | // for (int i = 0; i < 5; i++) 16 | // { 17 | // void* ptr = ConcurrentAlloc(6); 18 | // } 19 | //} 20 | // 21 | //void Alloc3() 22 | //{ 23 | // std::vector v; 24 | // for (size_t i = 0; i < 7; ++i) 25 | // { 26 | // void* ptr = ConcurrentAlloc(6); 27 | // v.push_back(ptr); 28 | // } 29 | // 30 | // for (auto e : v) 31 | // { 32 | // ConcurrentFree(e); 33 | // } 34 | //} 35 | // 36 | //void Alloc4() 37 | //{ 38 | // std::vector v; 39 | // for (size_t i = 0; i < 7; ++i) 40 | // { 41 | // void* ptr = ConcurrentAlloc(8); 42 | // v.push_back(ptr); 43 | // } 44 | // 45 | // for (auto e : v) 46 | // { 47 | // ConcurrentFree(e); 48 | // } 49 | //} 50 | // 51 | //void Test4() 52 | //{ 53 | // std::thread t1(Alloc3); 54 | // t1.join(); // wait 55 | // 56 | // std::thread t2(Alloc4); 57 | // t2.join(); 58 | //} 59 | 60 | // 61 | //void Test() 62 | //{ 63 | // std::thread t1(Alloc1); 64 | // t1.join(); // wait 65 | // 66 | // std::thread t2(Alloc2); 67 | // t2.join(); 68 | //} 69 | 70 | //void Test2() 71 | //{ 72 | // void* p1 = ConcurrentAlloc(6); 73 | // void* p2 = ConcurrentAlloc(1); 74 | // void* p3 = ConcurrentAlloc(8); 75 | // void* p4 = ConcurrentAlloc(3); 76 | // void* p5 = ConcurrentAlloc(5); 77 | //} 78 | 79 | //void Test3() 80 | //{ 81 | // for (int i = 0; i < 1023; i++) 82 | // { 83 | // void* p1 = ConcurrentAlloc(6); 84 | // cout << p1 << endl; 85 | // } 86 | // 87 | // void* p1 = ConcurrentAlloc(8); 88 | // cout << p1 << endl; 89 | // 90 | // void* p2 = ConcurrentAlloc(8); 91 | // cout << p2 << endl; 92 | // 93 | //} 94 | 95 | //int main() 96 | //{ 97 | // //Test(); 98 | // //Test2(); 99 | // //Test3(); 100 | // //Test4(); 101 | // 102 | // return 0; 103 | //} -------------------------------------------------------------------------------- /高并发内存池(完整版)/PageMap.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include"Common.h" 4 | 5 | // Single-level array 6 | template 7 | class TCMalloc_PageMap1 { 8 | private: 9 | static const int LENGTH = 1 << BITS; 10 | void** array_; 11 | 12 | public: 13 | typedef uintptr_t Number; 14 | 15 | //explicit TCMalloc_PageMap1(void* (*allocator)(size_t)) { 16 | explicit TCMalloc_PageMap1() { 17 | //array_ = reinterpret_cast((*allocator)(sizeof(void*) << BITS)); 18 | size_t size = sizeof(void*) << BITS; 19 | size_t alignSize = SizeClass::_RoundUp(size, 1 << PAGESHIFT); 20 | array_ = (void**)SystemAlloc(alignSize >> PAGESHIFT); 21 | memset(array_, 0, sizeof(void*) << BITS); 22 | } 23 | 24 | // Return the current value for KEY. Returns NULL if not yet set, 25 | // or if k is out of range. 26 | void* get(Number k) const { 27 | if ((k >> BITS) > 0) { 28 | return NULL; 29 | } 30 | return array_[k]; 31 | } 32 | 33 | // REQUIRES "k" is in range "[0,2^BITS-1]". 34 | // REQUIRES "k" has been ensured before. 35 | // 36 | // Sets the value 'v' for key 'k'. 37 | void set(Number k, void* v) { 38 | array_[k] = v; 39 | } 40 | }; 41 | 42 | // Two-level radix tree 43 | template 44 | class TCMalloc_PageMap2 { 45 | private: 46 | // Put 32 entries in the root and (2^BITS)/32 entries in each leaf. 47 | static const int ROOT_BITS = 5; 48 | static const int ROOT_LENGTH = 1 << ROOT_BITS; 49 | 50 | static const int LEAF_BITS = BITS - ROOT_BITS; 51 | static const int LEAF_LENGTH = 1 << LEAF_BITS; 52 | 53 | // Leaf node 54 | struct Leaf { 55 | void* values[LEAF_LENGTH]; 56 | }; 57 | 58 | Leaf* root_[ROOT_LENGTH]; // Pointers to 32 child nodes 59 | void* (*allocator_)(size_t); // Memory allocator 60 | 61 | public: 62 | typedef uintptr_t Number; 63 | 64 | //explicit TCMalloc_PageMap2(void* (*allocator)(size_t)) { 65 | explicit TCMalloc_PageMap2() { 66 | //allocator_ = allocator; 67 | memset(root_, 0, sizeof(root_)); 68 | 69 | PreallocateMoreMemory(); 70 | } 71 | 72 | void* get(Number k) const { 73 | const Number i1 = k >> LEAF_BITS; 74 | const Number i2 = k & (LEAF_LENGTH - 1); 75 | if ((k >> BITS) > 0 || root_[i1] == NULL) { 76 | return NULL; 77 | } 78 | return root_[i1]->values[i2]; 79 | } 80 | 81 | void set(Number k, void* v) { 82 | const Number i1 = k >> LEAF_BITS; 83 | const Number i2 = k & (LEAF_LENGTH - 1); 84 | ASSERT(i1 < ROOT_LENGTH); 85 | root_[i1]->values[i2] = v; 86 | } 87 | 88 | bool Ensure(Number start, size_t n) { 89 | for (Number key = start; key <= start + n - 1;) { 90 | const Number i1 = key >> LEAF_BITS; 91 | 92 | // Check for overflow 93 | if (i1 >= ROOT_LENGTH) 94 | return false; 95 | 96 | // Make 2nd level node if necessary 97 | if (root_[i1] == NULL) { 98 | //Leaf* leaf = reinterpret_cast((*allocator_)(sizeof(Leaf))); 99 | //if (leaf == NULL) return false; 100 | static ObjectPool leafPool; 101 | Leaf* leaf = (Leaf*)leafPool.New(); 102 | 103 | memset(leaf, 0, sizeof(*leaf)); 104 | root_[i1] = leaf; 105 | } 106 | 107 | // Advance key past whatever is covered by this leaf node 108 | key = ((key >> LEAF_BITS) + 1) << LEAF_BITS; 109 | } 110 | return true; 111 | } 112 | 113 | void PreallocateMoreMemory() { 114 | // Allocate enough to keep track of all possible pages 115 | Ensure(0, 1 << BITS); 116 | } 117 | }; 118 | 119 | // Three-level radix tree 120 | template 121 | class TCMalloc_PageMap3 { 122 | private: 123 | // How many bits should we consume at each interior level 124 | static const int INTERIOR_BITS = (BITS + 2) / 3; // Round-up 125 | static const int INTERIOR_LENGTH = 1 << INTERIOR_BITS; 126 | 127 | // How many bits should we consume at leaf level 128 | static const int LEAF_BITS = BITS - 2 * INTERIOR_BITS; 129 | static const int LEAF_LENGTH = 1 << LEAF_BITS; 130 | 131 | // Interior node 132 | struct Node { 133 | Node* ptrs[INTERIOR_LENGTH]; 134 | }; 135 | 136 | // Leaf node 137 | struct Leaf { 138 | void* values[LEAF_LENGTH]; 139 | }; 140 | 141 | Node* root_; // Root of radix tree 142 | void* (*allocator_)(size_t); // Memory allocator 143 | 144 | Node* NewNode() { 145 | Node* result = reinterpret_cast((*allocator_)(sizeof(Node))); 146 | if (result != NULL) { 147 | memset(result, 0, sizeof(*result)); 148 | } 149 | return result; 150 | } 151 | 152 | public: 153 | typedef uintptr_t Number; 154 | 155 | explicit TCMalloc_PageMap3(void* (*allocator)(size_t)) { 156 | allocator_ = allocator; 157 | root_ = NewNode(); 158 | } 159 | 160 | void* get(Number k) const { 161 | const Number i1 = k >> (LEAF_BITS + INTERIOR_BITS); 162 | const Number i2 = (k >> LEAF_BITS) & (INTERIOR_LENGTH - 1); 163 | const Number i3 = k & (LEAF_LENGTH - 1); 164 | if ((k >> BITS) > 0 || 165 | root_->ptrs[i1] == NULL || root_->ptrs[i1]->ptrs[i2] == NULL) { 166 | return NULL; 167 | } 168 | return reinterpret_cast(root_->ptrs[i1]->ptrs[i2])->values[i3]; 169 | } 170 | 171 | void set(Number k, void* v) { 172 | ASSERT(k >> BITS == 0); 173 | const Number i1 = k >> (LEAF_BITS + INTERIOR_BITS); 174 | const Number i2 = (k >> LEAF_BITS) & (INTERIOR_LENGTH - 1); 175 | const Number i3 = k & (LEAF_LENGTH - 1); 176 | reinterpret_cast(root_->ptrs[i1]->ptrs[i2])->values[i3] = v; 177 | } 178 | 179 | bool Ensure(Number start, size_t n) { 180 | for (Number key = start; key <= start + n - 1;) { 181 | const Number i1 = key >> (LEAF_BITS + INTERIOR_BITS); 182 | const Number i2 = (key >> LEAF_BITS) & (INTERIOR_LENGTH - 1); 183 | 184 | // Check for overflow 185 | if (i1 >= INTERIOR_LENGTH || i2 >= INTERIOR_LENGTH) 186 | return false; 187 | 188 | // Make 2nd level node if necessary 189 | if (root_->ptrs[i1] == NULL) { 190 | Node* n = NewNode(); 191 | if (n == NULL) return false; 192 | root_->ptrs[i1] = n; 193 | } 194 | 195 | // Make leaf node if necessary 196 | if (root_->ptrs[i1]->ptrs[i2] == NULL) { 197 | Leaf* leaf = reinterpret_cast((*allocator_)(sizeof(Leaf))); 198 | if (leaf == NULL) return false; 199 | memset(leaf, 0, sizeof(*leaf)); 200 | root_->ptrs[i1]->ptrs[i2] = reinterpret_cast(leaf); 201 | } 202 | 203 | // Advance key past whatever is covered by this leaf node 204 | key = ((key >> LEAF_BITS) + 1) << LEAF_BITS; 205 | } 206 | return true; 207 | } 208 | 209 | void PreallocateMoreMemory() { 210 | } 211 | }; --------------------------------------------------------------------------------