4 * Copyright (c) Artem Bityutskiy, 2007, 2008
5 * Copyright 2001, 2002 Red Hat, Inc.
6 * 2001 David A. Schleef <ds@lineo.com>
7 * 2002 Axis Communications AB
8 * 2001, 2002 Erik Andersen <andersen@codepoet.org>
9 * 2004 University of Szeged, Hungary
10 * 2006 KaiGai Kohei <kaigai@ak.jp.nec.com>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
20 * the GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #ifndef __MTD_UTILS_XALLOC_H__
28 #define __MTD_UTILS_XALLOC_H__
35 * Mark these functions as unused so that gcc does not emit warnings
36 * when people include this header but don't use every function.
39 __attribute__((unused))
40 static void *xmalloc(size_t size)
42 void *ptr = malloc(size);
44 if (ptr == NULL && size != 0)
45 sys_errmsg_die("out of memory");
49 __attribute__((unused))
50 static void *xcalloc(size_t nmemb, size_t size)
52 void *ptr = calloc(nmemb, size);
54 if (ptr == NULL && nmemb != 0 && size != 0)
55 sys_errmsg_die("out of memory");
59 __attribute__((unused))
60 static void *xzalloc(size_t size)
62 return xcalloc(1, size);
65 __attribute__((unused))
66 static void *xrealloc(void *ptr, size_t size)
68 ptr = realloc(ptr, size);
69 if (ptr == NULL && size != 0)
70 sys_errmsg_die("out of memory");
74 __attribute__((unused))
75 static char *xstrdup(const char *s)
83 sys_errmsg_die("out of memory");
89 __attribute__((unused))
90 static int xasprintf(char **strp, const char *fmt, ...)
96 cnt = vasprintf(strp, fmt, ap);
100 sys_errmsg_die("out of memory");
106 #endif /* !__MTD_UTILS_XALLOC_H__ */