1 /* -*- Mode: C ; c-basic-offset: 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.
29 # define _BSD_SOURCE 1
39 _lash_free(void **ptr
)
48 lash_strset(char **property
,
56 *property
= lash_strdup(value
);
65 lash_malloc(size_t nmemb
,
71 if (size
<= (SIZE_MAX
/ nmemb
)) {
72 if ((ptr
= malloc(nmemb
* size
))) {
75 lash_error("malloc returned NULL");
78 lash_error("Arguments would overflow");
81 lash_error("Can't allocate zero bytes");
88 lash_calloc(size_t nmemb
,
94 if (size
<= (SIZE_MAX
/ nmemb
)) {
95 if ((ptr
= calloc(nmemb
, size
))) {
98 lash_error("calloc returned NULL");
101 lash_error("Arguments would overflow");
104 lash_error("Can't allocate zero bytes");
111 lash_realloc(void *ptr
,
118 if (size
<= (SIZE_MAX
/ nmemb
)) {
119 if ((ptr2
= realloc(ptr
, nmemb
* size
))) {
122 lash_error("realloc returned NULL");
125 lash_error("Arguments would overflow");
128 lash_error("Can't allocate zero bytes");
135 lash_strdup(const char *s
)
137 char *ptr
= (s
) ? strdup(s
) : strdup("");
142 lash_error("strdup returned NULL");
147 #else /* LASH_DEBUG */
150 lash_malloc_dbg(size_t nmemb
,
154 const char *function
,
161 if (size
<= (SIZE_MAX
/ nmemb
)) {
162 if ((ptr
= malloc(nmemb
* size
))) {
165 lash_error_plain("%s:%d:%s: "
166 "lash_malloc(%s,%s) failed: "
167 "malloc returned NULL",
168 file
, line
, function
,
172 lash_error_plain("%s:%d:%s: "
173 "lash_malloc(%s,%s) failed: "
174 "Arguments would overflow",
175 file
, line
, function
, arg1
, arg2
);
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
);
188 lash_calloc_dbg(size_t nmemb
,
192 const char *function
,
199 if (size
<= (SIZE_MAX
/ nmemb
)) {
200 if ((ptr
= calloc(nmemb
, size
))) {
203 lash_error_plain("%s:%d:%s: "
204 "lash_calloc(%s,%s) failed: "
205 "calloc returned NULL",
206 file
, line
, function
,
210 lash_error_plain("%s:%d:%s: "
211 "lash_calloc(%s,%s) failed: "
212 "Arguments would overflow",
213 file
, line
, function
, arg1
, arg2
);
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
);
226 lash_realloc_dbg(void *ptr
,
231 const char *function
,
239 if (size
<= (SIZE_MAX
/ nmemb
)) {
240 if ((ptr2
= realloc(ptr
, nmemb
* size
))) {
243 lash_error_plain("%s:%d:%s: "
244 "lash_realloc(%s,%s,%s) failed: "
245 "calloc returned NULL",
246 file
, line
, function
,
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
);
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
);
266 lash_strdup_dbg(const char *s
,
269 const char *function
,
277 lash_error_plain("%s:%d:%s: lash_strdup(%s) failed: "
279 file
, line
, function
, arg1
);
286 lash_error_plain("%s:%d:%s: lash_strdup(%s) failed: "
287 "strdup returned NULL",
288 file
, line
, function
, arg1
);
293 #endif /* LASH_DEBUG */