v3-0002-fixup-Change-memory-context-fields-to-uint32.patch

text/plain

Filename: v3-0002-fixup-Change-memory-context-fields-to-uint32.patch
Type: text/plain
Part: 1
Message: Re: Changing types of block and chunk sizes in memory contexts

Patch

Format: format-patch
Series: patch v3-0002
Subject: fixup! Change memory context fields to uint32
File+
src/backend/utils/mmgr/aset.c 12 13
src/backend/utils/mmgr/generation.c 14 15
src/backend/utils/mmgr/slab.c 11 7
From 81825c3d3f7d12b5f5d68f3e132e5284f823e393 Mon Sep 17 00:00:00 2001
From: David Rowley <dgrowley@gmail.com>
Date: Thu, 13 Jul 2023 16:41:29 +1200
Subject: [PATCH v3 2/2] fixup! Change memory context fields to uint32

---
 src/backend/utils/mmgr/aset.c       | 25 ++++++++++++-------------
 src/backend/utils/mmgr/generation.c | 29 ++++++++++++++---------------
 src/backend/utils/mmgr/slab.c       | 18 +++++++++++-------
 3 files changed, 37 insertions(+), 35 deletions(-)

diff --git a/src/backend/utils/mmgr/aset.c b/src/backend/utils/mmgr/aset.c
index 797abef1b5..9b556396ee 100644
--- a/src/backend/utils/mmgr/aset.c
+++ b/src/backend/utils/mmgr/aset.c
@@ -241,12 +241,11 @@ typedef struct AllocBlockData
 #define MAX_FREE_CONTEXTS 100	/* arbitrary limit on freelist length */
 
 /* Obtain the keeper block for an allocation set */
-#define AllocSetKeeper(set) \
+#define KeeperBlock(set) \
 	((AllocBlock) (((char *) set) + MAXALIGN(sizeof(AllocSetContext))))
 
 /* Check if the block is the keeper block of the given allocation set */
-#define IsKeeperBlock(set, block) \
-	((block) == (AllocSetKeeper(set)))
+#define IsKeeperBlock(set, block) ((block) == (KeeperBlock(set)))
 
 typedef struct AllocSetFreeList
 {
@@ -424,7 +423,7 @@ AllocSetContextCreateInternal(MemoryContext parent,
 								name);
 
 			((MemoryContext) set)->mem_allocated =
-				AllocSetKeeper(set)->endptr - ((char *) set);
+				KeeperBlock(set)->endptr - ((char *) set);
 
 			return (MemoryContext) set;
 		}
@@ -460,10 +459,10 @@ AllocSetContextCreateInternal(MemoryContext parent,
 	 */
 
 	/*
-	 * Fill in the initial block's block header. The initial block is not to be
-	 * released at reset time, considered as keeper block.
+	 * Fill in the initial block's block header.  The initial block is not to
+	 * be released at reset time, considered as keeper block.
 	 */
-	block = AllocSetKeeper(set);
+	block = KeeperBlock(set);
 	block->aset = set;
 	block->freeptr = ((char *) block) + ALLOC_BLOCKHDRSZ;
 	block->endptr = ((char *) set) + firstBlockSize;
@@ -479,9 +478,9 @@ AllocSetContextCreateInternal(MemoryContext parent,
 	/* Finish filling in aset-specific parts of the context header */
 	MemSetAligned(set->freelist, 0, sizeof(set->freelist));
 
-	set->initBlockSize = (Size) initBlockSize;
-	set->maxBlockSize = (Size) maxBlockSize;
-	set->nextBlockSize = (Size) initBlockSize;
+	set->initBlockSize = (uint32) initBlockSize;
+	set->maxBlockSize = (uint32) maxBlockSize;
+	set->nextBlockSize = (uint32) initBlockSize;
 	set->freeListIndex = freeListIndex;
 
 	/*
@@ -552,7 +551,7 @@ AllocSetReset(MemoryContext context)
 #endif
 
 	/* Remember keeper block size for Assert below */
-	keepersize = AllocSetKeeper(set)->endptr - ((char *) set);
+	keepersize = KeeperBlock(set)->endptr - ((char *) set);
 
 	/* Clear chunk freelists */
 	MemSetAligned(set->freelist, 0, sizeof(set->freelist));
@@ -560,7 +559,7 @@ AllocSetReset(MemoryContext context)
 	block = set->blocks;
 
 	/* New blocks list will be just the keeper block */
-	set->blocks = AllocSetKeeper(set);
+	set->blocks = KeeperBlock(set);
 
 	while (block != NULL)
 	{
@@ -622,7 +621,7 @@ AllocSetDelete(MemoryContext context)
 #endif
 
 	/* Remember keeper block size for Assert below */
-	keepersize = AllocSetKeeper(set)->endptr - ((char *) set);
+	keepersize = KeeperBlock(set)->endptr - ((char *) set);
 
 	/*
 	 * If the context is a candidate for a freelist, put it into that freelist
diff --git a/src/backend/utils/mmgr/generation.c b/src/backend/utils/mmgr/generation.c
index d6a0811a72..a152fbf0c5 100644
--- a/src/backend/utils/mmgr/generation.c
+++ b/src/backend/utils/mmgr/generation.c
@@ -120,12 +120,11 @@ struct GenerationBlock
 	(GenerationBlock *) ((char *) chunk - Generation_BLOCKHDRSZ)
 
 /* Obtain the keeper block for a generation context */
-#define GenerationKeeper(set) \
+#define KeeperBlock(set) \
 	((GenerationBlock *) (((char *) set) + MAXALIGN(sizeof(GenerationContext))))
 
 /* Check if the block is the keeper block of the given generation context */
-#define IsKeeperBlock(set, block) \
-	((block) == (GenerationKeeper(set)))
+#define IsKeeperBlock(set, block) ((block) == (KeeperBlock(set)))
 
 /* Inlined helper functions */
 static inline void GenerationBlockInit(GenerationContext *context,
@@ -160,8 +159,8 @@ GenerationContextCreate(MemoryContext parent,
 						Size initBlockSize,
 						Size maxBlockSize)
 {
-	uint32		firstBlockSize;
-	uint32		allocSize;
+	Size		firstBlockSize;
+	Size		allocSize;
 	GenerationContext *set;
 	GenerationBlock *block;
 
@@ -221,10 +220,10 @@ GenerationContextCreate(MemoryContext parent,
 	dlist_init(&set->blocks);
 
 	/*
-	 * Fill in the initial block's block header. The initial block is not to be
-	 * released at reset time, considered as keeper block.
+	 * Fill in the initial block's block header.  The initial block is not to
+	 * be released at reset time, considered as keeper block.
 	 */
-	block = GenerationKeeper(set);
+	block = KeeperBlock(set);
 	/* determine the block size and initialize it */
 	firstBlockSize = allocSize - MAXALIGN(sizeof(GenerationContext));
 	GenerationBlockInit(set, block, firstBlockSize);
@@ -239,9 +238,9 @@ GenerationContextCreate(MemoryContext parent,
 	set->freeblock = NULL;
 
 	/* Fill in GenerationContext-specific header fields */
-	set->initBlockSize = (Size) initBlockSize;
-	set->maxBlockSize = (Size) maxBlockSize;
-	set->nextBlockSize = (Size) initBlockSize;
+	set->initBlockSize = (uint32) initBlockSize;
+	set->maxBlockSize = (uint32) maxBlockSize;
+	set->nextBlockSize = (uint32) initBlockSize;
 
 	/*
 	 * Compute the allocation chunk size limit for this context.
@@ -308,7 +307,7 @@ GenerationReset(MemoryContext context)
 	}
 
 	/* set it so new allocations to make use of the keeper block */
-	set->block = GenerationKeeper(set);
+	set->block = KeeperBlock(set);
 
 	/* Reset block size allocation sequence, too */
 	set->nextBlockSize = set->initBlockSize;
@@ -447,10 +446,10 @@ GenerationAlloc(MemoryContext context, Size size)
 			 */
 			set->freeblock = NULL;
 		}
-		else if (GenerationBlockIsEmpty(GenerationKeeper(set)) &&
-				 GenerationBlockFreeBytes(GenerationKeeper(set)) >= required_size)
+		else if (GenerationBlockIsEmpty(KeeperBlock(set)) &&
+				 GenerationBlockFreeBytes(KeeperBlock(set)) >= required_size)
 		{
-			block = GenerationKeeper(set);
+			block = KeeperBlock(set);
 		}
 		else
 		{
diff --git a/src/backend/utils/mmgr/slab.c b/src/backend/utils/mmgr/slab.c
index d5e0edbede..40c1d401c4 100644
--- a/src/backend/utils/mmgr/slab.c
+++ b/src/backend/utils/mmgr/slab.c
@@ -105,8 +105,8 @@ typedef struct SlabContext
 	MemoryContextData header;	/* Standard memory-context fields */
 	/* Allocation parameters for this context: */
 	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 */
+	uint32		fullChunkSize;	/* chunk size with chunk header and alignment */
+	uint32		blockSize;		/* the size to make each block of chunks */
 	int32		chunksPerBlock; /* number of chunks that fit in 1 block */
 	int32		curBlocklistIndex;	/* index into the blocklist[] element
 									 * containing the fullest, blocks */
@@ -314,7 +314,9 @@ SlabGetNextFreeChunk(SlabContext *slab, SlabBlock *block)
  * blockSize: allocation block size
  * chunkSize: allocation chunk size
  *
- * The MAXALIGN(chunkSize) may not exceed MEMORYCHUNK_MAX_VALUE
+ * The Slab_CHUNKHDRSZ + MAXALIGN(chunkSize + 1) may not exceed
+ * MEMORYCHUNK_MAX_VALUE.
+ * 'blockSize' may not exceed MEMORYCHUNK_MAX_BLOCKOFFSET.
  */
 MemoryContext
 SlabContextCreate(MemoryContext parent,
@@ -330,7 +332,7 @@ SlabContextCreate(MemoryContext parent,
 	/* ensure MemoryChunk's size is properly maxaligned */
 	StaticAssertDecl(Slab_CHUNKHDRSZ == MAXALIGN(Slab_CHUNKHDRSZ),
 					 "sizeof(MemoryChunk) is not maxaligned");
-	Assert(MAXALIGN(chunkSize) <= MEMORYCHUNK_MAX_VALUE);
+	Assert(blockSize <= MEMORYCHUNK_MAX_BLOCKOFFSET);
 
 	/*
 	 * Ensure there's enough space to store the pointer to the next free chunk
@@ -347,6 +349,8 @@ SlabContextCreate(MemoryContext parent,
 	fullChunkSize = Slab_CHUNKHDRSZ + MAXALIGN(chunkSize);
 #endif
 
+	Assert(fullChunkSize <= MEMORYCHUNK_MAX_VALUE);
+
 	/* compute the number of chunks that will fit on each block */
 	chunksPerBlock = (blockSize - Slab_BLOCKHDRSZ) / fullChunkSize;
 
@@ -374,9 +378,9 @@ SlabContextCreate(MemoryContext parent,
 	 */
 
 	/* Fill in SlabContext-specific header fields */
-	slab->chunkSize = (Size) chunkSize;
-	slab->fullChunkSize = fullChunkSize;
-	slab->blockSize = blockSize;
+	slab->chunkSize = (uint32) chunkSize;
+	slab->fullChunkSize = (uint32) fullChunkSize;
+	slab->blockSize = (uint32) blockSize;
 	slab->chunksPerBlock = chunksPerBlock;
 	slab->curBlocklistIndex = 0;
 
-- 
2.39.2