PostgreSQL Source Code  git master
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-2024, 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  */
37 extern char *pg_strdup(const char *in);
38 extern void *pg_malloc(size_t size);
39 extern void *pg_malloc0(size_t size);
40 extern void *pg_malloc_extended(size_t size, int flags);
41 extern void *pg_realloc(void *ptr, size_t size);
42 extern void pg_free(void *ptr);
43 
44 /*
45  * Variants with easier notation and more type safety
46  */
47 
48 /*
49  * Allocate space for one object of type "type"
50  */
51 #define pg_malloc_object(type) ((type *) pg_malloc(sizeof(type)))
52 #define pg_malloc0_object(type) ((type *) pg_malloc0(sizeof(type)))
53 
54 /*
55  * Allocate space for "count" objects of type "type"
56  */
57 #define pg_malloc_array(type, count) ((type *) pg_malloc(sizeof(type) * (count)))
58 #define pg_malloc0_array(type, count) ((type *) pg_malloc0(sizeof(type) * (count)))
59 
60 /*
61  * Change size of allocation pointed to by "pointer" to have space for "count"
62  * objects of type "type"
63  */
64 #define pg_realloc_array(pointer, type, count) ((type *) pg_realloc(pointer, sizeof(type) * (count)))
65 
66 /* Equivalent functions, deliberately named the same as backend functions */
67 extern char *pstrdup(const char *in);
68 extern char *pnstrdup(const char *in, Size size);
69 extern void *palloc(Size size);
70 extern void *palloc0(Size size);
71 extern void *palloc_extended(Size size, int flags);
72 extern void *repalloc(void *pointer, Size size);
73 extern void pfree(void *pointer);
74 
75 #define palloc_object(type) ((type *) palloc(sizeof(type)))
76 #define palloc0_object(type) ((type *) palloc0(sizeof(type)))
77 #define palloc_array(type, count) ((type *) palloc(sizeof(type) * (count)))
78 #define palloc0_array(type, count) ((type *) palloc0(sizeof(type) * (count)))
79 #define repalloc_array(pointer, type, count) ((type *) repalloc(pointer, sizeof(type) * (count)))
80 
81 /* sprintf into a palloc'd buffer --- these are in psprintf.c */
82 extern char *psprintf(const char *fmt,...) pg_attribute_printf(1, 2);
83 extern size_t pvsnprintf(char *buf, size_t len, const char *fmt, va_list args) pg_attribute_printf(3, 0);
84 
85 #endif /* FE_MEMUTILS_H */
#define pg_attribute_printf(f, a)
Definition: c.h:206
size_t Size
Definition: c.h:584
void * pg_realloc(void *ptr, size_t size)
Definition: fe_memutils.c:65
char * pstrdup(const char *in)
Definition: mcxt.c:1696
void pfree(void *pointer)
Definition: mcxt.c:1521
void * palloc0(Size size)
Definition: mcxt.c:1347
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1541
void * pg_malloc0(size_t size)
Definition: fe_memutils.c:53
void * palloc_extended(Size size, int flags)
Definition: mcxt.c:1368
void * pg_malloc_extended(size_t size, int flags)
Definition: fe_memutils.c:59
char size_t pvsnprintf(char *buf, size_t len, const char *fmt, va_list args) pg_attribute_printf(3
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
char * psprintf(const char *fmt,...) pg_attribute_printf(1
char * pnstrdup(const char *in, Size size)
Definition: mcxt.c:1707
void pg_free(void *ptr)
Definition: fe_memutils.c:105
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
void * palloc(Size size)
Definition: mcxt.c:1317
static void const char * fmt
const void size_t len
static char * buf
Definition: pg_test_fsync.c:72
static pg_noinline void Size size
Definition: slab.c:607