mblock support alloc_init_func

pull/2/head
yuqing 2014-10-26 11:52:28 +08:00
parent 503cdc6006
commit 6db41264ef
3 changed files with 34 additions and 6 deletions

View File

@ -1,6 +1,7 @@
Version 1.09 2014-10-24 Version 1.09 2014-10-26
* Version struct add variable: patch * Version struct add variable: patch
* get local ipaddr support interface based 1 * get local ipaddr support interface based 1
* mblock support alloc_init_func
Version 1.08 2014-10-09 Version 1.08 2014-10-09
* sched_thread.c: calculate next_call_time correctly * sched_thread.c: calculate next_call_time correctly

View File

@ -9,8 +9,8 @@
#include "shared_func.h" #include "shared_func.h"
#include "pthread_func.h" #include "pthread_func.h"
int fast_mblock_init(struct fast_mblock_man *mblock, const int element_size, \ int fast_mblock_init_ex(struct fast_mblock_man *mblock, const int element_size,
const int alloc_elements_once) const int alloc_elements_once, fast_mblock_alloc_init_func init_func)
{ {
int result; int result;
@ -43,6 +43,7 @@ int fast_mblock_init(struct fast_mblock_man *mblock, const int element_size, \
return result; return result;
} }
mblock->alloc_init_func = init_func;
mblock->malloc_chain_head = NULL; mblock->malloc_chain_head = NULL;
mblock->free_chain_head = NULL; mblock->free_chain_head = NULL;
mblock->total_count = 0; mblock->total_count = 0;
@ -58,6 +59,7 @@ static int fast_mblock_prealloc(struct fast_mblock_man *mblock)
char *pTrunkStart; char *pTrunkStart;
char *p; char *p;
char *pLast; char *pLast;
int result;
int block_size; int block_size;
int alloc_size; int alloc_size;
@ -84,8 +86,27 @@ static int fast_mblock_prealloc(struct fast_mblock_man *mblock)
for (p=pTrunkStart; p<pLast; p += block_size) for (p=pTrunkStart; p<pLast; p += block_size)
{ {
pNode = (struct fast_mblock_node *)p; 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); pNode->next = (struct fast_mblock_node *)(p + block_size);
} }
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; ((struct fast_mblock_node *)pLast)->next = NULL;
mblock->free_chain_head = (struct fast_mblock_node *)pTrunkStart; mblock->free_chain_head = (struct fast_mblock_node *)pTrunkStart;

View File

@ -31,10 +31,13 @@ struct fast_mblock_malloc
struct fast_mblock_malloc *next; struct fast_mblock_malloc *next;
}; };
typedef int (*fast_mblock_alloc_init_func)(void *element);
struct fast_mblock_man struct fast_mblock_man
{ {
struct fast_mblock_node *free_chain_head; //free node chain struct fast_mblock_node *free_chain_head; //free node chain
struct fast_mblock_malloc *malloc_chain_head; //malloc chain to be freed 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 element_size; //element size
int alloc_elements_once; //alloc elements once int alloc_elements_once; //alloc elements once
int total_count; //total element count int total_count; //total element count
@ -49,6 +52,9 @@ struct fast_mblock_man
extern "C" { extern "C" {
#endif #endif
#define fast_mblock_init(mblock, element_size, alloc_elements_once) \
fast_mblock_init_ex(mblock, element_size, alloc_elements_once, NULL)
/** /**
mblock init mblock init
parameters: parameters:
@ -57,8 +63,8 @@ parameters:
alloc_elements_once: malloc elements once, 0 for malloc 1MB once alloc_elements_once: malloc elements once, 0 for malloc 1MB once
return error no, 0 for success, != 0 fail return error no, 0 for success, != 0 fail
*/ */
int fast_mblock_init(struct fast_mblock_man *mblock, const int element_size, \ int fast_mblock_init_ex(struct fast_mblock_man *mblock, const int element_size,
const int alloc_elements_once); const int alloc_elements_once, fast_mblock_alloc_init_func init_func);
/** /**
mblock destroy mblock destroy