X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=include%2Flibfirm%2Fadt%2Fxmalloc.h;h=d9d8d75fce0c5a964a63049e2967d11cc0e520c2;hb=4d9d63f689d1afa836e69754ce09ae075d7de478;hp=30925b98960a668d8bafd111d90e752643a08452;hpb=435253f3a8766fec2ac2adf559bb10cfcb5d36f4;p=libfirm diff --git a/include/libfirm/adt/xmalloc.h b/include/libfirm/adt/xmalloc.h index 30925b989..d9d8d75fc 100644 --- a/include/libfirm/adt/xmalloc.h +++ b/include/libfirm/adt/xmalloc.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 1995-2007 University of Karlsruhe. All right reserved. + * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved. * * This file is part of libFirm. * @@ -28,26 +28,78 @@ #ifndef FIRM_ADT_XMALLOC_H #define FIRM_ADT_XMALLOC_H +#ifdef __cplusplus +extern "C" { +#endif + #include +#include /* xmalloc() & friends. */ void *xmalloc(size_t size); -void *xcalloc(size_t num, size_t size); void *xrealloc(void *ptr, size_t size); char *xstrdup(const char *str); -void free(void *ptr); #define xfree(ptr) free(ptr) +/** + * Allocate n objects of a certain type + */ +#define XMALLOCN(type, n) ((type*)xmalloc(sizeof(type) * (n))) + +/** + * Allocate n objects of a certain type and zero them + */ +#define XMALLOCNZ(type, n) ((type*)memset(xmalloc(sizeof(type) * (n)), 0, sizeof(type) * (n))) + +/** + * Allocate one object of a certain type + */ +#define XMALLOC(type) XMALLOCN(type, 1) + +/** + * Allocate one object of a certain type and zero it + */ +#define XMALLOCZ(type) XMALLOCNZ(type, 1) + +/** + * Reallocate n objects of a certain type + */ +#define XREALLOC(ptr, type, n) ((type*)xrealloc(ptr, sizeof(type) * (n))) + +/** + * 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))) + +/** + * 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))) + +/** + * Allocate n objects of a certain type on the stack + */ +#define ALLOCAN(type, n) ((type*)alloca(sizeof(type) * (n))) + +/** + * 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))) + /* Includes for alloca() */ -#if defined(__FreeBSD__) -#include -#elif defined(_WIN32) +#ifdef _WIN32 #include -#else +#endif +#ifdef HAVE_ALLOCA_H #include #endif +#ifdef __cplusplus +} +#endif + #endif