From 9600014545bef54334bc487fea5ebeac8e3815bf Mon Sep 17 00:00:00 2001
From: Ashutosh Bapat <ashutosh.bapat.oss@gmail.com>
Date: Fri, 30 Jan 2026 14:30:02 +0530
Subject: [PATCH v20260130 2/2] Remove ShmemBase and ShmemEnd

We have a new structure ShmemAllocatorData that controls the new alllocations.
Ideally ShmemBase and ShmemEnd should be part of that structure since they are
used for allocation purposes. But adding them to the shared memory structure
would increase the size of the shared memory consumed. Further it's easy to get
rid of them since they can be derived at run time from other values.

Author: Ashutosh Bapat <ashutosh.bapat.oss@gmail.com>
---
 src/backend/storage/ipc/shmem.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)

diff --git a/src/backend/storage/ipc/shmem.c b/src/backend/storage/ipc/shmem.c
index b0ea3d82386..61a5dda3c32 100644
--- a/src/backend/storage/ipc/shmem.c
+++ b/src/backend/storage/ipc/shmem.c
@@ -77,6 +77,8 @@
 #include "utils/builtins.h"
 
 /*
+ * Structure controlling shared memory allocations.
+ *
  * This is the first data structure stored in the shared memory segment, at
  * the offset that PGShmemHeader->content_offset points to.  Allocations by
  * ShmemAlloc() are carved out of the space after this.
@@ -97,10 +99,9 @@ static void *ShmemAllocRaw(Size size, Size *allocated_size);
 
 /* shared memory global variables */
 
-static PGShmemHeader *ShmemSegHdr;	/* shared mem segment header */
+static PGShmemHeader *ShmemSegHdr;	/* shared mem segment header and also the
+									 * base address of shared memory */
 
-static void *ShmemBase;			/* start address of shared memory */
-static void *ShmemEnd;			/* end+1 address of shared memory */
 static ShmemAllocatorData *ShmemAllocator;
 
 slock_t    *ShmemLock;			/* points to ShmemAllocator->shmem_lock */
@@ -133,8 +134,6 @@ InitShmemAllocator(PGShmemHeader *seghdr)
 	Assert(seghdr->content_offset == MAXALIGN(seghdr->content_offset));
 
 	ShmemSegHdr = seghdr;
-	ShmemBase = seghdr;
-	ShmemEnd = (char *) ShmemBase + seghdr->totalsize;
 
 #ifndef EXEC_BACKEND
 	Assert(!IsUnderPostmaster);
@@ -220,6 +219,7 @@ ShmemAllocRaw(Size size, Size *allocated_size)
 	Size		newStart;
 	Size		newFree;
 	void	   *newSpace;
+	char	   *ShmemBase = (char *) ShmemSegHdr;
 
 	/*
 	 * Ensure all space is adequately aligned.  We used to only MAXALIGN this
@@ -244,7 +244,7 @@ ShmemAllocRaw(Size size, Size *allocated_size)
 	newFree = newStart + size;
 	if (newFree <= ShmemSegHdr->totalsize)
 	{
-		newSpace = (char *) ShmemBase + newStart;
+		newSpace = ShmemBase + newStart;
 		ShmemAllocator->freeoffset = newFree;
 	}
 	else
@@ -266,6 +266,9 @@ ShmemAllocRaw(Size size, Size *allocated_size)
 bool
 ShmemAddrIsValid(const void *addr)
 {
+	void	   *ShmemBase = ShmemSegHdr;
+	void	   *ShmemEnd = (char *) ShmemBase + ShmemSegHdr->totalsize;
+
 	return (addr >= ShmemBase) && (addr < ShmemEnd);
 }
 
-- 
2.34.1

