#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; }
原文地址:http://blog.csdn.net/u012637838/article/details/44840621