]> git.sur5r.net Git - i3/i3/blob - libi3/safewrappers.c
Merge branch 'master' into next
[i3/i3] / libi3 / safewrappers.c
1 /*
2  * vim:ts=4:sw=4:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  *
6  * © 2009-2011 Michael Stapelberg and contributors
7  *
8  * See file LICENSE for license information.
9  *
10  */
11 #include <string.h>
12 #include <stdlib.h>
13 #include <err.h>
14
15
16 /*
17  * The s* functions (safe) are wrappers around malloc, strdup, …, which exits if one of
18  * the called functions returns NULL, meaning that there is no more memory available
19  *
20  */
21 void *smalloc(size_t size) {
22     void *result = malloc(size);
23     if (result == NULL)
24         err(EXIT_FAILURE, "malloc(%zd)", size);
25     return result;
26 }
27
28 void *scalloc(size_t size) {
29     void *result = calloc(size, 1);
30     if (result == NULL)
31         err(EXIT_FAILURE, "calloc(%zd)", size);
32     return result;
33 }
34
35 void *srealloc(void *ptr, size_t size) {
36     void *result = realloc(ptr, size);
37     if (result == NULL && size > 0)
38         err(EXIT_FAILURE, "realloc(%zd)", size);
39     return result;
40 }
41
42 char *sstrdup(const char *str) {
43     char *result = strdup(str);
44     if (result == NULL)
45         err(EXIT_FAILURE, "strdup()");
46     return result;
47 }