Add definition pg_attribute_aligned() for MSVC
authorMichael Paquier
Wed, 21 Sep 2022 01:11:23 +0000 (10:11 +0900)
committerMichael Paquier
Wed, 21 Sep 2022 01:11:23 +0000 (10:11 +0900)
Visual Studio 2015+ has support for a macro to control the alignement of
structures as of __declspec(align(#)), and this commit adds a definition
of pg_attribute_aligned() based on that.  It happens that this was
already used in the implementation of atomics for MSVC.  Note that there
is still no definition fo pg_attribute_packed(), so this does not impact
itemptr.h.

Author: James Coleman
Discussion: https://postgr.es/m/CAAaqYe-HbtZvR3msoMtk+hYW2S0e0OapzMW8icSMYTMA+mN8Aw@mail.gmail.com

config/c-compiler.m4
configure
src/include/c.h
src/include/port/atomics/generic-msvc.h

index 69efc5bb10a4e74870846e15a3d1ce42e211802c..000b075312e98212ce76bea4cd1a96bc95a1305c 100644 (file)
@@ -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)
index b04d9c8e800a1da9f67cf7cd176509a7453883ed..4efed743a1adccd96c34338724d87eac49c4792c 100755 (executable)
--- a/configure
+++ b/configure
@@ -17462,6 +17462,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)
index 101ba41331e8cd17cfa85cdd6c8d9dc6e464c235..c8f72e44d894f3d83d2dd53fc19ce10a3847807d 100644 (file)
 #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 it is
+ * declared before the definition while 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))
+#define pg_attribute_noreturn()
 #else
 /*
  * NB: aligned and packed are not given default definitions because they
index 1a4adfde686c650131aed9daa28ba9d09708fb0e..f3091b973188ca9883e7542368e05b88e48a0825 100644 (file)
@@ -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;