From 0864d770d44d15dda5f8a506bead12f8da5a5a26 Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <peter@eisentraut.org>
Date: Mon, 9 Dec 2024 16:35:23 +0100
Subject: [PATCH 2/3] Add const qualifiers to bufpage.h

---
 src/backend/storage/page/bufpage.c | 32 ++++++-------
 src/include/storage/bufpage.h      | 75 +++++++++++++++---------------
 src/tools/pgindent/typedefs.list   |  1 +
 3 files changed, 54 insertions(+), 54 deletions(-)

diff --git a/src/backend/storage/page/bufpage.c b/src/backend/storage/page/bufpage.c
index aa264f61b9c..b32c5c0de45 100644
--- a/src/backend/storage/page/bufpage.c
+++ b/src/backend/storage/page/bufpage.c
@@ -85,9 +85,9 @@ PageInit(Page page, Size pageSize, Size specialSize)
  * to pgstat.
  */
 bool
-PageIsVerifiedExtended(Page page, BlockNumber blkno, int flags)
+PageIsVerifiedExtended(const PageData *page, BlockNumber blkno, int flags)
 {
-	PageHeader	p = (PageHeader) page;
+	const PageHeaderData *p = (PageHeaderData *) page;
 	size_t	   *pagebytes;
 	bool		checksum_failure = false;
 	bool		header_sane = false;
@@ -351,7 +351,7 @@ PageAddItemExtended(Page page,
  *		The returned page is not initialized at all; caller must do that.
  */
 Page
-PageGetTempPage(Page page)
+PageGetTempPage(const PageData *page)
 {
 	Size		pageSize;
 	Page		temp;
@@ -368,7 +368,7 @@ PageGetTempPage(Page page)
  *		The page is initialized by copying the contents of the given page.
  */
 Page
-PageGetTempPageCopy(Page page)
+PageGetTempPageCopy(const PageData *page)
 {
 	Size		pageSize;
 	Page		temp;
@@ -388,7 +388,7 @@ PageGetTempPageCopy(Page page)
  *		given page, and the special space is copied from the given page.
  */
 Page
-PageGetTempPageCopySpecial(Page page)
+PageGetTempPageCopySpecial(const PageData *page)
 {
 	Size		pageSize;
 	Page		temp;
@@ -893,16 +893,16 @@ PageTruncateLinePointerArray(Page page)
  * PageGetHeapFreeSpace on heap pages.
  */
 Size
-PageGetFreeSpace(Page page)
+PageGetFreeSpace(const PageData *page)
 {
+	const PageHeaderData *phdr = (const PageHeaderData *) page;
 	int			space;
 
 	/*
 	 * Use signed arithmetic here so that we behave sensibly if pd_lower >
 	 * pd_upper.
 	 */
-	space = (int) ((PageHeader) page)->pd_upper -
-		(int) ((PageHeader) page)->pd_lower;
+	space = (int) phdr->pd_upper - (int) phdr->pd_lower;
 
 	if (space < (int) sizeof(ItemIdData))
 		return 0;
@@ -920,16 +920,16 @@ PageGetFreeSpace(Page page)
  * PageGetHeapFreeSpace on heap pages.
  */
 Size
-PageGetFreeSpaceForMultipleTuples(Page page, int ntups)
+PageGetFreeSpaceForMultipleTuples(const PageData *page, int ntups)
 {
+	const PageHeaderData *phdr = (const PageHeaderData *) page;
 	int			space;
 
 	/*
 	 * Use signed arithmetic here so that we behave sensibly if pd_lower >
 	 * pd_upper.
 	 */
-	space = (int) ((PageHeader) page)->pd_upper -
-		(int) ((PageHeader) page)->pd_lower;
+	space = (int) phdr->pd_upper - (int) phdr->pd_lower;
 
 	if (space < (int) (ntups * sizeof(ItemIdData)))
 		return 0;
@@ -944,16 +944,16 @@ PageGetFreeSpaceForMultipleTuples(Page page, int ntups)
  *		without any consideration for adding/removing line pointers.
  */
 Size
-PageGetExactFreeSpace(Page page)
+PageGetExactFreeSpace(const PageData *page)
 {
+	const PageHeaderData *phdr = (const PageHeaderData *) page;
 	int			space;
 
 	/*
 	 * Use signed arithmetic here so that we behave sensibly if pd_lower >
 	 * pd_upper.
 	 */
-	space = (int) ((PageHeader) page)->pd_upper -
-		(int) ((PageHeader) page)->pd_lower;
+	space = (int) phdr->pd_upper - (int) phdr->pd_lower;
 
 	if (space < 0)
 		return 0;
@@ -977,7 +977,7 @@ PageGetExactFreeSpace(Page page)
  * on the number of line pointers, we make this extra check.)
  */
 Size
-PageGetHeapFreeSpace(Page page)
+PageGetHeapFreeSpace(const PageData *page)
 {
 	Size		space;
 
@@ -1001,7 +1001,7 @@ PageGetHeapFreeSpace(Page page)
 				 */
 				for (offnum = FirstOffsetNumber; offnum <= nline; offnum = OffsetNumberNext(offnum))
 				{
-					ItemId		lp = PageGetItemId(page, offnum);
+					ItemId		lp = PageGetItemId(unconstify(PageData *, page), offnum);
 
 					if (!ItemIdIsUsed(lp))
 						break;
diff --git a/src/include/storage/bufpage.h b/src/include/storage/bufpage.h
index 9f3ed976e43..0ee0ed66e72 100644
--- a/src/include/storage/bufpage.h
+++ b/src/include/storage/bufpage.h
@@ -222,9 +222,9 @@ typedef PageHeaderData *PageHeader;
  *		returns true iff no itemid has been allocated on the page
  */
 static inline bool
-PageIsEmpty(Page page)
+PageIsEmpty(const PageData *page)
 {
-	return ((PageHeader) page)->pd_lower <= SizeOfPageHeaderData;
+	return ((const PageHeaderData *) page)->pd_lower <= SizeOfPageHeaderData;
 }
 
 /*
@@ -232,9 +232,9 @@ PageIsEmpty(Page page)
  *		returns true iff page has not been initialized (by PageInit)
  */
 static inline bool
-PageIsNew(Page page)
+PageIsNew(const PageData *page)
 {
-	return ((PageHeader) page)->pd_upper == 0;
+	return ((const PageHeaderData *) page)->pd_upper == 0;
 }
 
 /*
@@ -275,9 +275,9 @@ PageGetContents(Page page)
  * however, it can be called on a page that is not stored in a buffer.
  */
 static inline Size
-PageGetPageSize(Page page)
+PageGetPageSize(const PageData *page)
 {
-	return (Size) (((PageHeader) page)->pd_pagesize_version & (uint16) 0xFF00);
+	return (Size) (((const PageHeaderData *) page)->pd_pagesize_version & (uint16) 0xFF00);
 }
 
 /*
@@ -285,9 +285,9 @@ PageGetPageSize(Page page)
  *		Returns the page layout version of a page.
  */
 static inline uint8
-PageGetPageLayoutVersion(Page page)
+PageGetPageLayoutVersion(const PageData *page)
 {
-	return (((PageHeader) page)->pd_pagesize_version & 0x00FF);
+	return (((const PageHeaderData *) page)->pd_pagesize_version & 0x00FF);
 }
 
 /*
@@ -315,9 +315,9 @@ PageSetPageSizeAndVersion(Page page, Size size, uint8 version)
  *		Returns size of special space on a page.
  */
 static inline uint16
-PageGetSpecialSize(Page page)
+PageGetSpecialSize(const PageData *page)
 {
-	return (PageGetPageSize(page) - ((PageHeader) page)->pd_special);
+	return (PageGetPageSize(page) - ((const PageHeaderData *) page)->pd_special);
 }
 
 /*
@@ -326,23 +326,22 @@ PageGetSpecialSize(Page page)
  * This is intended to catch use of the pointer before page initialization.
  */
 static inline void
-PageValidateSpecialPointer(Page page)
+PageValidateSpecialPointer(const PageData *page)
 {
 	Assert(page);
-	Assert(((PageHeader) page)->pd_special <= BLCKSZ);
-	Assert(((PageHeader) page)->pd_special >= SizeOfPageHeaderData);
+	Assert(((const PageHeaderData *) page)->pd_special <= BLCKSZ);
+	Assert(((const PageHeaderData *) page)->pd_special >= SizeOfPageHeaderData);
 }
 
 /*
  * PageGetSpecialPointer
  *		Returns pointer to special space on a page.
  */
-static inline char *
-PageGetSpecialPointer(Page page)
-{
-	PageValidateSpecialPointer(page);
-	return (char *) page + ((PageHeader) page)->pd_special;
-}
+#define PageGetSpecialPointer(page) \
+( \
+	PageValidateSpecialPointer(page), \
+	((page) + ((PageHeader) (page))->pd_special) \
+)
 
 /*
  * PageGetItem
@@ -353,12 +352,12 @@ PageGetSpecialPointer(Page page)
  *		The semantics may change in the future.
  */
 static inline Item
-PageGetItem(Page page, ItemId itemId)
+PageGetItem(const PageData *page, const ItemIdData *itemId)
 {
 	Assert(page);
 	Assert(ItemIdHasStorage(itemId));
 
-	return (Item) (((char *) page) + ItemIdGetOffset(itemId));
+	return (Item) (((const char *) page) + ItemIdGetOffset(itemId));
 }
 
 /*
@@ -371,9 +370,9 @@ PageGetItem(Page page, ItemId itemId)
  *		return zero to ensure sane behavior.
  */
 static inline OffsetNumber
-PageGetMaxOffsetNumber(Page page)
+PageGetMaxOffsetNumber(const PageData *page)
 {
-	PageHeader	pageheader = (PageHeader) page;
+	const PageHeaderData *pageheader = (const PageHeaderData *) page;
 
 	if (pageheader->pd_lower <= SizeOfPageHeaderData)
 		return 0;
@@ -385,7 +384,7 @@ PageGetMaxOffsetNumber(Page page)
  * Additional functions for access to page headers.
  */
 static inline XLogRecPtr
-PageGetLSN(const char *page)
+PageGetLSN(const PageData *page)
 {
 	return PageXLogRecPtrGet(((const PageHeaderData *) page)->pd_lsn);
 }
@@ -396,9 +395,9 @@ PageSetLSN(Page page, XLogRecPtr lsn)
 }
 
 static inline bool
-PageHasFreeLinePointers(Page page)
+PageHasFreeLinePointers(const PageData *page)
 {
-	return ((PageHeader) page)->pd_flags & PD_HAS_FREE_LINES;
+	return ((const PageHeaderData *) page)->pd_flags & PD_HAS_FREE_LINES;
 }
 static inline void
 PageSetHasFreeLinePointers(Page page)
@@ -412,9 +411,9 @@ PageClearHasFreeLinePointers(Page page)
 }
 
 static inline bool
-PageIsFull(Page page)
+PageIsFull(const PageData *page)
 {
-	return ((PageHeader) page)->pd_flags & PD_PAGE_FULL;
+	return ((const PageHeaderData *) page)->pd_flags & PD_PAGE_FULL;
 }
 static inline void
 PageSetFull(Page page)
@@ -428,9 +427,9 @@ PageClearFull(Page page)
 }
 
 static inline bool
-PageIsAllVisible(Page page)
+PageIsAllVisible(const PageData *page)
 {
-	return ((PageHeader) page)->pd_flags & PD_ALL_VISIBLE;
+	return ((const PageHeaderData *) page)->pd_flags & PD_ALL_VISIBLE;
 }
 static inline void
 PageSetAllVisible(Page page)
@@ -490,19 +489,19 @@ StaticAssertDecl(BLCKSZ == ((BLCKSZ / sizeof(size_t)) * sizeof(size_t)),
 				 "BLCKSZ has to be a multiple of sizeof(size_t)");
 
 extern void PageInit(Page page, Size pageSize, Size specialSize);
-extern bool PageIsVerifiedExtended(Page page, BlockNumber blkno, int flags);
+extern bool PageIsVerifiedExtended(const PageData *page, BlockNumber blkno, int flags);
 extern OffsetNumber PageAddItemExtended(Page page, Item item, Size size,
 										OffsetNumber offsetNumber, int flags);
-extern Page PageGetTempPage(Page page);
-extern Page PageGetTempPageCopy(Page page);
-extern Page PageGetTempPageCopySpecial(Page page);
+extern Page PageGetTempPage(const PageData *page);
+extern Page PageGetTempPageCopy(const PageData *page);
+extern Page PageGetTempPageCopySpecial(const PageData *page);
 extern void PageRestoreTempPage(Page tempPage, Page oldPage);
 extern void PageRepairFragmentation(Page page);
 extern void PageTruncateLinePointerArray(Page page);
-extern Size PageGetFreeSpace(Page page);
-extern Size PageGetFreeSpaceForMultipleTuples(Page page, int ntups);
-extern Size PageGetExactFreeSpace(Page page);
-extern Size PageGetHeapFreeSpace(Page page);
+extern Size PageGetFreeSpace(const PageData *page);
+extern Size PageGetFreeSpaceForMultipleTuples(const PageData *page, int ntups);
+extern Size PageGetExactFreeSpace(const PageData *page);
+extern Size PageGetHeapFreeSpace(const PageData *page);
 extern void PageIndexTupleDelete(Page page, OffsetNumber offnum);
 extern void PageIndexMultiDelete(Page page, OffsetNumber *itemnos, int nitems);
 extern void PageIndexTupleDeleteNoCompact(Page page, OffsetNumber offnum);
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index ce33e55bf1d..0b23985e90f 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -1981,6 +1981,7 @@ PX_Combo
 PX_HMAC
 PX_MD
 Page
+PageData
 PageGistNSN
 PageHeader
 PageHeaderData
-- 
2.47.1