3 * memory management support for frontend code
5 * Copyright (c) 2003-2025, PostgreSQL Global Development Group
7 * src/include/common/fe_memutils.h
13 * Assumed maximum size for allocation requests.
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.
22 #define MaxAllocSize ((Size) 0x3fffffff) /* 1 gigabyte - 1 */
25 * Flags for pg_malloc_extended and palloc_extended, deliberately named
26 * the same as the backend flags.
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 */
34 * "Safe" memory allocation functions --- these exit(1) on failure
35 * (except pg_malloc_extended with MCXT_ALLOC_NO_OOM)
41extern void *
pg_realloc(
void *ptr,
size_t size);
45 * Variants with easier notation and more type safety
49 * Allocate space for one object of type "type"
51 #define pg_malloc_object(type) ((type *) pg_malloc(sizeof(type)))
52#define pg_malloc0_object(type) ((type *) pg_malloc0(sizeof(type)))
55 * Allocate space for "count" objects of type "type"
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)))
61 * Change size of allocation pointed to by "pointer" to have space for "count"
62 * objects of type "type"
64#define pg_realloc_array(pointer, type, count) ((type *) pg_realloc(pointer, sizeof(type) * (count)))
66/* Equivalent functions, deliberately named the same as backend functions */
67extern char *
pstrdup(
const char *in);
73extern void pfree(
void *pointer);
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)))
81 /* sprintf into a palloc'd buffer --- these are in psprintf.c */
85#endif /* FE_MEMUTILS_H */
#define pg_attribute_printf(f, a)
char * pstrdup(const char *in)
char * psprintf(const char *fmt,...) pg_attribute_printf(1
void * repalloc(void *pointer, Size size)
void pfree(void *pointer)
void * palloc0(Size size)
void * pg_malloc(size_t size)
void * palloc_extended(Size size, int flags)
void * pg_malloc_extended(size_t size, int flags)
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)
void * pg_malloc0(size_t size)
char * pnstrdup(const char *in, Size size)
void * pg_realloc(void *ptr, size_t size)