/*
* The memory management module
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <my_mem.h>
my_int64 mem_count = 0; //used by debug
my_void* my_mem_malloc(my_uint32 size) //申请堆内存
{
void* pdate = malloc(size);
if (!pdate)
{
//my_log_access_error("[MEM]memory malloc failed! size : %d\n", size);
return NULL;
}
return pdate;
}
my_void my_mem_free(my_void* data) //释放堆内存
{
free(data);
}
my_void my_mem_palloc_block(my_mem_buf_t* pmembuf)
{
if(!pmembuf)
{
//my_log_access_error("[MEM]pool malloc block failed! pool pointer is NULL\n");
return;
}
my_uchar* pdata;
my_mem_pool_t *p, *new_pool, *current;
my_uint32 psize;
psize = (my_uint32)(pmembuf->pool->pd.end - (my_uchar*)(pmembuf->pool));
#ifndef MEM_DEBUG
pdata = my_mem_malloc(psize);
#else
pdata = my_mem_debug_malloc(psize, &mem_count);
#endif
if (NULL == pdata)
{
//my_log_access_error("[MEM]block memory malloc failed!\n");
return;
}
new_pool = (my_mem_pool_t *)pdata;
new_pool->pd.end = pdata + psize;
new_pool->pd.next = NULL;
new_pool->pd.failed = 0;
pdata += sizeof(my_mem_pool_data_t);
new_pool->pd.last = pdata + pmembuf->size;
current = pmembuf->pool->current;
for (p = current; p->pd.next; p = p->pd.next)
{
if (p->pd.failed++ > 4)
{
current = p->pd.next; //失败4次以上移动current指针
}
}
p->pd.next = new_pool;
pmembuf->pool->current = current ? current : new_pool;
pmembuf->data = pdata;
//my_log_access_debug("[MEM]pool malloc new block! pointer is %p\n", new_pool);
}
my_void my_mem_alloc_block(my_mem_pool_t* pool)
{
if(NULL == pool)
{
//my_log_access_error("[MEM]pool malloc block failed! pool pointer is NULL\n");
return;
}
my_uchar* pdata;
my_mem_pool_t *p, *new_pool;
my_uint32 psize;
psize = (my_uint32)(pool->pd.end - (my_uchar*)pool);
#ifndef MEM_DEBUG
pdata = my_mem_malloc(psize);
#else
pdata = my_mem_debug_malloc(psize, &mem_count);
#endif
if (NULL == pdata)
{
//my_log_access_error("[MEM]block memory malloc failed!\n");
return;
}
new_pool = (my_mem_pool_t *)pdata;
new_pool->pd.end = pdata + psize;
new_pool->pd.next = NULL;
new_pool->pd.failed = 0;
new_pool->pd.last = pdata + sizeof(my_mem_pool_data_t);
p = pool;
while(p->pd.next)
{
p = p->pd.next;
}
p->pd.next = new_pool;
//my_log_access_debug("[MEM]pool malloc new block! pointer is %p\n", new_pool);
}
my_void my_mem_palloc_large(my_mem_buf_t* pmembuf) //申请大内存块
{
if(!pmembuf)
{
//my_log_access_error("[MEM]pool alloc large memory failed! memory buffer pointer is NULL\n");
return;
}
my_mem_pool_large_t* large;
my_void* pdata;
#ifndef MEM_DEBUG
pdata = (my_mem_pool_large_t*)my_mem_malloc(pmembuf->size);
#else
pdata = (my_mem_pool_large_t*)my_mem_debug_malloc(pmembuf->size, &mem_count);
#endif
if (NULL == pdata)
{
return;
}
large = (my_mem_pool_large_t*)my_mem_pool_malloc(pmembuf->pool, sizeof(my_mem_pool_large_t));
large->data = pdata;
large->size = pmembuf->size;
large->next = pmembuf->pool->large;
pmembuf->pool->large = large;
pmembuf->data = pdata;
}
my_mem_pool_t* my_mem_pool_create(my_uint32 size, my_uint32 block_num) //创建内存池
{
if (size < sizeof(my_mem_pool_t))
{
//my_log_access_error("[MEM]memory pool create too small! size : %d\n", size);
return NULL;
}
my_mem_pool_t* pool;
#ifndef MEM_DEBUG
pool = my_mem_malloc(size);
#else
pool = my_mem_debug_malloc(size, &mem_count);
#endif
if (!pool)
{
//my_log_access_error("[MEM]memory pool create failed! memory malloc faild! size is %d\n", size);
return NULL;
}
int reserve = size - sizeof(my_mem_pool_t);
pool->pd.last = (my_uchar*)pool + sizeof(my_mem_pool_t);
pool->pd.end = (my_uchar*)pool + size;
pool->pd.next = NULL;
pool->pd.failed = 0;
pool->current = pool;
pool->large = NULL;
pool->max = (reserve > MY_MAX_ALLOC_FROM_POOL) ? MY_MAX_ALLOC_FROM_POOL : reserve;
//下面代码创建block_num-1个block(内存池数据块)以链表形式pool的后面
int i;
for(i = 0; i < block_num-1; ++i)
{
my_mem_alloc_block(pool);
}
//my_log_access_info("[MEM]memory pool create successed! block size is %d, block number is %d\n", size, block_num);
#ifdef MEM_PRINT_POOL
my_mem_debug_print_pool(pool);
#endif
return pool;
}
my_int32 my_mem_pool_free( my_mem_pool_t* pool) //释放内存池
{
if (!pool)
{
//my_log_access_error("[MEM]pool free faild! pool pointer is NULL!");
return -1;
}
my_mem_pool_t *p, *n;
my_mem_pool_large_t *large, *l;
//my_log_access_info("[MEM]memory pool start free!\n");
//释放申请的大块儿内存
large = pool->large;
while(large)
{
if (large->data)
{
#ifndef MEM_DEBUG
my_mem_free(large->data);
#else
my_mem_debug_free(large->data, large->size, &mem_count);
#endif
large->data = NULL;
//my_log_access_debug("[MEM]memory free! %p\n", large->data);
}
l = large;
large = l->next;
}
pool->large = NULL;
//释放内存池数据块
p = pool;
while(p)
{
n = p->pd.next;
#ifndef MEM_DEBUG
my_mem_free(p);
#else
my_mem_debug_free(p, (my_uint32)(p->pd.end - (my_uchar*)p), &mem_count);
#endif
p = n;
}
//my_log_access_debug("[MEM]pool free! malloc(+) and free(-) is %d\n", mem_count);
return 0;
}
my_mem_buf_t* my_mem_buf_alloc( my_mem_pool_t* pool, my_uint32 size ) //从内存池申请内存
{
if (!pool)
{
//my_log_access_error("[MEM]pool memory alloc faild! pool pointer is NULL!\n");
return NULL;
}
my_mem_buf_t* pmem_buf = (my_mem_buf_t*)my_mem_pool_malloc(pool, sizeof( my_mem_buf_t ));
if (NULL == pmem_buf)
{
return NULL;
}
my_mem_pool_t* p;
pmem_buf->pool = pool;
pmem_buf->pos = 0;
pmem_buf->size = size;
pmem_buf->hd = 0;
//my_log_access_debug("[MEM]malloc memory from pool, size is %u\n", size);
if (size < pool->max)
{
p = pool->current;
do
{
pmem_buf->data = (my_char*)p->pd.last;
if ((my_uint32)(p->pd.end - (my_uchar*)pmem_buf->data) >= size)
{
p->pd.last = (my_uchar*)pmem_buf->data + size;
#ifdef MEM_PRINT_POOL
my_mem_debug_print_pool(pool); //test by zmy
#endif
return pmem_buf;
}
p = p->pd.next;
} while (p);
my_mem_palloc_block(pmem_buf);
#ifdef MEM_PRINT_POOL
my_mem_debug_print_pool(pool); //test by zmy
#endif
return pmem_buf;
}
else
{
my_mem_palloc_large(pmem_buf);
return pmem_buf;
}
}
my_void* my_mem_pool_malloc(my_mem_pool_t* pool, my_uint32 size)
{
if(NULL == pool || size >= MY_MAX_ALLOC_FROM_POOL)
return NULL;
//my_log_access_info("[MEM]pool malloc used by yourself! size is %d\n", size);
my_mem_buf_t pmem_buf;
pmem_buf.pool = pool;
pmem_buf.size = size;
pmem_buf.pos = 0;
my_mem_pool_t* p;
p = pool->current;
do
{
pmem_buf.data = (my_char*)p->pd.last;
if ((my_uint32)(p->pd.end - (my_uchar*)
- 1
- 2
前往页