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.
6  *
7  * Using this module is not necessary in typical D code. It is mostly
8  * useful when doing low-level _memory management.
9  *
10  * Notes_to_users:
11  *
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.
19    )
20 
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).)
35         )
36    )
37    )
38  *
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).)
47  *
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.)
54  *
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.)
62  *
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.)
66  *
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.)
70  *
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.)
75  *
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.)
84  *
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.)
89  *
90  * $(LI Implementations are free to assume that GC pointers are only
91  *   stored on word boundaries. Unaligned pointers may be ignored
92  *   entirely.)
93  *
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  * )
98  *
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)
103  */
104 
105 module core.memory;
106 
107 version (ARM)
108     version = AnyARM;
109 else version (AArch64)
110     version = AnyARM;
111 
112 version (iOS)
113     version = iOSDerived;
114 else version (TVOS)
115     version = iOSDerived;
116 else version (WatchOS)
117     version = iOSDerived;
118 
119 private
120 {
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;
124 
125     extern (C) void*   gc_addrOf( void* p ) pure nothrow @nogc;
126     extern (C) size_t  gc_sizeOf( void* p ) pure nothrow @nogc;
127 
128     struct BlkInfo_
129     {
130         void*  base;
131         size_t size;
132         uint   attr;
133     }
134 
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;
138 }
139 
140 version (CoreDoc)
141 {
142     /**
143      * The minimum size of a system page in bytes.
144      *
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.
149      *
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`.
152      */
153     enum minimumPageSize : size_t;
154 }
155 else version (AnyARM)
156 {
157     version (iOSDerived)
158         enum size_t minimumPageSize = 16384;
159     else
160         enum size_t minimumPageSize = 4096;
161 }
162 else
163     enum size_t minimumPageSize = 4096;
164 
165 ///
166 unittest
167 {
168     ubyte[minimumPageSize] buffer;
169 }
170 
171 /**
172  * The size of a system page in bytes.
173  *
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.
177  */
178 immutable size_t pageSize;
179 
180 ///
181 unittest
182 {
183     ubyte[] buffer = new ubyte[pageSize];
184 }
185 
186 // The reason for this elaborated way of declaring a function is:
187 //
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.
192 //
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.
196 //
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.
199 //
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
207 {
208     version (Posix)
209     {
210         import core.sys.posix.unistd : sysconf, _SC_PAGESIZE;
211 
212         (cast() pageSize) = cast(size_t) sysconf(_SC_PAGESIZE);
213     }
214     else version (Windows)
215     {
216         import core.sys.windows.winbase : GetSystemInfo, SYSTEM_INFO;
217 
218         SYSTEM_INFO si;
219         GetSystemInfo(&si);
220         (cast() pageSize) = cast(size_t) si.dwPageSize;
221     }
222     else
223         static assert(false, __FUNCTION__ ~ " is not implemented on this platform");
224 }
225 
226 /**
227  * This struct encapsulates all garbage collection functionality for the D
228  * programming language.
229  */
230 struct GC
231 {
232     @disable this();
233 
234     /**
235      * Aggregation of GC stats to be exposed via public API
236      */
237     static struct Stats
238     {
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;
245     }
246 
247     /**
248      * Aggregation of current profile information
249      */
250     static struct ProfileStats
251     {
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;
263     }
264 
265 extern(C):
266 
267     /**
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.
272      */
273     pragma(mangle, "gc_enable") static void enable() @safe nothrow pure;
274 
275 
276     /**
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.
282      */
283     pragma(mangle, "gc_disable") static void disable() @safe nothrow pure;
284 
285 
286     /**
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.
292      */
293     pragma(mangle, "gc_collect") static void collect() @safe nothrow pure;
294 
295     /**
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.
299      */
300     pragma(mangle, "gc_minimize") static void minimize() @safe nothrow pure;
301 
302 extern(D):
303 
304     /**
305      * Elements for a bit field representing memory block attributes.  These
306      * are manipulated via the getAttr, setAttr, clrAttr functions.
307      */
308     enum BlkAttr : uint
309     {
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.
314         /**
315         This block contains the info to allow appending.
316 
317         This can be used to manually allocate arrays. Initial slice size is 0.
318 
319         Note: The slice's usable size will not match the block size. Use
320         $(LREF capacity) to retrieve actual usable capacity.
321 
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         ----
334         */
335         APPENDABLE  = 0b0000_1000,
336 
337         /**
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.
342         */
343         NO_INTERIOR = 0b0001_0000,
344 
345         STRUCTFINAL = 0b0010_0000, // the block has a finalizer for (an array of) structs
346     }
347 
348 
349     /**
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.
353      *
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.
357      */
358     alias BlkInfo = BlkInfo_;
359 
360 
361     /**
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.
366      *
367      * Params:
368      *  p = A pointer to the root of a valid memory block or to null.
369      *
370      * Returns:
371      *  A bit field containing any bits set for the memory block referenced by
372      *  p or zero on error.
373      */
374     static uint getAttr( const scope void* p ) nothrow
375     {
376         return gc_getAttr(cast(void*) p);
377     }
378 
379 
380     /// ditto
381     static uint getAttr(void* p) pure nothrow
382     {
383         return gc_getAttr( p );
384     }
385 
386 
387     /**
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.
392      *
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.
396      *
397      * Returns:
398      *  The result of a call to getAttr after the specified bits have been
399      *  set.
400      */
401     static uint setAttr( const scope void* p, uint a ) nothrow
402     {
403         return gc_setAttr(cast(void*) p, a);
404     }
405 
406 
407     /// ditto
408     static uint setAttr(void* p, uint a) pure nothrow
409     {
410         return gc_setAttr( p, a );
411     }
412 
413 
414     /**
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.
419      *
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.
423      *
424      * Returns:
425      *  The result of a call to getAttr after the specified bits have been
426      *  cleared.
427      */
428     static uint clrAttr( const scope void* p, uint a ) nothrow
429     {
430         return gc_clrAttr(cast(void*) p, a);
431     }
432 
433 
434     /// ditto
435     static uint clrAttr(void* p, uint a) pure nothrow
436     {
437         return gc_clrAttr( p, a );
438     }
439 
440 extern(C):
441 
442     /**
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.
448      *
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.
454      *
455      * Returns:
456      *  A reference to the allocated memory or null if insufficient memory
457      *  is available.
458      *
459      * Throws:
460      *  OutOfMemoryError on allocation failure.
461      */
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;
467 
468     /**
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.
474      *
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.
480      *
481      * Returns:
482      *  Information regarding the allocated memory block or BlkInfo.init on
483      *  error.
484      *
485      * Throws:
486      *  OutOfMemoryError on allocation failure.
487      */
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;
493 
494 
495     /**
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.
502      *
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.
508      *
509      * Returns:
510      *  A reference to the allocated memory or null if insufficient memory
511      *  is available.
512      *
513      * Throws:
514      *  OutOfMemoryError on allocation failure.
515      */
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;
521 
522 
523     /**
524      * Extend, shrink or allocate a new block of memory keeping the contents of
525      * an existing block
526      *
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.
532      *
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.
538      *
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`.
544      *
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.
549      *
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.
556      *
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.
561      *
562      * Throws:
563      *  `OutOfMemoryError` on allocation failure.
564      */
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;
570 
571     // https://issues.dlang.org/show_bug.cgi?id=13111
572     ///
573     unittest
574     {
575         enum size1 = 1 << 11 + 1; // page in large object pool
576         enum size2 = 1 << 22 + 1; // larger than large object pool size
577 
578         auto data1 = cast(ubyte*)GC.calloc(size1);
579         auto data2 = cast(ubyte*)GC.realloc(data1, size2);
580 
581         GC.BlkInfo info = GC.query(data2);
582         assert(info.size >= size2);
583     }
584 
585 
586     /**
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.
592      *
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.
600      *
601      * Returns:
602      *  The size in bytes of the extended memory block referenced by p or zero
603      *  if no extension occurred.
604      *
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.
610      */
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;
616 
617     /// Standard extending
618     unittest
619     {
620         size_t size = 1000;
621         int* p = cast(int*)GC.malloc(size * int.sizeof, GC.BlkAttr.NO_SCAN);
622 
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;
627     }
628     /// slice extending
629     unittest
630     {
631         int[] slice = new int[](1000);
632         int*  p     = slice.ptr;
633 
634         //Check we have access to capacity before attempting the extend
635         if (slice.capacity)
636         {
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)
640             {
641                 slice.length = slice.capacity;
642                 assert(slice.length >= 2000);
643             }
644         }
645     }
646 
647 
648     /**
649      * Requests that at least sz bytes of memory be obtained from the operating
650      * system and marked as free.
651      *
652      * Params:
653      *  sz = The desired size in bytes.
654      *
655      * Returns:
656      *  The actual number of bytes reserved or zero on error.
657      */
658     pragma(mangle, "gc_reserve") static size_t reserve(size_t sz) nothrow pure;
659 
660 
661     /**
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`.
668      *
669      * Params:
670      *  p = A pointer to the root of a valid memory block or to null.
671      */
672     pragma(mangle, "gc_free") static void free(void* p) pure nothrow @nogc;
673 
674 extern(D):
675 
676     /**
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.
683      *
684      * Params:
685      *  p = A pointer to the root or the interior of a valid memory block or to
686      *      null.
687      *
688      * Returns:
689      *  The base address of the memory block referenced by p or null on error.
690      */
691     static inout(void)* addrOf( inout(void)* p ) nothrow @nogc pure @trusted
692     {
693         return cast(inout(void)*)gc_addrOf(cast(void*)p);
694     }
695 
696     /// ditto
697     static void* addrOf(void* p) pure nothrow @nogc @trusted
698     {
699         return gc_addrOf(p);
700     }
701 
702     /**
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.
708      *
709      * Params:
710      *  p = A pointer to the root of a valid memory block or to null.
711      *
712      * Returns:
713      *  The size in bytes of the memory block referenced by p or zero on error.
714      */
715     static size_t sizeOf( const scope void* p ) nothrow @nogc /* FIXME pure */
716     {
717         return gc_sizeOf(cast(void*)p);
718     }
719 
720 
721     /// ditto
722     static size_t sizeOf(void* p) pure nothrow @nogc
723     {
724         return gc_sizeOf( p );
725     }
726 
727     // verify that the reallocation doesn't leave the size cache in a wrong state
728     unittest
729     {
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);
736     }
737 
738     /**
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.
744      *
745      * Params:
746      *  p = A pointer to the root or the interior of a valid memory block or to
747      *      null.
748      *
749      * Returns:
750      *  Information regarding the memory block referenced by p or BlkInfo.init
751      *  on error.
752      */
753     static BlkInfo query(return scope const void* p) nothrow
754     {
755         return gc_query(cast(void*)p);
756     }
757 
758 
759     /// ditto
760     static BlkInfo query(return scope void* p) pure nothrow
761     {
762         return gc_query( p );
763     }
764 
765     /**
766      * Returns runtime stats for currently active GC implementation
767      * See `core.memory.GC.Stats` for list of available metrics.
768      */
769     static Stats stats() @safe nothrow @nogc
770     {
771         return gc_stats();
772     }
773 
774     /**
775      * Returns runtime profile stats for currently active GC implementation
776      * See `core.memory.GC.ProfileStats` for list of available metrics.
777      */
778     static ProfileStats profileStats() nothrow @nogc @safe
779     {
780         return gc_profileStats();
781     }
782 
783 extern(C):
784 
785     /**
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.
789      *
790      * If p is null, no operation is performed.
791      *
792      * Params:
793      *  p = A pointer into a GC-managed memory block or null.
794      *
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*);
801      *
802      * // Allocate an object on the GC heap (this would usually be
803      * // some application-specific context data).
804      * auto context = new Object;
805      *
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);
809      *
810      * // Also ensure that a moving collector does not relocate
811      * // the object.
812      * GC.setAttr(cast(void*)context, GC.BlkAttr.NO_MOVE);
813      *
814      * // Now context can be safely passed to the C library.
815      * addCallback(&myHandler, cast(void*)context);
816      *
817      * extern(C) void myHandler(void* ctx)
818      * {
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);
824      *
825      *     auto context = cast(Object)ctx;
826      *     // Use context here…
827      * }
828      * ---
829      */
830     pragma(mangle, "gc_addRoot") static void addRoot(const void* p) nothrow @nogc pure;
831 
832 
833     /**
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.
837      *
838      * Params:
839      *  p = A pointer into a GC-managed memory block or null.
840      */
841     pragma(mangle, "gc_removeRoot") static void removeRoot(const void* p) nothrow @nogc pure;
842 
843 
844     /**
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.
847      *
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.
851      *
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
858      *
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);
864      *
865      * // Add it as a GC range.
866      * GC.addRange(rawMemory, size);
867      *
868      * // Now, pointers to GC-managed memory stored in
869      * // rawMemory will be recognized on collection.
870      * ---
871      */
872     pragma(mangle, "gc_addRange")
873     static void addRange(const void* p, size_t sz, const TypeInfo ti = null) @nogc nothrow pure;
874 
875 
876     /**
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.
881      *
882      * Params:
883      *  p  = A pointer to a valid memory address or to null.
884      */
885     pragma(mangle, "gc_removeRange") static void removeRange(const void* p) nothrow @nogc pure;
886 
887 
888     /**
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.
894      *
895      * Params:
896      *  segment = address range of a code segment.
897      */
898     pragma(mangle, "gc_runFinalizers") static void runFinalizers(const scope void[] segment);
899 
900     /**
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.
903      *
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.
907      *
908      * Returns:
909      *  true if the current thread is in a finalizer, a destructor invoked by
910      *  the GC.
911      */
912     pragma(mangle, "gc_inFinalizer") static bool inFinalizer() nothrow @nogc @safe;
913 
914     ///
915     @safe nothrow @nogc unittest
916     {
917         // Only code called from a destructor is executed during finalization.
918         assert(!GC.inFinalizer);
919     }
920 
921     ///
922     unittest
923     {
924         enum Outcome
925         {
926             notCalled,
927             calledManually,
928             calledFromDruntime
929         }
930 
931         static class Resource
932         {
933             static Outcome outcome;
934 
935             this()
936             {
937                 outcome = Outcome.notCalled;
938             }
939 
940             ~this()
941             {
942                 if (GC.inFinalizer)
943                 {
944                     outcome = Outcome.calledFromDruntime;
945 
946                     import core.exception : InvalidMemoryOperationError;
947                     try
948                     {
949                         /*
950                          * Presently, allocating GC memory during finalization
951                          * is forbidden and leads to
952                          * `InvalidMemoryOperationError` being thrown.
953                          *
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.
958                          */
959                         cast(void) GC.malloc(1);
960                         assert(false);
961                     }
962                     catch (InvalidMemoryOperationError e)
963                     {
964                         return;
965                     }
966                     assert(false);
967                 }
968                 else
969                     outcome = Outcome.calledManually;
970             }
971         }
972 
973         static void createGarbage()
974         {
975             auto r = new Resource;
976             r = null;
977         }
978 
979         assert(Resource.outcome == Outcome.notCalled);
980         createGarbage();
981         GC.collect;
982         assert(
983             Resource.outcome == Outcome.notCalled ||
984             Resource.outcome == Outcome.calledFromDruntime);
985 
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;
990 
991         debug(MEMSTOMP) {} else
992         {
993             // assume Resource data is still available
994             r.destroy;
995             assert(Resource.outcome == Outcome.notCalled);
996         }
997 
998         r = new Resource;
999         assert(Resource.outcome == Outcome.notCalled);
1000         r.destroy;
1001         assert(Resource.outcome == Outcome.calledManually);
1002     }
1003 
1004     /**
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.
1008      */
1009     pragma(mangle, "gc_allocatedInCurrentThread") static ulong allocatedInCurrentThread() nothrow;
1010 
1011     /// Using allocatedInCurrentThread
1012     nothrow unittest
1013     {
1014         ulong currentlyAllocated = GC.allocatedInCurrentThread();
1015         struct DataStruct
1016         {
1017             long l1;
1018             long l2;
1019             long l3;
1020             long l4;
1021         }
1022         DataStruct* unused = new DataStruct;
1023         assert(GC.allocatedInCurrentThread() == currentlyAllocated + 32);
1024         assert(GC.stats().allocatedInCurrentThread == currentlyAllocated + 32);
1025     }
1026 }
1027 
1028 /**
1029  * Pure variants of C's memory allocation functions `malloc`, `calloc`, and
1030  * `realloc` and deallocation function `free`.
1031  *
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.
1035  *
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.
1039  */
1040 void* pureMalloc()(size_t size) @trusted pure @nogc nothrow
1041 {
1042     const errnosave = fakePureErrno;
1043     void* ret = fakePureMalloc(size);
1044     fakePureErrno = errnosave;
1045     return ret;
1046 }
1047 /// ditto
1048 void* pureCalloc()(size_t nmemb, size_t size) @trusted pure @nogc nothrow
1049 {
1050     const errnosave = fakePureErrno;
1051     void* ret = fakePureCalloc(nmemb, size);
1052     fakePureErrno = errnosave;
1053     return ret;
1054 }
1055 /// ditto
1056 void* pureRealloc()(void* ptr, size_t size) @system pure @nogc nothrow
1057 {
1058     const errnosave = fakePureErrno;
1059     void* ret = fakePureRealloc(ptr, size);
1060     fakePureErrno = errnosave;
1061     return ret;
1062 }
1063 
1064 /// ditto
1065 void pureFree()(void* ptr) @system pure @nogc nothrow
1066 {
1067     version (Posix)
1068     {
1069         // POSIX free doesn't set errno
1070         fakePureFree(ptr);
1071     }
1072     else
1073     {
1074         const errnosave = fakePureErrno;
1075         fakePureFree(ptr);
1076         fakePureErrno = errnosave;
1077     }
1078 }
1079 
1080 ///
1081 @system pure nothrow @nogc unittest
1082 {
1083     ubyte[] fun(size_t n) pure
1084     {
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];
1091     }
1092 
1093     auto buf = fun(100);
1094     assert(buf.length == 200);
1095     pureFree(buf.ptr);
1096 }
1097 
1098 @system pure nothrow @nogc unittest
1099 {
1100     const int errno = fakePureErrno();
1101 
1102     void* x = pureMalloc(10);            // normal allocation
1103     assert(errno == fakePureErrno()); // errno shouldn't change
1104     assert(x !is null);                   // allocation should succeed
1105 
1106     x = pureRealloc(x, 10);              // normal reallocation
1107     assert(errno == fakePureErrno()); // errno shouldn't change
1108     assert(x !is null);                   // allocation should succeed
1109 
1110     fakePureFree(x);
1111 
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
1115 
1116     fakePureFree(y);
1117 
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);
1123 }
1124 
1125 // locally purified for internal use here only
1126 
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")
1130 {
1131     extern(C) pragma(mangle, __traits(identifier, core.stdc.errno.errno))
1132     private ref int fakePureErrno() @nogc nothrow pure @system;
1133 }
1134 else
1135 {
1136     extern(C) private @nogc nothrow pure @system
1137     {
1138         pragma(mangle, __traits(identifier, core.stdc.errno.getErrno))
1139         @property int fakePureErrno();
1140 
1141         pragma(mangle, __traits(identifier, core.stdc.errno.setErrno))
1142         @property int fakePureErrno(int);
1143     }
1144 }
1145 
1146 version (D_BetterC) {}
1147 else // TODO: remove this function after Phobos no longer needs it.
1148 extern (C) private @system @nogc nothrow
1149 {
1150     ref int fakePureErrnoImpl()
1151     {
1152         import core.stdc.errno;
1153         return errno();
1154     }
1155 }
1156 
1157 extern (C) private pure @system @nogc nothrow
1158 {
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);
1162 
1163     pragma(mangle, "free") void fakePureFree(void* ptr);
1164 }
1165 
1166 /**
1167 Destroys and then deallocates an object.
1168 
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.
1178     )
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.
1186     )
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.
1190     )
1191 )
1192 
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.
1196 
1197 Params:
1198     x = aggregate object that should be destroyed
1199 
1200 See_Also: $(REF1 destroy, object), $(REF free, core,GC)
1201 
1202 History:
1203 
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.
1208 */
1209 void __delete(T)(ref T x) @system
1210 {
1211     static void _destructRecurse(S)(ref S s)
1212     if (is(S == struct))
1213     {
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();
1218     }
1219 
1220     // See also: https://github.com/dlang/dmd/blob/v2.078.0/src/dmd/e2ir.d#L3886
1221     static if (is(T == interface))
1222     {
1223         .object.destroy(x);
1224     }
1225     else static if (is(T == class))
1226     {
1227         .object.destroy(x);
1228     }
1229     else static if (is(T == U*, U))
1230     {
1231         static if (is(U == struct))
1232         {
1233             if (x)
1234                 _destructRecurse(*x);
1235         }
1236     }
1237     else static if (is(T : E[], E))
1238     {
1239         static if (is(E == struct))
1240         {
1241             foreach_reverse (ref e; x)
1242                 _destructRecurse(e);
1243         }
1244     }
1245     else
1246     {
1247         static assert(0, "It is not possible to delete: `" ~ T.stringof ~ "`");
1248     }
1249 
1250     static if (is(T == interface) ||
1251               is(T == class) ||
1252               is(T == U2*, U2))
1253     {
1254         GC.free(GC.addrOf(cast(void*) x));
1255         x = null;
1256     }
1257     else static if (is(T : E2[], E2))
1258     {
1259         GC.free(GC.addrOf(cast(void*) x.ptr));
1260         x = null;
1261     }
1262 }
1263 
1264 /// Deleting classes
1265 unittest
1266 {
1267     bool dtorCalled;
1268     class B
1269     {
1270         int test;
1271         ~this()
1272         {
1273             dtorCalled = true;
1274         }
1275     }
1276     B b = new B();
1277     B a = b;
1278     b.test = 10;
1279 
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
1288 }
1289 
1290 /// Deleting interfaces
1291 unittest
1292 {
1293     bool dtorCalled;
1294     interface A
1295     {
1296         int quack();
1297     }
1298     class B : A
1299     {
1300         int a;
1301         int quack()
1302         {
1303             a++;
1304             return a;
1305         }
1306         ~this()
1307         {
1308             dtorCalled = true;
1309         }
1310     }
1311     A a = new B();
1312     a.quack();
1313 
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);
1319 }
1320 
1321 /// Deleting structs
1322 unittest
1323 {
1324     bool dtorCalled;
1325     struct A
1326     {
1327         string test;
1328         ~this()
1329         {
1330             dtorCalled = true;
1331         }
1332     }
1333     auto a = new A("foo");
1334 
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);
1340 
1341     // https://issues.dlang.org/show_bug.cgi?id=22779
1342     A *aptr;
1343     __delete(aptr);
1344 }
1345 
1346 /// Deleting arrays
1347 unittest
1348 {
1349     int[] a = [1, 2, 3];
1350     auto b = a;
1351 
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
1359 }
1360 
1361 /// Deleting arrays of structs
1362 unittest
1363 {
1364     int dtorCalled;
1365     struct A
1366     {
1367         int a;
1368         ~this()
1369         {
1370             assert(dtorCalled == a);
1371             dtorCalled++;
1372         }
1373     }
1374     auto arr = [A(1), A(2), A(3)];
1375     arr[0].a = 2;
1376     arr[1].a = 1;
1377     arr[2].a = 0;
1378 
1379     assert(GC.addrOf(arr.ptr) != null);
1380     __delete(arr);
1381     assert(dtorCalled == 3);
1382     assert(GC.addrOf(arr.ptr) == null);
1383 }
1384 
1385 // Deleting raw memory
1386 unittest
1387 {
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);
1394 }
1395 
1396 // __delete returns with no effect if x is null
1397 unittest
1398 {
1399     Object x = null;
1400     __delete(x);
1401 
1402     struct S { ~this() { } }
1403     class C { }
1404     interface I { }
1405 
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);
1414 }
1415 
1416 // https://issues.dlang.org/show_bug.cgi?id=19092
1417 unittest
1418 {
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);
1424 
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);
1430 }
1431 
1432 // test realloc behaviour
1433 unittest
1434 {
1435     static void set(int* p, size_t size)
1436     {
1437         foreach (i; 0 .. size)
1438             *p++ = cast(int) i;
1439     }
1440     static void verify(int* p, size_t size)
1441     {
1442         foreach (i; 0 .. size)
1443             assert(*p++ == i);
1444     }
1445     static void test(size_t memsize)
1446     {
1447         int* p = cast(int*) GC.malloc(memsize * int.sizeof);
1448         assert(p);
1449         set(p, memsize);
1450         verify(p, memsize);
1451 
1452         int* q = cast(int*) GC.realloc(p + 4, 2 * memsize * int.sizeof);
1453         assert(q == null);
1454 
1455         q = cast(int*) GC.realloc(p + memsize / 2, 2 * memsize * int.sizeof);
1456         assert(q == null);
1457 
1458         q = cast(int*) GC.realloc(p + memsize - 1, 2 * memsize * int.sizeof);
1459         assert(q == null);
1460 
1461         int* r = cast(int*) GC.realloc(p, 5 * memsize * int.sizeof);
1462         verify(r, memsize);
1463         set(r, 5 * memsize);
1464 
1465         int* s = cast(int*) GC.realloc(r, 2 * memsize * int.sizeof);
1466         verify(s, 2 * memsize);
1467 
1468         assert(GC.realloc(s, 0) == null); // free
1469         assert(GC.addrOf(p) == null);
1470     }
1471 
1472     test(16);
1473     test(200);
1474     test(800); // spans large and small pools
1475     test(1200);
1476     test(8000);
1477 
1478     void* p = GC.malloc(100);
1479     assert(GC.realloc(&p, 50) == null); // non-GC pointer
1480 }
1481 
1482 // test GC.profileStats
1483 unittest
1484 {
1485     auto stats = GC.profileStats();
1486     GC.collect();
1487     auto nstats = GC.profileStats();
1488     assert(nstats.numCollections > stats.numCollections);
1489 }
1490 
1491 // in rt.lifetime:
1492 private extern (C) void* _d_newitemU(scope const TypeInfo _ti) @system pure nothrow;
1493 
1494 /**
1495 Moves a value to a new GC allocation.
1496 
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.
1500 
1501 Returns:
1502     A pointer to the new GC-allocated value.
1503 */
1504 T* moveToGC(T)(auto ref T value)
1505 {
1506     static T* doIt(ref T value) @trusted
1507     {
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;
1512     }
1513 
1514     return doIt(value); // T dtor might be @system
1515 }
1516 
1517 ///
1518 @safe pure nothrow unittest
1519 {
1520     struct S
1521     {
1522         int x;
1523         this(this) @disable;
1524         ~this() @safe pure nothrow @nogc {}
1525     }
1526 
1527     S* p;
1528 
1529     // rvalue
1530     p = moveToGC(S(123));
1531     assert(p.x == 123);
1532 
1533     // lvalue
1534     auto lval = S(456);
1535     p = moveToGC(lval);
1536     assert(p.x == 456);
1537     assert(lval.x == 0);
1538 }
1539 
1540 // @system dtor
1541 unittest
1542 {
1543     struct S
1544     {
1545         int x;
1546         ~this() @system {}
1547     }
1548 
1549     // lvalue case is @safe, ref param isn't destructed
1550     static assert(__traits(compiles, (ref S lval) @safe { moveToGC(lval); }));
1551 
1552     // rvalue case is @system, value param is destructed
1553     static assert(!__traits(compiles, () @safe { moveToGC(S(0)); }));
1554 }