From 8ce6f488b6e6bf091e53f49f20bb4f43de82f1d3 Mon Sep 17 00:00:00 2001 From: Pau Espin Pedrol Date: Fri, 17 Aug 2018 10:33:57 +0200 Subject: msgb: Introduce msgb_{de,en}queue_count APIs It's a common pattern having a list of msgb and having to maintain its size (for instance, to limit the maximum size of the list). Having the counter updated at the same time that the msgb is enqueued or dequeued helps avoiding introducing new bugs by forgetting to update the size counter at the right places. Change-Id: I33b501e89a8f29e4aa121696bcbb13d4b83db40f --- include/osmocom/core/msgb.h | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/include/osmocom/core/msgb.h b/include/osmocom/core/msgb.h index a8dc205b..b1cb6ec7 100644 --- a/include/osmocom/core/msgb.h +++ b/include/osmocom/core/msgb.h @@ -81,6 +81,40 @@ static inline void msgb_queue_free(struct llist_head *queue) while ((msg = msgb_dequeue(queue))) msgb_free(msg); } +/*! Enqueue message buffer to tail of a queue and increment queue size counter + * \param[in] queue linked list header of queue + * \param[in] msg message buffer to be added to the queue + * \param[in] count pointer to variable holding size of the queue + * + * The function will append the specified message buffer \a msg to the queue + * implemented by \ref llist_head \a queue using function \ref msgb_enqueue_count, + * then increment \a count + */ +static inline void msgb_enqueue_count(struct llist_head *queue, struct msgb *msg, + unsigned int *count) +{ + msgb_enqueue(queue, msg); + (*count)++; +} + +/*! Dequeue message buffer from head of queue and decrement queue size counter + * \param[in] queue linked list header of queue + * \param[in] count pointer to variable holding size of the queue + * \returns message buffer (if any) or NULL if queue empty + * + * The function will remove the first message buffer from the queue + * implemented by \ref llist_head \a queue using function \ref msgb_enqueue_count, + * and decrement \a count, all if queue is not empty. + */ +static inline struct msgb *msgb_dequeue_count(struct llist_head *queue, + unsigned int *count) +{ + struct msgb *msg = msgb_dequeue(queue); + if (msg) + (*count)--; + return msg; +} + #ifdef MSGB_DEBUG #include #define MSGB_ABORT(msg, fmt, args ...) do { \ -- cgit v1.2.3