-
#include<iostream>
-
using namespace std;
-
-
template<class T>
-
class FreeList
-
{
-
private:
-
FreeList<T>* next;//空闲链表的下一个元素
-
enum{EXPANSION_SIZE=20};//如果空闲链表为空,则按该SIZE扩展它
-
void expandTheFreeList(int initSize=EXPANSION_SIZE)//添加空闲元素到空闲列表
-
{
-
int size=sizeof(T)>sizeof(FreeList<T>*)? sizeof(T):sizeof(FreeList*);
-
FreeList<T> *ptr=(FreeList<T>*)new char[size];
-
next=ptr;
-
for(int i=0;i<initSize;i++)
-
{
-
ptr->next=(FreeList<T>*)new char[size];
-
ptr=ptr->next;
-
}
-
ptr->next=NULL;
-
}
-
public:
-
FreeList(int size=EXPANSION_SIZE)
-
{
-
expandTheFreeList(size);
-
}
-
~FreeList()
-
{
-
FreeList<T> *ptr=next;
-
for(ptr=next;ptr!=NULL;ptr=next)
-
{
-
next=next->next;
-
delete[] (char*)ptr;
-
}
-
}
-
void *alloc(int size)//从空闲列表中分配头节点
-
{
-
if(next==NULL)
-
expandTheFreeList();
-
FreeList<T> *head=next;
-
next=head->next;
-
return head;
-
}
-
void free(void *element)//释放T元素所占的空间
-
{
-
FreeList<T> *head=(FreeList<T>*)element;
-
head->next=next;
-
next=head;
-
}
-
-
};
-
-
class Rational{
-
private:
-
int a;
-
int b;
-
static FreeList<Rational> *pool;
-
public:
-
Rational(int a=0,int b=1)
-
{
-
Rational::a=a;
-
Rational::b=b;
-
}
-
void *operator new(size_t size)
-
{
-
return pool->alloc(size);
-
}
-
void operator delete(void *ptr,size_t size)
-
{
-
pool->free(ptr);
-
}
-
static void newPool()
-
{
-
pool=new FreeList<Rational>;
-
}
-
static void deletePool()
-
{
-
delete pool;
-
}
-
void show()
-
{
-
cout<<a<<' '<<b<<endl;
-
}
-
};
-
FreeList<Rational>* Rational::pool=NULL;
-
-
int main()
-
{
-
Rational::newPool();
-
-
for (int i=0; i<5; ++i)
-
{
-
Rational* ptr[1000];
-
for (int j=0; j<10; ++j)
-
{
-
ptr[j] = new Rational(i,j);
-
ptr[j]->show();
-
}
-
for (int k=0; k<10; ++k)
-
{
-
delete ptr[k];
-
}
-
}
-
-
Rational::deletePool();
-
}
-
//多线程版本
-
#include<iostream>
-
#include<pthread.h>
-
#include<unistd.h>
-
using namespace std;
-
-
-
template<class T>
-
class FreeList
-
{
-
private:
-
FreeList<T>* next;//空闲链表的下一个元素
-
enum{EXPANSION_SIZE=20};//如果空闲链表为空,则按该SIZE扩展它
-
void expandTheFreeList(int initSize=EXPANSION_SIZE)//添加空闲元素到空闲列表
-
{
-
int size=sizeof(T)>sizeof(FreeList<T>*)? sizeof(T):sizeof(FreeList*);
-
FreeList<T> *ptr=(FreeList<T>*)new char[size];
-
next=ptr;
-
for(int i=0;i<initSize;i++)
-
{
-
ptr->next=(FreeList<T>*)new char[size];
-
ptr=ptr->next;
-
}
-
ptr->next=NULL;
-
}
-
public:
-
FreeList(int size=EXPANSION_SIZE)
-
{
-
expandTheFreeList(size);
-
}
-
~FreeList()
-
{
-
FreeList<T> *ptr=next;
-
for(ptr=next;ptr!=NULL;ptr=next)
-
{
-
next=next->next;
-
delete[] (char*)ptr;
-
}
-
}
-
void *alloc(int size)//从空闲列表中分配头节点
-
{
-
if(next==NULL)
-
expandTheFreeList();
-
FreeList<T> *head=next;
-
next=head->next;
-
return head;
-
}
-
void free(void *element)//释放T元素所占的空间
-
{
-
FreeList<T> *head=(FreeList<T>*)element;
-
head->next=next;
-
next=head;
-
}
-
-
-
};
-
-
-
template<class POOLTYPE, class LOCK>
-
class MTMemoryPool
-
{
-
private:
-
POOLTYPE pool;
-
LOCK lockx;
-
public:
-
inline void *alloc(size_t size);
-
inline void free(void *element);
-
};
-
template<class M,class L>
-
inline void *MTMemoryPool<M,L>::alloc(size_t size)
-
{
-
void *mem;
-
lockx.lock();
-
mem=pool.alloc(size);
-
lockx.unlock();
-
return mem;
-
};
-
template<class M, class L>
-
inline void MTMemoryPool<M,L>::free(void *dmd)
-
{
-
lockx.lock();
-
pool.free(dmd);
-
lockx.unlock();
-
};
-
-
-
class ABCLock
-
{
-
public:
-
virtual ~ABCLock()
-
{
-
}
-
virtual void lock()=0;
-
virtual void unlock()=0;
-
};
-
-
-
class MutexLock:public ABCLock
-
{
-
private:
-
pthread_mutex_t lockx;
-
public:
-
MutexLock()
-
{
-
pthread_mutex_init(&lockx,NULL);
-
}
-
~MutexLock()
-
{
-
pthread_mutex_destroy(&lockx);
-
}
-
void lock()
-
{
-
pthread_mutex_lock(&lockx);
-
}
-
void unlock()
-
{
-
pthread_mutex_unlock(&lockx);
-
}
-
};
-
-
-
class Rational{
-
private:
-
int a;
-
int b;
-
static MTMemoryPool< FreeList<Rational>, MutexLock >* pool;//修改
-
public:
-
Rational(int a=0,int b=1)
-
{
-
Rational::a=a;
-
Rational::b=b;
-
}
-
void *operator new(size_t size)
-
{
-
return pool->alloc(size);
-
}
-
void operator delete(void *ptr,size_t size)
-
{
-
pool->free(ptr);
-
}
-
static void newPool()
-
{
-
pool=new MTMemoryPool<FreeList<Rational>, MutexLock> ;//修改
-
}
-
static void deletePool()
-
{
-
delete pool;
-
}
-
void show()
-
{
-
cout<<a<<' '<<b<<endl;
-
}
-
};
-
MTMemoryPool<FreeList<Rational>, MutexLock>* Rational::pool=NULL;
-
-
-
int main()
-
{
-
Rational::newPool();
-
-
-
for (int i=0; i<5; ++i)
-
{
-
Rational* ptr[1000];
-
for (int j=0; j<10; ++j)
-
{
-
ptr[j] = new Rational(i,j);
-
ptr[j]->show();
-
}
-
for (int k=0; k<10; ++k)
-
{
-
delete ptr[k];
-
}
-
}
-
-
-
Rational::deletePool();
- }
-
//多线程版本