Remove the useless gladep file
[ladish.git] / common / safety.c
blobbd3d609fb415615dd4aeb57cb2b0a64f273bd65b
1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
2 /*
3 * LADI Session Handler (ladish)
5 * Copyright (C) 2008 Juuso Alasuutari <juuso.alasuutari@gmail.com>
7 **************************************************************************
8 * This file contains "safe" memory and string helpers (obsolete)
9 **************************************************************************
11 * LADI Session Handler is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * LADI Session Handler is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with LADI Session Handler. If not, see <http://www.gnu.org/licenses/>
23 * or write to the Free Software Foundation, Inc.,
24 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
27 /* For strdup() */
28 #ifndef _BSD_SOURCE
29 # define _BSD_SOURCE 1
30 #endif
32 #include <stdint.h>
33 #include <string.h>
35 #include "safety.h"
36 #include "debug.h"
38 void
39 _lash_free(void **ptr)
41 if (ptr && *ptr) {
42 free(*ptr);
43 *ptr = NULL;
47 void
48 lash_strset(char **property,
49 const char *value)
51 if (property) {
52 if (*property)
53 free(*property);
55 if (value)
56 *property = lash_strdup(value);
57 else
58 *property = NULL;
62 #ifndef LASH_DEBUG
64 void *
65 lash_malloc(size_t nmemb,
66 size_t size)
68 void *ptr = NULL;
70 if (nmemb && size) {
71 if (size <= (SIZE_MAX / nmemb)) {
72 if ((ptr = malloc(nmemb * size))) {
73 return ptr;
74 } else {
75 lash_error("malloc returned NULL");
77 } else {
78 lash_error("Arguments would overflow");
80 } else {
81 lash_error("Can't allocate zero bytes");
84 abort();
87 void *
88 lash_calloc(size_t nmemb,
89 size_t size)
91 void *ptr = NULL;
93 if (nmemb && size) {
94 if (size <= (SIZE_MAX / nmemb)) {
95 if ((ptr = calloc(nmemb, size))) {
96 return ptr;
97 } else {
98 lash_error("calloc returned NULL");
100 } else {
101 lash_error("Arguments would overflow");
103 } else {
104 lash_error("Can't allocate zero bytes");
107 abort();
110 void *
111 lash_realloc(void *ptr,
112 size_t nmemb,
113 size_t size)
115 void *ptr2 = NULL;
117 if (nmemb && size) {
118 if (size <= (SIZE_MAX / nmemb)) {
119 if ((ptr2 = realloc(ptr, nmemb * size))) {
120 return ptr2;
121 } else {
122 lash_error("realloc returned NULL");
124 } else {
125 lash_error("Arguments would overflow");
127 } else {
128 lash_error("Can't allocate zero bytes");
131 abort();
134 char *
135 lash_strdup(const char *s)
137 char *ptr = (s) ? strdup(s) : strdup("");
139 if (ptr)
140 return ptr;
142 lash_error("strdup returned NULL");
144 abort();
147 #else /* LASH_DEBUG */
149 void *
150 lash_malloc_dbg(size_t nmemb,
151 size_t size,
152 const char *file,
153 int line,
154 const char *function,
155 const char *arg1,
156 const char *arg2)
158 void *ptr = NULL;
160 if (nmemb && size) {
161 if (size <= (SIZE_MAX / nmemb)) {
162 if ((ptr = malloc(nmemb * size))) {
163 return ptr;
164 } else {
165 lash_error_plain("%s:%d:%s: "
166 "lash_malloc(%s,%s) failed: "
167 "malloc returned NULL",
168 file, line, function,
169 arg1, arg2);
171 } else {
172 lash_error_plain("%s:%d:%s: "
173 "lash_malloc(%s,%s) failed: "
174 "Arguments would overflow",
175 file, line, function, arg1, arg2);
177 } else {
178 lash_error_plain("%s:%d:%s: "
179 "lash_malloc(%s,%s) failed: "
180 "Can't allocate zero bytes",
181 file, line, function, arg1, arg2);
184 abort();
187 void *
188 lash_calloc_dbg(size_t nmemb,
189 size_t size,
190 const char *file,
191 int line,
192 const char *function,
193 const char *arg1,
194 const char *arg2)
196 void *ptr = NULL;
198 if (nmemb && size) {
199 if (size <= (SIZE_MAX / nmemb)) {
200 if ((ptr = calloc(nmemb, size))) {
201 return ptr;
202 } else {
203 lash_error_plain("%s:%d:%s: "
204 "lash_calloc(%s,%s) failed: "
205 "calloc returned NULL",
206 file, line, function,
207 arg1, arg2);
209 } else {
210 lash_error_plain("%s:%d:%s: "
211 "lash_calloc(%s,%s) failed: "
212 "Arguments would overflow",
213 file, line, function, arg1, arg2);
215 } else {
216 lash_error_plain("%s:%d:%s: "
217 "lash_calloc(%s,%s) failed: "
218 "Can't allocate zero bytes",
219 file, line, function, arg1, arg2);
222 abort();
225 void *
226 lash_realloc_dbg(void *ptr,
227 size_t nmemb,
228 size_t size,
229 const char *file,
230 int line,
231 const char *function,
232 const char *arg1,
233 const char *arg2,
234 const char *arg3)
236 void *ptr2 = NULL;
238 if (nmemb && size) {
239 if (size <= (SIZE_MAX / nmemb)) {
240 if ((ptr2 = realloc(ptr, nmemb * size))) {
241 return ptr2;
242 } else {
243 lash_error_plain("%s:%d:%s: "
244 "lash_realloc(%s,%s,%s) failed: "
245 "calloc returned NULL",
246 file, line, function,
247 arg1, arg2, arg3);
249 } else {
250 lash_error_plain("%s:%d:%s: "
251 "lash_realloc(%s,%s,%s) failed: "
252 "Arguments would overflow",
253 file, line, function, arg1, arg2, arg3);
255 } else {
256 lash_error_plain("%s:%d:%s: "
257 "lash_realloc(%s,%s,%s) failed: "
258 "Can't allocate zero bytes",
259 file, line, function, arg1, arg2, arg3);
262 abort();
265 char *
266 lash_strdup_dbg(const char *s,
267 const char *file,
268 int line,
269 const char *function,
270 const char *arg1)
272 char *ptr;
274 if (s) {
275 ptr = strdup(s);
276 } else {
277 lash_error_plain("%s:%d:%s: lash_strdup(%s) failed: "
278 "Argument is NULL",
279 file, line, function, arg1);
280 ptr = strdup("");
283 if (ptr)
284 return ptr;
286 lash_error_plain("%s:%d:%s: lash_strdup(%s) failed: "
287 "strdup returned NULL",
288 file, line, function, arg1);
290 abort();
293 #endif /* LASH_DEBUG */
295 /* EOF */