Implement -Wswitch-default.
[cparser] / 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 #include <config.h>
16
17 #include <stdlib.h>
18 #include <string.h>
19
20 #include "xmalloc.h"
21 #include "error.h"
22 #include "util.h"
23
24 static inline __attribute__((noreturn))
25 void out_of_memory(void) {
26         panic("out of memory");
27 }
28
29 void *xmalloc(size_t size) {
30         void *res = malloc(size);
31
32         if (UNLIKELY(res == NULL))
33                 out_of_memory();
34
35         return res;
36 }
37
38 void *xcalloc(size_t num, size_t size) {
39         void *res = calloc(num, size);
40
41         if (UNLIKELY(res == NULL))
42                 out_of_memory();
43
44         return res;
45 }
46
47 void *xrealloc(void *ptr, size_t size) {
48         void *res = realloc (ptr, size);
49
50         if (UNLIKELY(res == NULL))
51                 out_of_memory();
52
53         return res;
54 }
55
56 char *xstrdup(const char *str) {
57         size_t len = strlen(str) + 1;
58         char *res = xmalloc(len);
59         memcpy(res, str, len);
60
61         return res;
62 }