From 7b29e4818bb3b780664fde2e8f5726b4f0b54b7b Mon Sep 17 00:00:00 2001
From: Heikki Linnakangas <heikki.linnakangas@iki.fi>
Date: Wed, 21 Feb 2024 17:15:22 +0200
Subject: [PATCH v1 1/2] Change datatype of the opaque types in dsm_impl.c

It's easy to make mistakes with "void *", as you can pass any pointer
without getting warnings from the compiler. Switch to a typedef over
uintptr_t to get a bit more type checking.
---
 src/backend/storage/ipc/dsm.c      | 28 +++++++++++------------
 src/backend/storage/ipc/dsm_impl.c | 36 +++++++++++++++---------------
 src/include/storage/dsm.h          |  2 +-
 src/include/storage/dsm_impl.h     | 21 +++++++++++++----
 4 files changed, 50 insertions(+), 37 deletions(-)

diff --git a/src/backend/storage/ipc/dsm.c b/src/backend/storage/ipc/dsm.c
index 6b12108dd10..d3f982c5b2a 100644
--- a/src/backend/storage/ipc/dsm.c
+++ b/src/backend/storage/ipc/dsm.c
@@ -70,7 +70,7 @@ struct dsm_segment
 	ResourceOwner resowner;		/* Resource owner. */
 	dsm_handle	handle;			/* Segment name. */
 	uint32		control_slot;	/* Slot in control segment. */
-	void	   *impl_private;	/* Implementation-specific private data. */
+	dsm_impl_private impl_private;	/* Implementation-specific private data. */
 	void	   *mapped_address; /* Mapping address, or NULL if unmapped. */
 	Size		mapped_size;	/* Size of our mapping. */
 	slist_head	on_detach;		/* On-detach callbacks. */
@@ -83,7 +83,7 @@ typedef struct dsm_control_item
 	uint32		refcnt;			/* 2+ = active, 1 = moribund, 0 = gone */
 	size_t		first_page;
 	size_t		npages;
-	void	   *impl_private_pm_handle; /* only needed on Windows */
+	dsm_impl_private_pm_handle impl_private_pm_handle; /* only needed on Windows */
 	bool		pinned;
 } dsm_control_item;
 
@@ -140,7 +140,7 @@ static dlist_head dsm_segment_list = DLIST_STATIC_INIT(dsm_segment_list);
 static dsm_handle dsm_control_handle;
 static dsm_control_header *dsm_control;
 static Size dsm_control_mapped_size = 0;
-static void *dsm_control_impl_private = NULL;
+static dsm_impl_private dsm_control_impl_private = 0;
 
 
 /* ResourceOwner callbacks to hold DSM segments */
@@ -240,8 +240,8 @@ dsm_cleanup_using_control_segment(dsm_handle old_control_handle)
 {
 	void	   *mapped_address = NULL;
 	void	   *junk_mapped_address = NULL;
-	void	   *impl_private = NULL;
-	void	   *junk_impl_private = NULL;
+	dsm_impl_private impl_private = 0;
+	dsm_impl_private junk_impl_private = 0;
 	Size		mapped_size = 0;
 	Size		junk_mapped_size = 0;
 	uint32		nitems;
@@ -362,7 +362,7 @@ dsm_postmaster_shutdown(int code, Datum arg)
 	uint32		i;
 	void	   *dsm_control_address;
 	void	   *junk_mapped_address = NULL;
-	void	   *junk_impl_private = NULL;
+	dsm_impl_private junk_impl_private = 0;
 	Size		junk_mapped_size = 0;
 	PGShmemHeader *shim = (PGShmemHeader *) DatumGetPointer(arg);
 
@@ -598,7 +598,7 @@ dsm_create(Size size, int flags)
 			dsm_control->item[i].handle = seg->handle;
 			/* refcnt of 1 triggers destruction, so start at 2 */
 			dsm_control->item[i].refcnt = 2;
-			dsm_control->item[i].impl_private_pm_handle = NULL;
+			dsm_control->item[i].impl_private_pm_handle = 0;
 			dsm_control->item[i].pinned = false;
 			seg->control_slot = i;
 			LWLockRelease(DynamicSharedMemoryControlLock);
@@ -637,7 +637,7 @@ dsm_create(Size size, int flags)
 	dsm_control->item[nitems].handle = seg->handle;
 	/* refcnt of 1 triggers destruction, so start at 2 */
 	dsm_control->item[nitems].refcnt = 2;
-	dsm_control->item[nitems].impl_private_pm_handle = NULL;
+	dsm_control->item[nitems].impl_private_pm_handle = 0;
 	dsm_control->item[nitems].pinned = false;
 	seg->control_slot = nitems;
 	dsm_control->nitems++;
@@ -842,7 +842,7 @@ dsm_detach(dsm_segment *seg)
 		if (!is_main_region_dsm_handle(seg->handle))
 			dsm_impl_op(DSM_OP_DETACH, seg->handle, 0, &seg->impl_private,
 						&seg->mapped_address, &seg->mapped_size, WARNING);
-		seg->impl_private = NULL;
+		seg->impl_private = 0;
 		seg->mapped_address = NULL;
 		seg->mapped_size = 0;
 	}
@@ -955,7 +955,7 @@ dsm_unpin_mapping(dsm_segment *seg)
 void
 dsm_pin_segment(dsm_segment *seg)
 {
-	void	   *handle = NULL;
+	dsm_impl_private_pm_handle pm_handle = 0;
 
 	/*
 	 * Bump reference count for this segment in shared memory. This will
@@ -967,10 +967,10 @@ dsm_pin_segment(dsm_segment *seg)
 	if (dsm_control->item[seg->control_slot].pinned)
 		elog(ERROR, "cannot pin a segment that is already pinned");
 	if (!is_main_region_dsm_handle(seg->handle))
-		dsm_impl_pin_segment(seg->handle, seg->impl_private, &handle);
+		dsm_impl_pin_segment(seg->handle, seg->impl_private, &pm_handle);
 	dsm_control->item[seg->control_slot].pinned = true;
 	dsm_control->item[seg->control_slot].refcnt++;
-	dsm_control->item[seg->control_slot].impl_private_pm_handle = handle;
+	dsm_control->item[seg->control_slot].impl_private_pm_handle = pm_handle;
 	LWLockRelease(DynamicSharedMemoryControlLock);
 }
 
@@ -1039,7 +1039,7 @@ dsm_unpin_segment(dsm_handle handle)
 	/* Clean up resources if that was the last reference. */
 	if (destroy)
 	{
-		void	   *junk_impl_private = NULL;
+		dsm_impl_private junk_impl_private = 0;
 		void	   *junk_mapped_address = NULL;
 		Size		junk_mapped_size = 0;
 
@@ -1211,7 +1211,7 @@ dsm_create_descriptor(void)
 
 	/* seg->handle must be initialized by the caller */
 	seg->control_slot = INVALID_CONTROL_SLOT;
-	seg->impl_private = NULL;
+	seg->impl_private = 0;
 	seg->mapped_address = NULL;
 	seg->mapped_size = 0;
 
diff --git a/src/backend/storage/ipc/dsm_impl.c b/src/backend/storage/ipc/dsm_impl.c
index 8dd669e0ce9..4478c58bb72 100644
--- a/src/backend/storage/ipc/dsm_impl.c
+++ b/src/backend/storage/ipc/dsm_impl.c
@@ -71,23 +71,23 @@
 
 #ifdef USE_DSM_POSIX
 static bool dsm_impl_posix(dsm_op op, dsm_handle handle, Size request_size,
-						   void **impl_private, void **mapped_address,
+						   dsm_impl_private *impl_private, void **mapped_address,
 						   Size *mapped_size, int elevel);
 static int	dsm_impl_posix_resize(int fd, off_t size);
 #endif
 #ifdef USE_DSM_SYSV
 static bool dsm_impl_sysv(dsm_op op, dsm_handle handle, Size request_size,
-						  void **impl_private, void **mapped_address,
+						  dsm_impl_private *impl_private, void **mapped_address,
 						  Size *mapped_size, int elevel);
 #endif
 #ifdef USE_DSM_WINDOWS
 static bool dsm_impl_windows(dsm_op op, dsm_handle handle, Size request_size,
-							 void **impl_private, void **mapped_address,
+							 dsm_impl_private *impl_private, void **mapped_address,
 							 Size *mapped_size, int elevel);
 #endif
 #ifdef USE_DSM_MMAP
 static bool dsm_impl_mmap(dsm_op op, dsm_handle handle, Size request_size,
-						  void **impl_private, void **mapped_address,
+						  dsm_impl_private *impl_private, void **mapped_address,
 						  Size *mapped_size, int elevel);
 #endif
 static int	errcode_for_dynamic_shared_memory(void);
@@ -157,7 +157,7 @@ int			min_dynamic_shared_memory;
  */
 bool
 dsm_impl_op(dsm_op op, dsm_handle handle, Size request_size,
-			void **impl_private, void **mapped_address, Size *mapped_size,
+			dsm_impl_private *impl_private, void **mapped_address, Size *mapped_size,
 			int elevel)
 {
 	Assert(op == DSM_OP_CREATE || request_size == 0);
@@ -210,7 +210,7 @@ dsm_impl_op(dsm_op op, dsm_handle handle, Size request_size,
  */
 static bool
 dsm_impl_posix(dsm_op op, dsm_handle handle, Size request_size,
-			   void **impl_private, void **mapped_address, Size *mapped_size,
+			   dsm_impl_private *impl_private, void **mapped_address, Size *mapped_size,
 			   int elevel)
 {
 	char		name[64];
@@ -421,7 +421,7 @@ dsm_impl_posix_resize(int fd, off_t size)
  */
 static bool
 dsm_impl_sysv(dsm_op op, dsm_handle handle, Size request_size,
-			  void **impl_private, void **mapped_address, Size *mapped_size,
+			  dsm_impl_private *impl_private, void **mapped_address, Size *mapped_size,
 			  int elevel)
 {
 	key_t		key;
@@ -477,9 +477,9 @@ dsm_impl_sysv(dsm_op op, dsm_handle handle, Size request_size,
 	 * the shared memory key to a shared memory identifier using shmget(). To
 	 * avoid repeated lookups, we store the key using impl_private.
 	 */
-	if (*impl_private != NULL)
+	if (*impl_private != 0)
 	{
-		ident_cache = *impl_private;
+		ident_cache = (int *) *impl_private;
 		ident = *ident_cache;
 	}
 	else
@@ -522,14 +522,14 @@ dsm_impl_sysv(dsm_op op, dsm_handle handle, Size request_size,
 		}
 
 		*ident_cache = ident;
-		*impl_private = ident_cache;
+		*impl_private = (uintptr_t) ident_cache;
 	}
 
 	/* Handle teardown cases. */
 	if (op == DSM_OP_DETACH || op == DSM_OP_DESTROY)
 	{
 		pfree(ident_cache);
-		*impl_private = NULL;
+		*impl_private = 0;
 		if (*mapped_address != NULL && shmdt(*mapped_address) != 0)
 		{
 			ereport(elevel,
@@ -608,7 +608,7 @@ dsm_impl_sysv(dsm_op op, dsm_handle handle, Size request_size,
  */
 static bool
 dsm_impl_windows(dsm_op op, dsm_handle handle, Size request_size,
-				 void **impl_private, void **mapped_address,
+				 dsm_impl_private *impl_private, void **mapped_address,
 				 Size *mapped_size, int elevel)
 {
 	char	   *address;
@@ -790,7 +790,7 @@ dsm_impl_windows(dsm_op op, dsm_handle handle, Size request_size,
  */
 static bool
 dsm_impl_mmap(dsm_op op, dsm_handle handle, Size request_size,
-			  void **impl_private, void **mapped_address, Size *mapped_size,
+			  dsm_impl_private *impl_private, void **mapped_address, Size *mapped_size,
 			  int elevel)
 {
 	char		name[64];
@@ -960,8 +960,8 @@ dsm_impl_mmap(dsm_op op, dsm_handle handle, Size request_size,
  * do anything to receive the handle; Windows transfers it automatically.
  */
 void
-dsm_impl_pin_segment(dsm_handle handle, void *impl_private,
-					 void **impl_private_pm_handle)
+dsm_impl_pin_segment(dsm_handle handle, dsm_impl_private impl_private,
+					 dsm_impl_private_pm_handle *pm_handle)
 {
 	switch (dynamic_shared_memory_type)
 	{
@@ -992,7 +992,7 @@ dsm_impl_pin_segment(dsm_handle handle, void *impl_private,
 				 * matter.  We're just holding onto it so that, if the segment
 				 * is unpinned, dsm_impl_unpin_segment can close it.
 				 */
-				*impl_private_pm_handle = hmap;
+				*pm_handle = hmap;
 			}
 			break;
 #endif
@@ -1011,7 +1011,7 @@ dsm_impl_pin_segment(dsm_handle handle, void *impl_private,
  * postmaster's process space.
  */
 void
-dsm_impl_unpin_segment(dsm_handle handle, void **impl_private)
+dsm_impl_unpin_segment(dsm_handle handle, dsm_impl_private_pm_handle *pm_handle)
 {
 	switch (dynamic_shared_memory_type)
 	{
@@ -1034,7 +1034,7 @@ dsm_impl_unpin_segment(dsm_handle handle, void **impl_private)
 									name)));
 				}
 
-				*impl_private = NULL;
+				*pm_handle = 0;
 			}
 			break;
 #endif
diff --git a/src/include/storage/dsm.h b/src/include/storage/dsm.h
index 1a22b32df1a..35ae4eb164e 100644
--- a/src/include/storage/dsm.h
+++ b/src/include/storage/dsm.h
@@ -13,7 +13,7 @@
 #ifndef DSM_H
 #define DSM_H
 
-#include "storage/dsm_impl.h"
+#include "dsm_impl.h"
 
 typedef struct dsm_segment dsm_segment;
 
diff --git a/src/include/storage/dsm_impl.h b/src/include/storage/dsm_impl.h
index 882269603da..f2bfb2a1a5c 100644
--- a/src/include/storage/dsm_impl.h
+++ b/src/include/storage/dsm_impl.h
@@ -66,14 +66,27 @@ typedef enum
 	DSM_OP_DESTROY,
 } dsm_op;
 
+/*
+ * When a segment is created or attached, the caller provides this space to
+ * hold implementation-specific information about the attachment. It is opaque
+ * to the caller, and is passed back to the implementation when detaching.
+ */
+typedef uintptr_t dsm_impl_private;
+
+/*
+ * Similar caller-provided space for implementation-specific information held
+ * when a segment is pinned.
+ */
+typedef uintptr_t dsm_impl_private_pm_handle;
+
 /* Create, attach to, detach from, resize, or destroy a segment. */
 extern bool dsm_impl_op(dsm_op op, dsm_handle handle, Size request_size,
-						void **impl_private, void **mapped_address, Size *mapped_size,
+						dsm_impl_private *impl_private, void **mapped_address, Size *mapped_size,
 						int elevel);
 
 /* Implementation-dependent actions required to keep segment until shutdown. */
-extern void dsm_impl_pin_segment(dsm_handle handle, void *impl_private,
-								 void **impl_private_pm_handle);
-extern void dsm_impl_unpin_segment(dsm_handle handle, void **impl_private);
+extern void dsm_impl_pin_segment(dsm_handle handle, dsm_impl_private impl_private,
+								 dsm_impl_private_pm_handle *impl_private_pm_handle);
+extern void dsm_impl_unpin_segment(dsm_handle handle, dsm_impl_private_pm_handle *pm_handle);
 
 #endif							/* DSM_IMPL_H */
-- 
2.39.2

