<p>Harald Welte <strong>merged</strong> this change.</p><p><a href="https://gerrit.osmocom.org/10485">View Change</a></p><div style="white-space:pre-wrap">Approvals:
  Jenkins Builder: Verified
  Harald Welte: Looks good to me, approved

</div><pre style="font-family: monospace,monospace; white-space: pre-wrap;">msgb: Introduce msgb_{de,en}queue_count APIs<br><br>It's a common pattern having a list of msgb and having to maintain its<br>size (for instance, to limit the maximum size of the list). Having the<br>counter updated at the same time that the msgb is enqueued or dequeued<br>helps avoiding introducing new bugs by forgetting to update the size<br>counter at the right places.<br><br>Change-Id: I33b501e89a8f29e4aa121696bcbb13d4b83db40f<br>---<br>M include/osmocom/core/msgb.h<br>1 file changed, 34 insertions(+), 0 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/include/osmocom/core/msgb.h b/include/osmocom/core/msgb.h</span><br><span>index a8dc205..b1cb6ec 100644</span><br><span>--- a/include/osmocom/core/msgb.h</span><br><span>+++ b/include/osmocom/core/msgb.h</span><br><span>@@ -81,6 +81,40 @@</span><br><span>  while ((msg = msgb_dequeue(queue))) msgb_free(msg);</span><br><span> }</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+/*! Enqueue message buffer to tail of a queue and increment queue size counter</span><br><span style="color: hsl(120, 100%, 40%);">+ * \param[in] queue linked list header of queue</span><br><span style="color: hsl(120, 100%, 40%);">+ * \param[in] msg message buffer to be added to the queue</span><br><span style="color: hsl(120, 100%, 40%);">+ * \param[in] count pointer to variable holding size of the queue</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * The function will append the specified message buffer \a msg to the queue</span><br><span style="color: hsl(120, 100%, 40%);">+ * implemented by \ref llist_head \a queue using function \ref msgb_enqueue_count,</span><br><span style="color: hsl(120, 100%, 40%);">+ * then increment \a count</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+static inline void msgb_enqueue_count(struct llist_head *queue, struct msgb *msg,</span><br><span style="color: hsl(120, 100%, 40%);">+                                 unsigned int *count)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+       msgb_enqueue(queue, msg);</span><br><span style="color: hsl(120, 100%, 40%);">+     (*count)++;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*! Dequeue message buffer from head of queue and decrement queue size counter</span><br><span style="color: hsl(120, 100%, 40%);">+ * \param[in] queue linked list header of queue</span><br><span style="color: hsl(120, 100%, 40%);">+ * \param[in] count pointer to variable holding size of the queue</span><br><span style="color: hsl(120, 100%, 40%);">+ * \returns message buffer (if any) or NULL if queue empty</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * The function will remove the first message buffer from the queue</span><br><span style="color: hsl(120, 100%, 40%);">+ * implemented by \ref llist_head \a queue using function \ref msgb_enqueue_count,</span><br><span style="color: hsl(120, 100%, 40%);">+ * and decrement \a count, all if queue is not empty.</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+static inline struct msgb *msgb_dequeue_count(struct llist_head *queue,</span><br><span style="color: hsl(120, 100%, 40%);">+                                            unsigned int *count)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+       struct msgb *msg = msgb_dequeue(queue);</span><br><span style="color: hsl(120, 100%, 40%);">+       if (msg)</span><br><span style="color: hsl(120, 100%, 40%);">+              (*count)--;</span><br><span style="color: hsl(120, 100%, 40%);">+   return msg;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> #ifdef MSGB_DEBUG</span><br><span> #include <osmocom/core/panic.h></span><br><span> #define MSGB_ABORT(msg, fmt, args ...) do {           \</span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.osmocom.org/10485">change 10485</a>. To unsubscribe, or for help writing mail filters, visit <a href="https://gerrit.osmocom.org/settings">settings</a>.</p><div itemscope itemtype="http://schema.org/EmailMessage"><div itemscope itemprop="action" itemtype="http://schema.org/ViewAction"><link itemprop="url" href="https://gerrit.osmocom.org/10485"/><meta itemprop="name" content="View Change"/></div></div>

<div style="display:none"> Gerrit-Project: libosmocore </div>
<div style="display:none"> Gerrit-Branch: master </div>
<div style="display:none"> Gerrit-MessageType: merged </div>
<div style="display:none"> Gerrit-Change-Id: I33b501e89a8f29e4aa121696bcbb13d4b83db40f </div>
<div style="display:none"> Gerrit-Change-Number: 10485 </div>
<div style="display:none"> Gerrit-PatchSet: 1 </div>
<div style="display:none"> Gerrit-Owner: Pau Espin Pedrol <pespin@sysmocom.de> </div>
<div style="display:none"> Gerrit-Reviewer: Harald Welte <laforge@gnumonks.org> </div>
<div style="display:none"> Gerrit-Reviewer: Jenkins Builder </div>