mempool.cpp

来自「这是一个可以检索大量数据的程序」· C++ 代码 · 共 69 行

CPP
69
字号
// MemPool.cpp: implementation of the MemPool class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "EggCilcle.h"
#include "MemPool.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
MemPool::MemPool()
{
	theblock=0;
}
bool MemPool::create(int the_size)
{
	if(theblock==0)
	{
		space=NUM;
		size=the_size;
		int i,j=NUM;
		theblock=(char*)(new char[size*j]);
		if(theblock==0)return false;
		thehead=theblock;
		for(i=0;i<j;i++)
		{
			*(char**)(theblock+i*size)=(theblock+i*size+size);
		}
		*(char**)(theblock+j*size-j)=(char*)0;
	}
	return true;
}
MemPool::~MemPool()
{
}
void*MemPool::memalloc()
{
	char* pt=thehead;
	if(thehead)
	{
		thehead=*(char**)(thehead);
		space--;
	}
	return pt;
}
void MemPool::memfree(void*p)
{
	*(char**)(p)=thehead;
	thehead=(char*)p;
	space++;
}
void MemPool::destory()
{
	if(theblock)
	{
		delete[]theblock;
		theblock=0;
		thehead=0;
		size=0;
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?