implement more suggestions by lorenz schori
[project/uci.git] / util.c
1 /*
2 * libuci - Library for the Unified Configuration Interface
3 * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License version 2.1
7 * as published by the Free Software Foundation
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 */
14
15 /*
16 * This file contains wrappers to standard functions, which
17 * throw exceptions upon failure.
18 */
19
20 static void *uci_malloc(struct uci_context *ctx, size_t size)
21 {
22 void *ptr;
23
24 ptr = malloc(size);
25 if (!ptr)
26 UCI_THROW(ctx, UCI_ERR_MEM);
27 memset(ptr, 0, size);
28
29 return ptr;
30 }
31
32 static void *uci_realloc(struct uci_context *ctx, void *ptr, size_t size)
33 {
34 ptr = realloc(ptr, size);
35 if (!ptr)
36 UCI_THROW(ctx, UCI_ERR_MEM);
37
38 return ptr;
39 }
40
41 static char *uci_strdup(struct uci_context *ctx, const char *str)
42 {
43 char *ptr;
44
45 ptr = strdup(str);
46 if (!ptr)
47 UCI_THROW(ctx, UCI_ERR_MEM);
48
49 return ptr;
50 }
51
52