Skip to content

Commit 3b4ac33

Browse files
committed
Avoid type cheats for invalid dsa_handles and dshash_table_handles.
Invent separate macros for "invalid" values of these types, so that we needn't embed knowledge of their representations into calling code. These are all zeroes anyway ATM, so this is not fixing any live bug, but it makes the code cleaner and more future-proof. I (tgl) also chose to move DSM_HANDLE_INVALID into dsm_impl.h, since it seems like it should live beside the typedef for dsm_handle. Hou Zhijie, Nathan Bossart, Kyotaro Horiguchi, Tom Lane Discussion: https://postgr.es/m/OS0PR01MB5716860B1454C34E5B179B6694C99@OS0PR01MB5716.jpnprd01.prod.outlook.com
1 parent d7c4830 commit 3b4ac33

File tree

7 files changed

+18
-12
lines changed

7 files changed

+18
-12
lines changed

src/backend/replication/logical/launcher.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -922,8 +922,8 @@ ApplyLauncherShmemInit(void)
922922

923923
memset(LogicalRepCtx, 0, ApplyLauncherShmemSize());
924924

925-
LogicalRepCtx->last_start_dsa = DSM_HANDLE_INVALID;
926-
LogicalRepCtx->last_start_dsh = DSM_HANDLE_INVALID;
925+
LogicalRepCtx->last_start_dsa = DSA_HANDLE_INVALID;
926+
LogicalRepCtx->last_start_dsh = DSHASH_HANDLE_INVALID;
927927

928928
/* Initialize memory and spin locks for each worker slot. */
929929
for (slot = 0; slot < max_logical_replication_workers; slot++)
@@ -947,7 +947,7 @@ logicalrep_launcher_attach_dshmem(void)
947947
MemoryContext oldcontext;
948948

949949
/* Quick exit if we already did this. */
950-
if (LogicalRepCtx->last_start_dsh != DSM_HANDLE_INVALID &&
950+
if (LogicalRepCtx->last_start_dsh != DSHASH_HANDLE_INVALID &&
951951
last_start_times != NULL)
952952
return;
953953

@@ -957,7 +957,7 @@ logicalrep_launcher_attach_dshmem(void)
957957
/* Be sure any local memory allocated by DSA routines is persistent. */
958958
oldcontext = MemoryContextSwitchTo(TopMemoryContext);
959959

960-
if (LogicalRepCtx->last_start_dsh == DSM_HANDLE_INVALID)
960+
if (LogicalRepCtx->last_start_dsh == DSHASH_HANDLE_INVALID)
961961
{
962962
/* Initialize dynamic shared hash table for last-start times. */
963963
last_start_times_dsa = dsa_create(LWTRANCHE_LAUNCHER_DSA);

src/backend/storage/ipc/dsm.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,8 @@ dsm_postmaster_startup(PGShmemHeader *shim)
173173

174174
/*
175175
* Loop until we find an unused identifier for the new control segment. We
176-
* sometimes use 0 as a sentinel value indicating that no control segment
177-
* is known to exist, so avoid using that value for a real control
178-
* segment.
176+
* sometimes use DSM_HANDLE_INVALID as a sentinel value indicating "no
177+
* control segment", so avoid generating that value for a real handle.
179178
*/
180179
for (;;)
181180
{

src/backend/utils/mmgr/dsa.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ dsa_create_in_place(void *place, size_t size,
505505
dsa_handle
506506
dsa_get_handle(dsa_area *area)
507507
{
508-
Assert(area->control->handle != DSM_HANDLE_INVALID);
508+
Assert(area->control->handle != DSA_HANDLE_INVALID);
509509
return area->control->handle;
510510
}
511511

@@ -554,7 +554,7 @@ dsa_attach_in_place(void *place, dsm_segment *segment)
554554
{
555555
dsa_area *area;
556556

557-
area = attach_internal(place, NULL, DSM_HANDLE_INVALID);
557+
area = attach_internal(place, NULL, DSA_HANDLE_INVALID);
558558

559559
/*
560560
* Clean up when the control segment detaches, if a containing DSM segment

src/include/lib/dshash.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ typedef struct dshash_table dshash_table;
2323
/* A handle for a dshash_table which can be shared with other processes. */
2424
typedef dsa_pointer dshash_table_handle;
2525

26+
/* Sentinel value to use for invalid dshash_table handles. */
27+
#define DSHASH_HANDLE_INVALID ((dshash_table_handle) InvalidDsaPointer)
28+
2629
/* The type for hash values. */
2730
typedef uint32 dshash_hash;
2831

src/include/storage/dsm.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@ typedef struct dsm_segment dsm_segment;
1919

2020
#define DSM_CREATE_NULL_IF_MAXSEGMENTS 0x0001
2121

22-
/* A sentinel value for an invalid DSM handle. */
23-
#define DSM_HANDLE_INVALID 0
24-
2522
/* Startup and shutdown functions. */
2623
struct PGShmemHeader; /* avoid including pg_shmem.h */
2724
extern void dsm_cleanup_using_control_segment(dsm_handle old_control_handle);

src/include/storage/dsm_impl.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ extern PGDLLIMPORT int min_dynamic_shared_memory;
5454
/* A "name" for a dynamic shared memory segment. */
5555
typedef uint32 dsm_handle;
5656

57+
/* Sentinel value to use for invalid DSM handles. */
58+
#define DSM_HANDLE_INVALID ((dsm_handle) 0)
59+
5760
/* All the shared-memory operations we know about. */
5861
typedef enum
5962
{

src/include/utils/dsa.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ typedef pg_atomic_uint64 dsa_pointer_atomic;
9999
*/
100100
typedef dsm_handle dsa_handle;
101101

102+
/* Sentinel value to use for invalid dsa_handles. */
103+
#define DSA_HANDLE_INVALID ((dsa_handle) DSM_HANDLE_INVALID)
104+
105+
102106
extern dsa_area *dsa_create(int tranche_id);
103107
extern dsa_area *dsa_create_in_place(void *place, size_t size,
104108
int tranche_id, dsm_segment *segment);

0 commit comments

Comments
 (0)