1 /*-------------------------------------------------------------------------
4 * memory management support for frontend code
6 * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * src/common/fe_memutils.c
13 *-------------------------------------------------------------------------
17 #error "This file is not expected to be compiled for backend code"
20 #include "postgres_fe.h"
23 pg_malloc_internal(size_t size
, int flags
)
27 /* Avoid unportable behavior of malloc(0) */
33 if ((flags
& MCXT_ALLOC_NO_OOM
) == 0)
35 fprintf(stderr
, _("out of memory\n"));
41 if ((flags
& MCXT_ALLOC_ZERO
) != 0)
47 pg_malloc(size_t size
)
49 return pg_malloc_internal(size
, 0);
53 pg_malloc0(size_t size
)
55 return pg_malloc_internal(size
, MCXT_ALLOC_ZERO
);
59 pg_malloc_extended(size_t size
, int flags
)
61 return pg_malloc_internal(size
, flags
);
65 pg_realloc(void *ptr
, size_t size
)
69 /* Avoid unportable behavior of realloc(NULL, 0) */
70 if (ptr
== NULL
&& size
== 0)
72 tmp
= realloc(ptr
, size
);
75 fprintf(stderr
, _("out of memory\n"));
82 * "Safe" wrapper around strdup().
85 pg_strdup(const char *in
)
92 _("cannot duplicate null pointer (internal error)\n"));
98 fprintf(stderr
, _("out of memory\n"));
111 * Frontend emulation of backend memory management functions. Useful for
112 * programs that compile backend files.
117 return pg_malloc_internal(size
, 0);
123 return pg_malloc_internal(size
, MCXT_ALLOC_ZERO
);
127 palloc_extended(Size size
, int flags
)
129 return pg_malloc_internal(size
, flags
);
139 pstrdup(const char *in
)
141 return pg_strdup(in
);
145 pnstrdup(const char *in
, Size size
)
153 _("cannot duplicate null pointer (internal error)\n"));
157 len
= strnlen(in
, size
);
158 tmp
= malloc(len
+ 1);
161 fprintf(stderr
, _("out of memory\n"));
165 memcpy(tmp
, in
, len
);
172 repalloc(void *pointer
, Size size
)
174 return pg_realloc(pointer
, size
);