搞笑mempool
class aa
{
public:
aa()
{
x = 577;
y = 66;
z = 5666.54;
}
virtual void haha(){}
int x;
int y;
double z;
};
class bb : public aa
{
public:
void haha()
{
x = 555555;
}
};
#define POOL_MAX 100000
int g_nIndex = 0;
char g_szBuffer[POOL_MAX];
template<class T>
class CMemPool : public T
{
public:
void *operator new(std::size_t size)
{
if((g_nIndex + size) > POOL_MAX)
return NULL;
void* p = (void*)(g_szBuffer + g_nIndex);
g_nIndex = g_nIndex + size;
return p;
}
void operator delete (void *)
{
}
};
class CMemPoolImpl
{
public:
template<class T>
static T* malloc()
{
CMemPool<T>* p = new CMemPool<T>;
return (T*) p;
}
template<class T>
static void free(T* t)
{
CMemPool<T>* p = (CMemPool<T> *)t;
delete p;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
aa* p = CMemPoolImpl::malloc<bb>();
p->haha();
return 0;
}