1 |
#include <stdlib.h> |
2 |
#include <string.h> |
3 |
#include <errno.h> |
4 |
|
5 |
#include "imapfilter.h" |
6 |
|
7 |
|
8 |
/* |
9 |
* A malloc() that checks the results and dies in case of error. |
10 |
*/ |
11 |
void *xmalloc(size_t size) |
12 |
{ |
13 |
void *ptr; |
14 |
|
15 |
ptr = (void *) malloc(size); |
16 |
|
17 |
if (!ptr) |
18 |
fatal(ERROR_MEMORY_ALLOCATION, |
19 |
"imapfilter: allocating memory; %s\n", strerror(errno)); |
20 |
|
21 |
return ptr; |
22 |
} |
23 |
|
24 |
|
25 |
/* |
26 |
* A realloc() that checks the results and dies in case of error. |
27 |
*/ |
28 |
void *xrealloc(void *ptr, size_t size) |
29 |
{ |
30 |
ptr = (void *) realloc(ptr, size); |
31 |
|
32 |
if (!ptr) |
33 |
fatal(ERROR_MEMORY_ALLOCATION, |
34 |
"imapfilter: allocating memory; %s\n", strerror(errno)); |
35 |
|
36 |
return ptr; |
37 |
} |
38 |
|
39 |
|
40 |
/* |
41 |
* A free() that dies if fed with NULL pointer. |
42 |
*/ |
43 |
void xfree(void *ptr) |
44 |
{ |
45 |
if (!ptr) |
46 |
fatal(ERROR_MEMORY_ALLOCATION, |
47 |
"imapfilter: NULL pointer given as argument"); |
48 |
free(ptr); |
49 |
} |
50 |
|
51 |
|
52 |
/* |
53 |
* A strdup() that checks the results and dies in case of error. |
54 |
*/ |
55 |
char *xstrdup(const char *s) |
56 |
{ |
57 |
char *cp; |
58 |
|
59 |
cp = strdup(s); |
60 |
|
61 |
if (!cp) |
62 |
fatal(ERROR_MEMORY_ALLOCATION, |
63 |
"imapfilter: allocating memory; %s\n", strerror(errno)); |
64 |
|
65 |
return cp; |
66 |
} |