PostgreSQL Source Code git master
Loading...
Searching...
No Matches
palloc.h
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * palloc.h
4 * POSTGRES memory allocator definitions.
5 *
6 * This file contains the basic memory allocation interface that is
7 * needed by almost every backend module. It is included directly by
8 * postgres.h, so the definitions here are automatically available
9 * everywhere. Keep it lean!
10 *
11 * Memory allocation occurs within "contexts". Every chunk obtained from
12 * palloc()/MemoryContextAlloc() is allocated within a specific context.
13 * The entire contents of a context can be freed easily and quickly by
14 * resetting or deleting the context --- this is both faster and less
15 * prone to memory-leakage bugs than releasing chunks individually.
16 * We organize contexts into context trees to allow fine-grain control
17 * over chunk lifetime while preserving the certainty that we will free
18 * everything that should be freed. See utils/mmgr/README for more info.
19 *
20 *
21 * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
22 * Portions Copyright (c) 1994, Regents of the University of California
23 *
24 * src/include/utils/palloc.h
25 *
26 *-------------------------------------------------------------------------
27 */
28#ifndef PALLOC_H
29#define PALLOC_H
30
31/*
32 * Type MemoryContextData is declared in nodes/memnodes.h. Most users
33 * of memory allocation should just treat it as an abstract type, so we
34 * do not provide the struct contents here.
35 */
37
38/*
39 * A memory context can have callback functions registered on it. Any such
40 * function will be called once just before the context is next reset or
41 * deleted. The MemoryContextCallback struct describing such a callback
42 * typically would be allocated within the context itself, thereby avoiding
43 * any need to manage it explicitly (the reset/delete action will free it).
44 */
46
48{
49 MemoryContextCallbackFunction func; /* function to call */
50 void *arg; /* argument to pass it */
51 struct MemoryContextCallback *next; /* next in list of callbacks */
53
54/*
55 * CurrentMemoryContext is the default allocation context for palloc().
56 * Avoid accessing it directly! Instead, use MemoryContextSwitchTo()
57 * to change the setting.
58 */
60
61/*
62 * Flags for MemoryContextAllocExtended.
63 */
64#define MCXT_ALLOC_HUGE 0x01 /* allow huge allocation (> 1 GB) */
65#define MCXT_ALLOC_NO_OOM 0x02 /* no failure if out-of-memory */
66#define MCXT_ALLOC_ZERO 0x04 /* zero allocated memory */
67
68/*
69 * Fundamental memory-allocation operations (more are in utils/memutils.h)
70 */
71extern void *MemoryContextAlloc(MemoryContext context, Size size);
72extern void *MemoryContextAllocZero(MemoryContext context, Size size);
73extern void *MemoryContextAllocExtended(MemoryContext context,
74 Size size, int flags);
75extern void *MemoryContextAllocAligned(MemoryContext context,
76 Size size, Size alignto, int flags);
77
78extern void *palloc(Size size);
79extern void *palloc0(Size size);
80extern void *palloc_extended(Size size, int flags);
81extern void *palloc_aligned(Size size, Size alignto, int flags);
82pg_nodiscard extern void *repalloc(void *pointer, Size size);
83pg_nodiscard extern void *repalloc_extended(void *pointer,
84 Size size, int flags);
85pg_nodiscard extern void *repalloc0(void *pointer, Size oldsize, Size size);
86extern void pfree(void *pointer);
87
88/*
89 * Support for safe calculation of memory request sizes
90 */
91extern Size add_size(Size s1, Size s2);
92extern Size mul_size(Size s1, Size s2);
93extern void *palloc_mul(Size s1, Size s2);
94extern void *palloc0_mul(Size s1, Size s2);
95extern void *palloc_mul_extended(Size s1, Size s2, int flags);
96pg_nodiscard extern void *repalloc_mul(void *p, Size s1, Size s2);
97pg_nodiscard extern void *repalloc_mul_extended(void *p, Size s1, Size s2,
98 int flags);
99
100/*
101 * Variants with easier notation and more type safety
102 */
103
104/*
105 * Allocate space for one object of type "type"
106 */
107#define palloc_object(type) ((type *) palloc(sizeof(type)))
108#define palloc0_object(type) ((type *) palloc0(sizeof(type)))
109
110/*
111 * Allocate space for "count" objects of type "type"
112 */
113#define palloc_array(type, count) ((type *) palloc_mul(sizeof(type), count))
114#define palloc0_array(type, count) ((type *) palloc0_mul(sizeof(type), count))
115#define palloc_array_extended(type, count, flags) ((type *) palloc_mul_extended(sizeof(type), count, flags))
116
117/*
118 * Change size of allocation pointed to by "pointer" to have space for "count"
119 * objects of type "type"
120 */
121#define repalloc_array(pointer, type, count) ((type *) repalloc_mul(pointer, sizeof(type), count))
122#define repalloc0_array(pointer, type, oldcount, count) ((type *) repalloc0(pointer, mul_size(sizeof(type), oldcount), mul_size(sizeof(type), count)))
123#define repalloc_array_extended(pointer, type, count, flags) ((type *) repalloc_mul_extended(pointer, sizeof(type), count, flags))
124
125/* Higher-limit allocators. */
126extern void *MemoryContextAllocHuge(MemoryContext context, Size size);
127pg_nodiscard extern void *repalloc_huge(void *pointer, Size size);
128
129/*
130 * Although this header file is nominally backend-only, certain frontend
131 * programs like pg_controldata include it via postgres.h. For some compilers
132 * it's necessary to hide the inline definition of MemoryContextSwitchTo in
133 * this scenario; hence the #ifndef FRONTEND.
134 */
135
136#ifndef FRONTEND
137static inline MemoryContext
145#endif /* FRONTEND */
146
147/* Registration of memory context reset/delete callbacks */
152
153/*
154 * These are like standard strdup() except the copied string is
155 * allocated in a context, not with malloc().
156 */
157extern char *MemoryContextStrdup(MemoryContext context, const char *string);
158extern char *pstrdup(const char *in);
159extern char *pnstrdup(const char *in, Size len);
160
161extern char *pchomp(const char *in);
162
163/* sprintf into a palloc'd buffer --- these are in psprintf.c */
164extern char *psprintf(const char *fmt, ...) pg_attribute_printf(1, 2);
165extern size_t pvsnprintf(char *buf, size_t len, const char *fmt, va_list args) pg_attribute_printf(3, 0);
166
167#endif /* PALLOC_H */
#define PGDLLIMPORT
Definition c.h:1421
#define pg_nodiscard
Definition c.h:173
#define pg_attribute_printf(f, a)
Definition c.h:268
size_t Size
Definition c.h:689
Datum arg
Definition elog.c:1323
void MemoryContextUnregisterResetCallback(MemoryContext context, MemoryContextCallback *cb)
Definition mcxt.c:610
char * MemoryContextStrdup(MemoryContext context, const char *string)
Definition mcxt.c:1897
void * palloc_mul_extended(Size s1, Size s2, int flags)
Definition mcxt.c:1807
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition mcxt.c:1235
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition mcxt.c:1269
Size add_size(Size s1, Size s2)
Definition mcxt.c:1733
char * pstrdup(const char *in)
Definition mcxt.c:1910
void MemoryContextRegisterResetCallback(MemoryContext context, MemoryContextCallback *cb)
Definition mcxt.c:585
char * psprintf(const char *fmt,...) pg_attribute_printf(1
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition palloc.h:138
pg_nodiscard void * repalloc_huge(void *pointer, Size size)
Definition mcxt.c:1886
void pfree(void *pointer)
Definition mcxt.c:1619
pg_nodiscard void * repalloc_mul(void *p, Size s1, Size s2)
Definition mcxt.c:1822
void * palloc0(Size size)
Definition mcxt.c:1420
void * MemoryContextAllocAligned(MemoryContext context, Size size, Size alignto, int flags)
Definition mcxt.c:1485
void(* MemoryContextCallbackFunction)(void *arg)
Definition palloc.h:45
char * pchomp(const char *in)
Definition mcxt.c:1938
Size mul_size(Size s1, Size s2)
Definition mcxt.c:1752
void * palloc(Size size)
Definition mcxt.c:1390
struct MemoryContextData * MemoryContext
Definition palloc.h:36
void * MemoryContextAllocExtended(MemoryContext context, Size size, int flags)
Definition mcxt.c:1292
void * palloc0_mul(Size s1, Size s2)
Definition mcxt.c:1792
char * pnstrdup(const char *in, Size len)
Definition mcxt.c:1921
void * palloc_extended(Size size, int flags)
Definition mcxt.c:1442
pg_nodiscard void * repalloc_mul_extended(void *p, Size s1, Size s2, int flags)
Definition mcxt.c:1837
pg_nodiscard void * repalloc_extended(void *pointer, Size size, int flags)
Definition mcxt.c:1670
char size_t pvsnprintf(char *buf, size_t len, const char *fmt, va_list args) pg_attribute_printf(3
void * palloc_mul(Size s1, Size s2)
Definition mcxt.c:1775
pg_nodiscard void * repalloc0(void *pointer, Size oldsize, Size size)
Definition mcxt.c:1707
pg_nodiscard void * repalloc(void *pointer, Size size)
Definition mcxt.c:1635
void * MemoryContextAllocHuge(MemoryContext context, Size size)
Definition mcxt.c:1854
PGDLLIMPORT MemoryContext CurrentMemoryContext
Definition mcxt.c:161
void * palloc_aligned(Size size, Size alignto, int flags)
Definition mcxt.c:1609
const void size_t len
static char buf[DEFAULT_XLOG_SEG_SIZE]
static int fb(int x)
char * s1
char * s2
struct MemoryContextCallback * next
Definition palloc.h:51
MemoryContextCallbackFunction func
Definition palloc.h:49