PostgreSQL Source Code git master
memutils_internal.h
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * memutils_internal.h
4 * This file contains declarations for memory allocation utility
5 * functions for internal use.
6 *
7 *
8 * Portions Copyright (c) 2022-2025, PostgreSQL Global Development Group
9 * Portions Copyright (c) 1994, Regents of the University of California
10 *
11 * src/include/utils/memutils_internal.h
12 *
13 *-------------------------------------------------------------------------
14 */
15
16#ifndef MEMUTILS_INTERNAL_H
17#define MEMUTILS_INTERNAL_H
18
19#include "utils/memutils.h"
20
21/* These functions implement the MemoryContext API for AllocSet context. */
22extern void *AllocSetAlloc(MemoryContext context, Size size, int flags);
23extern void AllocSetFree(void *pointer);
24extern void *AllocSetRealloc(void *pointer, Size size, int flags);
25extern void AllocSetReset(MemoryContext context);
26extern void AllocSetDelete(MemoryContext context);
27extern MemoryContext AllocSetGetChunkContext(void *pointer);
28extern Size AllocSetGetChunkSpace(void *pointer);
29extern bool AllocSetIsEmpty(MemoryContext context);
30extern void AllocSetStats(MemoryContext context,
31 MemoryStatsPrintFunc printfunc, void *passthru,
33 bool print_to_stderr);
34#ifdef MEMORY_CONTEXT_CHECKING
35extern void AllocSetCheck(MemoryContext context);
36#endif
37
38/* These functions implement the MemoryContext API for Generation context. */
39extern void *GenerationAlloc(MemoryContext context, Size size, int flags);
40extern void GenerationFree(void *pointer);
41extern void *GenerationRealloc(void *pointer, Size size, int flags);
42extern void GenerationReset(MemoryContext context);
43extern void GenerationDelete(MemoryContext context);
44extern MemoryContext GenerationGetChunkContext(void *pointer);
45extern Size GenerationGetChunkSpace(void *pointer);
46extern bool GenerationIsEmpty(MemoryContext context);
47extern void GenerationStats(MemoryContext context,
48 MemoryStatsPrintFunc printfunc, void *passthru,
50 bool print_to_stderr);
51#ifdef MEMORY_CONTEXT_CHECKING
52extern void GenerationCheck(MemoryContext context);
53#endif
54
55
56/* These functions implement the MemoryContext API for Slab context. */
57extern void *SlabAlloc(MemoryContext context, Size size, int flags);
58extern void SlabFree(void *pointer);
59extern void *SlabRealloc(void *pointer, Size size, int flags);
60extern void SlabReset(MemoryContext context);
61extern void SlabDelete(MemoryContext context);
62extern MemoryContext SlabGetChunkContext(void *pointer);
63extern Size SlabGetChunkSpace(void *pointer);
64extern bool SlabIsEmpty(MemoryContext context);
65extern void SlabStats(MemoryContext context,
66 MemoryStatsPrintFunc printfunc, void *passthru,
68 bool print_to_stderr);
69#ifdef MEMORY_CONTEXT_CHECKING
70extern void SlabCheck(MemoryContext context);
71#endif
72
73/*
74 * These functions support the implementation of palloc_aligned() and are not
75 * part of a fully-fledged MemoryContext type.
76 */
77extern void AlignedAllocFree(void *pointer);
78extern void *AlignedAllocRealloc(void *pointer, Size size, int flags);
79extern MemoryContext AlignedAllocGetChunkContext(void *pointer);
80extern Size AlignedAllocGetChunkSpace(void *pointer);
81
82 /* These functions implement the MemoryContext API for the Bump context. */
83extern void *BumpAlloc(MemoryContext context, Size size, int flags);
84extern void BumpFree(void *pointer);
85extern void *BumpRealloc(void *pointer, Size size, int flags);
86extern void BumpReset(MemoryContext context);
87extern void BumpDelete(MemoryContext context);
88extern MemoryContext BumpGetChunkContext(void *pointer);
89extern Size BumpGetChunkSpace(void *pointer);
90extern bool BumpIsEmpty(MemoryContext context);
91extern void BumpStats(MemoryContext context, MemoryStatsPrintFunc printfunc,
92 void *passthru, MemoryContextCounters *totals,
93 bool print_to_stderr);
94#ifdef MEMORY_CONTEXT_CHECKING
95extern void BumpCheck(MemoryContext context);
96#endif
97
98/*
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.
103 */
104#define PallocAlignedExtraBytes(alignto) \
105 ((alignto) + (sizeof(MemoryChunk) - MAXIMUM_ALIGNOF))
106
107/*
108 * MemoryContextMethodID
109 * A unique identifier for each MemoryContext implementation which
110 * indicates the index into the mcxt_methods[] array. See mcxt.c.
111 *
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.)
120 */
122{
123 MCTX_0_RESERVED_UNUSEDMEM_ID, /* 0000 occurs in never-used memory */
124 MCTX_1_RESERVED_GLIBC_ID, /* glibc malloc'd chunks usually match 0001 */
125 MCTX_2_RESERVED_GLIBC_ID, /* glibc malloc'd chunks > 128kB match 0010 */
138 MCTX_15_RESERVED_WIPEDMEM_ID /* 1111 occurs in wipe_mem'd memory */
140
141/*
142 * The number of bits that 8-byte memory chunk headers can use to encode the
143 * MemoryContextMethodID.
144 */
145#define MEMORY_CONTEXT_METHODID_BITS 4
146#define MEMORY_CONTEXT_METHODID_MASK \
147 ((((uint64) 1) << MEMORY_CONTEXT_METHODID_BITS) - 1)
148
149/*
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.
153 */
154extern void MemoryContextCreate(MemoryContext node,
155 NodeTag tag,
156 MemoryContextMethodID method_id,
157 MemoryContext parent,
158 const char *name);
159
161 int flags);
162
164 int flags) pg_attribute_noreturn();
165
166static inline void
168{
170 {
171 if (!(flags & MCXT_ALLOC_HUGE) || !AllocHugeSizeIsValid(size))
172 MemoryContextSizeFailure(context, size, flags);
173 }
174}
175
176#endif /* MEMUTILS_INTERNAL_H */
#define pg_attribute_noreturn()
Definition: c.h:239
#define unlikely(x)
Definition: c.h:333
size_t Size
Definition: c.h:562
#define MCXT_ALLOC_HUGE
Definition: fe_memutils.h:28
void(* MemoryStatsPrintFunc)(MemoryContext context, void *passthru, const char *stats_string, bool print_to_stderr)
Definition: memnodes.h:54
#define AllocHugeSizeIsValid(size)
Definition: memutils.h:49
#define AllocSizeIsValid(size)
Definition: memutils.h:42
void * GenerationRealloc(void *pointer, Size size, int flags)
Definition: generation.c:800
void AllocSetReset(MemoryContext context)
Definition: aset.c:537
void MemoryContextSizeFailure(MemoryContext context, Size size, int flags) pg_attribute_noreturn()
Definition: mcxt.c:1168
void * SlabAlloc(MemoryContext context, Size size, int flags)
Definition: slab.c:630
static void MemoryContextCheckSize(MemoryContext context, Size size, int flags)
void BumpFree(void *pointer)
Definition: bump.c:617
MemoryContext AlignedAllocGetChunkContext(void *pointer)
Definition: alignedalloc.c:121
void * AllocSetRealloc(void *pointer, Size size, int flags)
Definition: aset.c:1169
void BumpDelete(MemoryContext context)
Definition: bump.c:278
Size BumpGetChunkSpace(void *pointer)
Definition: bump.c:649
void BumpStats(MemoryContext context, MemoryStatsPrintFunc printfunc, void *passthru, MemoryContextCounters *totals, bool print_to_stderr)
Definition: bump.c:688
void MemoryContextCreate(MemoryContext node, NodeTag tag, MemoryContextMethodID method_id, MemoryContext parent, const char *name)
Definition: mcxt.c:1100
Size AllocSetGetChunkSpace(void *pointer)
Definition: aset.c:1462
void GenerationReset(MemoryContext context)
Definition: generation.c:283
MemoryContext AllocSetGetChunkContext(void *pointer)
Definition: aset.c:1433
void AllocSetStats(MemoryContext context, MemoryStatsPrintFunc printfunc, void *passthru, MemoryContextCounters *totals, bool print_to_stderr)
Definition: aset.c:1521
void GenerationFree(void *pointer)
Definition: generation.c:689
MemoryContext GenerationGetChunkContext(void *pointer)
Definition: generation.c:947
Size GenerationGetChunkSpace(void *pointer)
Definition: generation.c:973
void * AlignedAllocRealloc(void *pointer, Size size, int flags)
Definition: alignedalloc.c:60
MemoryContext BumpGetChunkContext(void *pointer)
Definition: bump.c:638
void SlabFree(void *pointer)
Definition: slab.c:701
void SlabReset(MemoryContext context)
Definition: slab.c:431
bool AllocSetIsEmpty(MemoryContext context)
Definition: aset.c:1496
Size SlabGetChunkSpace(void *pointer)
Definition: slab.c:887
void * MemoryContextAllocationFailure(MemoryContext context, Size size, int flags)
Definition: mcxt.c:1147
void * AllocSetAlloc(MemoryContext context, Size size, int flags)
Definition: aset.c:967
void BumpReset(MemoryContext context)
Definition: bump.c:243
bool GenerationIsEmpty(MemoryContext context)
Definition: generation.c:1002
void GenerationStats(MemoryContext context, MemoryStatsPrintFunc printfunc, void *passthru, MemoryContextCounters *totals, bool print_to_stderr)
Definition: generation.c:1033
void AllocSetFree(void *pointer)
Definition: aset.c:1062
bool SlabIsEmpty(MemoryContext context)
Definition: slab.c:912
MemoryContext SlabGetChunkContext(void *pointer)
Definition: slab.c:863
void AllocSetDelete(MemoryContext context)
Definition: aset.c:607
bool BumpIsEmpty(MemoryContext context)
Definition: bump.c:660
void * SlabRealloc(void *pointer, Size size, int flags)
Definition: slab.c:826
void SlabStats(MemoryContext context, MemoryStatsPrintFunc printfunc, void *passthru, MemoryContextCounters *totals, bool print_to_stderr)
Definition: slab.c:929
MemoryContextMethodID
@ MCTX_15_RESERVED_WIPEDMEM_ID
@ MCTX_GENERATION_ID
@ MCTX_14_UNUSED_ID
@ MCTX_12_UNUSED_ID
@ MCTX_10_UNUSED_ID
@ MCTX_BUMP_ID
@ MCTX_11_UNUSED_ID
@ MCTX_8_UNUSED_ID
@ MCTX_1_RESERVED_GLIBC_ID
@ MCTX_SLAB_ID
@ MCTX_9_UNUSED_ID
@ MCTX_0_RESERVED_UNUSEDMEM_ID
@ MCTX_ASET_ID
@ MCTX_2_RESERVED_GLIBC_ID
@ MCTX_ALIGNED_REDIRECT_ID
@ MCTX_13_UNUSED_ID
void * BumpRealloc(void *pointer, Size size, int flags)
Definition: bump.c:627
void SlabDelete(MemoryContext context)
Definition: slab.c:485
Size AlignedAllocGetChunkSpace(void *pointer)
Definition: alignedalloc.c:143
void GenerationDelete(MemoryContext context)
Definition: generation.c:328
void * GenerationAlloc(MemoryContext context, Size size, int flags)
Definition: generation.c:527
void AlignedAllocFree(void *pointer)
Definition: alignedalloc.c:29
void * BumpAlloc(MemoryContext context, Size size, int flags)
Definition: bump.c:491
NodeTag
Definition: nodes.h:27
static pg_noinline void Size size
Definition: slab.c:607
const char * name