Mercurial > projects > ldc
comparison druntime/src/common/core/memory.d @ 1458:e0b2d67cfe7c
Added druntime (this should be removed once it works).
author | Robert Clipsham <robert@octarineparrot.com> |
---|---|
date | Tue, 02 Jun 2009 17:43:06 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
1456:7b218ec1044f | 1458:e0b2d67cfe7c |
---|---|
1 /** | |
2 * The memory module provides an interface to the garbage collector and to | |
3 * any other OS or API-level memory management facilities. | |
4 * | |
5 * Copyright: Copyright Sean Kelly 2005 - 2009. | |
6 * License: <a href="http://www.boost.org/LICENSE_1_0.txt>Boost License 1.0</a>. | |
7 * Authors: Sean Kelly | |
8 * | |
9 * Copyright Sean Kelly 2005 - 2009. | |
10 * Distributed under the Boost Software License, Version 1.0. | |
11 * (See accompanying file LICENSE_1_0.txt or copy at | |
12 * http://www.boost.org/LICENSE_1_0.txt) | |
13 */ | |
14 module core.memory; | |
15 | |
16 | |
17 private | |
18 { | |
19 extern (C) void gc_init(); | |
20 extern (C) void gc_term(); | |
21 | |
22 extern (C) void gc_enable(); | |
23 extern (C) void gc_disable(); | |
24 extern (C) void gc_collect(); | |
25 extern (C) void gc_minimize(); | |
26 | |
27 extern (C) uint gc_getAttr( void* p ); | |
28 extern (C) uint gc_setAttr( void* p, uint a ); | |
29 extern (C) uint gc_clrAttr( void* p, uint a ); | |
30 | |
31 extern (C) void* gc_malloc( size_t sz, uint ba = 0 ); | |
32 extern (C) void* gc_calloc( size_t sz, uint ba = 0 ); | |
33 extern (C) void* gc_realloc( void* p, size_t sz, uint ba = 0 ); | |
34 extern (C) size_t gc_extend( void* p, size_t mx, size_t sz ); | |
35 extern (C) size_t gc_reserve( size_t sz ); | |
36 extern (C) void gc_free( void* p ); | |
37 | |
38 extern (C) void* gc_addrOf( void* p ); | |
39 extern (C) size_t gc_sizeOf( void* p ); | |
40 | |
41 struct BlkInfo_ | |
42 { | |
43 void* base; | |
44 size_t size; | |
45 uint attr; | |
46 } | |
47 | |
48 extern (C) BlkInfo_ gc_query( void* p ); | |
49 | |
50 extern (C) void gc_addRoot( void* p ); | |
51 extern (C) void gc_addRange( void* p, size_t sz ); | |
52 | |
53 extern (C) void gc_removeRoot( void* p ); | |
54 extern (C) void gc_removeRange( void* p ); | |
55 } | |
56 | |
57 | |
58 /** | |
59 * This struct encapsulates all garbage collection functionality for the D | |
60 * programming language. | |
61 */ | |
62 struct GC | |
63 { | |
64 /** | |
65 * Enables automatic garbage collection behavior if collections have | |
66 * previously been suspended by a call to disable. This function is | |
67 * reentrant, and must be called once for every call to disable before | |
68 * automatic collections are enabled. | |
69 */ | |
70 static void enable() | |
71 { | |
72 gc_enable(); | |
73 } | |
74 | |
75 | |
76 /** | |
77 * Disables automatic garbage collections performed to minimize the | |
78 * process footprint. Collections may continue to occur in instances | |
79 * where the implementation deems necessary for correct program behavior, | |
80 * such as during an out of memory condition. This function is reentrant, | |
81 * but enable must be called once for each call to disable. | |
82 */ | |
83 static void disable() | |
84 { | |
85 gc_disable(); | |
86 } | |
87 | |
88 | |
89 /** | |
90 * Begins a full collection. While the meaning of this may change based | |
91 * on the garbage collector implementation, typical behavior is to scan | |
92 * all stack segments for roots, mark accessible memory blocks as alive, | |
93 * and then to reclaim free space. This action may need to suspend all | |
94 * running threads for at least part of the collection process. | |
95 */ | |
96 static void collect() | |
97 { | |
98 gc_collect(); | |
99 } | |
100 | |
101 /** | |
102 * Indicates that the managed memory space be minimized by returning free | |
103 * physical memory to the operating system. The amount of free memory | |
104 * returned depends on the allocator design and on program behavior. | |
105 */ | |
106 static void minimize() | |
107 { | |
108 gc_minimize(); | |
109 } | |
110 | |
111 | |
112 /** | |
113 * Elements for a bit field representing memory block attributes. These | |
114 * are manipulated via the getAttr, setAttr, clrAttr functions. | |
115 */ | |
116 enum BlkAttr : uint | |
117 { | |
118 FINALIZE = 0b0000_0001, /// Finalize the data in this block on collect. | |
119 NO_SCAN = 0b0000_0010, /// Do not scan through this block on collect. | |
120 NO_MOVE = 0b0000_0100 /// Do not move this memory block on collect. | |
121 } | |
122 | |
123 | |
124 /** | |
125 * Contains aggregate information about a block of managed memory. The | |
126 * purpose of this struct is to support a more efficient query style in | |
127 * instances where detailed information is needed. | |
128 * | |
129 * base = A pointer to the base of the block in question. | |
130 * size = The size of the block, calculated from base. | |
131 * attr = Attribute bits set on the memory block. | |
132 */ | |
133 alias BlkInfo_ BlkInfo; | |
134 | |
135 | |
136 /** | |
137 * Returns a bit field representing all block attributes set for the memory | |
138 * referenced by p. If p references memory not originally allocated by | |
139 * this garbage collector, points to the interior of a memory block, or if | |
140 * p is null, zero will be returned. | |
141 * | |
142 * Params: | |
143 * p = A pointer to the root of a valid memory block or to null. | |
144 * | |
145 * Returns: | |
146 * A bit field containing any bits set for the memory block referenced by | |
147 * p or zero on error. | |
148 */ | |
149 static uint getAttr( void* p ) | |
150 { | |
151 return gc_getAttr( p ); | |
152 } | |
153 | |
154 | |
155 /** | |
156 * Sets the specified bits for the memory references by p. If p references | |
157 * memory not originally allocated by this garbage collector, points to the | |
158 * interior of a memory block, or if p is null, no action will be | |
159 * performed. | |
160 * | |
161 * Params: | |
162 * p = A pointer to the root of a valid memory block or to null. | |
163 * a = A bit field containing any bits to set for this memory block. | |
164 * | |
165 * The result of a call to getAttr after the specified bits have been | |
166 * set. | |
167 */ | |
168 static uint setAttr( void* p, uint a ) | |
169 { | |
170 return gc_setAttr( p, a ); | |
171 } | |
172 | |
173 | |
174 /** | |
175 * Clears the specified bits for the memory references by p. If p | |
176 * references memory not originally allocated by this garbage collector, | |
177 * points to the interior of a memory block, or if p is null, no action | |
178 * will be performed. | |
179 * | |
180 * Params: | |
181 * p = A pointer to the root of a valid memory block or to null. | |
182 * a = A bit field containing any bits to clear for this memory block. | |
183 * | |
184 * Returns: | |
185 * The result of a call to getAttr after the specified bits have been | |
186 * cleared. | |
187 */ | |
188 static uint clrAttr( void* p, uint a ) | |
189 { | |
190 return gc_clrAttr( p, a ); | |
191 } | |
192 | |
193 | |
194 /** | |
195 * Requests an aligned block of managed memory from the garbage collector. | |
196 * This memory may be deleted at will with a call to free, or it may be | |
197 * discarded and cleaned up automatically during a collection run. If | |
198 * allocation fails, this function will call onOutOfMemory which is | |
199 * expected to throw an OutOfMemoryException. | |
200 * | |
201 * Params: | |
202 * sz = The desired allocation size in bytes. | |
203 * ba = A bitmask of the attributes to set on this block. | |
204 * | |
205 * Returns: | |
206 * A reference to the allocated memory or null if insufficient memory | |
207 * is available. | |
208 * | |
209 * Throws: | |
210 * OutOfMemoryException on allocation failure. | |
211 */ | |
212 static void* malloc( size_t sz, uint ba = 0 ) | |
213 { | |
214 return gc_malloc( sz, ba ); | |
215 } | |
216 | |
217 | |
218 /** | |
219 * Requests an aligned block of managed memory from the garbage collector, | |
220 * which is initialized with all bits set to zero. This memory may be | |
221 * deleted at will with a call to free, or it may be discarded and cleaned | |
222 * up automatically during a collection run. If allocation fails, this | |
223 * function will call onOutOfMemory which is expected to throw an | |
224 * OutOfMemoryException. | |
225 * | |
226 * Params: | |
227 * sz = The desired allocation size in bytes. | |
228 * ba = A bitmask of the attributes to set on this block. | |
229 * | |
230 * Returns: | |
231 * A reference to the allocated memory or null if insufficient memory | |
232 * is available. | |
233 * | |
234 * Throws: | |
235 * OutOfMemoryException on allocation failure. | |
236 */ | |
237 static void* calloc( size_t sz, uint ba = 0 ) | |
238 { | |
239 return gc_calloc( sz, ba ); | |
240 } | |
241 | |
242 | |
243 /** | |
244 * If sz is zero, the memory referenced by p will be deallocated as if | |
245 * by a call to free. A new memory block of size sz will then be | |
246 * allocated as if by a call to malloc, or the implementation may instead | |
247 * resize the memory block in place. The contents of the new memory block | |
248 * will be the same as the contents of the old memory block, up to the | |
249 * lesser of the new and old sizes. Note that existing memory will only | |
250 * be freed by realloc if sz is equal to zero. The garbage collector is | |
251 * otherwise expected to later reclaim the memory block if it is unused. | |
252 * If allocation fails, this function will call onOutOfMemory which is | |
253 * expected to throw an OutOfMemoryException. If p references memory not | |
254 * originally allocated by this garbage collector, or if it points to the | |
255 * interior of a memory block, no action will be taken. If ba is zero | |
256 * (the default) and p references the head of a valid, known memory block | |
257 * then any bits set on the current block will be set on the new block if a | |
258 * reallocation is required. If ba is not zero and p references the head | |
259 * of a valid, known memory block then the bits in ba will replace those on | |
260 * the current memory block and will also be set on the new block if a | |
261 * reallocation is required. | |
262 * | |
263 * Params: | |
264 * p = A pointer to the root of a valid memory block or to null. | |
265 * sz = The desired allocation size in bytes. | |
266 * ba = A bitmask of the attributes to set on this block. | |
267 * | |
268 * Returns: | |
269 * A reference to the allocated memory on success or null if sz is | |
270 * zero. On failure, the original value of p is returned. | |
271 * | |
272 * Throws: | |
273 * OutOfMemoryException on allocation failure. | |
274 */ | |
275 static void* realloc( void* p, size_t sz, uint ba = 0 ) | |
276 { | |
277 return gc_realloc( p, sz, ba ); | |
278 } | |
279 | |
280 | |
281 /** | |
282 * Requests that the managed memory block referenced by p be extended in | |
283 * place by at least mx bytes, with a desired extension of sz bytes. If an | |
284 * extension of the required size is not possible, if p references memory | |
285 * not originally allocated by this garbage collector, or if p points to | |
286 * the interior of a memory block, no action will be taken. | |
287 * | |
288 * Params: | |
289 * mx = The minimum extension size in bytes. | |
290 * sz = The desired extension size in bytes. | |
291 * | |
292 * Returns: | |
293 * The size in bytes of the extended memory block referenced by p or zero | |
294 * if no extension occurred. | |
295 */ | |
296 static size_t extend( void* p, size_t mx, size_t sz ) | |
297 { | |
298 return gc_extend( p, mx, sz ); | |
299 } | |
300 | |
301 | |
302 /** | |
303 * Requests that at least sz bytes of memory be obtained from the operating | |
304 * system and marked as free. | |
305 * | |
306 * Params: | |
307 * sz = The desired size in bytes. | |
308 * | |
309 * Returns: | |
310 * The actual number of bytes reserved or zero on error. | |
311 */ | |
312 static size_t reserve( size_t sz ) | |
313 { | |
314 return gc_reserve( sz ); | |
315 } | |
316 | |
317 | |
318 /** | |
319 * Deallocates the memory referenced by p. If p is null, no action | |
320 * occurs. If p references memory not originally allocated by this | |
321 * garbage collector, or if it points to the interior of a memory block, | |
322 * no action will be taken. The block will not be finalized regardless | |
323 * of whether the FINALIZE attribute is set. If finalization is desired, | |
324 * use delete instead. | |
325 * | |
326 * Params: | |
327 * p = A pointer to the root of a valid memory block or to null. | |
328 */ | |
329 static void free( void* p ) | |
330 { | |
331 gc_free( p ); | |
332 } | |
333 | |
334 | |
335 /** | |
336 * Returns the base address of the memory block containing p. This value | |
337 * is useful to determine whether p is an interior pointer, and the result | |
338 * may be passed to routines such as sizeOf which may otherwise fail. If p | |
339 * references memory not originally allocated by this garbage collector, if | |
340 * p is null, or if the garbage collector does not support this operation, | |
341 * null will be returned. | |
342 * | |
343 * Params: | |
344 * p = A pointer to the root or the interior of a valid memory block or to | |
345 * null. | |
346 * | |
347 * Returns: | |
348 * The base address of the memory block referenced by p or null on error. | |
349 */ | |
350 static void* addrOf( void* p ) | |
351 { | |
352 return gc_addrOf( p ); | |
353 } | |
354 | |
355 | |
356 /** | |
357 * Returns the true size of the memory block referenced by p. This value | |
358 * represents the maximum number of bytes for which a call to realloc may | |
359 * resize the existing block in place. If p references memory not | |
360 * originally allocated by this garbage collector, points to the interior | |
361 * of a memory block, or if p is null, zero will be returned. | |
362 * | |
363 * Params: | |
364 * p = A pointer to the root of a valid memory block or to null. | |
365 * | |
366 * Returns: | |
367 * The size in bytes of the memory block referenced by p or zero on error. | |
368 */ | |
369 static size_t sizeOf( void* p ) | |
370 { | |
371 return gc_sizeOf( p ); | |
372 } | |
373 | |
374 | |
375 /** | |
376 * Returns aggregate information about the memory block containing p. If p | |
377 * references memory not originally allocated by this garbage collector, if | |
378 * p is null, or if the garbage collector does not support this operation, | |
379 * BlkInfo.init will be returned. Typically, support for this operation | |
380 * is dependent on support for addrOf. | |
381 * | |
382 * Params: | |
383 * p = A pointer to the root or the interior of a valid memory block or to | |
384 * null. | |
385 * | |
386 * Returns: | |
387 * Information regarding the memory block referenced by p or BlkInfo.init | |
388 * on error. | |
389 */ | |
390 static BlkInfo query( void* p ) | |
391 { | |
392 return gc_query( p ); | |
393 } | |
394 | |
395 | |
396 /** | |
397 * Adds the memory address referenced by p to an internal list of roots to | |
398 * be scanned during a collection. If p is null, no operation is | |
399 * performed. | |
400 * | |
401 * Params: | |
402 * p = A pointer to a valid memory address or to null. | |
403 */ | |
404 static void addRoot( void* p ) | |
405 { | |
406 gc_addRoot( p ); | |
407 } | |
408 | |
409 | |
410 /** | |
411 * Adds the memory block referenced by p and of size sz to an internal list | |
412 * of ranges to be scanned during a collection. If p is null, no operation | |
413 * is performed. | |
414 * | |
415 * Params: | |
416 * p = A pointer to a valid memory address or to null. | |
417 * sz = The size in bytes of the block to add. If sz is zero then the | |
418 * no operation will occur. If p is null then sz must be zero. | |
419 */ | |
420 static void addRange( void* p, size_t sz ) | |
421 { | |
422 gc_addRange( p, sz ); | |
423 } | |
424 | |
425 | |
426 /** | |
427 * Removes the memory block referenced by p from an internal list of roots | |
428 * to be scanned during a collection. If p is null or does not represent | |
429 * a value previously passed to add(void*) then no operation is performed. | |
430 * | |
431 * p = A pointer to a valid memory address or to null. | |
432 */ | |
433 static void removeRoot( void* p ) | |
434 { | |
435 gc_removeRoot( p ); | |
436 } | |
437 | |
438 | |
439 /** | |
440 * Removes the memory block referenced by p from an internal list of ranges | |
441 * to be scanned during a collection. If p is null or does not represent | |
442 * a value previously passed to add(void*, size_t) then no operation is | |
443 * performed. | |
444 * | |
445 * Params: | |
446 * p = A pointer to a valid memory address or to null. | |
447 */ | |
448 static void removeRange( void* p ) | |
449 { | |
450 gc_removeRange( p ); | |
451 } | |
452 } |