1/*-------------------------------------------------------------------------
4 * This file contains declarations for memory allocation utility
5 * functions for internal use.
8 * Portions Copyright (c) 2022-2025, PostgreSQL Global Development Group
9 * Portions Copyright (c) 1994, Regents of the University of California
11 * src/include/utils/memutils_internal.h
13 *-------------------------------------------------------------------------
16#ifndef MEMUTILS_INTERNAL_H
17#define MEMUTILS_INTERNAL_H
21/* These functions implement the MemoryContext API for AllocSet context. */
33 bool print_to_stderr);
34#ifdef MEMORY_CONTEXT_CHECKING
38/* These functions implement the MemoryContext API for Generation context. */
50 bool print_to_stderr);
51#ifdef MEMORY_CONTEXT_CHECKING
56/* These functions implement the MemoryContext API for Slab context. */
68 bool print_to_stderr);
69#ifdef MEMORY_CONTEXT_CHECKING
74 * These functions support the implementation of palloc_aligned() and are not
75 * part of a fully-fledged MemoryContext type.
82 /* These functions implement the MemoryContext API for the Bump context. */
93 bool print_to_stderr);
94#ifdef MEMORY_CONTEXT_CHECKING
99 * How many extra bytes do we need to request in order to ensure that we can
100 * align a pointer to 'alignto'. Since palloc'd pointers are already aligned
101 * to MAXIMUM_ALIGNOF we can subtract that amount. We also need to make sure
102 * there is enough space for the redirection MemoryChunk.
104 #define PallocAlignedExtraBytes(alignto) \
105 ((alignto) + (sizeof(MemoryChunk) - MAXIMUM_ALIGNOF))
108 * MemoryContextMethodID
109 * A unique identifier for each MemoryContext implementation which
110 * indicates the index into the mcxt_methods[] array. See mcxt.c.
112 * For robust error detection, ensure that MemoryContextMethodID has a value
113 * for each possible bit-pattern of MEMORY_CONTEXT_METHODID_MASK, and make
114 * dummy entries for unused IDs in the mcxt_methods[] array. We also try
115 * to avoid using bit-patterns as valid IDs if they are likely to occur in
116 * garbage data, or if they could falsely match on chunks that are really from
117 * malloc not palloc. (We can't tell that for most malloc implementations,
118 * but it happens that glibc stores flag bits in the same place where we put
119 * the MemoryContextMethodID, so the possible values are predictable for it.)
142 * The number of bits that 8-byte memory chunk headers can use to encode the
143 * MemoryContextMethodID.
145 #define MEMORY_CONTEXT_METHODID_BITS 4
146 #define MEMORY_CONTEXT_METHODID_MASK \
147 ((((uint64) 1) << MEMORY_CONTEXT_METHODID_BITS) - 1)
150 * This routine handles the context-type-independent part of memory
151 * context creation. It's intended to be called from context-type-
152 * specific creation routines, and noplace else.
176#endif /* MEMUTILS_INTERNAL_H */
void(* MemoryStatsPrintFunc)(MemoryContext context, void *passthru, const char *stats_string, bool print_to_stderr)
#define AllocHugeSizeIsValid(size)
#define AllocSizeIsValid(size)
void * GenerationRealloc(void *pointer, Size size, int flags)
void AllocSetReset(MemoryContext context)
void * SlabAlloc(MemoryContext context, Size size, int flags)
static void MemoryContextCheckSize(MemoryContext context, Size size, int flags)
void BumpFree(void *pointer)
MemoryContext AlignedAllocGetChunkContext(void *pointer)
void * AllocSetRealloc(void *pointer, Size size, int flags)
void BumpDelete(MemoryContext context)
Size BumpGetChunkSpace(void *pointer)
void BumpStats(MemoryContext context, MemoryStatsPrintFunc printfunc, void *passthru, MemoryContextCounters *totals, bool print_to_stderr)
void MemoryContextCreate(MemoryContext node, NodeTag tag, MemoryContextMethodID method_id, MemoryContext parent, const char *name)
Size AllocSetGetChunkSpace(void *pointer)
void GenerationReset(MemoryContext context)
MemoryContext AllocSetGetChunkContext(void *pointer)
void AllocSetStats(MemoryContext context, MemoryStatsPrintFunc printfunc, void *passthru, MemoryContextCounters *totals, bool print_to_stderr)
void GenerationFree(void *pointer)
MemoryContext GenerationGetChunkContext(void *pointer)
pg_noreturn void MemoryContextSizeFailure(MemoryContext context, Size size, int flags)
Size GenerationGetChunkSpace(void *pointer)
void * AlignedAllocRealloc(void *pointer, Size size, int flags)
MemoryContext BumpGetChunkContext(void *pointer)
void SlabFree(void *pointer)
void SlabReset(MemoryContext context)
bool AllocSetIsEmpty(MemoryContext context)
Size SlabGetChunkSpace(void *pointer)
void * MemoryContextAllocationFailure(MemoryContext context, Size size, int flags)
void * AllocSetAlloc(MemoryContext context, Size size, int flags)
void BumpReset(MemoryContext context)
bool GenerationIsEmpty(MemoryContext context)
void GenerationStats(MemoryContext context, MemoryStatsPrintFunc printfunc, void *passthru, MemoryContextCounters *totals, bool print_to_stderr)
void AllocSetFree(void *pointer)
bool SlabIsEmpty(MemoryContext context)
MemoryContext SlabGetChunkContext(void *pointer)
void AllocSetDelete(MemoryContext context)
bool BumpIsEmpty(MemoryContext context)
void * SlabRealloc(void *pointer, Size size, int flags)
void SlabStats(MemoryContext context, MemoryStatsPrintFunc printfunc, void *passthru, MemoryContextCounters *totals, bool print_to_stderr)
@ MCTX_15_RESERVED_WIPEDMEM_ID
@ MCTX_1_RESERVED_GLIBC_ID
@ MCTX_0_RESERVED_UNUSEDMEM_ID
@ MCTX_2_RESERVED_GLIBC_ID
@ MCTX_ALIGNED_REDIRECT_ID
void * BumpRealloc(void *pointer, Size size, int flags)
void SlabDelete(MemoryContext context)
Size AlignedAllocGetChunkSpace(void *pointer)
void GenerationDelete(MemoryContext context)
void * GenerationAlloc(MemoryContext context, Size size, int flags)
void AlignedAllocFree(void *pointer)
void * BumpAlloc(MemoryContext context, Size size, int flags)