[dpdk-dev,v2,1/2] eal: add tailq safe iterator macro

Message ID 1469196122-168989-2-git-send-email-pablo.de.lara.guarch@intel.com (mailing list archive)
State Accepted, archived
Headers

Commit Message

De Lara Guarch, Pablo July 22, 2016, 2:02 p.m. UTC
  Removing/freeing elements elements within a TAILQ_FOREACH loop is not safe.
FreeBSD defines TAILQ_FOREACH_SAFE macro, which permits
these operations safely.
This patch defines this macro for Linux systems, where it is not defined.

Signed-off-by: Pablo de Lara <pablo.de.lara.guarch@intel.com>
---
 lib/librte_eal/common/include/rte_tailq.h | 8 ++++++++
 1 file changed, 8 insertions(+)
  

Patch

diff --git a/lib/librte_eal/common/include/rte_tailq.h b/lib/librte_eal/common/include/rte_tailq.h
index 4a686e6..cc3c0f1 100644
--- a/lib/librte_eal/common/include/rte_tailq.h
+++ b/lib/librte_eal/common/include/rte_tailq.h
@@ -155,6 +155,14 @@  void __attribute__((constructor, used)) tailqinitfn_ ##t(void) \
 		rte_panic("Cannot initialize tailq: %s\n", t.name); \
 }
 
+/* This macro permits both remove and free var within the loop safely.*/
+#ifndef TAILQ_FOREACH_SAFE
+#define TAILQ_FOREACH_SAFE(var, head, field, tvar)		\
+	for ((var) = TAILQ_FIRST((head));			\
+	    (var) && ((tvar) = TAILQ_NEXT((var), field), 1);	\
+	    (var) = (tvar))
+#endif
+
 #ifdef __cplusplus
 }
 #endif