libFirm
xmalloc.h
1 /*
2  * This file is part of libFirm.
3  * Copyright (C) 2012 University of Karlsruhe.
4  */
5 
13 #ifndef FIRM_ADT_XMALLOC_H
14 #define FIRM_ADT_XMALLOC_H
15 
16 #include <stddef.h>
17 #include <stdlib.h>
18 #include <string.h>
19 
20 /* Includes for alloca() */
21 #ifndef alloca
22 # if defined(__GNUC__)
23 # define alloca(x) __builtin_alloca(x)
24 # elif defined(__WIN32)
25 # include <malloc.h>
26 # else
27 # error do not know how to get alloca
28 # endif
29 #endif
30 
31 #include "../begin.h"
32 
44 FIRM_API void *xmalloc(size_t size);
50 FIRM_API void *xrealloc(void *ptr, size_t size);
56 FIRM_API char *xstrdup(const char *str);
57 
61 #define XMALLOCN(type, n) ((type*)xmalloc(sizeof(type) * (n)))
62 
66 #define XMALLOCNZ(type, n) ((type*)memset(xmalloc(sizeof(type) * (n)), 0, sizeof(type) * (n)))
67 
71 #define XMALLOC(type) XMALLOCN(type, 1)
72 
76 #define XMALLOCZ(type) XMALLOCNZ(type, 1)
77 
81 #define XREALLOC(ptr, type, n) ((type*)xrealloc(ptr, sizeof(type) * (n)))
82 
86 #define XMALLOCF(type, member, n) ((type*)xmalloc(offsetof(type, member) + sizeof(*((type*)0)->member) * (n)))
87 
92 #define XMALLOCFZ(type, member, n) ((type*)memset(XMALLOCF(type, member, (n)), 0, offsetof(type, member) + sizeof(*((type*)0)->member) * (n)))
93 
97 #define ALLOCAN(type, n) ((type*)alloca(sizeof(type) * (n)))
98 
102 #define ALLOCANZ(type, n) ((type*)memset((type*)alloca(sizeof(type) * (n)), 0, sizeof(type) * (n)))
103 
107 #define ALLOCAF(type, member, n) ((type*)alloca(offsetof(type, member) + sizeof(*((type*)0)->member) * (n)))
108 
112 #define OALLOCN(obst, type, n) ((type*)obstack_alloc((obst), sizeof(type) * (n)))
113 
117 #define OALLOCNZ(obst, type, n) ((type*)memset(OALLOCN((obst), type, (n)), 0, sizeof(type) * (n)))
118 
122 #define OALLOC(obst, type) OALLOCN(obst, type, 1)
123 
127 #define OALLOCZ(obst, type) OALLOCNZ(obst, type, 1)
128 
133 #define OALLOCF(obst, type, member, n) ((type*)obstack_alloc((obst), offsetof(type, member) + sizeof(*((type*)0)->member) * (n)))
134 
139 #define OALLOCFZ(obst, type, member, n) ((type*)memset(OALLOCF((obst), type, member, (n)), 0, offsetof(type, member) + sizeof(*((type*)0)->member) * (n)))
140 
143 #include "../end.h"
144 
145 #endif
char * xstrdup(const char *str)
Allocates memory and copies string str into it.
void * xrealloc(void *ptr, size_t size)
Chane size of a previously allocated memory block to size bytes.
void * xmalloc(size_t size)
Allocate size bytes on the heap.