Skip to contentSkip to conversation

generic swap()

PATCH Peter Zijlstra 3 participants 4 messages Jan 16, 2009 – Feb 6, 2009

lore
Share & tools
lore
#0 lore raw

Hi,

Where did this patch come from? -- I can't seem to find it on lkml at all..

The reason I ask it that I wonder why swap() has a return value?

---

commit 91f68b7359144aa40bb9668124543d15284750b4

Author: Wu Fengguang <[email protected]>
Date:   Wed Jan 7 18:09:12 2009 -0800

generic swap(): introduce global macro swap(a, b)

There have been some local definitions of swap(), it's time to replace them all with a uniform one.

include/linux/kernel.h +6 -0
index 6b8e202..343df9e 100644
@@ -476,6 +476,12 @@ static inline char *pack_hex_byte(char *buf, u8 byte)
 	__val = __val < __min ? __min: __val;	\
 	__val > __max ? __max: __val; })
 
+
+/*
+ * swap - swap value of @a and @b
+ */
+#define swap(a, b) ({ typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; })
+
 /**
  * container_of - cast a member of a structure out to the containing structure
  * @ptr:	the pointer to the member.
#1 lore raw
Quoted 3 lines · On Fri, 16 Jan 2009 10:26:34 +0100 Peter Zijlstra <[email protected]> wrote:

Hi,

Where did this patch come from? -- I can't seem to find it on lkml at all..

That's a damn good question. I received:

From: Wu Fengguang <[email protected]>
To: Andrew Morton <[email protected]>
Cc: LKML <[email protected]>
Subject: [PATCH 0/9] [PATCH] make swap() a global macro
Date: Tue, 02 Dec 2008 19:45:24 +0800
User-Agent: quilt/0.46-1

but the cc didn't seem to work. quilt bustage?

Quoted 1 line

The reason I ask it that I wonder why swap() has a return value?

Accident, I guess.

Quoted 1 line

+#define swap(a, b) ({ typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; })

You think it should use the do{}while(0) thing?

#2 lore raw
Quoted 7 lines · Andrew Morton
Quoted 3 lines · On Fri, 16 Jan 2009 10:26:34 +0100 Peter Zijlstra <[email protected]> wrote:

Hi,

Where did this patch come from? -- I can't seem to find it on lkml at all..

That's a damn good question. I received:

Hehe, one of the mysteries of life then :-)

Quoted 7 lines
Quoted 1 line

The reason I ask it that I wonder why swap() has a return value?

Accident, I guess.

Quoted 1 line

+#define swap(a, b) ({ typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; })

You think it should use the do{}while(0) thing?

That was what I was thinking indeed. Non of the current users appear to make use of the (somewhat arbitrary) return value, so lets change it before someone gets creative ;-)

---

include/linux/kernel.h +2 -1
index 343df9e..7fa3718 100644
@@ -480,7 +480,8 @@ static inline char *pack_hex_byte(char *buf, u8 byte)
 /*
  * swap - swap value of @a and @b
  */
-#define swap(a, b) ({ typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; })
+#define swap(a, b) \
+	do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
 
 /**
  * container_of - cast a member of a structure out to the containing structure
Acked-by
#3 lore raw
Quoted 17 lines · Peter Zijlstra
Quoted 7 lines · Andrew Morton
Quoted 3 lines · On Fri, 16 Jan 2009 10:26:34 +0100 Peter Zijlstra <[email protected]> wrote:

Hi,

Where did this patch come from? -- I can't seem to find it on lkml at all..

That's a damn good question. I received:

Hehe, one of the mysteries of life then :-)

Quoted 6 lines
Quoted 1 line

The reason I ask it that I wonder why swap() has a return value?

Accident, I guess.

Quoted 1 line

+#define swap(a, b) ({ typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; })

Quoted 7 lines
Quoted 1 line

You think it should use the do{}while(0) thing?

That was what I was thinking indeed. Non of the current users appear to make use of the (somewhat arbitrary) return value, so lets change it before someone gets creative ;-)

Good change, thank you Peter.

//Sorry I was not able to read this email because of VPN issues.

Fengguang

Quoted 16 lines

---

include/linux/kernel.h +2 -1
index 343df9e..7fa3718 100644
@@ -480,7 +480,8 @@ static inline char *pack_hex_byte(char *buf, u8 byte)
 /*
  * swap - swap value of @a and @b
  */
-#define swap(a, b) ({ typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; })
+#define swap(a, b) \
+	do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
 
 /**
  * container_of - cast a member of a structure out to the containing structure