Skip to content

Commit 481018f

Browse files
committed
Add macro to cast away volatile without allowing changes to underlying type
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
1 parent 572e3e6 commit 481018f

File tree

3 files changed

+9
-3
lines changed

3 files changed

+9
-3
lines changed

src/backend/postmaster/pgstat.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -3311,7 +3311,7 @@ pgstat_read_current_status(void)
33113311
localentry->backendStatus.st_procpid = beentry->st_procpid;
33123312
if (localentry->backendStatus.st_procpid > 0)
33133313
{
3314-
memcpy(&localentry->backendStatus, (char *) beentry, sizeof(PgBackendStatus));
3314+
memcpy(&localentry->backendStatus, unvolatize(PgBackendStatus *, beentry), sizeof(PgBackendStatus));
33153315

33163316
/*
33173317
* strcpy is safe even if the string is modified concurrently,

src/backend/storage/ipc/pmsignal.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ PMSignalShmemInit(void)
134134

135135
if (!found)
136136
{
137-
MemSet(PMSignalState, 0, PMSignalShmemSize());
137+
MemSet(unvolatize(PMSignalData *, PMSignalState), 0, PMSignalShmemSize());
138138
PMSignalState->num_child_flags = MaxLivePostmasterChildren();
139139
}
140140
}

src/include/c.h

+7-1
Original file line numberDiff line numberDiff line change
@@ -1122,7 +1122,7 @@ typedef union PGAlignedXLogBlock
11221122
#endif
11231123

11241124
/*
1125-
* Macro that allows to cast constness away from an expression, but doesn't
1125+
* Macro that allows to cast constness and volatile away from an expression, but doesn't
11261126
* allow changing the underlying type. Enforcement of the latter
11271127
* currently only works for gcc like compilers.
11281128
*
@@ -1141,9 +1141,15 @@ typedef union PGAlignedXLogBlock
11411141
(StaticAssertExpr(__builtin_types_compatible_p(__typeof(expr), const underlying_type), \
11421142
"wrong cast"), \
11431143
(underlying_type) (expr))
1144+
#define unvolatize(underlying_type, expr) \
1145+
(StaticAssertExpr(__builtin_types_compatible_p(__typeof(expr), volatile underlying_type), \
1146+
"wrong cast"), \
1147+
(underlying_type) (expr))
11441148
#else
11451149
#define unconstify(underlying_type, expr) \
11461150
((underlying_type) (expr))
1151+
#define unvolatize(underlying_type, expr) \
1152+
((underlying_type) (expr))
11471153
#endif
11481154

11491155
/* ----------------------------------------------------------------

0 commit comments

Comments
 (0)