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