Chinaunix首页 | 论坛 | 博客
  • 博客访问: 357570
  • 博文数量: 60
  • 博客积分: 15
  • 博客等级: 民兵
  • 技术积分: 1138
  • 用 户 组: 普通用户
  • 注册时间: 2012-03-20 16:18
个人简介

最多140个字

文章分类

全部博文(60)

文章存档

2016年(1)

2015年(34)

2014年(25)

分类: C/C++

2015-04-03 16:41:05


  1. #include<iostream>
  2. using namespace std;

  3. template<class T>
  4. class FreeList
  5. {
  6.     private:
  7.         FreeList<T>* next;//空闲链表的下一个元素
  8.         enum{EXPANSION_SIZE=20};//如果空闲链表为空,则按该SIZE扩展它
  9.         void expandTheFreeList(int initSize=EXPANSION_SIZE)//添加空闲元素到空闲列表
  10.         {
  11.             int size=sizeof(T)>sizeof(FreeList<T>*)? sizeof(T):sizeof(FreeList*);
  12.             FreeList<T> *ptr=(FreeList<T>*)new char[size];
  13.             next=ptr;
  14.             for(int i=0;i<initSize;i++)
  15.             {
  16.                 ptr->next=(FreeList<T>*)new char[size];
  17.                 ptr=ptr->next;
  18.             }
  19.             ptr->next=NULL;
  20.         }
  21.     public:
  22.         FreeList(int size=EXPANSION_SIZE)
  23.         {
  24.             expandTheFreeList(size);
  25.         }
  26.         ~FreeList()
  27.         {
  28.             FreeList<T> *ptr=next;
  29.             for(ptr=next;ptr!=NULL;ptr=next)
  30.             {
  31.                 next=next->next;
  32.                 delete[] (char*)ptr;
  33.             }
  34.         }
  35.         void *alloc(int size)//从空闲列表中分配头节点
  36.         {
  37.             if(next==NULL)
  38.                 expandTheFreeList();
  39.             FreeList<T> *head=next;
  40.             next=head->next;
  41.             return head;
  42.         }
  43.         void free(void *element)//释放T元素所占的空间
  44.         {
  45.             FreeList<T> *head=(FreeList<T>*)element;
  46.             head->next=next;
  47.             next=head;
  48.         }

  49. };

  50. class Rational{
  51. private:
  52.     int a;
  53.     int b;
  54.     static FreeList<Rational> *pool;
  55. public:
  56.     Rational(int a=0,int b=1)
  57.     {
  58.         Rational::a=a;
  59.         Rational::b=b;
  60.     }
  61.     void *operator new(size_t size)
  62.     {
  63.         return pool->alloc(size);
  64.     }
  65.     void operator delete(void *ptr,size_t size)
  66.     {
  67.         pool->free(ptr);
  68.     }
  69.     static void newPool()
  70.     {
  71.         pool=new FreeList<Rational>;
  72.     }
  73.     static void deletePool()
  74.     {
  75.         delete pool;
  76.     }
  77.     void show()
  78.     {
  79.         cout<<a<<' '<<b<<endl;
  80.     }
  81. };
  82. FreeList<Rational>* Rational::pool=NULL;

  83. int main()
  84. {
  85.     Rational::newPool();

  86.     for (int i=0; i<5; ++i)
  87.     {
  88.         Rational* ptr[1000];
  89.         for (int j=0; j<10; ++j)
  90.         {
  91.             ptr[j] = new Rational(i,j);
  92.             ptr[j]->show();
  93.         }
  94.         for (int k=0; k<10; ++k)
  95.         {
  96.             delete ptr[k];
  97.         }
  98.     }

  99.     Rational::deletePool();
  100. }
    1. //多线程版本
    2. #include<iostream>
    3. #include<pthread.h>
    4. #include<unistd.h>
    5. using namespace std;


    6. template<class T>
    7. class FreeList
    8. {
    9.     private:
    10.         FreeList<T>* next;//空闲链表的下一个元素
    11.         enum{EXPANSION_SIZE=20};//如果空闲链表为空,则按该SIZE扩展它
    12.         void expandTheFreeList(int initSize=EXPANSION_SIZE)//添加空闲元素到空闲列表
    13.         {
    14.             int size=sizeof(T)>sizeof(FreeList<T>*)? sizeof(T):sizeof(FreeList*);
    15.             FreeList<T> *ptr=(FreeList<T>*)new char[size];
    16.             next=ptr;
    17.             for(int i=0;i<initSize;i++)
    18.             {
    19.                 ptr->next=(FreeList<T>*)new char[size];
    20.                 ptr=ptr->next;
    21.             }
    22.             ptr->next=NULL;
    23.         }
    24.     public:
    25.         FreeList(int size=EXPANSION_SIZE)
    26.         {
    27.             expandTheFreeList(size);
    28.         }
    29.         ~FreeList()
    30.         {
    31.             FreeList<T> *ptr=next;
    32.             for(ptr=next;ptr!=NULL;ptr=next)
    33.             {
    34.                 next=next->next;
    35.                 delete[] (char*)ptr;
    36.             }
    37.         }
    38.         void *alloc(int size)//从空闲列表中分配头节点
    39.         {
    40.             if(next==NULL)
    41.                 expandTheFreeList();
    42.             FreeList<T> *head=next;
    43.             next=head->next;
    44.             return head;
    45.         }
    46.         void free(void *element)//释放T元素所占的空间
    47.         {
    48.             FreeList<T> *head=(FreeList<T>*)element;
    49.             head->next=next;
    50.             next=head;
    51.         }


    52. };


    53. template<class POOLTYPE, class LOCK>
    54. class MTMemoryPool
    55. {
    56. private:
    57. POOLTYPE pool;
    58. LOCK lockx;
    59. public:
    60. inline void *alloc(size_t size);
    61. inline void free(void *element);
    62. };
    63. template<class M,class L>
    64. inline void *MTMemoryPool<M,L>::alloc(size_t size)
    65. {
    66. void *mem;
    67. lockx.lock();
    68. mem=pool.alloc(size);
    69. lockx.unlock();
    70. return mem;
    71. };
    72. template<class M, class L>
    73. inline void MTMemoryPool<M,L>::free(void *dmd)
    74. {
    75. lockx.lock();
    76. pool.free(dmd);
    77. lockx.unlock();
    78. };


    79. class ABCLock
    80. {
    81. public:
    82. virtual ~ABCLock()
    83. {
    84. }
    85. virtual void lock()=0;
    86. virtual void unlock()=0;
    87. };


    88. class MutexLock:public ABCLock
    89. {
    90. private:
    91. pthread_mutex_t lockx;
    92. public:
    93. MutexLock()
    94. {
    95. pthread_mutex_init(&lockx,NULL);
    96. }
    97. ~MutexLock()
    98. {
    99. pthread_mutex_destroy(&lockx);
    100. }
    101. void lock()
    102. {
    103. pthread_mutex_lock(&lockx);
    104. }
    105. void unlock()
    106. {
    107. pthread_mutex_unlock(&lockx);
    108. }
    109. };


    110. class Rational{
    111. private:
    112.     int a;
    113.     int b;
    114.     static MTMemoryPool< FreeList<Rational>, MutexLock >* pool;//修改
    115. public:
    116.     Rational(int a=0,int b=1)
    117.     {
    118.         Rational::a=a;
    119.         Rational::b=b;
    120.     }
    121.     void *operator new(size_t size)
    122.     {
    123.         return pool->alloc(size);
    124.     }
    125.     void operator delete(void *ptr,size_t size)
    126.     {
    127.         pool->free(ptr);
    128.     }
    129.     static void newPool()
    130.     {
    131.         pool=new MTMemoryPool<FreeList<Rational>, MutexLock> ;//修改
    132.     }
    133.     static void deletePool()
    134.     {
    135.         delete pool;
    136.     }
    137.     void show()
    138.     {
    139.         cout<<a<<' '<<b<<endl;
    140.     }
    141. };
    142. MTMemoryPool<FreeList<Rational>, MutexLock>* Rational::pool=NULL;


    143. int main()
    144. {
    145.     Rational::newPool();


    146.     for (int i=0; i<5; ++i)
    147.     {
    148.         Rational* ptr[1000];
    149.         for (int j=0; j<10; ++j)
    150.         {
    151.             ptr[j] = new Rational(i,j);
    152.             ptr[j]->show();
    153.         }
    154.         for (int k=0; k<10; ++k)
    155.         {
    156.             delete ptr[k];
    157.         }
    158.     }


    159.     Rational::deletePool();
    160. }

阅读(2400) | 评论(0) | 转发(0) |
0

上一篇:链表快排

下一篇:chattr

给主人留下些什么吧!~~