function fast_mblock_batch_alloc changed

posix_api
YuQing 2022-01-09 15:22:32 +08:00
parent af68bf5d6a
commit 0381982ac2
4 changed files with 46 additions and 21 deletions

View File

@ -1,4 +1,8 @@
Version 1.55 2022-01-09
* fastcommon php extension adapt to php 8
* function fast_mblock_batch_alloc changed
Version 1.54 2021-12-23
* fast_allocator.[hc]: correct reclaim_interval logic
* shared_func.[hc]: add functions getFileContentEx1 and getFileContent1

View File

@ -838,10 +838,9 @@ static inline void batch_free(struct fast_mblock_man *mblock,
}
}
struct fast_mblock_node *fast_mblock_batch_alloc(
struct fast_mblock_man *mblock, const int count)
int fast_mblock_batch_alloc(struct fast_mblock_man *mblock,
const int count, struct fast_mblock_chain *chain)
{
struct fast_mblock_chain chain;
struct fast_mblock_node *pNode;
int i;
int result;
@ -853,28 +852,31 @@ struct fast_mblock_node *fast_mblock_batch_alloc(
"call pthread_mutex_lock fail, "
"errno: %d, error info: %s",
__LINE__, result, STRERROR(result));
return NULL;
return result;
}
if ((chain.head=alloc_node(mblock)) != NULL)
{
chain.tail = chain.head;
for (i=1; i<count; i++)
{
if ((pNode=alloc_node(mblock)) == NULL)
{
if ((chain->head=alloc_node(mblock)) != NULL) {
chain->tail = chain->head;
for (i=1; i<count; i++) {
if ((pNode=alloc_node(mblock)) == NULL) {
break;
}
chain.tail->next = pNode;
chain.tail = pNode;
chain->tail->next = pNode;
chain->tail = pNode;
}
chain.tail->next = NULL;
chain->tail->next = NULL;
if (i != count) { //fail
batch_free(mblock, &chain);
chain.head = NULL;
if (i == count) {
result = 0;
} else { //fail
batch_free(mblock, chain);
chain->head = chain->tail = NULL;
result = ENOMEM;
}
} else {
chain->head = chain->tail = NULL;
result = ENOMEM;
}
if (mblock->need_lock && (result=pthread_mutex_unlock(
@ -886,7 +888,7 @@ struct fast_mblock_node *fast_mblock_batch_alloc(
__LINE__, result, STRERROR(result));
}
return chain.head;
return result;
}
int fast_mblock_batch_free(struct fast_mblock_man *mblock,

View File

@ -255,6 +255,17 @@ return 0 for success, return none zero if fail
int fast_mblock_free(struct fast_mblock_man *mblock,
struct fast_mblock_node *pNode);
/**
batch alloc nodes from the mblock
parameters:
mblock: the mblock pointer
count: alloc count
chain: return the mblock node chain
return 0 for success, return none zero if fail
*/
int fast_mblock_batch_alloc(struct fast_mblock_man *mblock,
const int count, struct fast_mblock_chain *chain);
/**
batch alloc nodes from the mblock
parameters:
@ -262,8 +273,16 @@ parameters:
count: alloc count
return the alloced node head, return NULL if fail
*/
struct fast_mblock_node *fast_mblock_batch_alloc(
struct fast_mblock_man *mblock, const int count);
static inline struct fast_mblock_node *fast_mblock_batch_alloc1(
struct fast_mblock_man *mblock, const int count)
{
struct fast_mblock_chain chain;
if (fast_mblock_batch_alloc(mblock, count, &chain) == 0) {
return chain.head;
} else {
return NULL;
}
}
/**
batch free nodes

View File

@ -211,7 +211,7 @@ int fc_queue_alloc_chain(struct fc_queue *queue, struct fast_mblock_man
{
struct fast_mblock_node *node;
if ((node=fast_mblock_batch_alloc(mblock, count)) == NULL) {
if ((node=fast_mblock_batch_alloc1(mblock, count)) == NULL) {
chain->head = chain->tail = NULL;
return ENOMEM;
}