From 81950ac246b03d63945f4ae816af2396f0e4a233 Mon Sep 17 00:00:00 2001 From: YuQing <384681@qq.com> Date: Sat, 11 Sep 2021 18:40:26 +0800 Subject: [PATCH] add function fc_queue_free_chain --- src/fast_mblock.c | 8 +++++--- src/fast_mblock.h | 17 ----------------- src/fc_queue.c | 27 +++++++++++++++++++++++++++ src/fc_queue.h | 4 ++++ src/sorted_queue.h | 6 ++++++ 5 files changed, 42 insertions(+), 20 deletions(-) diff --git a/src/fast_mblock.c b/src/fast_mblock.c index ab05200..38198dd 100644 --- a/src/fast_mblock.c +++ b/src/fast_mblock.c @@ -864,6 +864,7 @@ void fast_mblock_free_objects(struct fast_mblock_man *mblock, void **obj; void **end; struct fast_mblock_node *previous; + struct fast_mblock_node *current; struct fast_mblock_chain chain; if (count == 0) { @@ -873,12 +874,13 @@ void fast_mblock_free_objects(struct fast_mblock_man *mblock, chain.head = previous = fast_mblock_to_node_ptr(objs[0]); end = objs + count; for (obj=objs+1; objnext = *obj; - previous = *obj; + current = fast_mblock_to_node_ptr(*obj); + previous->next = current; + previous = current; } previous->next = NULL; - chain.tail = fast_mblock_to_node_ptr(previous); + chain.tail = previous; fast_mblock_batch_free(mblock, &chain); } diff --git a/src/fast_mblock.h b/src/fast_mblock.h index c18796b..fac9720 100644 --- a/src/fast_mblock.h +++ b/src/fast_mblock.h @@ -263,23 +263,6 @@ return 0 for success, return none zero if fail int fast_mblock_batch_free(struct fast_mblock_man *mblock, struct fast_mblock_chain *chain); -/** -batch free objects -parameters: - mblock: the mblock pointer - head: the head object - tail: the tail object -return 0 for success, return none zero if fail -*/ -static inline int fast_mblock_batch_free_objs(struct fast_mblock_man - *mblock, void *head, void *tail) -{ - struct fast_mblock_chain chain; - chain.head = fast_mblock_to_node_ptr(head); - chain.tail = fast_mblock_to_node_ptr(tail); - return fast_mblock_batch_free(mblock, &chain); -} - /** delay free a node (put a node to the mblock) parameters: diff --git a/src/fc_queue.c b/src/fc_queue.c index f31ab0c..00c1eac 100644 --- a/src/fc_queue.c +++ b/src/fc_queue.c @@ -205,3 +205,30 @@ void *fc_queue_timedpeek(struct fc_queue *queue, return data; } + +int fc_queue_free_chain(struct fc_queue *queue, struct fast_mblock_man + *mblock, struct fc_queue_info *qinfo) +{ + struct fast_mblock_node *previous; + struct fast_mblock_node *current; + struct fast_mblock_chain chain; + void *data; + + if (qinfo->head == NULL) { + return 0; + } + + chain.head = previous = fast_mblock_to_node_ptr(qinfo->head); + data = FC_QUEUE_NEXT_PTR(queue, qinfo->head); + while (data != NULL) { + current = fast_mblock_to_node_ptr(data); + previous->next = current; + + previous = current; + data = FC_QUEUE_NEXT_PTR(queue, data); + } + + previous->next = NULL; + chain.tail = previous; + return fast_mblock_batch_free(mblock, &chain); +} diff --git a/src/fc_queue.h b/src/fc_queue.h index 742de5a..b0bdee4 100644 --- a/src/fc_queue.h +++ b/src/fc_queue.h @@ -19,6 +19,7 @@ #define _FC_QUEUE_H #include "common_define.h" +#include "fast_mblock.h" struct fc_queue_info { @@ -172,6 +173,9 @@ void *fc_queue_timedpeek(struct fc_queue *queue, #define fc_queue_timedpeek_us(queue, timeout_us) \ fc_queue_timedpeek(queue, timeout_us, FC_TIME_UNIT_USECOND) +int fc_queue_free_chain(struct fc_queue *queue, struct fast_mblock_man + *mblock, struct fc_queue_info *qinfo); + #ifdef __cplusplus } #endif diff --git a/src/sorted_queue.h b/src/sorted_queue.h index 0004226..f04357d 100644 --- a/src/sorted_queue.h +++ b/src/sorted_queue.h @@ -121,6 +121,12 @@ static inline void *sorted_queue_timedpeek(struct sorted_queue *sq, #define sorted_queue_timedpeek_us(sq, timeout_us) \ sorted_queue_timedpeek(sq, timeout_us, FC_TIME_UNIT_USECOND) +static inline int sorted_queue_free_chain(struct sorted_queue *sq, + struct fast_mblock_man *mblock, struct fc_queue_info *qinfo) +{ + return fc_queue_free_chain(&sq->queue, mblock, qinfo); +} + #ifdef __cplusplus } #endif