一 简易版

  1. 定义一个该类私有成员变量指针,用于串起内存池中没有使用的内存。
  2. 定义一个该类静态私有成员变量指针,用于指向内存池的首地址
  3. 重载 operator new 和 operator delete
  4. 在 operator new 中进行内存池的建立和分配
  5. 在 operator delete中进行内存的释放

代码:

  1. #include <iostream>
  2. using namespace std;
  3. class A {
  4. public:
  5. static void* operator new(size_t size);
  6. static void operator delete(void* phead);
  7. static size_t m_MallocCount;
  8. static size_t m_NewCount;
  9. private:
  10. A* next;
  11. static A* m_FreePos;
  12. static size_t m_sTrunkCount;
  13. };
  14. A* A::m_FreePos = nullptr;
  15. size_t A::m_sTrunkCount = 5;
  16. size_t A::m_MallocCount = 0;
  17. size_t A::m_NewCount = 0;
  18. void* A::operator new(size_t size)
  19. {
  20. A* tmplink = nullptr;
  21. if (m_FreePos == nullptr)
  22. {
  23. m_FreePos = reinterpret_cast<A*>(malloc(m_sTrunkCount*size));
  24. tmplink = m_FreePos;
  25. for (; tmplink != m_FreePos+m_sTrunkCount-1; ++tmplink)
  26. {
  27. tmplink->next = tmplink + 1;
  28. }
  29. tmplink->next = nullptr;
  30. m_MallocCount++;
  31. }
  32. tmplink = m_FreePos;
  33. m_FreePos = m_FreePos->next;
  34. m_NewCount++;
  35. return tmplink;
  36. }
  37. void A::operator delete(void* phead)
  38. {
  39. (reinterpret_cast<A*>(phead))->next = m_FreePos;
  40. m_FreePos->next = reinterpret_cast<A*>(phead);
  41. }
  42. void function()
  43. {
  44. for (int i = 0; i < 5000000; i++)
  45. {
  46. A* tmpA = new A();
  47. }
  48. cout << "malloc次数: " << A::m_MallocCount << "new次数" << A::m_NewCount << endl;
  49. }
  50. int main()
  51. {
  52. function();
  53. getchar();
  54. }

二 嵌入式指针版

  1. 定义一个myallocator类进行内存的管理
  2. 为了保证嵌入式指针的正常运行,需要保证myallocator的size大于等于4
  3. 在myallocator类中声明一个结构体类型obj,用它的指针来管理内存
  4. 在myallocator类中声明一个obj类型的指针,用于指向未分配的内存
  5. 在myallocator类中声明一个size_t类型,用于表示分配的内存块大小
  6. 由于代码高度重用,可以声明为宏

代码如下:

  1. class myallocator {
  2. public:
  3. void* allocator(size_t size);
  4. void deallocator(void* phead);
  5. private:
  6. struct obj
  7. {
  8. obj* next;
  9. };
  10. size_t m_sTrunkCount = 5;
  11. obj* m_FreePos = nullptr;
  12. };
  13. void* myallocator::allocator(size_t size)
  14. {
  15. obj* tmplink = nullptr;
  16. obj* m_FreePos = (obj*)malloc(m_sTrunkCount * size);
  17. tmplink = m_FreePos;
  18. if (m_FreePos == nullptr)
  19. {
  20. for (int i = 0; i < m_sTrunkCount - 1; i++)
  21. {
  22. tmplink->next = (obj*)((char*)tmplink + size);
  23. tmplink = (obj*)((char*)tmplink + size);
  24. }
  25. tmplink->next = nullptr;
  26. }
  27. tmplink = m_FreePos;
  28. m_FreePos = m_FreePos->next;
  29. return (void*)tmplink;
  30. }
  31. void myallocator::deallocator(void* phead)
  32. {
  33. ((obj*)phead)->next = m_FreePos;
  34. m_FreePos = (obj*)phead;
  35. }
  36. #define DECLARE_POOL_ALLOC()\
  37. public:\
  38. static myallocator myalloc;\
  39. static void *operator new(size_t size)\
  40. {\
  41. return myalloc.allocator(size);\
  42. }\
  43. static void operator delete(void *phead)\
  44. {\
  45. return myalloc.deallocator(phead);\
  46. }\
  47. #define IMPLEMENT_POOL_ALLOC(classname)\
  48. myallocator classname::myalloc;
  49. class A
  50. {
  51. DECLARE_POOL_ALLOC()
  52. public:
  53. int m_i;
  54. int m_j; //为了保证sizeof(A)凑够4字节,老师演示时定义了两个int成员变量;
  55. };
  56. IMPLEMENT_POOL_ALLOC(A)
  57. void func()
  58. {
  59. A *mypa[100];
  60. for (int i = 0; i < 15; ++i)
  61. {
  62. mypa[i] = new A();
  63. mypa[i]->m_i = 12;
  64. mypa[i]->m_j = 15;
  65. printf("%p\n", mypa[i]);
  66. }
  67. for (int i = 0; i < 15; ++i)
  68. {
  69. delete mypa[i];
  70. }
  71. }