/*
*
*
*/
#include "mem.h"
MEM_POOL g_stMemPool;
/***************************Begin of Memory Pool Section********************************/
/* @x : size of memory block (32 /64 /.../256....)
* @tpblk: ptr to MEM_BLK used as temporary storage
*/
#define MEMPOOL_ALLOC(x, tpblk) BUCKET_ALLOC(&(g_stMemPool.m_bucket##x), tpblk)
/* @x : size of memory block (32 /64 /.../256....)
* @pdata : ptr to data member of struct _memBlk
*/
#define MEMPOOL_FREE(x, pdata) BUCKET_FREE(&(g_stMemPool.m_bucket##x), pdata)
/* @x : size of memory block (32 /64 /.../256....)
* @n : number of malloced memory blocks
*/
#define MEMPOOL_INIT(x, n) { BUCKET_INIT(&(g_stMemPool.m_bucket##x), x, n);\
g_stMemPool.ulTotalSize += (n)*(x);\
}
/*
* @x : size of memory block (32 /64 /.../256....)
*/
#define MEMPOOL_CLEANUP(x) BUCKET_CLEANUP(&(g_stMemPool.m_bucket##x))
/*initialize the memory pool, should be called first*/
ULONG MemPoolInit()
{
memset(&g_stMemPool, 0, sizeof(MEM_POOL));
SEM_INIT(&g_stMemPool.sem);
SEM_LOCK(&g_stMemPool.sem);
MEMPOOL_INIT(32, M_MEM_BLK32_SIZE);
MEMPOOL_INIT(64, M_MEM_BLK64_SIZE);
MEMPOOL_INIT(96, M_MEM_BLK96_SIZE);
MEMPOOL_INIT(128, M_MEM_BLK128_SIZE);
MEMPOOL_INIT(160, M_MEM_BLK160_SIZE);
MEMPOOL_INIT(192, M_MEM_BLK192_SIZE);
MEMPOOL_INIT(224, M_MEM_BLK224_SIZE);
MEMPOOL_INIT(256, M_MEM_BLK256_SIZE);
SEM_UNLOCK(&g_stMemPool.sem);
printf("Init Memory pool succeed,\n(%uMb)have been pre-allocated!\n", g_stMemPool.ulTotalSize>>20);
return RET_OK;
}
/*finalize*/
void MemPoolCleanup()
{
SEM_LOCK(&g_stMemPool.sem);
MEMPOOL_CLEANUP(32);
MEMPOOL_CLEANUP(64);
MEMPOOL_CLEANUP(96);
MEMPOOL_CLEANUP(128);
MEMPOOL_CLEANUP(160);
MEMPOOL_CLEANUP(192);
MEMPOOL_CLEANUP(224);
MEMPOOL_CLEANUP(256);
g_stMemPool.ulTotalSize = 0;
SEM_UNLOCK(&g_stMemPool.sem);
SEM_DESTROY(&g_stMemPool.sem);
}
/*exported interface to allocate memory*/
void *MemAlloc(ULONG ulSize)
{
void *p = NULL;
MEM_BLK *tp = NULL;
if(0 == ulSize)
return NULL;
SEM_LOCK(&g_stMemPool.sem);
switch(ulSize){
case 1 ... 32:
p = MEMPOOL_ALLOC(32, tp);
break;
case 33 ... 64:
p = MEMPOOL_ALLOC(64, tp);
break;
case 65 ... 96:
p = MEMPOOL_ALLOC(96, tp);
break;
case 97 ... 128:
p = MEMPOOL_ALLOC(128, tp);
break;
case 129 ... 160:
p = MEMPOOL_ALLOC(160, tp);
break;
case 161 ... 192:
p = MEMPOOL_ALLOC(192, tp);
break;
case 193 ... 224:
p = MEMPOOL_ALLOC(224, tp);
break;
case 225 ... 256:
p = MEMPOOL_ALLOC(256, tp);
break;
default:
p = malloc(ulSize);
}
SEM_UNLOCK(&g_stMemPool.sem);
return p;
}
/*exported interface to free memory*/
void MemFree(void *p)
{
MEM_BLK *pstBlk = NULL;
if(NULL == p) return;
SEM_LOCK(&g_stMemPool.sem);
pstBlk = mem_blk_entry(p, MEM_BLK, data);
switch(pstBlk->ulBlkSize)
{
case 32:
MEMPOOL_FREE(32, p);
break;
case 64:
MEMPOOL_FREE(64, p);
break;
case 96:
MEMPOOL_FREE(96, p);
break;
case 128:
MEMPOOL_FREE(128, p);
break;
case 160:
MEMPOOL_FREE(160, p);
break;
case 192:
MEMPOOL_FREE(192, p);
break;
case 224:
MEMPOOL_FREE(224, p);
break;
case 256:
MEMPOOL_FREE(256, p);
break;
default:
free(p);
p = NULL;
}
SEM_UNLOCK(&g_stMemPool.sem);
return;
}
/***************************End of Memory Pool Section********************************/