diff --git a/HISTORY b/HISTORY index 0622599..2ff1c94 100644 --- a/HISTORY +++ b/HISTORY @@ -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 diff --git a/src/fast_mblock.c b/src/fast_mblock.c index a519056..8fc0f50 100644 --- a/src/fast_mblock.c +++ b/src/fast_mblock.c @@ -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; ihead=alloc_node(mblock)) != NULL) { + chain->tail = chain->head; + for (i=1; inext = 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, diff --git a/src/fast_mblock.h b/src/fast_mblock.h index 9428fb8..bc79bc9 100644 --- a/src/fast_mblock.h +++ b/src/fast_mblock.h @@ -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 diff --git a/src/fc_queue.c b/src/fc_queue.c index a5a307d..74d7837 100644 --- a/src/fc_queue.c +++ b/src/fc_queue.c @@ -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; }