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

Message ID 1469203278-91363-1-git-send-email-sergio.gonzalez.monroy@intel.com (mailing list archive)
State Rejected, archived
Headers

Commit Message

Sergio Gonzalez Monroy July 22, 2016, 4:01 p.m. UTC
  Removing/freeing elements elements within a STAILQ_FOREACH loop
is not safe. FreeBSD defines STAILQ_FOREACH_SAFE macro, which permits
these operations safely.

This patch defines this macro for Linux systems, where it is not defined.

Signed-off-by: Sergio Gonzalez Monroy <sergio.gonzalez.monroy@intel.com>
---

NOTE: This patch is based on top of:
	http://dpdk.org/dev/patchwork/patch/14995/

 lib/librte_eal/common/include/rte_tailq.h | 7 +++++++
 1 file changed, 7 insertions(+)
  

Comments

Thomas Monjalon July 22, 2016, 4:16 p.m. UTC | #1
2016-07-22 17:01, Sergio Gonzalez Monroy:
> Removing/freeing elements elements within a STAILQ_FOREACH loop
> is not safe. FreeBSD defines STAILQ_FOREACH_SAFE macro, which permits
> these operations safely.
> 
> This patch defines this macro for Linux systems, where it is not defined.
[...]
> +#ifndef SLIST_FOREACH_SAFE
> +#define SLIST_FOREACH_SAFE(var, head, field, tvar)                      \
> +	for ((var) = SLIST_FIRST((head));                               \
> +	    (var) && ((tvar) = SLIST_NEXT((var), field), 1);            \
> +	    (var) = (tvar))
> +#endif

The patch 2 requires STAILQ_FOREACH_SAFE, not SLIST_FOREACH_SAFE.
  

Patch

diff --git a/lib/librte_eal/common/include/rte_tailq.h b/lib/librte_eal/common/include/rte_tailq.h
index cc3c0f1..bba2835 100644
--- a/lib/librte_eal/common/include/rte_tailq.h
+++ b/lib/librte_eal/common/include/rte_tailq.h
@@ -163,6 +163,13 @@  void __attribute__((constructor, used)) tailqinitfn_ ##t(void) \
 	    (var) = (tvar))
 #endif
 
+#ifndef SLIST_FOREACH_SAFE
+#define SLIST_FOREACH_SAFE(var, head, field, tvar)                      \
+	for ((var) = SLIST_FIRST((head));                               \
+	    (var) && ((tvar) = SLIST_NEXT((var), field), 1);            \
+	    (var) = (tvar))
+#endif
+
 #ifdef __cplusplus
 }
 #endif