1204433Sraj#ifndef _UTIL_H
2204433Sraj#define _UTIL_H
3204433Sraj
4204433Sraj/*
5204433Sraj * Copyright 2008 Jon Loeliger, Freescale Semiconductor, Inc.
6204433Sraj *
7204433Sraj * This program is free software; you can redistribute it and/or
8204433Sraj * modify it under the terms of the GNU General Public License as
9204433Sraj * published by the Free Software Foundation; either version 2 of the
10204433Sraj * License, or (at your option) any later version.
11204433Sraj *
12204433Sraj *  This program is distributed in the hope that it will be useful,
13204433Sraj *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14204433Sraj *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15204433Sraj *  General Public License for more details.
16204433Sraj *
17204433Sraj *  You should have received a copy of the GNU General Public License
18204433Sraj *  along with this program; if not, write to the Free Software
19204433Sraj *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
20204433Sraj *                                                                   USA
21204433Sraj */
22204433Sraj
23204433Srajstatic inline void __attribute__((noreturn)) die(char * str, ...)
24204433Sraj{
25204433Sraj	va_list ap;
26204433Sraj
27204433Sraj	va_start(ap, str);
28204433Sraj	fprintf(stderr, "FATAL ERROR: ");
29204433Sraj	vfprintf(stderr, str, ap);
30204433Sraj	exit(1);
31204433Sraj}
32204433Sraj
33204433Srajstatic inline void *xmalloc(size_t len)
34204433Sraj{
35204433Sraj	void *new = malloc(len);
36204433Sraj
37204433Sraj	if (!new)
38204433Sraj		die("malloc() failed\n");
39204433Sraj
40204433Sraj	return new;
41204433Sraj}
42204433Sraj
43204433Srajstatic inline void *xrealloc(void *p, size_t len)
44204433Sraj{
45204433Sraj	void *new = realloc(p, len);
46204433Sraj
47204433Sraj	if (!new)
48204433Sraj		die("realloc() failed (len=%d)\n", len);
49204433Sraj
50204433Sraj	return new;
51204433Sraj}
52204433Sraj
53204433Srajextern char *xstrdup(const char *s);
54204433Sraj
55204433Sraj#endif /* _UTIL_H */
56