X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=include%2Flibfirm%2Fadt%2Fxmalloc.h;h=b07d0b7a22d2c23eb911fcbea8a87cf69267491a;hb=07c77ebb4b165865ecdfc45a048ac55dea14d84e;hp=d9d8d75fce0c5a964a63049e2967d11cc0e520c2;hpb=e84184056559e90f2dffc7f7648f40705864e088;p=libfirm diff --git a/include/libfirm/adt/xmalloc.h b/include/libfirm/adt/xmalloc.h index d9d8d75fc..b07d0b7a2 100644 --- a/include/libfirm/adt/xmalloc.h +++ b/include/libfirm/adt/xmalloc.h @@ -21,26 +21,51 @@ * @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. */ #ifndef FIRM_ADT_XMALLOC_H #define FIRM_ADT_XMALLOC_H -#ifdef __cplusplus -extern "C" { -#endif - #include #include +#include + +/* Includes for alloca() */ +#ifdef _WIN32 +#include +#endif -/* xmalloc() & friends. */ +#include "../begin.h" -void *xmalloc(size_t size); -void *xrealloc(void *ptr, size_t size); -char *xstrdup(const char *str); +/** + * @ingroup adt + * @defgroup xmalloc Memory Allocation + * @{ + */ +/** + * Allocate @p size bytes on the heap. + * This is a wrapper for malloc which calls panic() in case of errors, so no + * error handling is required for code using it. + */ +FIRM_API void *xmalloc(size_t size); +/** + * Chane size of a previously allocated memory block to @p size bytes. + * This is a wrapper for realloc which calls panic() in case of errors, so no + * error handling is required for code using it. + */ +FIRM_API void *xrealloc(void *ptr, size_t size); +/** + * Allocates memory and copies string @p str into it. + * This is a wrapper for strdup which calls panic() in case of errors, so no + * error handling is required for code using it. + */ +FIRM_API char *xstrdup(const char *str); +/** + * Another name for the free function + * @deprecated + */ #define xfree(ptr) free(ptr) /** @@ -71,13 +96,13 @@ char *xstrdup(const char *str); /** * 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 @@ -89,17 +114,40 @@ char *xstrdup(const char *str); */ #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))) -/* Includes for alloca() */ -#ifdef _WIN32 -#include -#endif -#ifdef HAVE_ALLOCA_H -#include -#endif +/** + * 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))) -#ifdef __cplusplus -} -#endif +/** + * 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