From 327597738ae4315a0af858c14b7d0e8d1764ea9a Mon Sep 17 00:00:00 2001
From: jcoleman <jtc331@gmail.com>
Date: Sat, 29 Jan 2022 11:28:45 -0500
Subject: [PATCH v3] Support pg_attribute_aligned in MSVC

---
 config/c-compiler.m4                    |  2 ++
 configure                               |  2 ++
 src/include/c.h                         | 12 ++++++++++++
 src/include/port/atomics/generic-msvc.h |  2 +-
 4 files changed, 17 insertions(+), 1 deletion(-)

diff --git a/config/c-compiler.m4 b/config/c-compiler.m4
index 69efc5bb10..000b075312 100644
--- a/config/c-compiler.m4
+++ b/config/c-compiler.m4
@@ -139,6 +139,8 @@ if test x"$pgac_cv__128bit_int" = xyes ; then
 /* This must match the corresponding code in c.h: */
 #if defined(__GNUC__) || defined(__SUNPRO_C) || defined(__IBMC__)
 #define pg_attribute_aligned(a) __attribute__((aligned(a)))
+#elif defined(_MSC_VER)
+#define pg_attribute_aligned(a) __declspec(align(a))
 #endif
 typedef __int128 int128a
 #if defined(pg_attribute_aligned)
diff --git a/configure b/configure
index f325bd85b8..5488574c02 100755
--- a/configure
+++ b/configure
@@ -17643,6 +17643,8 @@ else
 /* This must match the corresponding code in c.h: */
 #if defined(__GNUC__) || defined(__SUNPRO_C) || defined(__IBMC__)
 #define pg_attribute_aligned(a) __attribute__((aligned(a)))
+#elif defined(_MSC_VER)
+#define pg_attribute_aligned(a) __declspec(align(a))
 #endif
 typedef __int128 int128a
 #if defined(pg_attribute_aligned)
diff --git a/src/include/c.h b/src/include/c.h
index 101ba41331..9f3122b4ab 100644
--- a/src/include/c.h
+++ b/src/include/c.h
@@ -181,6 +181,18 @@
 #define pg_attribute_noreturn() __attribute__((noreturn))
 #define pg_attribute_packed() __attribute__((packed))
 #define HAVE_PG_ATTRIBUTE_NORETURN 1
+#elif defined(_MSC_VER)
+/*
+ * MSVC supports aligned
+ *
+ * noreturn is also possible but in MSVC is declared before the definition
+ * while our pg_attribute_noreturn() macro is currently used after
+ * the definition.
+ *
+ * Packing is also possible but only by wrapping the entire struct definition
+ * which doesn't fit into our current macro declarations.
+ */
+#define pg_attribute_aligned(a) __declspec(align(a))
 #else
 /*
  * NB: aligned and packed are not given default definitions because they
diff --git a/src/include/port/atomics/generic-msvc.h b/src/include/port/atomics/generic-msvc.h
index 1a4adfde68..f3091b9731 100644
--- a/src/include/port/atomics/generic-msvc.h
+++ b/src/include/port/atomics/generic-msvc.h
@@ -39,7 +39,7 @@ typedef struct pg_atomic_uint32
 } pg_atomic_uint32;
 
 #define PG_HAVE_ATOMIC_U64_SUPPORT
-typedef struct __declspec(align(8)) pg_atomic_uint64
+typedef struct pg_attribute_aligned(8) pg_atomic_uint64
 {
 	volatile uint64 value;
 } pg_atomic_uint64;
-- 
2.32.1 (Apple Git-133)

