memset is needed by some macros and defined in string.h
[libfirm] / include / libfirm / adt / xmalloc.h
1 /*
2  * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief       never failing wrappers for malloc() & friends.
23  * @author      Markus Armbruster
24  * @version     $Id$
25  * @note        The functions here never fail because they simply abort your
26  *              program in case of an error.
27  */
28 #ifndef FIRM_ADT_XMALLOC_H
29 #define FIRM_ADT_XMALLOC_H
30
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34
35 #include <stddef.h>
36 #include <stdlib.h>
37 #include <string.h>
38
39 /* xmalloc() & friends. */
40
41 void *xmalloc(size_t size);
42 void *xrealloc(void *ptr, size_t size);
43 char *xstrdup(const char *str);
44
45 #define xfree(ptr)      free(ptr)
46
47 /**
48  * Allocate n objects of a certain type
49  */
50 #define XMALLOCN(type, n) ((type*)xmalloc(sizeof(type) * (n)))
51
52 /**
53  * Allocate n objects of a certain type and zero them
54  */
55 #define XMALLOCNZ(type, n) ((type*)memset(xmalloc(sizeof(type) * (n)), 0, sizeof(type) * (n)))
56
57 /**
58  * Allocate one object of a certain type
59  */
60 #define XMALLOC(type) XMALLOCN(type, 1)
61
62 /**
63  * Allocate one object of a certain type and zero it
64  */
65 #define XMALLOCZ(type) XMALLOCNZ(type, 1)
66
67 /**
68  * Reallocate n objects of a certain type
69  */
70 #define XREALLOC(ptr, type, n) ((type*)xrealloc(ptr, sizeof(type) * (n)))
71
72 /**
73  * Allocate an object with n elements of a flexible array member
74  */
75 #define XMALLOCF(type, member, n) ((type*)xmalloc(offsetof(type, member) + sizeof(*((type*)0)->member) * (n)))
76
77 /**
78  * Allocate an object with n elements of a flexible array member and zero the
79  * whole object
80  */
81 #define XMALLOCFZ(type, member, n) ((type*)memset(XMALLOCF(type, member, (n)), 0, offsetof(type, member) + sizeof(*((type*)0)->member) * (n)))
82
83 /**
84  * Allocate n objects of a certain type on the stack
85  */
86 #define ALLOCAN(type, n) ((type*)alloca(sizeof(type) * (n)))
87
88 /**
89  * Allocate n objects of a certain type on the stack and zero them
90  */
91 #define ALLOCANZ(type, n) ((type*)memset((type*)alloca(sizeof(type) * (n)), 0, sizeof(type) * (n)))
92
93 /**
94  * Allocate n objects of a certain type on the given obstack
95  */
96 #define OALLOCN(obst, type, n) ((type*)obstack_alloc((obst), sizeof(type) * (n)))
97
98 /**
99  * Allocate n objects of a certain type on the given obstack and zero them
100  */
101 #define OALLOCNZ(obst, type, n) ((type*)memset(OALLOCN((obst), type, (n)), 0, sizeof(type) * (n)))
102
103 /**
104  * Allocate one object of a certain type on the given obstack
105  */
106 #define OALLOC(obst, type) OALLOCN(obst, type, 1)
107
108 /**
109  * Allocate one object of a certain type on the given obstack and zero it
110  */
111 #define OALLOCZ(obst, type) OALLOCNZ(obst, type, 1)
112
113 /**
114  * Allocate an object with n elements of a flexible array member on the given
115  * obstck
116  */
117 #define OALLOCF(obst, type, member, n) ((type*)obstack_alloc((obst), offsetof(type, member) + sizeof(*((type*)0)->member) * (n)))
118
119 /**
120  * Allocate an object with n elements of a flexible array member on the given
121  * obstack and zero the whole object
122  */
123 #define OALLOCFZ(obst, type, member, n) ((type*)memset(OALLOCF((obst), type, member, (n)), 0, offsetof(type, member) + sizeof(*((type*)0)->member) * (n)))
124
125
126 /* Includes for alloca() */
127 #ifdef _WIN32
128 #include <malloc.h>
129 #endif
130 #ifdef HAVE_ALLOCA_H
131 #include <alloca.h>
132 #endif
133
134 #ifdef __cplusplus
135 }
136 #endif
137
138 #endif