[bpf-next PATCH v1] samples/bpf: Cleanup repetitive swap function

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Use the macro version of swap function and move its definition to
bpf_util.h since it is repetitive in some files. This commit also fixes
a warning from coccinelle:

- ./samples/bpf/xdp_sample_user.c:1493:8-9: WARNING opportunity for swap()
- ./samples/bpf/xdp_rxq_info_user.c:435:8-9: WARNING opportunity for swap()

Signed-off-by: Anh Tuan Phan <tuananhlfc@xxxxxxxxx>
---
 samples/bpf/xdp_rxq_info_user.c        | 12 +-----------
 samples/bpf/xdp_sample_user.c          | 12 +-----------
 tools/testing/selftests/bpf/bpf_util.h |  3 +++
 3 files changed, 5 insertions(+), 22 deletions(-)

diff --git a/samples/bpf/xdp_rxq_info_user.c b/samples/bpf/xdp_rxq_info_user.c
index b95e0ef61f06..862ee370f96a 100644
--- a/samples/bpf/xdp_rxq_info_user.c
+++ b/samples/bpf/xdp_rxq_info_user.c
@@ -426,16 +426,6 @@ static void stats_print(struct stats_record *stats_rec,
 }
 
 
-/* Pointer swap trick */
-static inline void swap(struct stats_record **a, struct stats_record **b)
-{
-	struct stats_record *tmp;
-
-	tmp = *a;
-	*a = *b;
-	*b = tmp;
-}
-
 static void stats_poll(int interval, int action, __u32 cfg_opt)
 {
 	struct stats_record *record, *prev;
@@ -445,7 +435,7 @@ static void stats_poll(int interval, int action, __u32 cfg_opt)
 	stats_collect(record);
 
 	while (1) {
-		swap(&prev, &record);
+		swap(prev, record);
 		stats_collect(record);
 		stats_print(record, prev, action, cfg_opt);
 		sleep(interval);
diff --git a/samples/bpf/xdp_sample_user.c b/samples/bpf/xdp_sample_user.c
index 158682852162..9508bc0c2f2f 100644
--- a/samples/bpf/xdp_sample_user.c
+++ b/samples/bpf/xdp_sample_user.c
@@ -1484,16 +1484,6 @@ static int sample_signal_cb(void)
 	return 0;
 }
 
-/* Pointer swap trick */
-static void swap(struct stats_record **a, struct stats_record **b)
-{
-	struct stats_record *tmp;
-
-	tmp = *a;
-	*a = *b;
-	*b = tmp;
-}
-
 static int sample_timer_cb(int timerfd, struct stats_record **rec,
 			   struct stats_record **prev)
 {
@@ -1505,7 +1495,7 @@ static int sample_timer_cb(int timerfd, struct stats_record **rec,
 	if (ret < 0)
 		return -errno;
 
-	swap(prev, rec);
+	swap(*prev, *rec);
 	ret = sample_stats_collect(*rec);
 	if (ret < 0)
 		return ret;
diff --git a/tools/testing/selftests/bpf/bpf_util.h b/tools/testing/selftests/bpf/bpf_util.h
index 10587a29b967..ba3c44d64d44 100644
--- a/tools/testing/selftests/bpf/bpf_util.h
+++ b/tools/testing/selftests/bpf/bpf_util.h
@@ -8,6 +8,9 @@
 #include <errno.h>
 #include <bpf/libbpf.h> /* libbpf_num_possible_cpus */
 
+#define swap(a, b) \
+	do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
+
 static inline unsigned int bpf_num_possible_cpus(void)
 {
 	int possible_cpus = libbpf_num_possible_cpus();
-- 
2.34.1





[Index of Archives]     [Linux Samsung SoC]     [Linux Rockchip SoC]     [Linux Actions SoC]     [Linux for Synopsys ARC Processors]     [Linux NFS]     [Linux NILFS]     [Linux USB Devel]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]


  Powered by Linux