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

全部博文(101)

文章存档

2013年(15)

2012年(86)

我的朋友

分类: C/C++

2012-10-02 10:43:34

对于内存池这一块接触都有几年的时间了,看了很多代码也不明白,真是差啊,今天把这本书的代码抄了一遍,代码在Linux下编译有些问题
1.全局函数
关于static_cast在gcc 4.1.2上使用有误

点击(此处)折叠或打开

  1. #include <iostream>

  2. class Rational
  3. {
  4.     public:
  5.     Rational (int a = 0, int b = 1) : n(a),d(b) {}

  6.     private:
  7.         int n;
  8.         int d;
  9. };


  10. int main()
  11. {
  12.     Rational *array[1000];

  13.     for (int j = 0; j < 500; j++)
  14.     {
  15.         for (int i = 0; i < 1000; i++)
  16.         {
  17.             array[i] = new Rational(i);
  18.         }
  19.         for (int i = 0; i < 1000; i++)
  20.         {
  21.             delete array[i];
  22.         }
  23.     }
  24.     return 0;
  25. }
2 .专用Rational 内存管理器
1.依然是static_cast的使用


点击(此处)折叠或打开

  1. #include <iostream>


  2. class NextOnFreeList
  3. {
  4.     public:
  5.         NextOnFreeList *next;
  6. };

  7. class Rational
  8. {
  9.     public:
  10.     Rational (int a = 0, int b = 1) : n(a),d(b) {}

  11.     inline void *operator new(size_t size);
  12.     inline void operator delete(void *doomed,size_t size);

  13.     static void newMemPool() { expandTheFreeList();}
  14.     static void deleteMemPool();

  15.     private:
  16.         static NextOnFreeList *freeList;
  17.         static void expandTheFreeList();
  18.         enum { EXPANSION_SIZE = 32};

  19.         int n; //numerator
  20.         int d; //denominator
  21. };

  22. inline size_t mymax(size_t a,size_t b) { return a > b ? a: b;}

  23. inline
  24. void* Rational::operator new(size_t size)
  25. {
  26.     if (0 == freeList)
  27.     {
  28.         expandTheFreeList();
  29.     }
  30.     NextOnFreeList *head = freeList;
  31.     freeList = head->next;

  32.     return head;
  33. }

  34. inline
  35. void Rational::operator delete(void* doomed, size_t size)
  36. {
  37.     NextOnFreeList *head = static_cast<NextOnFreeList *> (doomed);

  38.     head->next = freeList;
  39.     freeList = head;
  40. }

  41. void Rational::expandTheFreeList()
  42. {
  43.     size_t size = mymax(sizeof(Rational),sizeof(NextOnFreeList *));

  44.     NextOnFreeList *runner = static_cast<NextOnFreeList *> (static_cast<void *>(new char[size]));

  45.     freeList = runner;
  46.     for (int i = 0; i < EXPANSION_SIZE; i++)
  47.     {
  48.         runner->next = static_cast<NextOnFreeList *> (static_cast<void *>(new char[size]));
  49.         runner = runner->next;
  50.     }
  51.     runner->next = NULL;
  52. }

  53. NextOnFreeList* Rational::freeList = NULL;
  54. int main()
  55. {
  56.     Rational *array[1000];

  57.     for (int j = 0; j < 500; j++)
  58.     {
  59.         for (int i = 0; i < 1000; i++)
  60.         {
  61.             array[i] = new Rational(i);
  62.         }
  63.         for (int i = 0; i < 1000; i++)
  64.         {
  65.             delete array[i];
  66.         }
  67.     }
  68.     return 0;
  69. }
我所使用的系统是虚拟机,经过测试第二个是第一个效率的2倍
使用Tcmalloc的效率提升多少没有实验
阅读(788) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~