[PATCH xserver 3/3] Remove support for unaligned swaps.

Eric Anholt eric at anholt.net
Fri Apr 21 19:12:37 UTC 2017


The previous misc.h code went out of its way to allow swapping of
unaligned pointers to values.  However, the members of an X
request/response are always naturally aligned within the struct, and
the buffers containing a request/response will also be aligned to at
least 8 bytes, so we can just drop it.

        text      data   bss    dec      hex    filename
before: 2215167	  51552	 132016	2398735	 249a0f	hw/xfree86/Xorg
after:  2214919	  51552	 132016	2398487	 249917	hw/xfree86/Xorg

Signed-off-by: Eric Anholt <eric at anholt.net>
---
 include/misc.h | 15 +++------------
 test/misc.c    | 25 ++++++-------------------
 2 files changed, 9 insertions(+), 31 deletions(-)

diff --git a/include/misc.h b/include/misc.h
index a75eb617c642..38af70ff9e89 100644
--- a/include/misc.h
+++ b/include/misc.h
@@ -310,12 +310,9 @@ bswap_64(uint64_t x)
 }
 
 #define swapll(x) do { \
-		uint64_t temp; \
 		if (sizeof(*(x)) != 8) \
 			wrong_size(); \
-		memcpy(&temp, x, 8); \
-		temp = bswap_64(temp); \
-		memcpy(x, &temp, 8); \
+		*(x) = bswap_64(*(x));          \
 	} while (0)
 
 static inline uint32_t
@@ -328,12 +325,9 @@ bswap_32(uint32_t x)
 }
 
 #define swapl(x) do { \
-		uint32_t temp; \
 		if (sizeof(*(x)) != 4) \
 			wrong_size(); \
-		memcpy(&temp, x, 4); \
-		temp = bswap_32(temp); \
-		memcpy(x, &temp, 4); \
+		*(x) = bswap_32(*(x)); \
 	} while (0)
 
 static inline uint16_t
@@ -344,12 +338,9 @@ bswap_16(uint16_t x)
 }
 
 #define swaps(x) do { \
-		uint16_t temp; \
 		if (sizeof(*(x)) != 2) \
 			wrong_size(); \
-		memcpy(&temp, x, 2); \
-		temp = bswap_16(temp); \
-		memcpy(x, &temp, 2); \
+		*(x) = bswap_16(*(x)); \
 	} while (0)
 
 /* copy 32-bit value from src to dst byteswapping on the way */
diff --git a/test/misc.c b/test/misc.c
index c10a2b935bc4..3c669b6776ff 100644
--- a/test/misc.c
+++ b/test/misc.c
@@ -204,34 +204,21 @@ bswap_test(void)
     uint16_t result_16;
     uint32_t result_32;
     uint64_t result_64;
-    unsigned buffer[sizeof(test_64) + 4];
-    void *unaligned = &buffer[1];
 
     assert(bswap_16(test_16) == expect_16);
     assert(bswap_32(test_32) == expect_32);
     assert(bswap_64(test_64) == expect_64);
 
-    /* Test the swapping-in-a-pointer functions, with unaligned
-     * addresses (the functions shouldn't cause traps in that case).
-     */
-    for (int i = 0; i < 2; i++) {
-        unaligned = buffer + i;
-        if (((uintptr_t)unaligned & 1) == 1)
-            break;
-    }
-    memcpy(unaligned, &test_16, sizeof(test_16));
-    swaps((uint16_t *)unaligned);
-    memcpy(&result_16, unaligned, sizeof(result_16));
+    result_16 = test_16;
+    swaps(&result_16);
     assert(result_16 == expect_16);
 
-    memcpy(unaligned, &test_32, sizeof(test_32));
-    swapl((uint32_t *)unaligned);
-    memcpy(&result_32, unaligned, sizeof(result_32));
+    result_32 = test_32;
+    swapl(&result_32);
     assert(result_32 == expect_32);
 
-    memcpy(unaligned, &test_64, sizeof(test_64));
-    swapll((uint64_t *)unaligned);
-    memcpy(&result_64, unaligned, sizeof(result_64));
+    result_64 = test_64;
+    swapll(&result_64);
     assert(result_64 == expect_64);
 }
 
-- 
2.11.0



More information about the xorg-devel mailing list