From bc58fb3d3c5e5a0f59283912e0548ca15be16afb Mon Sep 17 00:00:00 2001
From: Andrew Kim <andrew.kim@intel.com>
Date: Tue, 4 Nov 2025 18:34:49 -0800
Subject: [PATCH 1/3] Move checksum functions from backend storage to port

This refactoring moves checksum implementation from src/backend/storage/page/ to src/port/
---
 contrib/pageinspect/rawpage.c                 | 2 +-
 src/backend/backup/basebackup.c               | 2 +-
 src/backend/storage/page/Makefile             | 6 +-----
 src/backend/storage/page/bufpage.c            | 2 +-
 src/backend/storage/page/meson.build          | 9 ---------
 src/bin/pg_checksums/pg_checksums.c           | 3 +--
 src/bin/pg_upgrade/file.c                     | 3 +--
 src/include/{storage => port}/checksum.h      | 2 +-
 src/include/{storage => port}/checksum_impl.h | 4 ++--
 src/port/Makefile                             | 6 ++++++
 src/{backend/storage/page => port}/checksum.c | 8 ++++----
 src/port/meson.build                          | 4 ++--
 src/test/modules/test_aio/test_aio.c          | 2 +-
 13 files changed, 22 insertions(+), 31 deletions(-)
 rename src/include/{storage => port}/checksum.h (94%)
 rename src/include/{storage => port}/checksum_impl.h (98%)
 rename src/{backend/storage/page => port}/checksum.c (73%)

diff --git a/contrib/pageinspect/rawpage.c b/contrib/pageinspect/rawpage.c
index aef442b5db3..7beb7765da9 100644
--- a/contrib/pageinspect/rawpage.c
+++ b/contrib/pageinspect/rawpage.c
@@ -23,7 +23,7 @@
 #include "miscadmin.h"
 #include "pageinspect.h"
 #include "storage/bufmgr.h"
-#include "storage/checksum.h"
+#include "port/checksum.h"
 #include "utils/builtins.h"
 #include "utils/pg_lsn.h"
 #include "utils/rel.h"
diff --git a/src/backend/backup/basebackup.c b/src/backend/backup/basebackup.c
index bb7d90aa5d9..d84ced4b47c 100644
--- a/src/backend/backup/basebackup.c
+++ b/src/backend/backup/basebackup.c
@@ -39,7 +39,7 @@
 #include "replication/walsender.h"
 #include "replication/walsender_private.h"
 #include "storage/bufpage.h"
-#include "storage/checksum.h"
+#include "port/checksum.h"
 #include "storage/dsm_impl.h"
 #include "storage/ipc.h"
 #include "storage/reinit.h"
diff --git a/src/backend/storage/page/Makefile b/src/backend/storage/page/Makefile
index da539b113a6..788fee403f6 100644
--- a/src/backend/storage/page/Makefile
+++ b/src/backend/storage/page/Makefile
@@ -12,12 +12,8 @@ subdir = src/backend/storage/page
 top_builddir = ../../../..
 include $(top_builddir)/src/Makefile.global
 
-OBJS =  \
+OBJS = \
 	bufpage.o \
-	checksum.o \
 	itemptr.o
 
 include $(top_srcdir)/src/backend/common.mk
-
-# Provide special optimization flags for checksum.c
-checksum.o: CFLAGS += ${CFLAGS_UNROLL_LOOPS} ${CFLAGS_VECTORIZE}
diff --git a/src/backend/storage/page/bufpage.c b/src/backend/storage/page/bufpage.c
index aac6e695954..73f42dc0c49 100644
--- a/src/backend/storage/page/bufpage.c
+++ b/src/backend/storage/page/bufpage.c
@@ -18,7 +18,7 @@
 #include "access/itup.h"
 #include "access/xlog.h"
 #include "pgstat.h"
-#include "storage/checksum.h"
+#include "port/checksum.h"
 #include "utils/memdebug.h"
 #include "utils/memutils.h"
 
diff --git a/src/backend/storage/page/meson.build b/src/backend/storage/page/meson.build
index 112f00ff365..cf92a8f55f0 100644
--- a/src/backend/storage/page/meson.build
+++ b/src/backend/storage/page/meson.build
@@ -1,14 +1,5 @@
 # Copyright (c) 2022-2025, PostgreSQL Global Development Group
 
-checksum_backend_lib = static_library('checksum_backend_lib',
-  'checksum.c',
-  dependencies: backend_build_deps,
-  kwargs: internal_lib_args,
-  c_args: vectorize_cflags + unroll_loops_cflags,
-)
-
-backend_link_with += checksum_backend_lib
-
 backend_sources += files(
   'bufpage.c',
   'itemptr.c',
diff --git a/src/bin/pg_checksums/pg_checksums.c b/src/bin/pg_checksums/pg_checksums.c
index 46cb2f36efa..2e0212c029c 100644
--- a/src/bin/pg_checksums/pg_checksums.c
+++ b/src/bin/pg_checksums/pg_checksums.c
@@ -29,8 +29,7 @@
 #include "getopt_long.h"
 #include "pg_getopt.h"
 #include "storage/bufpage.h"
-#include "storage/checksum.h"
-#include "storage/checksum_impl.h"
+#include "port/checksum.h"
 
 
 static int64 files_scanned = 0;
diff --git a/src/bin/pg_upgrade/file.c b/src/bin/pg_upgrade/file.c
index 91ed16acb08..f9a5ed02ee4 100644
--- a/src/bin/pg_upgrade/file.c
+++ b/src/bin/pg_upgrade/file.c
@@ -24,8 +24,7 @@
 #include "common/file_perm.h"
 #include "pg_upgrade.h"
 #include "storage/bufpage.h"
-#include "storage/checksum.h"
-#include "storage/checksum_impl.h"
+#include "port/checksum.h"
 
 
 /*
diff --git a/src/include/storage/checksum.h b/src/include/port/checksum.h
similarity index 94%
rename from src/include/storage/checksum.h
rename to src/include/port/checksum.h
index 25d13a798d1..c2faed83ede 100644
--- a/src/include/storage/checksum.h
+++ b/src/include/port/checksum.h
@@ -6,7 +6,7 @@
  * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * src/include/storage/checksum.h
+ * src/include/port/checksum.h
  *
  *-------------------------------------------------------------------------
  */
diff --git a/src/include/storage/checksum_impl.h b/src/include/port/checksum_impl.h
similarity index 98%
rename from src/include/storage/checksum_impl.h
rename to src/include/port/checksum_impl.h
index da87d61ba52..00cb0549f24 100644
--- a/src/include/storage/checksum_impl.h
+++ b/src/include/port/checksum_impl.h
@@ -5,13 +5,13 @@
  *
  * This file exists for the benefit of external programs that may wish to
  * check Postgres page checksums.  They can #include this to get the code
- * referenced by storage/checksum.h.  (Note: you may need to redefine
+ * referenced by port/checksum.h.  (Note: you may need to redefine
  * Assert() as empty to compile this successfully externally.)
  *
  * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * src/include/storage/checksum_impl.h
+ * src/include/port/checksum_impl.h
  *
  *-------------------------------------------------------------------------
  */
diff --git a/src/port/Makefile b/src/port/Makefile
index 4274949dfa4..4f1f460bff2 100644
--- a/src/port/Makefile
+++ b/src/port/Makefile
@@ -39,6 +39,7 @@ OBJS = \
 	$(LIBOBJS) \
 	$(PG_CRC32C_OBJS) \
 	bsearch_arg.o \
+	checksum.o \
 	chklocale.o \
 	inet_net_ntop.o \
 	noblock.o \
@@ -90,6 +91,11 @@ pg_crc32c_armv8.o: CFLAGS+=$(CFLAGS_CRC)
 pg_crc32c_armv8_shlib.o: CFLAGS+=$(CFLAGS_CRC)
 pg_crc32c_armv8_srv.o: CFLAGS+=$(CFLAGS_CRC)
 
+# Provide special optimization flags for checksum.c
+checksum.o: CFLAGS += ${CFLAGS_UNROLL_LOOPS} ${CFLAGS_VECTORIZE}
+checksum_shlib.o: CFLAGS += ${CFLAGS_UNROLL_LOOPS} ${CFLAGS_VECTORIZE}
+checksum_srv.o: CFLAGS += ${CFLAGS_UNROLL_LOOPS} ${CFLAGS_VECTORIZE}
+
 #
 # Shared library versions of object files
 #
diff --git a/src/backend/storage/page/checksum.c b/src/port/checksum.c
similarity index 73%
rename from src/backend/storage/page/checksum.c
rename to src/port/checksum.c
index c913459b5a3..de61a46231d 100644
--- a/src/backend/storage/page/checksum.c
+++ b/src/port/checksum.c
@@ -7,16 +7,16 @@
  * Portions Copyright (c) 1994, Regents of the University of California
  *
  * IDENTIFICATION
- *	  src/backend/storage/page/checksum.c
+ *	  src/port/checksum.c
  *
  *-------------------------------------------------------------------------
  */
 #include "postgres.h"
 
-#include "storage/checksum.h"
+#include "port/checksum.h"
 /*
- * The actual code is in storage/checksum_impl.h.  This is done so that
+ * The actual code is in port/checksum_impl.h.  This is done so that
  * external programs can incorporate the checksum code by #include'ing
  * that file from the exported Postgres headers.  (Compare our CRC code.)
  */
-#include "storage/checksum_impl.h"	/* IWYU pragma: keep */
+#include "port/checksum_impl.h"		/* IWYU pragma: keep */
diff --git a/src/port/meson.build b/src/port/meson.build
index fc7b059fee5..d3e63bce9e7 100644
--- a/src/port/meson.build
+++ b/src/port/meson.build
@@ -104,8 +104,8 @@ replace_funcs_pos = [
   ['pg_crc32c_sb8', 'USE_SLICING_BY_8_CRC32C'],
 ]
 
-pgport_cflags = {'crc': cflags_crc}
-pgport_sources_cflags = {'crc': []}
+pgport_cflags = {'crc': cflags_crc, 'checksum': vectorize_cflags + unroll_loops_cflags}
+pgport_sources_cflags = {'crc': [], 'checksum': [files('checksum.c')]}
 
 foreach f : replace_funcs_neg
   func = f.get(0)
diff --git a/src/test/modules/test_aio/test_aio.c b/src/test/modules/test_aio/test_aio.c
index c55cf6c0aac..175e491c0bc 100644
--- a/src/test/modules/test_aio/test_aio.c
+++ b/src/test/modules/test_aio/test_aio.c
@@ -24,7 +24,7 @@
 #include "storage/aio_internal.h"
 #include "storage/buf_internals.h"
 #include "storage/bufmgr.h"
-#include "storage/checksum.h"
+#include "port/checksum.h"
 #include "storage/ipc.h"
 #include "storage/lwlock.h"
 #include "utils/builtins.h"
-- 
2.43.0

