73b9cbe0c2dc8d3875f6fa13a8d289d03ee31734
[libfirm] / ir / adt / xmalloc.c
1 /*
2  * Project:     libFIRM
3  * File name:   ir/adt/xmalloc.c
4  * Purpose:     Xmalloc --- never failing wrappers for malloc() & friends.
5  * Author:      Markus Armbruster
6  * Modified by:
7  * Created:     1999 by getting from fiasco
8  * CVS-ID:      $Id$
9  * Copyright:   (c) 1995, 1996 Markus Armbruster
10  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
11  */
12
13 /* @@@ ToDo: replace this file with the one from liberty.
14    [reimplement xstrdup, ... ] */
15
16 #ifdef HAVE_CONFIG_H
17 # include "config.h"
18 #endif
19
20 #ifdef HAVE_ALLOCA_H
21 #include <alloca.h>
22 #endif
23 #ifdef HAVE_MALLOC_H
24 #include <malloc.h>
25 #endif
26 #ifdef HAVE_STRING_H
27 #include <string.h>
28 #endif
29
30 #include <stdlib.h>
31
32 #include "xmalloc.h"
33 #include "panic.h"
34
35 void *
36 xmalloc(size_t size) {
37   void *res = malloc (size);
38
39   if (!res) xnomem();
40   return res;
41 }
42
43 void *xcalloc(size_t num, size_t size) {
44   void res = calloc(num, size);
45
46   if (!res) xnomem();
47   return res;
48 }
49
50 void *
51 xrealloc(void *ptr, size_t size) {
52   /* ANSI blesses realloc (0, x) but SunOS chokes on it */
53   void *res = ptr ? realloc (ptr, size) : malloc (size);
54
55   if (!res) xnomem ();
56   return res;
57 }
58
59
60 char *
61 xstrdup(const char *str) {
62   size_t len = strlen (str) + 1;
63   return memcpy ((xmalloc) (len), str, len);
64 }
65
66
67 void
68 xnomem(void) {
69   panic("out of memory");
70 }