git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 1/5] add SWAP macro

From
René Scharfe <l.s.r@web.de>
Date
Jan 28, 2017, 21:38 UTC
Message-ID
<0bdb58a6-3a7f-2218-4b70-c591ae90e95e@web.de>
In-Reply-To
<8ef4c833-45bd-6831-0683-6d01f30aa518@web.de>

Add a macro for exchanging the values of variables. It allows users to avoid repetition and takes care of the temporary variable for them. It also makes sure that the storage sizes of its two parameters are the same. Its memcpy(1) calls are optimized away by current compilers.

Also add a conservative semantic patch for transforming only swaps of variables of the same type.

Signed-off-by: Rene Scharfe <l.s.r@web.de>
---
 contrib/coccinelle/swap.cocci | 28 ++++++++++++++++++++++++++++
 git-compat-util.h             | 10 ++++++++++
 2 files changed, 38 insertions(+)
 create mode 100644 contrib/coccinelle/swap.cocci
diff --git a/contrib/coccinelle/swap.cocci b/contrib/coccinelle/swap.cocci
new file mode 100644
index 0000000000..a0934d1fda
--- /dev/null
+++ b/contrib/coccinelle/swap.cocci
@@ -0,0 +1,28 @@
+@ swap_with_declaration @
+type T;
+identifier tmp;
+T a, b;
+@@
+- T tmp = a;
++ T tmp;
++ tmp = a;
+  a = b;
+  b = tmp;
+
+@ swap @
+type T;
+T tmp, a, b;
+@@
+- tmp = a;
+- a = b;
+- b = tmp;
++ SWAP(a, b);
+
+@ extends swap @
+identifier unused;
+@@
+  {
+  ...
+- T unused;
+  ... when != unused
+  }
diff --git a/git-compat-util.h b/git-compat-util.h
index 87237b092b..66cd466eea 100644
--- a/git-compat-util.h
+++ b/git-compat-util.h
@@ -527,6 +527,16 @@ static inline int ends_with(const char *str, const char *suffix)
 	return strip_suffix(str, suffix, &len);
 }
 
+#define SWAP(a, b) do {						\
+	void *_swap_a_ptr = &(a);				\
+	void *_swap_b_ptr = &(b);				\
+	unsigned char _swap_buffer[sizeof(a)];			\
+	memcpy(_swap_buffer, _swap_a_ptr, sizeof(a));		\
+	memcpy(_swap_a_ptr, _swap_b_ptr, sizeof(a) +		\
+	       BUILD_ASSERT_OR_ZERO(sizeof(a) == sizeof(b)));	\
+	memcpy(_swap_b_ptr, _swap_buffer, sizeof(a));		\
+} while (0)
+
 #if defined(NO_MMAP) || defined(USE_WIN32_MMAP)
 
 #ifndef PROT_READ
-- 
2.11.0
Previous: René ScharfeNext: Johannes Schindelin
Message 2 of 50 in “introduce SWAP macro”
  1. 0/5 introduce SWAP macroRené Scharfe, Jan 28, 2017
  2. 1/5 add SWAP macroRené Scharfe, Jan 28, 2017
  3. Johannes SchindelinJan 30, 2017
  4. René ScharfeJan 30, 2017
  5. Johannes SchindelinJan 30, 2017
  6. René ScharfeJan 30, 2017
  7. Johannes SchindelinJan 31, 2017
  8. René ScharfeJan 31, 2017
  9. Ramsay JonesFeb 1, 2017
  10. Johannes SchindelinFeb 1, 2017
  11. Johannes SchindelinJan 30, 2017
  12. René ScharfeJan 30, 2017
  13. Johannes SixtJan 30, 2017
  14. Johannes SchindelinJan 30, 2017
  15. René ScharfeJan 30, 2017
  16. Brandon WilliamsJan 30, 2017
  17. René ScharfeJan 31, 2017
  18. Jeff KingJan 31, 2017
  19. Junio C HamanoJan 31, 2017
  20. Jeff KingJan 31, 2017
  21. Johannes SchindelinFeb 1, 2017
  22. Jeff KingFeb 1, 2017
  23. René ScharfeFeb 1, 2017
  24. Junio C HamanoFeb 1, 2017
  25. René ScharfeFeb 7, 2017
  26. Junio C HamanoFeb 7, 2017
  27. Johannes SchindelinFeb 8, 2017
  28. Johannes SchindelinJan 31, 2017
  29. Jeff KingApr 24, 2017
  30. Jeff KingApr 24, 2017
  31. Duy NguyenApr 24, 2017
  32. René ScharfeApr 28, 2017
  33. Jeff KingApr 28, 2017
  34. René ScharfeApr 29, 2017
  35. Jeff KingApr 30, 2017
  36. René ScharfeMay 2, 2017
  37. 2/5 apply: use SWAP macroRené Scharfe, Jan 28, 2017
  38. 3/5 use SWAP macroRené Scharfe, Jan 28, 2017
  39. Johannes SchindelinJan 30, 2017
  40. René ScharfeJan 30, 2017
  41. Junio C HamanoJan 30, 2017
  42. René ScharfeJan 31, 2017
  43. 4/5 diff: use SWAP macroRené Scharfe, Jan 28, 2017
  44. Johannes SchindelinJan 30, 2017
  45. René ScharfeJan 30, 2017
  46. Junio C HamanoJan 30, 2017
  47. 5/5 graph: use SWAP macroRené Scharfe, Jan 28, 2017
  48. Johannes SchindelinJan 30, 2017
  49. René ScharfeJan 30, 2017
  50. Junio C HamanoJan 30, 2017

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.