* @file
* @brief never failing wrappers for malloc() & friends.
* @author Markus Armbruster
- * @version $Id$
* @note The functions here never fail because they simply abort your
* program in case of an error.
*/
#include <stddef.h>
#include <stdlib.h>
+#include <string.h>
+
+/* Includes for alloca() */
+#ifdef _WIN32
+#include <malloc.h>
+#endif
+
+#include "../begin.h"
/* xmalloc() & friends. */
-void *xmalloc(size_t size);
-void *xrealloc(void *ptr, size_t size);
-char *xstrdup(const char *str);
+FIRM_API void *xmalloc(size_t size);
+FIRM_API void *xrealloc(void *ptr, size_t size);
+FIRM_API char *xstrdup(const char *str);
#define xfree(ptr) free(ptr)
/**
* Allocate an object with n elements of a flexible array member
*/
-#define XMALLOCF(type, member, n) ((type*)xmalloc(offsetof(type, member) + sizeof(((type*)0)->member) * (n)))
+#define XMALLOCF(type, member, n) ((type*)xmalloc(offsetof(type, member) + sizeof(*((type*)0)->member) * (n)))
/**
* Allocate an object with n elements of a flexible array member and zero the
* whole object
*/
-#define XMALLOCFZ(type, member, n) ((type*)memset(xmalloc(offsetof(type, member) + sizeof(((type*)0)->member) * (n)), 0, offsetof(type, member) + sizeof(((type*)0)->member) * (n)))
+#define XMALLOCFZ(type, member, n) ((type*)memset(XMALLOCF(type, member, (n)), 0, offsetof(type, member) + sizeof(*((type*)0)->member) * (n)))
+/**
+ * Allocate n objects of a certain type on the stack
+ */
+#define ALLOCAN(type, n) ((type*)alloca(sizeof(type) * (n)))
-/* Includes for alloca() */
-#if defined(__FreeBSD__)
-#include <stdlib.h>
-#elif defined(_WIN32)
-#include <malloc.h>
-#else
-#include <alloca.h>
-#endif
+/**
+ * Allocate n objects of a certain type on the stack and zero them
+ */
+#define ALLOCANZ(type, n) ((type*)memset((type*)alloca(sizeof(type) * (n)), 0, sizeof(type) * (n)))
+
+/**
+ * Allocate n objects of a certain type on the given obstack
+ */
+#define OALLOCN(obst, type, n) ((type*)obstack_alloc((obst), sizeof(type) * (n)))
+
+/**
+ * Allocate n objects of a certain type on the given obstack and zero them
+ */
+#define OALLOCNZ(obst, type, n) ((type*)memset(OALLOCN((obst), type, (n)), 0, sizeof(type) * (n)))
+
+/**
+ * Allocate one object of a certain type on the given obstack
+ */
+#define OALLOC(obst, type) OALLOCN(obst, type, 1)
+
+/**
+ * Allocate one object of a certain type on the given obstack and zero it
+ */
+#define OALLOCZ(obst, type) OALLOCNZ(obst, type, 1)
+
+/**
+ * Allocate an object with n elements of a flexible array member on the given
+ * obstck
+ */
+#define OALLOCF(obst, type, member, n) ((type*)obstack_alloc((obst), offsetof(type, member) + sizeof(*((type*)0)->member) * (n)))
+
+/**
+ * Allocate an object with n elements of a flexible array member on the given
+ * obstack and zero the whole object
+ */
+#define OALLOCFZ(obst, type, member, n) ((type*)memset(OALLOCF((obst), type, member, (n)), 0, offsetof(type, member) + sizeof(*((type*)0)->member) * (n)))
+
+
+#include "../end.h"
#endif