| 1 |
frodo |
322 |
/* |
| 2 |
|
|
* Copyright (C) 2010 Davidlohr Bueso <dave@gnu.org> |
| 3 |
|
|
* |
| 4 |
|
|
* This file may be redistributed under the terms of the |
| 5 |
|
|
* GNU Lesser General Public License. |
| 6 |
|
|
* |
| 7 |
|
|
* General memory allocation wrappers for malloc, realloc, calloc and strdup |
| 8 |
|
|
*/ |
| 9 |
|
|
|
| 10 |
|
|
#ifndef UTIL_LINUX_XALLOC_H |
| 11 |
|
|
#define UTIL_LINUX_XALLOC_H |
| 12 |
|
|
|
| 13 |
|
|
#include <stdlib.h> |
| 14 |
|
|
#include <string.h> |
| 15 |
|
|
|
| 16 |
|
|
#ifndef XALLOC_EXIT_CODE |
| 17 |
|
|
# define XALLOC_EXIT_CODE 3 |
| 18 |
|
|
#endif |
| 19 |
|
|
|
| 20 |
|
|
static void *xmalloc(const size_t size) |
| 21 |
|
|
{ |
| 22 |
|
|
void *ret = malloc(size); |
| 23 |
|
|
|
| 24 |
|
|
if (!ret && size) { |
| 25 |
frodo |
323 |
fprintf(stderr, "%s: error: cannot allocate %zu bytes", program_invocation_short_name, size); |
| 26 |
frodo |
322 |
exit(XALLOC_EXIT_CODE); |
| 27 |
|
|
} |
| 28 |
|
|
return ret; |
| 29 |
|
|
} |
| 30 |
|
|
|
| 31 |
|
|
static void *xrealloc(void *ptr, const size_t size) |
| 32 |
|
|
{ |
| 33 |
|
|
void *ret = realloc(ptr, size); |
| 34 |
|
|
|
| 35 |
|
|
if (!ret && size) |
| 36 |
frodo |
323 |
fprintf(stderr, "%s: Error: cannot allocate %zu bytes", program_invocation_short_name, size); |
| 37 |
frodo |
322 |
return ret; |
| 38 |
|
|
} |
| 39 |
|
|
|
| 40 |
|
|
#endif |