summaryrefslogtreecommitdiff
path: root/include/xalloc.h
blob: f1cc8d4a1ab7fe5f689a037c9480762f3882e69d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
 * memory wrappers
 *
 * Copyright (c) Artem Bityutskiy, 2007, 2008
 * Copyright 2001, 2002 Red Hat, Inc.
 *           2001 David A. Schleef <ds@lineo.com>
 *           2002 Axis Communications AB
 *           2001, 2002 Erik Andersen <andersen@codepoet.org>
 *           2004 University of Szeged, Hungary
 *           2006 KaiGai Kohei <kaigai@ak.jp.nec.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
 * the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#ifndef __MTD_UTILS_XALLOC_H__
#define __MTD_UTILS_XALLOC_H__

#include <stdlib.h>
#include <string.h>

/*
 * Mark these functions as unused so that gcc does not emit warnings
 * when people include this header but don't use every function.
 */

__attribute__((unused))
static void *xmalloc(size_t size)
{
	void *ptr = malloc(size);

	if (ptr == NULL && size != 0)
		sys_errmsg_die("out of memory");
	return ptr;
}

__attribute__((unused))
static void *xcalloc(size_t nmemb, size_t size)
{
	void *ptr = calloc(nmemb, size);

	if (ptr == NULL && nmemb != 0 && size != 0)
		sys_errmsg_die("out of memory");
	return ptr;
}

__attribute__((unused))
static void *xzalloc(size_t size)
{
	return xcalloc(1, size);
}

__attribute__((unused))
static void *xrealloc(void *ptr, size_t size)
{
	ptr = realloc(ptr, size);
	if (ptr == NULL && size != 0)
		sys_errmsg_die("out of memory");
	return ptr;
}

__attribute__((unused))
static char *xstrdup(const char *s)
{
	char *t;

	if (s == NULL)
		return NULL;
	t = strdup(s);
	if (t == NULL)
		sys_errmsg_die("out of memory");
	return t;
}

#ifdef _GNU_SOURCE
#include <stdarg.h>

__attribute__((unused))
static int xasprintf(char **strp, const char *fmt, ...)
{
	int cnt;
	va_list ap;

	va_start(ap, fmt);
	cnt = vasprintf(strp, fmt, ap);
	va_end(ap);

	if (cnt == -1)
		sys_errmsg_die("out of memory");

	return cnt;
}
#endif

#endif /* !__MTD_UTILS_XALLOC_H__ */