2 * This file is part of libFirm.
3 * Copyright (C) 2012 University of Karlsruhe.
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.
13 #ifndef FIRM_ADT_XMALLOC_H
14 #define FIRM_ADT_XMALLOC_H
20 /* Includes for alloca() */
24 #if defined(__linux__) || defined(__APPLE__)
32 * @defgroup xmalloc Memory Allocation
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.
41 FIRM_API void *xmalloc(size_t size);
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.
47 FIRM_API void *xrealloc(void *ptr, size_t size);
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.
53 FIRM_API char *xstrdup(const char *str);
55 * Another name for the free function
58 #define xfree(ptr) free(ptr)
61 * Allocate n objects of a certain type
63 #define XMALLOCN(type, n) ((type*)xmalloc(sizeof(type) * (n)))
66 * Allocate n objects of a certain type and zero them
68 #define XMALLOCNZ(type, n) ((type*)memset(xmalloc(sizeof(type) * (n)), 0, sizeof(type) * (n)))
71 * Allocate one object of a certain type
73 #define XMALLOC(type) XMALLOCN(type, 1)
76 * Allocate one object of a certain type and zero it
78 #define XMALLOCZ(type) XMALLOCNZ(type, 1)
81 * Reallocate n objects of a certain type
83 #define XREALLOC(ptr, type, n) ((type*)xrealloc(ptr, sizeof(type) * (n)))
86 * Allocate an object with n elements of a flexible array member
88 #define XMALLOCF(type, member, n) ((type*)xmalloc(offsetof(type, member) + sizeof(*((type*)0)->member) * (n)))
91 * Allocate an object with n elements of a flexible array member and zero the
94 #define XMALLOCFZ(type, member, n) ((type*)memset(XMALLOCF(type, member, (n)), 0, offsetof(type, member) + sizeof(*((type*)0)->member) * (n)))
97 * Allocate n objects of a certain type on the stack
99 #define ALLOCAN(type, n) ((type*)alloca(sizeof(type) * (n)))
102 * Allocate n objects of a certain type on the stack and zero them
104 #define ALLOCANZ(type, n) ((type*)memset((type*)alloca(sizeof(type) * (n)), 0, sizeof(type) * (n)))
107 * Allocate n objects of a certain type on the given obstack
109 #define OALLOCN(obst, type, n) ((type*)obstack_alloc((obst), sizeof(type) * (n)))
112 * Allocate n objects of a certain type on the given obstack and zero them
114 #define OALLOCNZ(obst, type, n) ((type*)memset(OALLOCN((obst), type, (n)), 0, sizeof(type) * (n)))
117 * Allocate one object of a certain type on the given obstack
119 #define OALLOC(obst, type) OALLOCN(obst, type, 1)
122 * Allocate one object of a certain type on the given obstack and zero it
124 #define OALLOCZ(obst, type) OALLOCNZ(obst, type, 1)
127 * Allocate an object with n elements of a flexible array member on the given
130 #define OALLOCF(obst, type, member, n) ((type*)obstack_alloc((obst), offsetof(type, member) + sizeof(*((type*)0)->member) * (n)))
133 * Allocate an object with n elements of a flexible array member on the given
134 * obstack and zero the whole object
136 #define OALLOCFZ(obst, type, member, n) ((type*)memset(OALLOCF((obst), type, member, (n)), 0, offsetof(type, member) + sizeof(*((type*)0)->member) * (n)))