Report a bug
		
				If you spot a problem with this page, click here to create a Bugzilla issue.
		
			Improve this page
		
			Quickly fork, edit online, and submit a pull request for this page.
			Requires a signed-in GitHub account. This works well for small changes.
			If you'd like to make larger changes you may want to consider using
			a local clone.
		
	rt.lifetime
This module contains all functions related to an object's lifetime:
 allocation, resizing, deallocation, and finalization.
License: 
Distributed under the
      Boost Software License 1.0.
    (See accompanying file LICENSE)
Authors: 
Walter Bright, Sean Kelly, Steven Schveighoffer
Source rt/lifetime.d
- void*_d_allocmemory(size_tsz);
- Allocate memory using the garbage collectorDMD uses this to allocate closures:void f(byte[24] x) { return () => x; // `x` is on stack, must be moved to heap to keep it alive } Parameters:size_t sznumber of bytes to allocate Returns:pointer toszbytes of free, uninitialized memory, managed by the GC.
- Object_d_newclass(const ClassInfoci);
- Create a new class instance.Allocates memory and sets fields to their initial value, but does not call a constructor.new Object() // _d_newclass(typeid(Object)) Parameters:ClassInfo ciTypeInfo_Class object, to provide instance size and initial bytes to copy Returns:newly created object
- void_d_delinterface(void**p);
- void_d_delclass(Object*p);
- nothrow void_d_arrayshrinkfit(const TypeInfoti, void[]arr);
- Shrink the "allocated" length of an array to be the exact size of the array.It doesn't matter what the current allocated length of the array is, the user is telling the runtime that he knows what he is doing.Parameters:TypeInfo tiTypeInfo of array type void[] arrarray to shrink. Its .length is element length, not byte length, despite void type 
- size_t_d_arraysetcapacity(const TypeInfoti, size_tnewcapacity, void[]*p);
- Set the array capacity.If the array capacity isn't currently large enough to hold the requested capacity (in number of elements), then the array is resized/reallocated to the appropriate size. Pass in a requested capacity of 0 to get the current capacity.Parameters:TypeInfo titype info of element type size_t newcapacityrequested new capacity void[]* ppointer to array to set. Its length is left unchanged. Returns:the number of elements that can actually be stored once the resizing is done
- pure nothrow void[]_d_newarrayU(scope const TypeInfoti, size_tlength);
 pure nothrow void[]_d_newarrayT(const TypeInfoti, size_tlength);
 pure nothrow void[]_d_newarrayiT(const TypeInfoti, size_tlength);
- Allocate an array with the garbage collector.Has three variants:- _d_newarrayUleave elements uninitialized
- _d_newarrayTinitializes to 0 (e.g new int[])
- _d_newarrayiTinitializes based on initializer retrieved from TypeInfo (e.g new float[])
 Parameters:TypeInfo tithe type of the resulting array, (may also be the corresponding array.ptr type) size_t length. lengthof resulting arrayReturns:newly allocated array
- pure nothrow void*_d_newitemU(scope const TypeInfo_ti);
- Non-template version of core.lifetime.d_newitemT that does not perform initialization. Needed for rt.aaA.allocEntry.Parameters:TypeInfo _tiTypeInfo of item to allocate Returns:newly allocated item
- void_d_delmemory(void**p);
- void_d_callinterfacefinalizer(void*p);
- void_d_callfinalizer(void*p);
- voidrt_setCollectHandler(CollectHandlerh);
- CollectHandlerrt_getCollectHandler();
- nothrow intrt_hasFinalizerInSegment(void*p, size_tsize, TypeInfotypeInfo, scope const(void)[]segment);
- nothrow voidrt_finalize2(void*p, booldet= true, boolresetMemory= true);
- nothrow voidrt_finalize(void*p, booldet= true);
- Backwards compatibility
- void[]_d_arraysetlengthT(const TypeInfoti, size_tnewlength, void[]*p);
 void[]_d_arraysetlengthiT(const TypeInfoti, size_tnewlength, void[]*p);
- Resize a dynamic array by setting the .length propertyNewly created elements are initialized to their default value. Has two variants:- _d_arraysetlengthTfor arrays with elements that initialize to 0
- _d_arraysetlengthiTfor non-zero initializers retrieved from TypeInfo
 void main() { int[] a = [1, 2]; a.length = 3; // gets lowered to `_d_arraysetlengthT(typeid(int[]), 3, &a)` } Parameters:TypeInfo tiTypeInfo of array size_t newlengthnew value for the array's .length void[]* ppointer to array to update the .length of. While it's cast to void[], its .length is still treated as element length. Returns:*pafter being updated
- size_tnewCapacity(size_tnewlength, size_telemsize);
- Given an array of length size that needs to be expanded tonewlength, compute a new capacity.Better version by Dave Fladebo, enhanced by Steven Schveighoffer: This uses an inverse logorithmic algorithm to pre-allocate a bit more space for larger arrays.- The maximum "extra" space is about 80% of the requested space. This is for
 - As the arrays grow, the relative pre-allocated space shrinks.
- Perhaps most importantly, overall memory usage and stress on the GC
 - The algorithm is tuned to avoid any division at runtime.
 Parameters:size_t newlengthnew .length size_t elemsizesize of the element in the new array Returns:new capacity for array
- byte[]_d_arrayappendcTX(const TypeInfoti, ref return scope byte[]px, size_tn);
- Extend an array by n elements.Caller must initialize those elements.Parameters:TypeInfo titype info of array type (not element type) byte[] pxarray to append to, cast to byte[] while keeping the same .length. Will be updated. size_t nnumber of elements to append Returns:pxafter being appended to
- void[]_d_arrayappendcd(ref byte[]x, dcharc);
- Append dchar to char[], converting UTF-32 to UTF-8void main() { char[] s; s ~= 'α'; } Parameters:byte[] xarray to append to cast to byte[]. Will be modified. dchar cdchar to append Returns:updatedxcast to void[]
- void[]_d_arrayappendwd(ref byte[]x, dcharc);
- Append dchar to wchar[], converting UTF-32 to UTF-16void main() { dchar x; wchar[] s; s ~= 'α'; } Parameters:byte[] xarray to append to cast to byte[]. Will be modified. dchar cdchar to append Returns:updatedxcast to void[]
- void*_d_arrayliteralTX(const TypeInfoti, size_tlength);
- Allocate an array literalRely on the caller to do the initialization of the array.int[] getArr() { return [10, 20]; // auto res = cast(int*) _d_arrayliteralTX(typeid(int[]), 2); // res[0] = 10; // res[1] = 20; // return res[0..2]; } Parameters:TypeInfo tiTypeInfo of resulting array type size_t length. lengthof array literalReturns:pointer to allocated array
Copyright © 1999-2025 by the D Language Foundation | Page generated by
Ddoc on Mon Mar 31 10:28:10 2025