d: Merge upstream dmd, druntime 26f049fb26, phobos 330d6a4fd.
[official-gcc.git] / libphobos / libdruntime / core / memory.d
blob96c2af478d39e0923129ef4aacf47d445fc8a49b
1 /**
2 * This module provides an interface to the garbage collector used by
3 * applications written in the D programming language. It allows the
4 * garbage collector in the runtime to be swapped without affecting
5 * binary compatibility of applications.
7 * Using this module is not necessary in typical D code. It is mostly
8 * useful when doing low-level _memory management.
10 * Notes_to_users:
12 $(OL
13 $(LI The GC is a conservative mark-and-sweep collector. It only runs a
14 collection cycle when an allocation is requested of it, never
15 otherwise. Hence, if the program is not doing allocations,
16 there will be no GC collection pauses. The pauses occur because
17 all threads the GC knows about are halted so the threads' stacks
18 and registers can be scanned for references to GC allocated data.
21 $(LI The GC does not know about threads that were created by directly calling
22 the OS/C runtime thread creation APIs and D threads that were detached
23 from the D runtime after creation.
24 Such threads will not be paused for a GC collection, and the GC might not detect
25 references to GC allocated data held by them. This can cause memory corruption.
26 There are several ways to resolve this issue:
27 $(OL
28 $(LI Do not hold references to GC allocated data in such threads.)
29 $(LI Register/unregister such data with calls to $(LREF addRoot)/$(LREF removeRoot) and
30 $(LREF addRange)/$(LREF removeRange).)
31 $(LI Maintain another reference to that same data in another thread that the
32 GC does know about.)
33 $(LI Disable GC collection cycles while that thread is active with $(LREF disable)/$(LREF enable).)
34 $(LI Register the thread with the GC using $(REF thread_attachThis, core,thread,osthread)/$(REF thread_detachThis, core,thread,threadbase).)
39 * Notes_to_implementors:
40 * $(UL
41 * $(LI On POSIX systems, the signals `SIGRTMIN` and `SIGRTMIN + 1` are reserved
42 * by this module for use in the garbage collector implementation.
43 * Typically, they will be used to stop and resume other threads
44 * when performing a collection, but an implementation may choose
45 * not to use this mechanism (or not stop the world at all, in the
46 * case of concurrent garbage collectors).)
48 * $(LI Registers, the stack, and any other _memory locations added through
49 * the $(D GC.$(LREF addRange)) function are always scanned conservatively.
50 * This means that even if a variable is e.g. of type $(D float),
51 * it will still be scanned for possible GC pointers. And, if the
52 * word-interpreted representation of the variable matches a GC-managed
53 * _memory block's address, that _memory block is considered live.)
55 * $(LI Implementations are free to scan the non-root heap in a precise
56 * manner, so that fields of types like $(D float) will not be considered
57 * relevant when scanning the heap. Thus, casting a GC pointer to an
58 * integral type (e.g. $(D size_t)) and storing it in a field of that
59 * type inside the GC heap may mean that it will not be recognized
60 * if the _memory block was allocated with precise type info or with
61 * the $(D GC.BlkAttr.$(LREF NO_SCAN)) attribute.)
63 * $(LI Destructors will always be executed while other threads are
64 * active; that is, an implementation that stops the world must not
65 * execute destructors until the world has been resumed.)
67 * $(LI A destructor of an object must not access object references
68 * within the object. This means that an implementation is free to
69 * optimize based on this rule.)
71 * $(LI An implementation is free to perform heap compaction and copying
72 * so long as no valid GC pointers are invalidated in the process.
73 * However, _memory allocated with $(D GC.BlkAttr.$(LREF NO_MOVE)) must
74 * not be moved/copied.)
76 * $(LI Implementations must support interior pointers. That is, if the
77 * only reference to a GC-managed _memory block points into the
78 * middle of the block rather than the beginning (for example), the
79 * GC must consider the _memory block live. The exception to this
80 * rule is when a _memory block is allocated with the
81 * $(D GC.BlkAttr.$(LREF NO_INTERIOR)) attribute; it is the user's
82 * responsibility to make sure such _memory blocks have a proper pointer
83 * to them when they should be considered live.)
85 * $(LI It is acceptable for an implementation to store bit flags into
86 * pointer values and GC-managed _memory blocks, so long as such a
87 * trick is not visible to the application. In practice, this means
88 * that only a stop-the-world collector can do this.)
90 * $(LI Implementations are free to assume that GC pointers are only
91 * stored on word boundaries. Unaligned pointers may be ignored
92 * entirely.)
94 * $(LI Implementations are free to run collections at any point. It is,
95 * however, recommendable to only do so when an allocation attempt
96 * happens and there is insufficient _memory available.)
97 * )
99 * Copyright: Copyright Sean Kelly 2005 - 2015.
100 * License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
101 * Authors: Sean Kelly, Alex Rønne Petersen
102 * Source: $(DRUNTIMESRC core/_memory.d)
105 module core.memory;
107 version (ARM)
108 version = AnyARM;
109 else version (AArch64)
110 version = AnyARM;
112 version (iOS)
113 version = iOSDerived;
114 else version (TVOS)
115 version = iOSDerived;
116 else version (WatchOS)
117 version = iOSDerived;
119 private
121 extern (C) uint gc_getAttr( void* p ) pure nothrow;
122 extern (C) uint gc_setAttr( void* p, uint a ) pure nothrow;
123 extern (C) uint gc_clrAttr( void* p, uint a ) pure nothrow;
125 extern (C) void* gc_addrOf( void* p ) pure nothrow @nogc;
126 extern (C) size_t gc_sizeOf( void* p ) pure nothrow @nogc;
128 struct BlkInfo_
130 void* base;
131 size_t size;
132 uint attr;
135 extern (C) BlkInfo_ gc_query(return scope void* p) pure nothrow;
136 extern (C) GC.Stats gc_stats ( ) @safe nothrow @nogc;
137 extern (C) GC.ProfileStats gc_profileStats ( ) nothrow @nogc @safe;
140 version (CoreDoc)
143 * The minimum size of a system page in bytes.
145 * This is a compile time, platform specific value. This value might not
146 * be accurate, since it might be possible to change this value. Whenever
147 * possible, please use $(LREF pageSize) instead, which is initialized
148 * during runtime.
150 * The minimum size is useful when the context requires a compile time known
151 * value, like the size of a static array: `ubyte[minimumPageSize] buffer`.
153 enum minimumPageSize : size_t;
155 else version (AnyARM)
157 version (iOSDerived)
158 enum size_t minimumPageSize = 16384;
159 else
160 enum size_t minimumPageSize = 4096;
162 else
163 enum size_t minimumPageSize = 4096;
166 unittest
168 ubyte[minimumPageSize] buffer;
172 * The size of a system page in bytes.
174 * This value is set at startup time of the application. It's safe to use
175 * early in the start process, like in shared module constructors and
176 * initialization of the D runtime itself.
178 immutable size_t pageSize;
181 unittest
183 ubyte[] buffer = new ubyte[pageSize];
186 // The reason for this elaborated way of declaring a function is:
188 // * `pragma(crt_constructor)` is used to declare a constructor that is called by
189 // the C runtime, before C main. This allows the `pageSize` value to be used
190 // during initialization of the D runtime. This also avoids any issues with
191 // static module constructors and circular references.
193 // * `pragma(mangle)` is used because `pragma(crt_constructor)` requires a
194 // function with C linkage. To avoid any name conflict with other C symbols,
195 // standard D mangling is used.
197 // * The extra function declaration, without the body, is to be able to get the
198 // D mangling of the function without the need to hardcode the value.
200 // * The extern function declaration also has the side effect of making it
201 // impossible to manually call the function with standard syntax. This is to
202 // make it more difficult to call the function again, manually.
203 private void initialize();
204 pragma(crt_constructor)
205 pragma(mangle, initialize.mangleof)
206 private extern (C) void _initialize() @system
208 version (Posix)
210 import core.sys.posix.unistd : sysconf, _SC_PAGESIZE;
212 (cast() pageSize) = cast(size_t) sysconf(_SC_PAGESIZE);
214 else version (Windows)
216 import core.sys.windows.winbase : GetSystemInfo, SYSTEM_INFO;
218 SYSTEM_INFO si;
219 GetSystemInfo(&si);
220 (cast() pageSize) = cast(size_t) si.dwPageSize;
222 else
223 static assert(false, __FUNCTION__ ~ " is not implemented on this platform");
227 * This struct encapsulates all garbage collection functionality for the D
228 * programming language.
230 struct GC
232 @disable this();
235 * Aggregation of GC stats to be exposed via public API
237 static struct Stats
239 /// number of used bytes on the GC heap (might only get updated after a collection)
240 size_t usedSize;
241 /// number of free bytes on the GC heap (might only get updated after a collection)
242 size_t freeSize;
243 /// number of bytes allocated for current thread since program start
244 ulong allocatedInCurrentThread;
248 * Aggregation of current profile information
250 static struct ProfileStats
252 import core.time : Duration;
253 /// total number of GC cycles
254 size_t numCollections;
255 /// total time spent doing GC
256 Duration totalCollectionTime;
257 /// total time threads were paused doing GC
258 Duration totalPauseTime;
259 /// largest time threads were paused during one GC cycle
260 Duration maxPauseTime;
261 /// largest time spent doing one GC cycle
262 Duration maxCollectionTime;
265 extern(C):
268 * Enables automatic garbage collection behavior if collections have
269 * previously been suspended by a call to disable. This function is
270 * reentrant, and must be called once for every call to disable before
271 * automatic collections are enabled.
273 pragma(mangle, "gc_enable") static void enable() nothrow pure;
277 * Disables automatic garbage collections performed to minimize the
278 * process footprint. Collections may continue to occur in instances
279 * where the implementation deems necessary for correct program behavior,
280 * such as during an out of memory condition. This function is reentrant,
281 * but enable must be called once for each call to disable.
283 pragma(mangle, "gc_disable") static void disable() nothrow pure;
287 * Begins a full collection. While the meaning of this may change based
288 * on the garbage collector implementation, typical behavior is to scan
289 * all stack segments for roots, mark accessible memory blocks as alive,
290 * and then to reclaim free space. This action may need to suspend all
291 * running threads for at least part of the collection process.
293 pragma(mangle, "gc_collect") static void collect() nothrow pure;
296 * Indicates that the managed memory space be minimized by returning free
297 * physical memory to the operating system. The amount of free memory
298 * returned depends on the allocator design and on program behavior.
300 pragma(mangle, "gc_minimize") static void minimize() nothrow pure;
302 extern(D):
305 * Elements for a bit field representing memory block attributes. These
306 * are manipulated via the getAttr, setAttr, clrAttr functions.
308 enum BlkAttr : uint
310 NONE = 0b0000_0000, /// No attributes set.
311 FINALIZE = 0b0000_0001, /// Finalize the data in this block on collect.
312 NO_SCAN = 0b0000_0010, /// Do not scan through this block on collect.
313 NO_MOVE = 0b0000_0100, /// Do not move this memory block on collect.
315 This block contains the info to allow appending.
317 This can be used to manually allocate arrays. Initial slice size is 0.
319 Note: The slice's usable size will not match the block size. Use
320 $(LREF capacity) to retrieve actual usable capacity.
322 Example:
323 ----
324 // Allocate the underlying array.
325 int* pToArray = cast(int*)GC.malloc(10 * int.sizeof, GC.BlkAttr.NO_SCAN | GC.BlkAttr.APPENDABLE);
326 // Bind a slice. Check the slice has capacity information.
327 int[] slice = pToArray[0 .. 0];
328 assert(capacity(slice) > 0);
329 // Appending to the slice will not relocate it.
330 slice.length = 5;
331 slice ~= 1;
332 assert(slice.ptr == p);
333 ----
335 APPENDABLE = 0b0000_1000,
338 This block is guaranteed to have a pointer to its base while it is
339 alive. Interior pointers can be safely ignored. This attribute is
340 useful for eliminating false pointers in very large data structures
341 and is only implemented for data structures at least a page in size.
343 NO_INTERIOR = 0b0001_0000,
345 STRUCTFINAL = 0b0010_0000, // the block has a finalizer for (an array of) structs
350 * Contains aggregate information about a block of managed memory. The
351 * purpose of this struct is to support a more efficient query style in
352 * instances where detailed information is needed.
354 * base = A pointer to the base of the block in question.
355 * size = The size of the block, calculated from base.
356 * attr = Attribute bits set on the memory block.
358 alias BlkInfo = BlkInfo_;
362 * Returns a bit field representing all block attributes set for the memory
363 * referenced by p. If p references memory not originally allocated by
364 * this garbage collector, points to the interior of a memory block, or if
365 * p is null, zero will be returned.
367 * Params:
368 * p = A pointer to the root of a valid memory block or to null.
370 * Returns:
371 * A bit field containing any bits set for the memory block referenced by
372 * p or zero on error.
374 static uint getAttr( const scope void* p ) nothrow
376 return gc_getAttr(cast(void*) p);
380 /// ditto
381 static uint getAttr(void* p) pure nothrow
383 return gc_getAttr( p );
388 * Sets the specified bits for the memory references by p. If p references
389 * memory not originally allocated by this garbage collector, points to the
390 * interior of a memory block, or if p is null, no action will be
391 * performed.
393 * Params:
394 * p = A pointer to the root of a valid memory block or to null.
395 * a = A bit field containing any bits to set for this memory block.
397 * Returns:
398 * The result of a call to getAttr after the specified bits have been
399 * set.
401 static uint setAttr( const scope void* p, uint a ) nothrow
403 return gc_setAttr(cast(void*) p, a);
407 /// ditto
408 static uint setAttr(void* p, uint a) pure nothrow
410 return gc_setAttr( p, a );
415 * Clears the specified bits for the memory references by p. If p
416 * references memory not originally allocated by this garbage collector,
417 * points to the interior of a memory block, or if p is null, no action
418 * will be performed.
420 * Params:
421 * p = A pointer to the root of a valid memory block or to null.
422 * a = A bit field containing any bits to clear for this memory block.
424 * Returns:
425 * The result of a call to getAttr after the specified bits have been
426 * cleared.
428 static uint clrAttr( const scope void* p, uint a ) nothrow
430 return gc_clrAttr(cast(void*) p, a);
434 /// ditto
435 static uint clrAttr(void* p, uint a) pure nothrow
437 return gc_clrAttr( p, a );
440 extern(C):
443 * Requests an aligned block of managed memory from the garbage collector.
444 * This memory may be deleted at will with a call to free, or it may be
445 * discarded and cleaned up automatically during a collection run. If
446 * allocation fails, this function will call onOutOfMemory which is
447 * expected to throw an OutOfMemoryError.
449 * Params:
450 * sz = The desired allocation size in bytes.
451 * ba = A bitmask of the attributes to set on this block.
452 * ti = TypeInfo to describe the memory. The GC might use this information
453 * to improve scanning for pointers or to call finalizers.
455 * Returns:
456 * A reference to the allocated memory or null if insufficient memory
457 * is available.
459 * Throws:
460 * OutOfMemoryError on allocation failure.
462 version (D_ProfileGC)
463 pragma(mangle, "gc_mallocTrace") static void* malloc(size_t sz, uint ba = 0, const scope TypeInfo ti = null,
464 string file = __FILE__, int line = __LINE__, string func = __FUNCTION__) pure nothrow;
465 else
466 pragma(mangle, "gc_malloc") static void* malloc(size_t sz, uint ba = 0, const scope TypeInfo ti = null) pure nothrow;
469 * Requests an aligned block of managed memory from the garbage collector.
470 * This memory may be deleted at will with a call to free, or it may be
471 * discarded and cleaned up automatically during a collection run. If
472 * allocation fails, this function will call onOutOfMemory which is
473 * expected to throw an OutOfMemoryError.
475 * Params:
476 * sz = The desired allocation size in bytes.
477 * ba = A bitmask of the attributes to set on this block.
478 * ti = TypeInfo to describe the memory. The GC might use this information
479 * to improve scanning for pointers or to call finalizers.
481 * Returns:
482 * Information regarding the allocated memory block or BlkInfo.init on
483 * error.
485 * Throws:
486 * OutOfMemoryError on allocation failure.
488 version (D_ProfileGC)
489 pragma(mangle, "gc_qallocTrace") static BlkInfo qalloc(size_t sz, uint ba = 0, const scope TypeInfo ti = null,
490 string file = __FILE__, int line = __LINE__, string func = __FUNCTION__) pure nothrow;
491 else
492 pragma(mangle, "gc_qalloc") static BlkInfo qalloc(size_t sz, uint ba = 0, const scope TypeInfo ti = null) pure nothrow;
496 * Requests an aligned block of managed memory from the garbage collector,
497 * which is initialized with all bits set to zero. This memory may be
498 * deleted at will with a call to free, or it may be discarded and cleaned
499 * up automatically during a collection run. If allocation fails, this
500 * function will call onOutOfMemory which is expected to throw an
501 * OutOfMemoryError.
503 * Params:
504 * sz = The desired allocation size in bytes.
505 * ba = A bitmask of the attributes to set on this block.
506 * ti = TypeInfo to describe the memory. The GC might use this information
507 * to improve scanning for pointers or to call finalizers.
509 * Returns:
510 * A reference to the allocated memory or null if insufficient memory
511 * is available.
513 * Throws:
514 * OutOfMemoryError on allocation failure.
516 version (D_ProfileGC)
517 pragma(mangle, "gc_callocTrace") static void* calloc(size_t sz, uint ba = 0, const TypeInfo ti = null,
518 string file = __FILE__, int line = __LINE__, string func = __FUNCTION__) pure nothrow;
519 else
520 pragma(mangle, "gc_calloc") static void* calloc(size_t sz, uint ba = 0, const TypeInfo ti = null) pure nothrow;
524 * Extend, shrink or allocate a new block of memory keeping the contents of
525 * an existing block
527 * If `sz` is zero, the memory referenced by p will be deallocated as if
528 * by a call to `free`.
529 * If `p` is `null`, new memory will be allocated via `malloc`.
530 * If `p` is pointing to memory not allocated from the GC or to the interior
531 * of an allocated memory block, no operation is performed and null is returned.
533 * Otherwise, a new memory block of size `sz` will be allocated as if by a
534 * call to `malloc`, or the implementation may instead resize or shrink the memory
535 * block in place.
536 * The contents of the new memory block will be the same as the contents
537 * of the old memory block, up to the lesser of the new and old sizes.
539 * The caller guarantees that there are no other live pointers to the
540 * passed memory block, still it might not be freed immediately by `realloc`.
541 * The garbage collector can reclaim the memory block in a later
542 * collection if it is unused.
543 * If allocation fails, this function will throw an `OutOfMemoryError`.
545 * If `ba` is zero (the default) the attributes of the existing memory
546 * will be used for an allocation.
547 * If `ba` is not zero and no new memory is allocated, the bits in ba will
548 * replace those of the current memory block.
550 * Params:
551 * p = A pointer to the base of a valid memory block or to `null`.
552 * sz = The desired allocation size in bytes.
553 * ba = A bitmask of the BlkAttr attributes to set on this block.
554 * ti = TypeInfo to describe the memory. The GC might use this information
555 * to improve scanning for pointers or to call finalizers.
557 * Returns:
558 * A reference to the allocated memory on success or `null` if `sz` is
559 * zero or the pointer does not point to the base of an GC allocated
560 * memory block.
562 * Throws:
563 * `OutOfMemoryError` on allocation failure.
565 version (D_ProfileGC)
566 pragma(mangle, "gc_reallocTrace") static void* realloc(return scope void* p, size_t sz, uint ba = 0, const TypeInfo ti = null,
567 string file = __FILE__, int line = __LINE__, string func = __FUNCTION__) pure nothrow;
568 else
569 pragma(mangle, "gc_realloc") static void* realloc(return scope void* p, size_t sz, uint ba = 0, const TypeInfo ti = null) pure nothrow;
571 // https://issues.dlang.org/show_bug.cgi?id=13111
573 unittest
575 enum size1 = 1 << 11 + 1; // page in large object pool
576 enum size2 = 1 << 22 + 1; // larger than large object pool size
578 auto data1 = cast(ubyte*)GC.calloc(size1);
579 auto data2 = cast(ubyte*)GC.realloc(data1, size2);
581 GC.BlkInfo info = GC.query(data2);
582 assert(info.size >= size2);
587 * Requests that the managed memory block referenced by p be extended in
588 * place by at least mx bytes, with a desired extension of sz bytes. If an
589 * extension of the required size is not possible or if p references memory
590 * not originally allocated by this garbage collector, no action will be
591 * taken.
593 * Params:
594 * p = A pointer to the root of a valid memory block or to null.
595 * mx = The minimum extension size in bytes.
596 * sz = The desired extension size in bytes.
597 * ti = TypeInfo to describe the full memory block. The GC might use
598 * this information to improve scanning for pointers or to
599 * call finalizers.
601 * Returns:
602 * The size in bytes of the extended memory block referenced by p or zero
603 * if no extension occurred.
605 * Note:
606 * Extend may also be used to extend slices (or memory blocks with
607 * $(LREF APPENDABLE) info). However, use the return value only
608 * as an indicator of success. $(LREF capacity) should be used to
609 * retrieve actual usable slice capacity.
611 version (D_ProfileGC)
612 pragma(mangle, "gc_extendTrace") static size_t extend(void* p, size_t mx, size_t sz, const TypeInfo ti = null,
613 string file = __FILE__, int line = __LINE__, string func = __FUNCTION__) pure nothrow;
614 else
615 pragma(mangle, "gc_extend") static size_t extend(void* p, size_t mx, size_t sz, const TypeInfo ti = null) pure nothrow;
617 /// Standard extending
618 unittest
620 size_t size = 1000;
621 int* p = cast(int*)GC.malloc(size * int.sizeof, GC.BlkAttr.NO_SCAN);
623 //Try to extend the allocated data by 1000 elements, preferred 2000.
624 size_t u = GC.extend(p, 1000 * int.sizeof, 2000 * int.sizeof);
625 if (u != 0)
626 size = u / int.sizeof;
628 /// slice extending
629 unittest
631 int[] slice = new int[](1000);
632 int* p = slice.ptr;
634 //Check we have access to capacity before attempting the extend
635 if (slice.capacity)
637 //Try to extend slice by 1000 elements, preferred 2000.
638 size_t u = GC.extend(p, 1000 * int.sizeof, 2000 * int.sizeof);
639 if (u != 0)
641 slice.length = slice.capacity;
642 assert(slice.length >= 2000);
649 * Requests that at least sz bytes of memory be obtained from the operating
650 * system and marked as free.
652 * Params:
653 * sz = The desired size in bytes.
655 * Returns:
656 * The actual number of bytes reserved or zero on error.
658 pragma(mangle, "gc_reserve") static size_t reserve(size_t sz) nothrow pure;
662 * Deallocates the memory referenced by p. If p is null, no action occurs.
663 * If p references memory not originally allocated by this garbage
664 * collector, if p points to the interior of a memory block, or if this
665 * method is called from a finalizer, no action will be taken. The block
666 * will not be finalized regardless of whether the FINALIZE attribute is
667 * set. If finalization is desired, call $(REF1 destroy, object) prior to `GC.free`.
669 * Params:
670 * p = A pointer to the root of a valid memory block or to null.
672 pragma(mangle, "gc_free") static void free(void* p) pure nothrow @nogc;
674 extern(D):
677 * Returns the base address of the memory block containing p. This value
678 * is useful to determine whether p is an interior pointer, and the result
679 * may be passed to routines such as sizeOf which may otherwise fail. If p
680 * references memory not originally allocated by this garbage collector, if
681 * p is null, or if the garbage collector does not support this operation,
682 * null will be returned.
684 * Params:
685 * p = A pointer to the root or the interior of a valid memory block or to
686 * null.
688 * Returns:
689 * The base address of the memory block referenced by p or null on error.
691 static inout(void)* addrOf( inout(void)* p ) nothrow @nogc pure @trusted
693 return cast(inout(void)*)gc_addrOf(cast(void*)p);
696 /// ditto
697 static void* addrOf(void* p) pure nothrow @nogc @trusted
699 return gc_addrOf(p);
703 * Returns the true size of the memory block referenced by p. This value
704 * represents the maximum number of bytes for which a call to realloc may
705 * resize the existing block in place. If p references memory not
706 * originally allocated by this garbage collector, points to the interior
707 * of a memory block, or if p is null, zero will be returned.
709 * Params:
710 * p = A pointer to the root of a valid memory block or to null.
712 * Returns:
713 * The size in bytes of the memory block referenced by p or zero on error.
715 static size_t sizeOf( const scope void* p ) nothrow @nogc /* FIXME pure */
717 return gc_sizeOf(cast(void*)p);
721 /// ditto
722 static size_t sizeOf(void* p) pure nothrow @nogc
724 return gc_sizeOf( p );
727 // verify that the reallocation doesn't leave the size cache in a wrong state
728 unittest
730 auto data = cast(int*)realloc(null, 4096);
731 size_t size = GC.sizeOf(data);
732 assert(size >= 4096);
733 data = cast(int*)GC.realloc(data, 4100);
734 size = GC.sizeOf(data);
735 assert(size >= 4100);
739 * Returns aggregate information about the memory block containing p. If p
740 * references memory not originally allocated by this garbage collector, if
741 * p is null, or if the garbage collector does not support this operation,
742 * BlkInfo.init will be returned. Typically, support for this operation
743 * is dependent on support for addrOf.
745 * Params:
746 * p = A pointer to the root or the interior of a valid memory block or to
747 * null.
749 * Returns:
750 * Information regarding the memory block referenced by p or BlkInfo.init
751 * on error.
753 static BlkInfo query(return scope const void* p) nothrow
755 return gc_query(cast(void*)p);
759 /// ditto
760 static BlkInfo query(return scope void* p) pure nothrow
762 return gc_query( p );
766 * Returns runtime stats for currently active GC implementation
767 * See `core.memory.GC.Stats` for list of available metrics.
769 static Stats stats() @safe nothrow @nogc
771 return gc_stats();
775 * Returns runtime profile stats for currently active GC implementation
776 * See `core.memory.GC.ProfileStats` for list of available metrics.
778 static ProfileStats profileStats() nothrow @nogc @safe
780 return gc_profileStats();
783 extern(C):
786 * Adds an internal root pointing to the GC memory block referenced by p.
787 * As a result, the block referenced by p itself and any blocks accessible
788 * via it will be considered live until the root is removed again.
790 * If p is null, no operation is performed.
792 * Params:
793 * p = A pointer into a GC-managed memory block or null.
795 * Example:
796 * ---
797 * // Typical C-style callback mechanism; the passed function
798 * // is invoked with the user-supplied context pointer at a
799 * // later point.
800 * extern(C) void addCallback(void function(void*), void*);
802 * // Allocate an object on the GC heap (this would usually be
803 * // some application-specific context data).
804 * auto context = new Object;
806 * // Make sure that it is not collected even if it is no
807 * // longer referenced from D code (stack, GC heap, …).
808 * GC.addRoot(cast(void*)context);
810 * // Also ensure that a moving collector does not relocate
811 * // the object.
812 * GC.setAttr(cast(void*)context, GC.BlkAttr.NO_MOVE);
814 * // Now context can be safely passed to the C library.
815 * addCallback(&myHandler, cast(void*)context);
817 * extern(C) void myHandler(void* ctx)
819 * // Assuming that the callback is invoked only once, the
820 * // added root can be removed again now to allow the GC
821 * // to collect it later.
822 * GC.removeRoot(ctx);
823 * GC.clrAttr(ctx, GC.BlkAttr.NO_MOVE);
825 * auto context = cast(Object)ctx;
826 * // Use context here…
828 * ---
830 pragma(mangle, "gc_addRoot") static void addRoot(const void* p) nothrow @nogc pure;
834 * Removes the memory block referenced by p from an internal list of roots
835 * to be scanned during a collection. If p is null or is not a value
836 * previously passed to addRoot() then no operation is performed.
838 * Params:
839 * p = A pointer into a GC-managed memory block or null.
841 pragma(mangle, "gc_removeRoot") static void removeRoot(const void* p) nothrow @nogc pure;
845 * Adds $(D p[0 .. sz]) to the list of memory ranges to be scanned for
846 * pointers during a collection. If p is null, no operation is performed.
848 * Note that $(D p[0 .. sz]) is treated as an opaque range of memory assumed
849 * to be suitably managed by the caller. In particular, if p points into a
850 * GC-managed memory block, addRange does $(I not) mark this block as live.
852 * Params:
853 * p = A pointer to a valid memory address or to null.
854 * sz = The size in bytes of the block to add. If sz is zero then the
855 * no operation will occur. If p is null then sz must be zero.
856 * ti = TypeInfo to describe the memory. The GC might use this information
857 * to improve scanning for pointers or to call finalizers
859 * Example:
860 * ---
861 * // Allocate a piece of memory on the C heap.
862 * enum size = 1_000;
863 * auto rawMemory = core.stdc.stdlib.malloc(size);
865 * // Add it as a GC range.
866 * GC.addRange(rawMemory, size);
868 * // Now, pointers to GC-managed memory stored in
869 * // rawMemory will be recognized on collection.
870 * ---
872 pragma(mangle, "gc_addRange")
873 static void addRange(const void* p, size_t sz, const TypeInfo ti = null) @nogc nothrow pure;
877 * Removes the memory range starting at p from an internal list of ranges
878 * to be scanned during a collection. If p is null or does not represent
879 * a value previously passed to addRange() then no operation is
880 * performed.
882 * Params:
883 * p = A pointer to a valid memory address or to null.
885 pragma(mangle, "gc_removeRange") static void removeRange(const void* p) nothrow @nogc pure;
889 * Runs any finalizer that is located in address range of the
890 * given code segment. This is used before unloading shared
891 * libraries. All matching objects which have a finalizer in this
892 * code segment are assumed to be dead, using them while or after
893 * calling this method has undefined behavior.
895 * Params:
896 * segment = address range of a code segment.
898 pragma(mangle, "gc_runFinalizers") static void runFinalizers(const scope void[] segment);
901 * Queries the GC whether the current thread is running object finalization
902 * as part of a GC collection, or an explicit call to runFinalizers.
904 * As some GC implementations (such as the current conservative one) don't
905 * support GC memory allocation during object finalization, this function
906 * can be used to guard against such programming errors.
908 * Returns:
909 * true if the current thread is in a finalizer, a destructor invoked by
910 * the GC.
912 pragma(mangle, "gc_inFinalizer") static bool inFinalizer() nothrow @nogc @safe;
915 @safe nothrow @nogc unittest
917 // Only code called from a destructor is executed during finalization.
918 assert(!GC.inFinalizer);
922 unittest
924 enum Outcome
926 notCalled,
927 calledManually,
928 calledFromDruntime
931 static class Resource
933 static Outcome outcome;
935 this()
937 outcome = Outcome.notCalled;
940 ~this()
942 if (GC.inFinalizer)
944 outcome = Outcome.calledFromDruntime;
946 import core.exception : InvalidMemoryOperationError;
950 * Presently, allocating GC memory during finalization
951 * is forbidden and leads to
952 * `InvalidMemoryOperationError` being thrown.
954 * `GC.inFinalizer` can be used to guard against
955 * programming erros such as these and is also a more
956 * efficient way to verify whether a destructor was
957 * invoked by the GC.
959 cast(void) GC.malloc(1);
960 assert(false);
962 catch (InvalidMemoryOperationError e)
964 return;
966 assert(false);
968 else
969 outcome = Outcome.calledManually;
973 static void createGarbage()
975 auto r = new Resource;
976 r = null;
979 assert(Resource.outcome == Outcome.notCalled);
980 createGarbage();
981 GC.collect;
982 assert(
983 Resource.outcome == Outcome.notCalled ||
984 Resource.outcome == Outcome.calledFromDruntime);
986 auto r = new Resource;
987 GC.runFinalizers((cast(const void*)typeid(Resource).destructor)[0..1]);
988 assert(Resource.outcome == Outcome.calledFromDruntime);
989 Resource.outcome = Outcome.notCalled;
991 debug(MEMSTOMP) {} else
993 // assume Resource data is still available
994 r.destroy;
995 assert(Resource.outcome == Outcome.notCalled);
998 r = new Resource;
999 assert(Resource.outcome == Outcome.notCalled);
1000 r.destroy;
1001 assert(Resource.outcome == Outcome.calledManually);
1005 * Returns the number of bytes allocated for the current thread
1006 * since program start. It is the same as
1007 * GC.stats().allocatedInCurrentThread, but faster.
1009 pragma(mangle, "gc_allocatedInCurrentThread") static ulong allocatedInCurrentThread() nothrow;
1011 /// Using allocatedInCurrentThread
1012 nothrow unittest
1014 ulong currentlyAllocated = GC.allocatedInCurrentThread();
1015 struct DataStruct
1017 long l1;
1018 long l2;
1019 long l3;
1020 long l4;
1022 DataStruct* unused = new DataStruct;
1023 assert(GC.allocatedInCurrentThread() == currentlyAllocated + 32);
1024 assert(GC.stats().allocatedInCurrentThread == currentlyAllocated + 32);
1029 * Pure variants of C's memory allocation functions `malloc`, `calloc`, and
1030 * `realloc` and deallocation function `free`.
1032 * UNIX 98 requires that errno be set to ENOMEM upon failure.
1033 * Purity is achieved by saving and restoring the value of `errno`, thus
1034 * behaving as if it were never changed.
1036 * See_Also:
1037 * $(LINK2 https://dlang.org/spec/function.html#pure-functions, D's rules for purity),
1038 * which allow for memory allocation under specific circumstances.
1040 void* pureMalloc()(size_t size) @trusted pure @nogc nothrow
1042 const errnosave = fakePureErrno;
1043 void* ret = fakePureMalloc(size);
1044 fakePureErrno = errnosave;
1045 return ret;
1047 /// ditto
1048 void* pureCalloc()(size_t nmemb, size_t size) @trusted pure @nogc nothrow
1050 const errnosave = fakePureErrno;
1051 void* ret = fakePureCalloc(nmemb, size);
1052 fakePureErrno = errnosave;
1053 return ret;
1055 /// ditto
1056 void* pureRealloc()(void* ptr, size_t size) @system pure @nogc nothrow
1058 const errnosave = fakePureErrno;
1059 void* ret = fakePureRealloc(ptr, size);
1060 fakePureErrno = errnosave;
1061 return ret;
1064 /// ditto
1065 void pureFree()(void* ptr) @system pure @nogc nothrow
1067 version (Posix)
1069 // POSIX free doesn't set errno
1070 fakePureFree(ptr);
1072 else
1074 const errnosave = fakePureErrno;
1075 fakePureFree(ptr);
1076 fakePureErrno = errnosave;
1081 @system pure nothrow @nogc unittest
1083 ubyte[] fun(size_t n) pure
1085 void* p = pureMalloc(n);
1086 p !is null || n == 0 || assert(0);
1087 scope(failure) p = pureRealloc(p, 0);
1088 p = pureRealloc(p, n *= 2);
1089 p !is null || n == 0 || assert(0);
1090 return cast(ubyte[]) p[0 .. n];
1093 auto buf = fun(100);
1094 assert(buf.length == 200);
1095 pureFree(buf.ptr);
1098 @system pure nothrow @nogc unittest
1100 const int errno = fakePureErrno();
1102 void* x = pureMalloc(10); // normal allocation
1103 assert(errno == fakePureErrno()); // errno shouldn't change
1104 assert(x !is null); // allocation should succeed
1106 x = pureRealloc(x, 10); // normal reallocation
1107 assert(errno == fakePureErrno()); // errno shouldn't change
1108 assert(x !is null); // allocation should succeed
1110 fakePureFree(x);
1112 void* y = pureCalloc(10, 1); // normal zeroed allocation
1113 assert(errno == fakePureErrno()); // errno shouldn't change
1114 assert(y !is null); // allocation should succeed
1116 fakePureFree(y);
1118 // Workaround bug in glibc 2.26
1119 // See also: https://issues.dlang.org/show_bug.cgi?id=17956
1120 void* z = pureMalloc(size_t.max & ~255); // won't affect `errno`
1121 assert(errno == fakePureErrno()); // errno shouldn't change
1122 assert(z is null);
1125 // locally purified for internal use here only
1127 static import core.stdc.errno;
1128 static if (__traits(getOverloads, core.stdc.errno, "errno").length == 1
1129 && __traits(getLinkage, core.stdc.errno.errno) == "C")
1131 extern(C) pragma(mangle, __traits(identifier, core.stdc.errno.errno))
1132 private ref int fakePureErrno() @nogc nothrow pure @system;
1134 else
1136 extern(C) private @nogc nothrow pure @system
1138 pragma(mangle, __traits(identifier, core.stdc.errno.getErrno))
1139 @property int fakePureErrno();
1141 pragma(mangle, __traits(identifier, core.stdc.errno.setErrno))
1142 @property int fakePureErrno(int);
1146 version (D_BetterC) {}
1147 else // TODO: remove this function after Phobos no longer needs it.
1148 extern (C) private @system @nogc nothrow
1150 ref int fakePureErrnoImpl()
1152 import core.stdc.errno;
1153 return errno();
1157 extern (C) private pure @system @nogc nothrow
1159 pragma(mangle, "malloc") void* fakePureMalloc(size_t);
1160 pragma(mangle, "calloc") void* fakePureCalloc(size_t nmemb, size_t size);
1161 pragma(mangle, "realloc") void* fakePureRealloc(void* ptr, size_t size);
1163 pragma(mangle, "free") void fakePureFree(void* ptr);
1167 Destroys and then deallocates an object.
1169 In detail, `__delete(x)` returns with no effect if `x` is `null`. Otherwise, it
1170 performs the following actions in sequence:
1171 $(UL
1172 $(LI
1173 Calls the destructor `~this()` for the object referred to by `x`
1174 (if `x` is a class or interface reference) or
1175 for the object pointed to by `x` (if `x` is a pointer to a `struct`).
1176 Arrays of structs call the destructor, if defined, for each element in the array.
1177 If no destructor is defined, this step has no effect.
1179 $(LI
1180 Frees the memory allocated for `x`. If `x` is a reference to a class
1181 or interface, the memory allocated for the underlying instance is freed. If `x` is
1182 a pointer, the memory allocated for the pointed-to object is freed. If `x` is a
1183 built-in array, the memory allocated for the array is freed.
1184 If `x` does not refer to memory previously allocated with `new` (or the lower-level
1185 equivalents in the GC API), the behavior is undefined.
1187 $(LI
1188 Lastly, `x` is set to `null`. Any attempt to read or write the freed memory via
1189 other references will result in undefined behavior.
1193 Note: Users should prefer $(REF1 destroy, object) to explicitly finalize objects,
1194 and only resort to $(REF __delete, core,memory) when $(REF destroy, object)
1195 wouldn't be a feasible option.
1197 Params:
1198 x = aggregate object that should be destroyed
1200 See_Also: $(REF1 destroy, object), $(REF free, core,GC)
1202 History:
1204 The `delete` keyword allowed to free GC-allocated memory.
1205 As this is inherently not `@safe`, it has been deprecated.
1206 This function has been added to provide an easy transition from `delete`.
1207 It performs the same functionality as the former `delete` keyword.
1209 void __delete(T)(ref T x) @system
1211 static void _destructRecurse(S)(ref S s)
1212 if (is(S == struct))
1214 static if (__traits(hasMember, S, "__xdtor") &&
1215 // Bugzilla 14746: Check that it's the exact member of S.
1216 __traits(isSame, S, __traits(parent, s.__xdtor)))
1217 s.__xdtor();
1220 // See also: https://github.com/dlang/dmd/blob/v2.078.0/src/dmd/e2ir.d#L3886
1221 static if (is(T == interface))
1223 .object.destroy(x);
1225 else static if (is(T == class))
1227 .object.destroy(x);
1229 else static if (is(T == U*, U))
1231 static if (is(U == struct))
1233 if (x)
1234 _destructRecurse(*x);
1237 else static if (is(T : E[], E))
1239 static if (is(E == struct))
1241 foreach_reverse (ref e; x)
1242 _destructRecurse(e);
1245 else
1247 static assert(0, "It is not possible to delete: `" ~ T.stringof ~ "`");
1250 static if (is(T == interface) ||
1251 is(T == class) ||
1252 is(T == U2*, U2))
1254 GC.free(GC.addrOf(cast(void*) x));
1255 x = null;
1257 else static if (is(T : E2[], E2))
1259 GC.free(GC.addrOf(cast(void*) x.ptr));
1260 x = null;
1264 /// Deleting classes
1265 unittest
1267 bool dtorCalled;
1268 class B
1270 int test;
1271 ~this()
1273 dtorCalled = true;
1276 B b = new B();
1277 B a = b;
1278 b.test = 10;
1280 assert(GC.addrOf(cast(void*) b) != null);
1281 __delete(b);
1282 assert(b is null);
1283 assert(dtorCalled);
1284 assert(GC.addrOf(cast(void*) b) == null);
1285 // but be careful, a still points to it
1286 assert(a !is null);
1287 assert(GC.addrOf(cast(void*) a) == null); // but not a valid GC pointer
1290 /// Deleting interfaces
1291 unittest
1293 bool dtorCalled;
1294 interface A
1296 int quack();
1298 class B : A
1300 int a;
1301 int quack()
1303 a++;
1304 return a;
1306 ~this()
1308 dtorCalled = true;
1311 A a = new B();
1312 a.quack();
1314 assert(GC.addrOf(cast(void*) a) != null);
1315 __delete(a);
1316 assert(a is null);
1317 assert(dtorCalled);
1318 assert(GC.addrOf(cast(void*) a) == null);
1321 /// Deleting structs
1322 unittest
1324 bool dtorCalled;
1325 struct A
1327 string test;
1328 ~this()
1330 dtorCalled = true;
1333 auto a = new A("foo");
1335 assert(GC.addrOf(cast(void*) a) != null);
1336 __delete(a);
1337 assert(a is null);
1338 assert(dtorCalled);
1339 assert(GC.addrOf(cast(void*) a) == null);
1341 // https://issues.dlang.org/show_bug.cgi?id=22779
1342 A *aptr;
1343 __delete(aptr);
1346 /// Deleting arrays
1347 unittest
1349 int[] a = [1, 2, 3];
1350 auto b = a;
1352 assert(GC.addrOf(b.ptr) != null);
1353 __delete(b);
1354 assert(b is null);
1355 assert(GC.addrOf(b.ptr) == null);
1356 // but be careful, a still points to it
1357 assert(a !is null);
1358 assert(GC.addrOf(a.ptr) == null); // but not a valid GC pointer
1361 /// Deleting arrays of structs
1362 unittest
1364 int dtorCalled;
1365 struct A
1367 int a;
1368 ~this()
1370 assert(dtorCalled == a);
1371 dtorCalled++;
1374 auto arr = [A(1), A(2), A(3)];
1375 arr[0].a = 2;
1376 arr[1].a = 1;
1377 arr[2].a = 0;
1379 assert(GC.addrOf(arr.ptr) != null);
1380 __delete(arr);
1381 assert(dtorCalled == 3);
1382 assert(GC.addrOf(arr.ptr) == null);
1385 // Deleting raw memory
1386 unittest
1388 import core.memory : GC;
1389 auto a = GC.malloc(5);
1390 assert(GC.addrOf(cast(void*) a) != null);
1391 __delete(a);
1392 assert(a is null);
1393 assert(GC.addrOf(cast(void*) a) == null);
1396 // __delete returns with no effect if x is null
1397 unittest
1399 Object x = null;
1400 __delete(x);
1402 struct S { ~this() { } }
1403 class C { }
1404 interface I { }
1406 int[] a; __delete(a);
1407 S[] as; __delete(as);
1408 C c; __delete(c);
1409 I i; __delete(i);
1410 C* pc = &c; __delete(*pc);
1411 I* pi = &i; __delete(*pi);
1412 int* pint; __delete(pint);
1413 S* ps; __delete(ps);
1416 // https://issues.dlang.org/show_bug.cgi?id=19092
1417 unittest
1419 const(int)[] x = [1, 2, 3];
1420 assert(GC.addrOf(x.ptr) != null);
1421 __delete(x);
1422 assert(x is null);
1423 assert(GC.addrOf(x.ptr) == null);
1425 immutable(int)[] y = [1, 2, 3];
1426 assert(GC.addrOf(y.ptr) != null);
1427 __delete(y);
1428 assert(y is null);
1429 assert(GC.addrOf(y.ptr) == null);
1432 // test realloc behaviour
1433 unittest
1435 static void set(int* p, size_t size)
1437 foreach (i; 0 .. size)
1438 *p++ = cast(int) i;
1440 static void verify(int* p, size_t size)
1442 foreach (i; 0 .. size)
1443 assert(*p++ == i);
1445 static void test(size_t memsize)
1447 int* p = cast(int*) GC.malloc(memsize * int.sizeof);
1448 assert(p);
1449 set(p, memsize);
1450 verify(p, memsize);
1452 int* q = cast(int*) GC.realloc(p + 4, 2 * memsize * int.sizeof);
1453 assert(q == null);
1455 q = cast(int*) GC.realloc(p + memsize / 2, 2 * memsize * int.sizeof);
1456 assert(q == null);
1458 q = cast(int*) GC.realloc(p + memsize - 1, 2 * memsize * int.sizeof);
1459 assert(q == null);
1461 int* r = cast(int*) GC.realloc(p, 5 * memsize * int.sizeof);
1462 verify(r, memsize);
1463 set(r, 5 * memsize);
1465 int* s = cast(int*) GC.realloc(r, 2 * memsize * int.sizeof);
1466 verify(s, 2 * memsize);
1468 assert(GC.realloc(s, 0) == null); // free
1469 assert(GC.addrOf(p) == null);
1472 test(16);
1473 test(200);
1474 test(800); // spans large and small pools
1475 test(1200);
1476 test(8000);
1478 void* p = GC.malloc(100);
1479 assert(GC.realloc(&p, 50) == null); // non-GC pointer
1482 // test GC.profileStats
1483 unittest
1485 auto stats = GC.profileStats();
1486 GC.collect();
1487 auto nstats = GC.profileStats();
1488 assert(nstats.numCollections > stats.numCollections);
1491 // in rt.lifetime:
1492 private extern (C) void* _d_newitemU(scope const TypeInfo _ti) @system pure nothrow;
1495 Moves a value to a new GC allocation.
1497 Params:
1498 value = Value to be moved. If the argument is an lvalue and a struct with a
1499 destructor or postblit, it will be reset to its `.init` value.
1501 Returns:
1502 A pointer to the new GC-allocated value.
1504 T* moveToGC(T)(auto ref T value)
1506 static T* doIt(ref T value) @trusted
1508 import core.lifetime : moveEmplace;
1509 auto mem = cast(T*) _d_newitemU(typeid(T)); // allocate but don't initialize
1510 moveEmplace(value, *mem);
1511 return mem;
1514 return doIt(value); // T dtor might be @system
1518 @safe pure nothrow unittest
1520 struct S
1522 int x;
1523 this(this) @disable;
1524 ~this() @safe pure nothrow @nogc {}
1527 S* p;
1529 // rvalue
1530 p = moveToGC(S(123));
1531 assert(p.x == 123);
1533 // lvalue
1534 auto lval = S(456);
1535 p = moveToGC(lval);
1536 assert(p.x == 456);
1537 assert(lval.x == 0);
1540 // @system dtor
1541 unittest
1543 struct S
1545 int x;
1546 ~this() @system {}
1549 // lvalue case is @safe, ref param isn't destructed
1550 static assert(__traits(compiles, (ref S lval) @safe { moveToGC(lval); }));
1552 // rvalue case is @system, value param is destructed
1553 static assert(!__traits(compiles, () @safe { moveToGC(S(0)); }));