1 |
rakinar2 |
23 |
#include "xmalloc.h" |
2 |
|
|
#include <errno.h> |
3 |
|
|
#include <stdio.h> |
4 |
|
|
#include <stdlib.h> |
5 |
|
|
#include <string.h> |
6 |
|
|
|
7 |
|
|
void * |
8 |
|
|
xmalloc (size_t size) |
9 |
|
|
{ |
10 |
|
|
void *ptr = malloc (size); |
11 |
|
|
|
12 |
|
|
if (ptr == NULL) |
13 |
|
|
{ |
14 |
|
|
fprintf (stderr, "xmalloc(%zu): failed to allocate memory: %s", |
15 |
|
|
size, strerror (errno)); |
16 |
|
|
abort (); |
17 |
|
|
} |
18 |
|
|
|
19 |
|
|
return ptr; |
20 |
|
|
} |
21 |
|
|
|
22 |
|
|
void * |
23 |
|
|
xcalloc (size_t nmemb, size_t size) |
24 |
|
|
{ |
25 |
|
|
void *ptr = calloc (nmemb, size); |
26 |
|
|
|
27 |
|
|
if (ptr == NULL) |
28 |
|
|
{ |
29 |
|
|
fprintf (stderr, "xcalloc(%zu, %zu): failed to allocate memory: %s", |
30 |
|
|
nmemb, size, strerror (errno)); |
31 |
|
|
abort (); |
32 |
|
|
} |
33 |
|
|
|
34 |
|
|
return ptr; |
35 |
|
|
} |
36 |
|
|
|
37 |
|
|
void * |
38 |
|
|
xrealloc (void *ptr, size_t size) |
39 |
|
|
{ |
40 |
|
|
void *new_ptr = realloc (ptr, size); |
41 |
|
|
|
42 |
|
|
if (new_ptr == NULL) |
43 |
|
|
{ |
44 |
|
|
fprintf (stderr, |
45 |
|
|
"xrealloc(%p, %zu): failed to re-allocate memory: %s", ptr, |
46 |
|
|
size, strerror (errno)); |
47 |
|
|
abort (); |
48 |
|
|
} |
49 |
|
|
|
50 |
|
|
return new_ptr; |
51 |
|
|
} |