-
Notifications
You must be signed in to change notification settings - Fork 86
/
fe_memutils.c
131 lines (115 loc) · 2 KB
/
fe_memutils.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*-------------------------------------------------------------------------
*
* fe_memutils.c
* memory management support for frontend code
*
* Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
*
* IDENTIFICATION
* src/common/fe_memutils.c
*
*-------------------------------------------------------------------------
*/
#ifndef POOL_PRIVATE
#error "This file is not expected to be compiled for pgpool utilities only"
#endif
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "utils/fe_ports.h"
void *
pg_malloc(Size size)
{
void *tmp;
/* Avoid unportable behavior of malloc(0) */
if (size == 0)
size = 1;
tmp = malloc(size);
if (!tmp)
{
fprintf(stderr, "out of memory\n");
exit(EXIT_FAILURE);
}
return tmp;
}
void *
pg_malloc0(Size size)
{
void *tmp;
tmp = pg_malloc(size);
memset(tmp, 0, size);
return tmp;
}
void *
pg_realloc(void *ptr, Size size)
{
void *tmp;
/* Avoid unportable behavior of realloc(NULL, 0) */
if (ptr == NULL && size == 0)
size = 1;
tmp = realloc(ptr, size);
if (!tmp)
{
fprintf(stderr, "out of memory\n");
exit(EXIT_FAILURE);
}
return tmp;
}
/*
* "Safe" wrapper around strdup().
*/
char *
pg_strdup(const char *in)
{
char *tmp;
if (!in)
{
fprintf(stderr,
"cannot duplicate null pointer (internal error)\n");
exit(EXIT_FAILURE);
}
tmp = strdup(in);
if (!tmp)
{
fprintf(stderr, "out of memory\n");
exit(EXIT_FAILURE);
}
return tmp;
}
void
pg_free(void *ptr)
{
if (ptr != NULL)
free(ptr);
}
/*
* Frontend emulation of backend memory management functions. Useful for
* programs that compile backend files.
*/
void *
palloc(Size size)
{
return pg_malloc(size);
}
void *
palloc0(Size size)
{
return pg_malloc0(size);
}
void
pfree(void *pointer)
{
pg_free(pointer);
}
char *
pstrdup(const char *in)
{
return pg_strdup(in);
}
void *
repalloc(void *pointer, Size size)
{
return pg_realloc(pointer, size);
}