Chinaunix首页 | 论坛 | 博客
  • 博客访问: 410614
  • 博文数量: 101
  • 博客积分: 2207
  • 博客等级: 大尉
  • 技术积分: 2508
  • 用 户 组: 普通用户
  • 注册时间: 2011-04-19 20:45
文章分类

全部博文(101)

文章存档

2013年(15)

2012年(86)

我的朋友

分类: C/C++

2012-10-02 12:13:55

固定大小对象的内存池
1.未解决问题,删除内存池报错问题

点击(此处)折叠或打开

  1. #include <iostream>
  2. template<typename T>
  3. class MemoryPool
  4. {
  5.     public:
  6.     MemoryPool (size_t size = EXPANSION_SIZE);
  7.     ~MemoryPool ();

  8.     inline void* alloc(size_t size);
  9.     inline void free(void* someElement);

  10.     private:
  11.     MemoryPool<T>* next;

  12.     enum { EXPANSION_SIZE = 32 };

  13.     void expandTheFreeList(int howMany = EXPANSION_SIZE);
  14. };

  15. template <typename T>
  16. MemoryPool<T>::MemoryPool(size_t size)
  17. {
  18.     expandTheFreeList(size);
  19. }

  20. template <typename T>
  21. MemoryPool<T>::~MemoryPool()
  22. {
  23.     MemoryPool<T>* nextPtr = next;
  24.     for (nextPtr = next ; nextPtr != NULL; nextPtr = next)
  25.     {
  26.         next = next->next;
  27.         delete [] nextPtr;
  28.     }
  29. }

  30. template <typename T>
  31. inline
  32. void* MemoryPool<T>::alloc(size_t size)
  33. {
  34.     if (next == NULL)
  35.     {
  36.         expandTheFreeList();
  37.     }
  38.     MemoryPool<T> *head = next;
  39.     next = head->next;

  40.     return head;
  41. }

  42. template<typename T>
  43. inline
  44. void MemoryPool<T>::free(void *doomed)
  45. {
  46.     MemoryPool<T>* head = static_cast<MemoryPool<T>*>(doomed);

  47.     head->next = next;
  48.     next = head;
  49. }

  50. template<typename T>
  51. void MemoryPool<T>::expandTheFreeList(int howMany)
  52. {
  53.     size_t size = (sizeof(T) > sizeof(MemoryPool<T>*)) ? sizeof(T):sizeof(MemoryPool<T>*);
  54.     MemoryPool<T>* runner = static_cast<MemoryPool<T>*>(static_cast<void *>(new char[size]));

  55.     next = runner;
  56.     for (int i = 0; i < howMany; i++)
  57.     {
  58.         runner->next = static_cast<MemoryPool<T>*> (static_cast<void*>(new char[size]));
  59.         runner = runner->next;
  60.     }
  61.     runner->next = NULL;
  62. }

  63. class Rational
  64. {
  65.     public:
  66.     Rational (int a = 0,int b = 1) : n(a),d(b) {}

  67.     void *operator new(size_t size) { return memPool->alloc(size); }
  68.     void operator delete(void* doomed, size_t size)
  69.     {
  70.         memPool->free(doomed);
  71.     }
  72.     static void newMemPool() { memPool = new MemoryPool<Rational>; }
  73.     static void deleteMemPool() { delete memPool;}

  74.     private:
  75.     int n;
  76.     int d;
  77.     static MemoryPool<Rational>* memPool;
  78. };
  79. MemoryPool<Rational> *Rational::memPool = NULL;

  80. int main()
  81. {
  82.     Rational *array[1000];

  83.     Rational::newMemPool();

  84.     for (int j = 0; j < 500; j++)
  85.     {
  86.         for (int i = 0; i < 1000; i++)
  87.         {
  88.             array[i] = new Rational(i);
  89.         }
  90.         for (int i = 0; i < 1000; i++)
  91.         {
  92.             delete array[i];
  93.         }
  94.     }
  95.     Rational::deleteMemPool();
  96.     return 0;
  97. }

阅读(724) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~