From a11795b222b98ea51afc6c09b4ad8859d6b4284e Mon Sep 17 00:00:00 2001
From: Melih Mutlu <m.melihmutlu@gmail.com>
Date: Mon, 12 Jun 2023 08:50:28 +0300
Subject: [PATCH] Change memory context fields to uint32

Block sizes and chunk sizes can be upper bounded by
MEMORYCHUNK_MAX_BLOCKOFFSET and MEMORYCHUNK_MAX_VALUE respectively.
Values of both bounds correspond to 1 less than 1GB. This allows us
to store the sizes of any block or chunk size limited by those bounds
in 32 bits.

This patch changes types of such fields that represents block or chunk sizes
from 64-bit Size to 32-bit unsigned integers.
---
 src/backend/utils/mmgr/aset.c       | 16 ++++++++--------
 src/backend/utils/mmgr/generation.c | 18 +++++++++---------
 src/backend/utils/mmgr/slab.c       |  8 ++++----
 src/include/utils/memutils.h        | 14 +++++++-------
 4 files changed, 28 insertions(+), 28 deletions(-)

diff --git a/src/backend/utils/mmgr/aset.c b/src/backend/utils/mmgr/aset.c
index 0bbbf93672..62fb7e1f90 100644
--- a/src/backend/utils/mmgr/aset.c
+++ b/src/backend/utils/mmgr/aset.c
@@ -156,10 +156,10 @@ typedef struct AllocSetContext
 	AllocBlock	blocks;			/* head of list of blocks in this set */
 	MemoryChunk *freelist[ALLOCSET_NUM_FREELISTS];	/* free chunk lists */
 	/* Allocation parameters for this context: */
-	Size		initBlockSize;	/* initial block size */
-	Size		maxBlockSize;	/* maximum block size */
-	Size		nextBlockSize;	/* next block size to allocate */
-	Size		allocChunkLimit;	/* effective chunk size limit */
+	uint32		initBlockSize;	/* initial block size */
+	uint32		maxBlockSize;	/* maximum block size */
+	uint32		nextBlockSize;	/* next block size to allocate */
+	uint32		allocChunkLimit;	/* effective chunk size limit */
 	AllocBlock	keeper;			/* keep this block over resets */
 	/* freelist this context could be put in, or -1 if not a candidate: */
 	int			freeListIndex;	/* index in context_freelists[], or -1 */
@@ -340,12 +340,12 @@ AllocSetFreeIndex(Size size)
 MemoryContext
 AllocSetContextCreateInternal(MemoryContext parent,
 							  const char *name,
-							  Size minContextSize,
-							  Size initBlockSize,
-							  Size maxBlockSize)
+							  uint32 minContextSize,
+							  uint32 initBlockSize,
+							  uint32 maxBlockSize)
 {
 	int			freeListIndex;
-	Size		firstBlockSize;
+	uint32		firstBlockSize;
 	AllocSet	set;
 	AllocBlock	block;
 
diff --git a/src/backend/utils/mmgr/generation.c b/src/backend/utils/mmgr/generation.c
index 4fb8663cd6..a27d113af8 100644
--- a/src/backend/utils/mmgr/generation.c
+++ b/src/backend/utils/mmgr/generation.c
@@ -61,10 +61,10 @@ typedef struct GenerationContext
 	MemoryContextData header;	/* Standard memory-context fields */
 
 	/* Generational context parameters */
-	Size		initBlockSize;	/* initial block size */
-	Size		maxBlockSize;	/* maximum block size */
-	Size		nextBlockSize;	/* next block size to allocate */
-	Size		allocChunkLimit;	/* effective chunk size limit */
+	uint32		initBlockSize;	/* initial block size */
+	uint32		maxBlockSize;	/* maximum block size */
+	uint32		nextBlockSize;	/* next block size to allocate */
+	uint32		allocChunkLimit;	/* effective chunk size limit */
 
 	GenerationBlock *block;		/* current (most recently allocated) block, or
 								 * NULL if we've just freed the most recent
@@ -149,12 +149,12 @@ static inline void GenerationBlockFree(GenerationContext *set,
 MemoryContext
 GenerationContextCreate(MemoryContext parent,
 						const char *name,
-						Size minContextSize,
-						Size initBlockSize,
-						Size maxBlockSize)
+						uint32 minContextSize,
+						uint32 initBlockSize,
+						uint32 maxBlockSize)
 {
-	Size		firstBlockSize;
-	Size		allocSize;
+	uint32		firstBlockSize;
+	uint32		allocSize;
 	GenerationContext *set;
 	GenerationBlock *block;
 
diff --git a/src/backend/utils/mmgr/slab.c b/src/backend/utils/mmgr/slab.c
index 718dd2ba03..43e01a480f 100644
--- a/src/backend/utils/mmgr/slab.c
+++ b/src/backend/utils/mmgr/slab.c
@@ -104,7 +104,7 @@ typedef struct SlabContext
 {
 	MemoryContextData header;	/* Standard memory-context fields */
 	/* Allocation parameters for this context: */
-	Size		chunkSize;		/* the requested (non-aligned) chunk size */
+	uint32		chunkSize;		/* the requested (non-aligned) chunk size */
 	Size		fullChunkSize;	/* chunk size with chunk header and alignment */
 	Size		blockSize;		/* the size to make each block of chunks */
 	int32		chunksPerBlock; /* number of chunks that fit in 1 block */
@@ -320,7 +320,7 @@ MemoryContext
 SlabContextCreate(MemoryContext parent,
 				  const char *name,
 				  Size blockSize,
-				  Size chunkSize)
+				  uint32 chunkSize)
 {
 	int			chunksPerBlock;
 	Size		fullChunkSize;
@@ -352,7 +352,7 @@ SlabContextCreate(MemoryContext parent,
 
 	/* Make sure the block can store at least one chunk. */
 	if (chunksPerBlock == 0)
-		elog(ERROR, "block size %zu for slab is too small for %zu-byte chunks",
+		elog(ERROR, "block size %zu for slab is too small for %u-byte chunks",
 			 blockSize, chunkSize);
 
 
@@ -506,7 +506,7 @@ SlabAlloc(MemoryContext context, Size size)
 
 	/* make sure we only allow correct request size */
 	if (unlikely(size != slab->chunkSize))
-		elog(ERROR, "unexpected alloc chunk size %zu (expected %zu)",
+		elog(ERROR, "unexpected alloc chunk size %zu (expected %u)",
 			 size, slab->chunkSize);
 
 	/*
diff --git a/src/include/utils/memutils.h b/src/include/utils/memutils.h
index 21640d62a6..c0c3f102a5 100644
--- a/src/include/utils/memutils.h
+++ b/src/include/utils/memutils.h
@@ -111,9 +111,9 @@ extern void ProcessLogMemoryContextInterrupt(void);
 /* aset.c */
 extern MemoryContext AllocSetContextCreateInternal(MemoryContext parent,
 												   const char *name,
-												   Size minContextSize,
-												   Size initBlockSize,
-												   Size maxBlockSize);
+												   uint32 minContextSize,
+												   uint32 initBlockSize,
+												   uint32 maxBlockSize);
 
 /*
  * This wrapper macro exists to check for non-constant strings used as context
@@ -134,14 +134,14 @@ extern MemoryContext AllocSetContextCreateInternal(MemoryContext parent,
 extern MemoryContext SlabContextCreate(MemoryContext parent,
 									   const char *name,
 									   Size blockSize,
-									   Size chunkSize);
+									   uint32 chunkSize);
 
 /* generation.c */
 extern MemoryContext GenerationContextCreate(MemoryContext parent,
 											 const char *name,
-											 Size minContextSize,
-											 Size initBlockSize,
-											 Size maxBlockSize);
+											 uint32 minContextSize,
+											 uint32 initBlockSize,
+											 uint32 maxBlockSize);
 
 /*
  * Recommended default alloc parameters, suitable for "ordinary" contexts
-- 
2.25.1

