[2/7] utils: Introduce SWAP() helper to exchange two variables

Submitted by Kirill Tkhai on June 28, 2017, 3:54 p.m.

Details

Message ID 149866524746.31392.14909158802170469190.stgit@localhost.localdomain
State New
Series "Skip iteration on open files to find parent's leftovers (optimize child creation)"
Headers show

Commit Message

Kirill Tkhai June 28, 2017, 3:54 p.m.
Signed-off-by: Kirill Tkhai <ktkhai@virtuozzo.com>
---
 criu/include/pid.h        |    8 ++------
 include/common/compiler.h |    7 +++++++
 2 files changed, 9 insertions(+), 6 deletions(-)

Patch hide | download patch | download mbox

diff --git a/criu/include/pid.h b/criu/include/pid.h
index 04549022e..37e39428e 100644
--- a/criu/include/pid.h
+++ b/criu/include/pid.h
@@ -48,14 +48,10 @@  struct pid {
 
 static inline bool equal_pid(struct pid *a, struct pid *b)
 {
-	struct pid *t;
 	int i;
 
-	if (a->level > b->level) {
-		t = a;
-		a = b;
-		b = t;
-	}
+	if (a->level > b->level)
+		SWAP(a, b);
 
 	for(i = 0; i < b->level; i++) {
 		if (i < a->level) {
diff --git a/include/common/compiler.h b/include/common/compiler.h
index 235413070..ada79f293 100644
--- a/include/common/compiler.h
+++ b/include/common/compiler.h
@@ -85,6 +85,13 @@ 
 	type __max2 = (y);			\
 	__max1 > __max2 ? __max1: __max2; })
 
+#define SWAP(x, y)				\
+	do {					\
+		typeof(x) ____val = x;		\
+		x = y;				\
+		y = ____val;			\
+	} while (0)
+
 #define is_log2(v)		(((v) & ((v) - 1)) == 0)
 
 #endif /* __CR_COMPILER_H__ */