2
* Copyright (C) 2010 Davidlohr Bueso <dave@gnu.org>
4
* This file may be redistributed under the terms of the
5
* GNU Lesser General Public License.
7
* General memory allocation wrappers for malloc, realloc and calloc
10
#ifndef UTIL_LINUX_XALLOC_H
11
#define UTIL_LINUX_XALLOC_H
19
#ifndef XALLOC_EXIT_CODE
20
# define XALLOC_EXIT_CODE EXIT_FAILURE
23
static inline __ul_alloc_size(1)
24
void *xmalloc(const size_t size)
26
void *ret = malloc(size);
29
err(XALLOC_EXIT_CODE, "cannot allocate %zu bytes", size);
33
static inline __ul_alloc_size(2)
34
void *xrealloc(void *ptr, const size_t size)
36
void *ret = realloc(ptr, size);
39
err(XALLOC_EXIT_CODE, "cannot allocate %zu bytes", size);
43
static inline __ul_calloc_size(1, 2)
44
void *xcalloc(const size_t nelems, const size_t size)
46
void *ret = calloc(nelems, size);
48
if (!ret && size && nelems)
49
err(XALLOC_EXIT_CODE, "cannot allocate %zu bytes", size);
53
static inline char *xstrdup(const char *str)
55
char *ret = strdup(str);
58
err(XALLOC_EXIT_CODE, "cannot duplicate string");