On 2019-02-18 16:42, Andres Freund wrote:
> On February 18, 2019 7:39:25 AM PST, Peter Eisentraut 
> <peter.eisentr...@2ndquadrant.com> wrote:
>> I propose to add an equivalent to unconstify() for volatile.  When
>> working on this, I picked the name unvolatize() mostly as a joke, but
>> it
>> appears it's a real word.  Other ideas?
> 
> Shouldn't we just remove just about all those use of volatile? Basically the 
> only valid use these days is on sigsetjmp call sites.

So, after some recent cleanups and another one attached here, we're now
down to 1.5 uses of this.  (0.5 because the hunk in pmsignal.c doesn't
have a cast right now, but it would need one if s/MemSet/memset/.)
Those seem legitimate.

-- 
Peter Eisentraut              http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services
From 89aada5bdc9ef5c0b0125a72eb9d5494bf360282 Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <pe...@eisentraut.org>
Date: Fri, 15 Mar 2019 08:46:06 +0100
Subject: [PATCH v2 01/10] Initialize structure at declaration

Avoids extra memset call and cast.
---
 contrib/dblink/dblink.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/contrib/dblink/dblink.c b/contrib/dblink/dblink.c
index d95e6bfa71..d35e5ba3d8 100644
--- a/contrib/dblink/dblink.c
+++ b/contrib/dblink/dblink.c
@@ -982,13 +982,11 @@ materializeQueryResult(FunctionCallInfo fcinfo,
 {
        ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
        PGresult   *volatile res = NULL;
-       volatile storeInfo sinfo;
+       volatile storeInfo sinfo = {0};
 
        /* prepTuplestoreResult must have been called previously */
        Assert(rsinfo->returnMode == SFRM_Materialize);
 
-       /* initialize storeInfo to empty */
-       memset((void *) &sinfo, 0, sizeof(sinfo));
        sinfo.fcinfo = fcinfo;
 
        PG_TRY();

base-commit: 53680c116ce8c501e4081332d32ba0e93aa1aaa2
-- 
2.21.0

From 81a4e16fd06e8571d08b1d564e0c6ba1cb647476 Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <pe...@eisentraut.org>
Date: Mon, 18 Feb 2019 16:08:41 +0100
Subject: [PATCH v2 02/10] Add macro to cast away volatile without allowing
 changes to underlying type

This adds unvolatize(), which works just like unconstify() but for volatile.
---
 src/backend/postmaster/pgstat.c    | 2 +-
 src/backend/storage/ipc/pmsignal.c | 2 +-
 src/include/c.h                    | 8 +++++++-
 3 files changed, 9 insertions(+), 3 deletions(-)

diff --git a/src/backend/postmaster/pgstat.c b/src/backend/postmaster/pgstat.c
index 2fbfadd9f0..2a8472b91a 100644
--- a/src/backend/postmaster/pgstat.c
+++ b/src/backend/postmaster/pgstat.c
@@ -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,
diff --git a/src/backend/storage/ipc/pmsignal.c 
b/src/backend/storage/ipc/pmsignal.c
index d707993bf6..48f4311464 100644
--- a/src/backend/storage/ipc/pmsignal.c
+++ b/src/backend/storage/ipc/pmsignal.c
@@ -134,7 +134,7 @@ PMSignalShmemInit(void)
 
        if (!found)
        {
-               MemSet(PMSignalState, 0, PMSignalShmemSize());
+               MemSet(unvolatize(PMSignalData *, PMSignalState), 0, 
PMSignalShmemSize());
                PMSignalState->num_child_flags = MaxLivePostmasterChildren();
        }
 }
diff --git a/src/include/c.h b/src/include/c.h
index 658be50e0d..33c9518195 100644
--- a/src/include/c.h
+++ b/src/include/c.h
@@ -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
 
 /* ----------------------------------------------------------------
-- 
2.21.0

Reply via email to