Commit | Line | Data |
---|---|---|
a7868768 BA |
1 | /** |
2 | * @file safe_alloc.c | |
3 | */ | |
4 | ||
5 | #include "cgds/safe_alloc.h" | |
6 | ||
7 | void* safe_malloc(size_t size) | |
8 | { | |
e45132ac BA |
9 | void* res = malloc(size); |
10 | if (res == NULL) | |
11 | { | |
12 | fprintf(stderr, "Error: unable to allocate memory\n"); | |
13 | exit(EXIT_FAILURE); | |
14 | } | |
15 | return res; | |
a7868768 BA |
16 | } |
17 | ||
18 | void* safe_calloc(size_t count, size_t size) | |
19 | { | |
e45132ac BA |
20 | void* res = calloc(count, size); |
21 | if (res == NULL) | |
22 | { | |
23 | fprintf(stderr, "Error: unable to allocate memory\n"); | |
24 | exit(EXIT_FAILURE); | |
25 | } | |
26 | return res; | |
a7868768 BA |
27 | } |
28 | ||
29 | void* safe_realloc(void* ptr, size_t size) | |
30 | { | |
e45132ac BA |
31 | void* res = realloc(ptr, size); |
32 | if (res == NULL) | |
33 | { | |
34 | fprintf(stderr, "Error: unable to reallocate memory\n"); | |
35 | exit(EXIT_FAILURE); | |
36 | } | |
37 | return res; | |
a7868768 BA |
38 | } |
39 | ||
40 | void safe_free(void* ptr) | |
41 | { | |
e45132ac BA |
42 | if (ptr != NULL) |
43 | free(ptr); | |
a7868768 | 44 | } |