]> granicus.if.org Git - postgresql/commitdiff
Add macro to cast away volatile without allowing changes to underlying type
authorPeter Eisentraut <peter@eisentraut.org>
Mon, 25 Mar 2019 08:35:29 +0000 (09:35 +0100)
committerPeter Eisentraut <peter@eisentraut.org>
Mon, 25 Mar 2019 08:37:03 +0000 (09:37 +0100)
This adds unvolatize(), which works just like unconstify() but for volatile.

Discussion: https://www.postgresql.org/message-id/flat/7a5cbea7-b8df-e910-0f10-04014bcad701%402ndquadrant.com

src/backend/postmaster/pgstat.c
src/backend/storage/ipc/pmsignal.c
src/include/c.h

index 2fbfadd9f0c755cb623e04010dcf2145c6048457..2a8472b91aee2453a8c34ac916ed93743dcbf62a 100644 (file)
@@ -3311,7 +3311,7 @@ pgstat_read_current_status(void)
                        localentry->backendStatus.st_procpid = beentry->st_procpid;
                        if (localentry->backendStatus.st_procpid > 0)
                        {
-                               memcpy(&localentry->backendStatus, (char *) beentry, sizeof(PgBackendStatus));
+                               memcpy(&localentry->backendStatus, unvolatize(PgBackendStatus *, beentry), sizeof(PgBackendStatus));
 
                                /*
                                 * strcpy is safe even if the string is modified concurrently,
index d707993bf6313ce862ef048e852fe3dbf2595f8e..48f4311464278fe0cec1ee13d6c912a0a28e2490 100644 (file)
@@ -134,7 +134,7 @@ PMSignalShmemInit(void)
 
        if (!found)
        {
-               MemSet(PMSignalState, 0, PMSignalShmemSize());
+               MemSet(unvolatize(PMSignalData *, PMSignalState), 0, PMSignalShmemSize());
                PMSignalState->num_child_flags = MaxLivePostmasterChildren();
        }
 }
index 658be50e0d3ae840022839dfa366a845c59c2f9d..33c95181952735e6b78c94dca67df913ebe9c470 100644 (file)
@@ -1122,7 +1122,7 @@ typedef union PGAlignedXLogBlock
 #endif
 
 /*
- * Macro that allows to cast constness away from an expression, but doesn't
+ * Macro that allows to cast constness and volatile away from an expression, but doesn't
  * allow changing the underlying type.  Enforcement of the latter
  * currently only works for gcc like compilers.
  *
@@ -1141,9 +1141,15 @@ typedef union PGAlignedXLogBlock
        (StaticAssertExpr(__builtin_types_compatible_p(__typeof(expr), const underlying_type), \
                                          "wrong cast"), \
         (underlying_type) (expr))
+#define unvolatize(underlying_type, expr) \
+       (StaticAssertExpr(__builtin_types_compatible_p(__typeof(expr), volatile underlying_type), \
+                                         "wrong cast"), \
+        (underlying_type) (expr))
 #else
 #define unconstify(underlying_type, expr) \
        ((underlying_type) (expr))
+#define unvolatize(underlying_type, expr) \
+       ((underlying_type) (expr))
 #endif
 
 /* ----------------------------------------------------------------