From a0a388a897167f0efaf1657d9cdc32b14bd83604 Mon Sep 17 00:00:00 2001 From: Shijith Thotton Date: Wed, 28 Jul 2021 20:51:42 +0530 Subject: [PATCH] eal: add macro to swap two variables Add a macro to swap two variables and updat common autotest for the same. Signed-off-by: Shijith Thotton Acked-by: Jerin Jacob --- app/test/test_common.c | 4 ++++ lib/eal/include/rte_common.h | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/app/test/test_common.c b/app/test/test_common.c index 12bd1cad90..ef177cecb1 100644 --- a/app/test/test_common.c +++ b/app/test/test_common.c @@ -30,9 +30,13 @@ test_macros(int __rte_unused unused_parm) return -1;} uintptr_t unused = 0; + unsigned int smaller = SMALLER, bigger = BIGGER; RTE_SET_USED(unused); + RTE_SWAP(smaller, bigger); + if (smaller != BIGGER && bigger != SMALLER) + FAIL_MACRO(RTE_SWAP); if ((uintptr_t)RTE_PTR_ADD(SMALLER, PTR_DIFF) != BIGGER) FAIL_MACRO(RTE_PTR_ADD); if ((uintptr_t)RTE_PTR_SUB(BIGGER, PTR_DIFF) != SMALLER) diff --git a/lib/eal/include/rte_common.h b/lib/eal/include/rte_common.h index 99eb5f1820..4a399cc7c8 100644 --- a/lib/eal/include/rte_common.h +++ b/lib/eal/include/rte_common.h @@ -778,6 +778,14 @@ rte_log2_u64(uint64_t v) }) #endif +/** Swap two variables. */ +#define RTE_SWAP(a, b) \ + __extension__ ({ \ + typeof (a) _a = a; \ + a = b; \ + b = _a; \ + }) + /** * Get the size of a field in a structure. * -- 2.20.1