]> granicus.if.org Git - postgresql/blob - src/common/fe_memutils.c
Update copyright for 2014
[postgresql] / src / common / fe_memutils.c
1 /*-------------------------------------------------------------------------
2  *
3  * fe_memutils.c
4  *        memory management support for frontend code
5  *
6  * Portions Copyright (c) 1996-2014, 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
22 void *
23 pg_malloc(size_t size)
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)
32         {
33                 fprintf(stderr, _("out of memory\n"));
34                 exit(EXIT_FAILURE);
35         }
36         return tmp;
37 }
38
39 void *
40 pg_malloc0(size_t size)
41 {
42         void       *tmp;
43
44         tmp = pg_malloc(size);
45         MemSet(tmp, 0, size);
46         return tmp;
47 }
48
49 void *
50 pg_realloc(void *ptr, size_t size)
51 {
52         void       *tmp;
53
54         /* Avoid unportable behavior of realloc(NULL, 0) */
55         if (ptr == NULL && size == 0)
56                 size = 1;
57         tmp = realloc(ptr, size);
58         if (!tmp)
59         {
60                 fprintf(stderr, _("out of memory\n"));
61                 exit(EXIT_FAILURE);
62         }
63         return tmp;
64 }
65
66 /*
67  * "Safe" wrapper around strdup().
68  */
69 char *
70 pg_strdup(const char *in)
71 {
72         char       *tmp;
73
74         if (!in)
75         {
76                 fprintf(stderr,
77                                 _("cannot duplicate null pointer (internal error)\n"));
78                 exit(EXIT_FAILURE);
79         }
80         tmp = strdup(in);
81         if (!tmp)
82         {
83                 fprintf(stderr, _("out of memory\n"));
84                 exit(EXIT_FAILURE);
85         }
86         return tmp;
87 }
88
89 void
90 pg_free(void *ptr)
91 {
92         if (ptr != NULL)
93                 free(ptr);
94 }
95
96 /*
97  * Frontend emulation of backend memory management functions.  Useful for
98  * programs that compile backend files.
99  */
100 void *
101 palloc(Size size)
102 {
103         return pg_malloc(size);
104 }
105
106 void *
107 palloc0(Size size)
108 {
109         return pg_malloc0(size);
110 }
111
112 void
113 pfree(void *pointer)
114 {
115         pg_free(pointer);
116 }
117
118 char *
119 pstrdup(const char *in)
120 {
121         return pg_strdup(in);
122 }
123
124 void *
125 repalloc(void *pointer, Size size)
126 {
127         return pg_realloc(pointer, size);
128 }