From 05f90842369538cf94bbd09970a008b9357f4101 Mon Sep 17 00:00:00 2001 From: David Rowley Date: Wed, 31 Aug 2022 07:33:54 +1200 Subject: [PATCH] Various cleanups of the new memory context header code Robert Haas reported that his older clang compiler didn't like the two Asserts which were verifying that the given MemoryContextMethodID was <= MEMORY_CONTEXT_METHODID_MASK when building with -Wtautological-constant-out-of-range-compare. In my (David's) opinion, the compiler is wrong to warn about that. Newer versions of clang don't warn about the out of range enum value, so perhaps this was a bug that has now been fixed. To keep older clang versions happy, let's just cast the enum value to int to stop the compiler complaining. The main reason for the Asserts mentioned above to exist are to inform future developers which are adding new MemoryContexts if they run out of bit space in MemoryChunk to store the MemoryContextMethodID. As pointed out by Tom Lane, it seems wise to also add a comment to the header for that enum to document the restriction on these enum values. Additionally, also fix an incorrect usage of UINT64CONST() which was introduced in c6e0fe1f2. Author: Robert Haas, David Rowley Discussion: https://postgr.es/m/CA+TgmoYGG2C7Vbw1cjkQRRBL3zOk8SmhrQnsJgzscX=N9AwPrw@mail.gmail.com --- src/include/utils/memutils_internal.h | 5 ++++- src/include/utils/memutils_memorychunk.h | 4 ++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/include/utils/memutils_internal.h b/src/include/utils/memutils_internal.h index 9a9f52ef16..377cee7a84 100644 --- a/src/include/utils/memutils_internal.h +++ b/src/include/utils/memutils_internal.h @@ -74,6 +74,9 @@ extern void SlabCheck(MemoryContext context); * MemoryContextMethodID * A unique identifier for each MemoryContext implementation which * indicates the index into the mcxt_methods[] array. See mcxt.c. + * The maximum value for this enum is constrained by + * MEMORY_CONTEXT_METHODID_MASK. If an enum value higher than that is + * required then MEMORY_CONTEXT_METHODID_BITS will need to be increased. */ typedef enum MemoryContextMethodID { @@ -88,7 +91,7 @@ typedef enum MemoryContextMethodID */ #define MEMORY_CONTEXT_METHODID_BITS 3 #define MEMORY_CONTEXT_METHODID_MASK \ - UINT64CONST((1 << MEMORY_CONTEXT_METHODID_BITS) - 1) + ((((uint64) 1) << MEMORY_CONTEXT_METHODID_BITS) - 1) /* * This routine handles the context-type-independent part of memory diff --git a/src/include/utils/memutils_memorychunk.h b/src/include/utils/memutils_memorychunk.h index 685c177b68..2eefc138e3 100644 --- a/src/include/utils/memutils_memorychunk.h +++ b/src/include/utils/memutils_memorychunk.h @@ -159,7 +159,7 @@ MemoryChunkSetHdrMask(MemoryChunk *chunk, void *block, Assert((char *) chunk > (char *) block); Assert(blockoffset <= MEMORYCHUNK_MAX_BLOCKOFFSET); Assert(value <= MEMORYCHUNK_MAX_VALUE); - Assert(methodid <= MEMORY_CONTEXT_METHODID_MASK); + Assert((int) methodid <= MEMORY_CONTEXT_METHODID_MASK); chunk->hdrmask = (((uint64) blockoffset) << MEMORYCHUNK_BLOCKOFFSET_BASEBIT) | (((uint64) value) << MEMORYCHUNK_VALUE_BASEBIT) | @@ -175,7 +175,7 @@ static inline void MemoryChunkSetHdrMaskExternal(MemoryChunk *chunk, MemoryContextMethodID methodid) { - Assert(methodid <= MEMORY_CONTEXT_METHODID_MASK); + Assert((int) methodid <= MEMORY_CONTEXT_METHODID_MASK); chunk->hdrmask = MEMORYCHUNK_MAGIC | (((uint64) 1) << MEMORYCHUNK_EXTERNAL_BASEBIT) | methodid;