提交 6db41264 编写于 作者: Y yuqing

mblock support alloc_init_func

上级 503cdc60
Version 1.09 2014-10-24
Version 1.09 2014-10-26
* Version struct add variable: patch
* get local ipaddr support interface based 1
* mblock support alloc_init_func
Version 1.08 2014-10-09
* sched_thread.c: calculate next_call_time correctly
......
......@@ -9,8 +9,8 @@
#include "shared_func.h"
#include "pthread_func.h"
int fast_mblock_init(struct fast_mblock_man *mblock, const int element_size, \
const int alloc_elements_once)
int fast_mblock_init_ex(struct fast_mblock_man *mblock, const int element_size,
const int alloc_elements_once, fast_mblock_alloc_init_func init_func)
{
int result;
......@@ -43,6 +43,7 @@ int fast_mblock_init(struct fast_mblock_man *mblock, const int element_size, \
return result;
}
mblock->alloc_init_func = init_func;
mblock->malloc_chain_head = NULL;
mblock->free_chain_head = NULL;
mblock->total_count = 0;
......@@ -58,6 +59,7 @@ static int fast_mblock_prealloc(struct fast_mblock_man *mblock)
char *pTrunkStart;
char *p;
char *pLast;
int result;
int block_size;
int alloc_size;
......@@ -84,9 +86,28 @@ static int fast_mblock_prealloc(struct fast_mblock_man *mblock)
for (p=pTrunkStart; p<pLast; p += block_size)
{
pNode = (struct fast_mblock_node *)p;
if (mblock->alloc_init_func != NULL)
{
if ((result=mblock->alloc_init_func(pNode->data)) != 0)
{
free(pNew);
return result;
}
}
pNode->next = (struct fast_mblock_node *)(p + block_size);
}
((struct fast_mblock_node *)pLast)->next = NULL;
if (mblock->alloc_init_func != NULL)
{
if ((result=mblock->alloc_init_func(((struct fast_mblock_node *)
pLast)->data)) != 0)
{
free(pNew);
return result;
}
}
((struct fast_mblock_node *)pLast)->next = NULL;
mblock->free_chain_head = (struct fast_mblock_node *)pTrunkStart;
pMallocNode->next = mblock->malloc_chain_head;
......
......@@ -31,10 +31,13 @@ struct fast_mblock_malloc
struct fast_mblock_malloc *next;
};
typedef int (*fast_mblock_alloc_init_func)(void *element);
struct fast_mblock_man
{
struct fast_mblock_node *free_chain_head; //free node chain
struct fast_mblock_malloc *malloc_chain_head; //malloc chain to be freed
fast_mblock_alloc_init_func alloc_init_func;
int element_size; //element size
int alloc_elements_once; //alloc elements once
int total_count; //total element count
......@@ -49,6 +52,9 @@ struct fast_mblock_man
extern "C" {
#endif
#define fast_mblock_init(mblock, element_size, alloc_elements_once) \
fast_mblock_init_ex(mblock, element_size, alloc_elements_once, NULL)
/**
mblock init
parameters:
......@@ -57,8 +63,8 @@ parameters:
alloc_elements_once: malloc elements once, 0 for malloc 1MB once
return error no, 0 for success, != 0 fail
*/
int fast_mblock_init(struct fast_mblock_man *mblock, const int element_size, \
const int alloc_elements_once);
int fast_mblock_init_ex(struct fast_mblock_man *mblock, const int element_size,
const int alloc_elements_once, fast_mblock_alloc_init_func init_func);
/**
mblock destroy
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册