beifg: Factorise code to count interference components.
[libfirm] / ir / adt / xmalloc.c
1 /*
2  * This file is part of libFirm.
3  * Copyright (C) 2012 University of Karlsruhe.
4  */
5
6 /**
7  * @file
8  * @brief       implementation of xmalloc & friends
9  * @author      Markus Armbruster
10  */
11
12 /* @@@ ToDo: replace this file with the one from liberty.
13    [reimplement xstrdup, ... ] */
14 #include "config.h"
15
16 #include <string.h>
17 #include <stdio.h>
18 #include <stdlib.h>
19
20 #include "xmalloc.h"
21 #include "error.h"
22
23 static NORETURN xnomem(void)
24 {
25         /* Do not use panic() here, because it might try to allocate memory! */
26         fputs("out of memory", stderr);
27         abort();
28 }
29
30 void *xmalloc(size_t size)
31 {
32         void *res = malloc(size);
33
34         if (!res) xnomem();
35         return res;
36 }
37
38 void *xrealloc(void *ptr, size_t size)
39 {
40         /* ANSI blesses realloc (0, x) but SunOS chokes on it */
41         void *res = ptr ? realloc (ptr, size) : malloc (size);
42
43         if (!res) xnomem();
44         return res;
45 }
46
47 char *xstrdup(const char *str)
48 {
49         size_t len = strlen (str) + 1;
50         return (char*) memcpy(xmalloc(len), str, len);
51 }