PostgreSQL Source Code git master
Loading...
Searching...
No Matches
fe_memutils.h
Go to the documentation of this file.
1/*
2 * fe_memutils.h
3 * memory management support for frontend code
4 *
5 * Copyright (c) 2003-2026, PostgreSQL Global Development Group
6 *
7 * src/include/common/fe_memutils.h
8 */
9#ifndef FE_MEMUTILS_H
10#define FE_MEMUTILS_H
11
12/*
13 * Assumed maximum size for allocation requests.
14 *
15 * We don't enforce this, so the actual maximum is the platform's SIZE_MAX.
16 * But it's useful to have it defined in frontend builds, so that common
17 * code can check for oversized requests without having frontend-vs-backend
18 * differences. Also, some code relies on MaxAllocSize being no more than
19 * INT_MAX/2, so rather than setting this to SIZE_MAX, make it the same as
20 * the backend's value.
21 */
22#define MaxAllocSize ((Size) 0x3fffffff) /* 1 gigabyte - 1 */
23
24/*
25 * Flags for pg_malloc_extended and palloc_extended, deliberately named
26 * the same as the backend flags.
27 */
28#define MCXT_ALLOC_HUGE 0x01 /* allow huge allocation (> 1 GB) not
29 * actually used for frontends */
30#define MCXT_ALLOC_NO_OOM 0x02 /* no failure if out-of-memory */
31#define MCXT_ALLOC_ZERO 0x04 /* zero allocated memory */
32
33/*
34 * "Safe" memory allocation functions --- these exit(1) on failure
35 * (except pg_malloc_extended with MCXT_ALLOC_NO_OOM)
36 */
37extern char *pg_strdup(const char *in);
38extern void *pg_malloc(size_t size);
39extern void *pg_malloc0(size_t size);
40extern void *pg_malloc_extended(size_t size, int flags);
41extern void *pg_realloc(void *ptr, size_t size);
42extern void pg_free(void *ptr);
43
44/*
45 * Support for safe calculation of memory request sizes
46 */
47extern Size add_size(Size s1, Size s2);
48extern Size mul_size(Size s1, Size s2);
49extern void *pg_malloc_mul(Size s1, Size s2);
50extern void *pg_malloc0_mul(Size s1, Size s2);
51extern void *pg_malloc_mul_extended(Size s1, Size s2, int flags);
52extern void *pg_realloc_mul(void *p, Size s1, Size s2);
53
54/*
55 * Variants with easier notation and more type safety
56 */
57
58/*
59 * Allocate space for one object of type "type"
60 */
61#define pg_malloc_object(type) ((type *) pg_malloc(sizeof(type)))
62#define pg_malloc0_object(type) ((type *) pg_malloc0(sizeof(type)))
63
64/*
65 * Allocate space for "count" objects of type "type"
66 */
67#define pg_malloc_array(type, count) ((type *) pg_malloc_mul(sizeof(type), count))
68#define pg_malloc0_array(type, count) ((type *) pg_malloc0_mul(sizeof(type), count))
69#define pg_malloc_array_extended(type, count, flags) ((type *) pg_malloc_mul_extended(sizeof(type), count, flags))
70
71/*
72 * Change size of allocation pointed to by "pointer" to have space for "count"
73 * objects of type "type"
74 */
75#define pg_realloc_array(pointer, type, count) ((type *) pg_realloc_mul(pointer, sizeof(type), count))
76
77/* Equivalent functions, deliberately named the same as backend functions */
78extern char *pstrdup(const char *in);
79extern char *pnstrdup(const char *in, Size size);
80extern void *palloc(Size size);
81extern void *palloc0(Size size);
82extern void *palloc_extended(Size size, int flags);
83extern void *repalloc(void *pointer, Size size);
84extern void pfree(void *pointer);
85extern void *palloc_mul(Size s1, Size s2);
86extern void *palloc0_mul(Size s1, Size s2);
87extern void *palloc_mul_extended(Size s1, Size s2, int flags);
88extern void *repalloc_mul(void *p, Size s1, Size s2);
90#define palloc_object(type) ((type *) palloc(sizeof(type)))
91#define palloc0_object(type) ((type *) palloc0(sizeof(type)))
92#define palloc_array(type, count) ((type *) palloc_mul(sizeof(type), count))
93#define palloc0_array(type, count) ((type *) palloc0_mul(sizeof(type), count))
94#define palloc_array_extended(type, count, flags) ((type *) palloc_mul_extended(sizeof(type), count, flags))
95#define repalloc_array(pointer, type, count) ((type *) repalloc_mul(pointer, sizeof(type), count))
96
97/* sprintf into a palloc'd buffer --- these are in psprintf.c */
98extern char *psprintf(const char *fmt, ...) pg_attribute_printf(1, 2);
99extern size_t pvsnprintf(char *buf, size_t len, const char *fmt, va_list args) pg_attribute_printf(3, 0);
100
101#endif /* FE_MEMUTILS_H */
#define pg_attribute_printf(f, a)
Definition c.h:268
size_t Size
Definition c.h:689
void * pg_malloc_mul_extended(Size s1, Size s2, int flags)
void * palloc_mul_extended(Size s1, Size s2, int flags)
Definition mcxt.c:1807
Size add_size(Size s1, Size s2)
Definition mcxt.c:1733
char * pstrdup(const char *in)
Definition mcxt.c:1910
char * psprintf(const char *fmt,...) pg_attribute_printf(1
void * repalloc(void *pointer, Size size)
Definition mcxt.c:1635
void pfree(void *pointer)
Definition mcxt.c:1619
void * palloc0(Size size)
Definition mcxt.c:1420
void * pg_malloc(size_t size)
Definition fe_memutils.c:53
Size mul_size(Size s1, Size s2)
Definition mcxt.c:1752
void * palloc(Size size)
Definition mcxt.c:1390
void * palloc0_mul(Size s1, Size s2)
Definition mcxt.c:1792
void * palloc_extended(Size size, int flags)
Definition mcxt.c:1442
void * pg_malloc_extended(size_t size, int flags)
Definition fe_memutils.c:65
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
char * pg_strdup(const char *in)
Definition fe_memutils.c:91
void * pg_malloc0_mul(Size s1, Size s2)
void * repalloc_mul(void *p, Size s1, Size s2)
Definition mcxt.c:1822
void * pg_realloc_mul(void *p, Size s1, Size s2)
void * pg_malloc0(size_t size)
Definition fe_memutils.c:59
void pg_free(void *ptr)
void * pg_malloc_mul(Size s1, Size s2)
char * pnstrdup(const char *in, Size size)
Definition mcxt.c:1921
void * pg_realloc(void *ptr, size_t size)
Definition fe_memutils.c:71
const void size_t len
static char buf[DEFAULT_XLOG_SEG_SIZE]
static int fb(int x)
char * s1
char * s2