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  * Flags for pg_malloc_extended and palloc_extended, deliberately named
14  * the same as the backend flags.
15  */
16 #define MCXT_ALLOC_HUGE 0x01 /* allow huge allocation (> 1 GB) not
17  * actually used for frontends */
18 #define MCXT_ALLOC_NO_OOM 0x02 /* no failure if out-of-memory */
19 #define MCXT_ALLOC_ZERO 0x04 /* zero allocated memory */
20 
21 /*
22  * "Safe" memory allocation functions --- these exit(1) on failure
23  * (except pg_malloc_extended with MCXT_ALLOC_NO_OOM)
24  */
25 extern char *pg_strdup(const char *in);
26 extern void *pg_malloc(size_t size);
27 extern void *pg_malloc0(size_t size);
28 extern void *pg_malloc_extended(size_t size, int flags);
29 extern void *pg_realloc(void *ptr, size_t size);
30 extern void pg_free(void *ptr);
31 
32 /*
33  * Variants with easier notation and more type safety
34  */
35 
36 /*
37  * Allocate space for one object of type "type"
38  */
39 #define pg_malloc_object(type) ((type *) pg_malloc(sizeof(type)))
40 #define pg_malloc0_object(type) ((type *) pg_malloc0(sizeof(type)))
41 
42 /*
43  * Allocate space for "count" objects of type "type"
44  */
45 #define pg_malloc_array(type, count) ((type *) pg_malloc(sizeof(type) * (count)))
46 #define pg_malloc0_array(type, count) ((type *) pg_malloc0(sizeof(type) * (count)))
47 
48 /*
49  * Change size of allocation pointed to by "pointer" to have space for "count"
50  * objects of type "type"
51  */
52 #define pg_realloc_array(pointer, type, count) ((type *) pg_realloc(pointer, sizeof(type) * (count)))
53 
54 /* Equivalent functions, deliberately named the same as backend functions */
55 extern char *pstrdup(const char *in);
56 extern char *pnstrdup(const char *in, Size size);
57 extern void *palloc(Size size);
58 extern void *palloc0(Size size);
59 extern void *palloc_extended(Size size, int flags);
60 extern void *repalloc(void *pointer, Size size);
61 extern void pfree(void *pointer);
62 
63 #define palloc_object(type) ((type *) palloc(sizeof(type)))
64 #define palloc0_object(type) ((type *) palloc0(sizeof(type)))
65 #define palloc_array(type, count) ((type *) palloc(sizeof(type) * (count)))
66 #define palloc0_array(type, count) ((type *) palloc0(sizeof(type) * (count)))
67 #define repalloc_array(pointer, type, count) ((type *) repalloc(pointer, sizeof(type) * (count)))
68 
69 /* sprintf into a palloc'd buffer --- these are in psprintf.c */
70 extern char *psprintf(const char *fmt,...) pg_attribute_printf(1, 2);
71 extern size_t pvsnprintf(char *buf, size_t len, const char *fmt, va_list args) pg_attribute_printf(3, 0);
72 
73 #endif /* FE_MEMUTILS_H */
#define pg_attribute_printf(f, a)
Definition: c.h:178
size_t Size
Definition: c.h:592
void * pg_realloc(void *ptr, size_t size)
Definition: fe_memutils.c:65
char * pstrdup(const char *in)
Definition: mcxt.c:1683
void pfree(void *pointer)
Definition: mcxt.c:1508
void * palloc0(Size size)
Definition: mcxt.c:1334
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1528
void * pg_malloc0(size_t size)
Definition: fe_memutils.c:53
void * palloc_extended(Size size, int flags)
Definition: mcxt.c:1355
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:1694
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:1304
static void const char * fmt
const void size_t len
static char * buf
Definition: pg_test_fsync.c:73
static pg_noinline void Size size
Definition: slab.c:607