简单内存池实现

#include <iostream>
#include <cstdlib>
#include <cassert>
#include <ctime>

using namespace std;

class MemPool{
	struct FreeNode{
		struct FreeNode* next;
	};
private:
	static const int allocNum = 8;
	static const int step = 4;
	static char *head;
	static char *tail;
	static unsigned int poolSize;
	static int  allocSize[allocNum];
	static FreeNode* FreeList[allocNum];
	static void* GetFromPool(int n);
	static void* PoolAlloc(int n, int &num);
	static int UpToFourTimes(int n);
//	static int IndexOfFreeList(int n);
public:
	static void *alloc(int n);
	static void dealloc(void *p, int n);
};

char *MemPool::head = NULL;
char *MemPool::tail = NULL;
unsigned int MemPool::poolSize = 0;
int MemPool::allocSize[MemPool::allocNum]= { 4, 8, 12, 16, 20, 24, 28, 32 };
MemPool::FreeNode *MemPool::FreeList[MemPool::allocNum] = { 0, 0, 0, 0, 0, 0, 0, 0 };

void *MemPool::GetFromPool(int n)
{
	int num = 16;
	int size = UpToFourTimes(n);
	void *result = PoolAlloc(size, num);
	if (result == NULL)
		return NULL;
	if (num == 1)
		return result;
	FreeNode *next,*curr =(FreeNode*)( (char*)result + size);
	FreeNode **indexNode = FreeList+size / step-1;
	*indexNode = curr;
	for (int i = 1; i < num-1; i++){
		next = (FreeNode*)((char*)curr + size);
		curr->next = next;
		curr = next;
	}
	curr->next = NULL;
	return result;
}

void *MemPool::PoolAlloc(int n, int &num)
{
	int needSize = n*num;
	char *result;
	int totalSize = tail - head;
	if (totalSize >= needSize){
		result = head;
		head += needSize;
		return result;
	}
	else if (totalSize >= n){
		num = totalSize / n;
		needSize = num*n;
		result = head;
		head += needSize;
		return result;
	}
	else{
		//将剩余的资源送到链表中
		FreeNode **indexNode = FreeList+totalSize / step-1;
		if (head != NULL){
			FreeNode *temp = (FreeNode*)head;
			int reallocSize = 2 * needSize;
	
			temp->next = *indexNode;
			*indexNode = temp;
			head = (char*)malloc(reallocSize);
			tail = head + reallocSize;
			poolSize += reallocSize;
			temp = (FreeNode*)head;
			head += needSize;
			return temp;
		}
		
	}
}


int MemPool::UpToFourTimes(int n)
{
	return n%step == 0 ? n : n += step - n % step;
}

void *MemPool::alloc(int n)
{
	if (n > 32)
		return malloc(n);
		int size = UpToFourTimes(n);
		FreeNode **indexNode = FreeList+size / step-1;
		if (*indexNode == NULL)
			return GetFromPool(size);
		FreeNode *result = *indexNode;
		*indexNode = result->next;
		return result;
}

void MemPool::dealloc(void *p, int n)
{
	if (n > 32)
		return free(p);
	FreeNode *result = (FreeNode*)p;
	int size = UpToFourTimes(n);
	FreeNode **indexNode = FreeList+size / step-1;
	result->next = *indexNode;
	*indexNode = result;
	return;
}

int main(void)
{
	srand(time(NULL));
	clock_t start, end;
	start = clock();
	for (int i = 0; i < 100000000; i++){
		int size = rand() % 32;
		char *a = (char*)MemPool::alloc(size);
		MemPool::dealloc(a, size);
	}
	end = clock();
	cout << "my time:" << end - start << "ms" << endl;

	start = clock();
	for (int i = 0; i < 100000000; i++){
		int size = rand() % 32;
		char *a = new char[size];
		delete[] a;
	}
	end = clock();
	cout << "sys time:" << end - start << "ms" << endl;

	return 0;
}







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值