comparison dmd2/mem.c @ 758:f04dde6e882c

Added initial D2 support, D2 frontend and changes to codegen to make things compile.
author Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
date Tue, 11 Nov 2008 01:38:48 +0100
parents
children 638d16625da2
comparison
equal deleted inserted replaced
757:2c730d530c98 758:f04dde6e882c
1
2 /* Copyright (c) 2000 Digital Mars */
3 /* All Rights Reserved */
4
5 #include <cstdio>
6 #include <cstdlib>
7 #include <cstring>
8 #include <cassert>
9
10 #include "mem.h"
11
12 #if USE_BOEHM_GC
13 // I needed to perfix the dir after upgrading to gc 7.0
14 #include "gc/gc.h"
15 #endif
16
17 /* This implementation of the storage allocator uses the standard C allocation package.
18 */
19
20 Mem mem;
21
22 #if USE_BOEHM_GC
23
24 static bool gc_was_init = false;
25
26 void Mem::init()
27 {
28 GC_init();
29 gc_was_init = true;
30 }
31
32 char *Mem::strdup(const char *s)
33 {
34 char *p;
35
36 if (s)
37 {
38 p = GC_strdup(s);
39 if (p)
40 return p;
41 error();
42 }
43 return NULL;
44 }
45
46 void *Mem::malloc(size_t size)
47 { void *p;
48
49 if (!size)
50 p = NULL;
51 else
52 {
53 p = GC_malloc(size);
54 if (!p)
55 error();
56 }
57 return p;
58 }
59
60 void *Mem::calloc(size_t size, size_t n)
61 { void *p;
62
63 if (!size || !n)
64 p = NULL;
65 else
66 {
67 p = GC_malloc(size * n);
68 if (!p)
69 error();
70 memset(p, 0, size * n);
71 }
72 return p;
73 }
74
75 void *Mem::realloc(void *p, size_t size)
76 {
77 if (!size)
78 { if (p)
79 { GC_free(p);
80 p = NULL;
81 }
82 }
83 else if (!p)
84 {
85 p = GC_malloc(size);
86 if (!p)
87 error();
88 }
89 else
90 {
91 p = GC_realloc(p, size);
92 if (!p)
93 error();
94 }
95 return p;
96 }
97
98 void Mem::free(void *p)
99 {
100 if (p)
101 GC_free(p);
102 }
103
104 void *Mem::mallocdup(void *o, size_t size)
105 { void *p;
106
107 if (!size)
108 p = NULL;
109 else
110 {
111 p = GC_malloc(size);
112 if (!p)
113 error();
114 else
115 memcpy(p,o,size);
116 }
117 return p;
118 }
119
120 void Mem::error()
121 {
122 printf("Error: out of memory\n");
123 exit(EXIT_FAILURE);
124 }
125
126 void Mem::fullcollect()
127 {
128 GC_gcollect();
129 }
130
131 void Mem::mark(void *pointer)
132 {
133 (void) pointer; // necessary for VC /W4
134 }
135
136 /* =================================================== */
137
138 void * operator new(size_t m_size)
139 {
140 // without this we segfault with gc 7.0
141 if (!gc_was_init) {
142 mem.init();
143 }
144 void *p = GC_malloc(m_size);
145 if (p)
146 return p;
147 printf("Error: out of memory\n");
148 exit(EXIT_FAILURE);
149 return p;
150 }
151
152 void operator delete(void *p)
153 {
154 GC_free(p);
155 }
156
157 #elif !USE_BOEHM_GC
158
159 void Mem::init()
160 {
161 }
162
163 char *Mem::strdup(const char *s)
164 {
165 char *p;
166
167 if (s)
168 {
169 p = ::strdup(s);
170 if (p)
171 return p;
172 error();
173 }
174 return NULL;
175 }
176
177 void *Mem::malloc(size_t size)
178 { void *p;
179
180 if (!size)
181 p = NULL;
182 else
183 {
184 p = ::malloc(size);
185 if (!p)
186 error();
187 }
188 return p;
189 }
190
191 void *Mem::calloc(size_t size, size_t n)
192 { void *p;
193
194 if (!size || !n)
195 p = NULL;
196 else
197 {
198 p = ::malloc(size * n);
199 if (!p)
200 error();
201 memset(p, 0, size * n);
202 }
203 return p;
204 }
205
206 void *Mem::realloc(void *p, size_t size)
207 {
208 if (!size)
209 { if (p)
210 { ::free(p);
211 p = NULL;
212 }
213 }
214 else if (!p)
215 {
216 p = ::malloc(size);
217 if (!p)
218 error();
219 }
220 else
221 {
222 p = ::realloc(p, size);
223 if (!p)
224 error();
225 }
226 return p;
227 }
228
229 void Mem::free(void *p)
230 {
231 if (p)
232 ::free(p);
233 }
234
235 void *Mem::mallocdup(void *o, size_t size)
236 { void *p;
237
238 if (!size)
239 p = NULL;
240 else
241 {
242 p = ::malloc(size);
243 if (!p)
244 error();
245 else
246 memcpy(p,o,size);
247 }
248 return p;
249 }
250
251 void Mem::error()
252 {
253 printf("Error: out of memory\n");
254 exit(EXIT_FAILURE);
255 }
256
257 void Mem::fullcollect()
258 {
259 }
260
261 void Mem::mark(void *pointer)
262 {
263 }
264
265 #endif // USE_BOEHM_GC