mirror of
https://github.com/postgres/postgres.git
synced 2025-05-31 03:21:24 +03:00
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
131 lines
4.5 KiB
C
131 lines
4.5 KiB
C
/*-------------------------------------------------------------------------
|
|
*
|
|
* memutils_internal.h
|
|
* This file contains declarations for memory allocation utility
|
|
* functions for internal use.
|
|
*
|
|
*
|
|
* Portions Copyright (c) 2022, PostgreSQL Global Development Group
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
|
*
|
|
* src/include/utils/memutils_internal.h
|
|
*
|
|
*-------------------------------------------------------------------------
|
|
*/
|
|
|
|
#ifndef MEMUTILS_INTERNAL_H
|
|
#define MEMUTILS_INTERNAL_H
|
|
|
|
#include "utils/memutils.h"
|
|
|
|
/* These functions implement the MemoryContext API for AllocSet context. */
|
|
extern void *AllocSetAlloc(MemoryContext context, Size size);
|
|
extern void AllocSetFree(void *pointer);
|
|
extern void *AllocSetRealloc(void *pointer, Size size);
|
|
extern void AllocSetReset(MemoryContext context);
|
|
extern void AllocSetDelete(MemoryContext context);
|
|
extern MemoryContext AllocSetGetChunkContext(void *pointer);
|
|
extern Size AllocSetGetChunkSpace(void *pointer);
|
|
extern bool AllocSetIsEmpty(MemoryContext context);
|
|
extern void AllocSetStats(MemoryContext context,
|
|
MemoryStatsPrintFunc printfunc, void *passthru,
|
|
MemoryContextCounters *totals,
|
|
bool print_to_stderr);
|
|
#ifdef MEMORY_CONTEXT_CHECKING
|
|
extern void AllocSetCheck(MemoryContext context);
|
|
#endif
|
|
|
|
/* These functions implement the MemoryContext API for Generation context. */
|
|
extern void *GenerationAlloc(MemoryContext context, Size size);
|
|
extern void GenerationFree(void *pointer);
|
|
extern void *GenerationRealloc(void *pointer, Size size);
|
|
extern void GenerationReset(MemoryContext context);
|
|
extern void GenerationDelete(MemoryContext context);
|
|
extern MemoryContext GenerationGetChunkContext(void *pointer);
|
|
extern Size GenerationGetChunkSpace(void *pointer);
|
|
extern bool GenerationIsEmpty(MemoryContext context);
|
|
extern void GenerationStats(MemoryContext context,
|
|
MemoryStatsPrintFunc printfunc, void *passthru,
|
|
MemoryContextCounters *totals,
|
|
bool print_to_stderr);
|
|
#ifdef MEMORY_CONTEXT_CHECKING
|
|
extern void GenerationCheck(MemoryContext context);
|
|
#endif
|
|
|
|
|
|
/* These functions implement the MemoryContext API for Slab context. */
|
|
extern void *SlabAlloc(MemoryContext context, Size size);
|
|
extern void SlabFree(void *pointer);
|
|
extern void *SlabRealloc(void *pointer, Size size);
|
|
extern void SlabReset(MemoryContext context);
|
|
extern void SlabDelete(MemoryContext context);
|
|
extern MemoryContext SlabGetChunkContext(void *pointer);
|
|
extern Size SlabGetChunkSpace(void *pointer);
|
|
extern bool SlabIsEmpty(MemoryContext context);
|
|
extern void SlabStats(MemoryContext context,
|
|
MemoryStatsPrintFunc printfunc, void *passthru,
|
|
MemoryContextCounters *totals,
|
|
bool print_to_stderr);
|
|
#ifdef MEMORY_CONTEXT_CHECKING
|
|
extern void SlabCheck(MemoryContext context);
|
|
#endif
|
|
|
|
/*
|
|
* 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
|
|
{
|
|
MCTX_ASET_ID,
|
|
MCTX_GENERATION_ID,
|
|
MCTX_SLAB_ID,
|
|
} MemoryContextMethodID;
|
|
|
|
/*
|
|
* The number of bits that 8-byte memory chunk headers can use to encode the
|
|
* MemoryContextMethodID.
|
|
*/
|
|
#define MEMORY_CONTEXT_METHODID_BITS 3
|
|
#define MEMORY_CONTEXT_METHODID_MASK \
|
|
((((uint64) 1) << MEMORY_CONTEXT_METHODID_BITS) - 1)
|
|
|
|
/*
|
|
* This routine handles the context-type-independent part of memory
|
|
* context creation. It's intended to be called from context-type-
|
|
* specific creation routines, and noplace else.
|
|
*/
|
|
extern void MemoryContextCreate(MemoryContext node,
|
|
NodeTag tag,
|
|
MemoryContextMethodID method_id,
|
|
MemoryContext parent,
|
|
const char *name);
|
|
|
|
/*
|
|
* GetMemoryChunkMethodID
|
|
* Return the MemoryContextMethodID from the uint64 chunk header which
|
|
* directly precedes 'pointer'.
|
|
*/
|
|
static inline MemoryContextMethodID
|
|
GetMemoryChunkMethodID(void *pointer)
|
|
{
|
|
uint64 header;
|
|
|
|
/*
|
|
* Try to detect bogus pointers handed to us, poorly though we can.
|
|
* Presumably, a pointer that isn't MAXALIGNED isn't pointing at an
|
|
* allocated chunk.
|
|
*/
|
|
Assert(pointer != NULL);
|
|
Assert(pointer == (void *) MAXALIGN(pointer));
|
|
|
|
header = *((uint64 *) ((char *) pointer - sizeof(uint64)));
|
|
|
|
return (MemoryContextMethodID) (header & MEMORY_CONTEXT_METHODID_MASK);
|
|
}
|
|
|
|
#endif /* MEMUTILS_INTERNAL_H */
|