becopyheur2: Remove unnecessary indirection.
[libfirm] / include / libfirm / adt / xmalloc.h
1 /*
2  * This file is part of libFirm.
3  * Copyright (C) 2012 University of Karlsruhe.
4  */
5
6 /**
7  * @file
8  * @brief       never failing wrappers for malloc() & friends.
9  * @author      Markus Armbruster
10  * @note        The functions here never fail because they simply abort your
11  *              program in case of an error.
12  */
13 #ifndef FIRM_ADT_XMALLOC_H
14 #define FIRM_ADT_XMALLOC_H
15
16 #include <stddef.h>
17 #include <stdlib.h>
18 #include <string.h>
19
20 /* Includes for alloca() */
21 #ifdef _WIN32
22 #include <malloc.h>
23 #endif
24 #if defined(__linux__) || defined(__APPLE__)
25 #include <alloca.h>
26 #endif
27
28 #include "../begin.h"
29
30 /**
31  * @ingroup adt
32  * @defgroup xmalloc Memory Allocation
33  * @{
34  */
35
36 /**
37  * Allocate @p size bytes on the heap.
38  * This is a wrapper for malloc which calls panic() in case of errors, so no
39  * error handling is required for code using it.
40  */
41 FIRM_API void *xmalloc(size_t size);
42 /**
43  * Chane size of a previously allocated memory block to @p size bytes.
44  * This is a wrapper for realloc which calls panic() in case of errors, so no
45  * error handling is required for code using it.
46  */
47 FIRM_API void *xrealloc(void *ptr, size_t size);
48 /**
49  * Allocates memory and copies string @p str into it.
50  * This is a wrapper for strdup which calls panic() in case of errors, so no
51  * error handling is required for code using it.
52  */
53 FIRM_API char *xstrdup(const char *str);
54 /**
55  * Another name for the free function
56  * @deprecated
57  */
58 #define xfree(ptr)      free(ptr)
59
60 /**
61  * Allocate n objects of a certain type
62  */
63 #define XMALLOCN(type, n) ((type*)xmalloc(sizeof(type) * (n)))
64
65 /**
66  * Allocate n objects of a certain type and zero them
67  */
68 #define XMALLOCNZ(type, n) ((type*)memset(xmalloc(sizeof(type) * (n)), 0, sizeof(type) * (n)))
69
70 /**
71  * Allocate one object of a certain type
72  */
73 #define XMALLOC(type) XMALLOCN(type, 1)
74
75 /**
76  * Allocate one object of a certain type and zero it
77  */
78 #define XMALLOCZ(type) XMALLOCNZ(type, 1)
79
80 /**
81  * Reallocate n objects of a certain type
82  */
83 #define XREALLOC(ptr, type, n) ((type*)xrealloc(ptr, sizeof(type) * (n)))
84
85 /**
86  * Allocate an object with n elements of a flexible array member
87  */
88 #define XMALLOCF(type, member, n) ((type*)xmalloc(offsetof(type, member) + sizeof(*((type*)0)->member) * (n)))
89
90 /**
91  * Allocate an object with n elements of a flexible array member and zero the
92  * whole object
93  */
94 #define XMALLOCFZ(type, member, n) ((type*)memset(XMALLOCF(type, member, (n)), 0, offsetof(type, member) + sizeof(*((type*)0)->member) * (n)))
95
96 /**
97  * Allocate n objects of a certain type on the stack
98  */
99 #define ALLOCAN(type, n) ((type*)alloca(sizeof(type) * (n)))
100
101 /**
102  * Allocate n objects of a certain type on the stack and zero them
103  */
104 #define ALLOCANZ(type, n) ((type*)memset((type*)alloca(sizeof(type) * (n)), 0, sizeof(type) * (n)))
105
106 /**
107  * Allocate n objects of a certain type on the given obstack
108  */
109 #define OALLOCN(obst, type, n) ((type*)obstack_alloc((obst), sizeof(type) * (n)))
110
111 /**
112  * Allocate n objects of a certain type on the given obstack and zero them
113  */
114 #define OALLOCNZ(obst, type, n) ((type*)memset(OALLOCN((obst), type, (n)), 0, sizeof(type) * (n)))
115
116 /**
117  * Allocate one object of a certain type on the given obstack
118  */
119 #define OALLOC(obst, type) OALLOCN(obst, type, 1)
120
121 /**
122  * Allocate one object of a certain type on the given obstack and zero it
123  */
124 #define OALLOCZ(obst, type) OALLOCNZ(obst, type, 1)
125
126 /**
127  * Allocate an object with n elements of a flexible array member on the given
128  * obstck
129  */
130 #define OALLOCF(obst, type, member, n) ((type*)obstack_alloc((obst), offsetof(type, member) + sizeof(*((type*)0)->member) * (n)))
131
132 /**
133  * Allocate an object with n elements of a flexible array member on the given
134  * obstack and zero the whole object
135  */
136 #define OALLOCFZ(obst, type, member, n) ((type*)memset(OALLOCF((obst), type, member, (n)), 0, offsetof(type, member) + sizeof(*((type*)0)->member) * (n)))
137
138 /** @} */
139
140 #include "../end.h"
141
142 #endif