Simple memory pool implementation

Source: Internet
Author: User

Simple memory pool implementation

# Include
 
  
# Include
  
   
# Include
   
    
# Include
    
     
Using namespace std; class MemPool {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 IndexOfFreeLis T (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, 0}; void * MemPool: GetFromPool (int n) {int num = 16; int size = UpToFourTime S (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 {// send the remaining resources to the linked list 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 ;}
    
   
  
 

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.