PostgreSQL Source Code git master
Loading...
Searching...
No Matches
fe_memutils.c
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * fe_memutils.c
4 * memory management support for frontend code
5 *
6 * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
8 *
9 *
10 * IDENTIFICATION
11 * src/common/fe_memutils.c
12 *
13 *-------------------------------------------------------------------------
14 */
15
16#ifndef FRONTEND
17#error "This file is not expected to be compiled for backend code"
18#endif
19
20#include "postgres_fe.h"
21
22static inline void *
23pg_malloc_internal(size_t size, int flags)
24{
25 void *tmp;
26
27 /* Avoid unportable behavior of malloc(0) */
28 if (size == 0)
29 size = 1;
30 tmp = malloc(size);
31 if (tmp == NULL)
32 {
33 if ((flags & MCXT_ALLOC_NO_OOM) == 0)
34 {
35 fprintf(stderr, _("out of memory\n"));
37 }
38 return NULL;
39 }
40
41 if ((flags & MCXT_ALLOC_ZERO) != 0)
42 MemSet(tmp, 0, size);
43 return tmp;
44}
45
46void *
47pg_malloc(size_t size)
48{
49 return pg_malloc_internal(size, 0);
50}
51
52void *
53pg_malloc0(size_t size)
54{
56}
57
58void *
59pg_malloc_extended(size_t size, int flags)
60{
61 return pg_malloc_internal(size, flags);
62}
63
64void *
65pg_realloc(void *ptr, size_t size)
66{
67 void *tmp;
68
69 /* Avoid unportable behavior of realloc(NULL, 0) */
70 if (ptr == NULL && size == 0)
71 size = 1;
72 tmp = realloc(ptr, size);
73 if (!tmp)
74 {
75 fprintf(stderr, _("out of memory\n"));
77 }
78 return tmp;
79}
80
81/*
82 * "Safe" wrapper around strdup().
83 */
84char *
85pg_strdup(const char *in)
86{
87 char *tmp;
88
89 if (!in)
90 {
92 _("cannot duplicate null pointer (internal error)\n"));
94 }
95 tmp = strdup(in);
96 if (!tmp)
97 {
98 fprintf(stderr, _("out of memory\n"));
100 }
101 return tmp;
102}
103
104void
105pg_free(void *ptr)
106{
107 free(ptr);
108}
109
110/*
111 * Frontend emulation of backend memory management functions. Useful for
112 * programs that compile backend files.
113 */
114void *
116{
117 return pg_malloc_internal(size, 0);
118}
119
120void *
122{
124}
125
126void *
127palloc_extended(Size size, int flags)
128{
129 return pg_malloc_internal(size, flags);
130}
131
132void
133pfree(void *pointer)
134{
135 pg_free(pointer);
136}
137
138char *
139pstrdup(const char *in)
140{
141 return pg_strdup(in);
142}
143
144char *
145pnstrdup(const char *in, Size size)
146{
147 char *tmp;
148 int len;
149
150 if (!in)
151 {
153 _("cannot duplicate null pointer (internal error)\n"));
155 }
156
157 len = strnlen(in, size);
158 tmp = malloc(len + 1);
159 if (tmp == NULL)
160 {
161 fprintf(stderr, _("out of memory\n"));
163 }
164
165 memcpy(tmp, in, len);
166 tmp[len] = '\0';
167
168 return tmp;
169}
170
171void *
172repalloc(void *pointer, Size size)
173{
174 return pg_realloc(pointer, size);
175}
#define MemSet(start, val, len)
Definition c.h:1013
size_t Size
Definition c.h:619
#define fprintf(file, fmt, msg)
Definition cubescan.l:21
#define _(x)
Definition elog.c:91
char * pstrdup(const char *in)
void * repalloc(void *pointer, Size size)
void pfree(void *pointer)
void * palloc0(Size size)
void * pg_malloc(size_t size)
Definition fe_memutils.c:47
void * palloc(Size size)
static void * pg_malloc_internal(size_t size, int flags)
Definition fe_memutils.c:23
void * palloc_extended(Size size, int flags)
void * pg_malloc_extended(size_t size, int flags)
Definition fe_memutils.c:59
char * pg_strdup(const char *in)
Definition fe_memutils.c:85
void * pg_malloc0(size_t size)
Definition fe_memutils.c:53
void pg_free(void *ptr)
char * pnstrdup(const char *in, Size size)
void * pg_realloc(void *ptr, size_t size)
Definition fe_memutils.c:65
#define MCXT_ALLOC_ZERO
Definition fe_memutils.h:30
#define MCXT_ALLOC_NO_OOM
Definition fe_memutils.h:29
const void size_t len
static int fb(int x)
#define EXIT_FAILURE
Definition settings.h:197
#define realloc(a, b)
#define free(a)
#define malloc(a)