Thread overview | |||||
---|---|---|---|---|---|
|
May 22, 2016 Overload new and delete to not use GC? | ||||
---|---|---|---|---|
| ||||
I know it's possible to do [explicit object allocation](http://wiki.dlang.org/Memory_Management#Explicit_Class_Instance_Allocation) on the heap, but I find that quite cumbersome. So.. is it possible to overload 'new' and 'delete' to not use GC? Also, it seems many features of the language rely on GC. Is there a definitive list of those? |
May 22, 2016 Re: Overload new and delete to not use GC? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Rusty | On Sunday, 22 May 2016 at 07:35:32 UTC, Rusty wrote: > I know it's possible to do [explicit object allocation](http://wiki.dlang.org/Memory_Management#Explicit_Class_Instance_Allocation) on the heap, but I find that quite cumbersome. > > So.. is it possible to overload 'new' and 'delete' to not use GC? The way to do it is to use emplace() and destroy() instead. https://p0nce.github.io/d-idioms/#Placement-new-with-emplace Unique!T, RefCounted!T, and Scoped!T use emplace() internally. > Also, it seems many features of the language rely on GC. Is there a definitive list of those? - new - Appending and concatenating slices - Homogeneous template parameters void f( - Some array literals - Closures that escape - other features I don't recall @nogc lets you avoid all of them and is necessary to avoid unintentional allocations. |
May 22, 2016 Re: Overload new and delete to not use GC? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Rusty | On Sunday, 22 May 2016 at 07:35:32 UTC, Rusty wrote: > I know it's possible to do [explicit object allocation](http://wiki.dlang.org/Memory_Management#Explicit_Class_Instance_Allocation) on the heap, but I find that quite cumbersome. > > So.. is it possible to overload 'new' and 'delete' to not use GC? use the Mallocator with make and dispose: import std.stdio; import std.experimental.allocator: make, dispose; import std.experimental.allocator.mallocator: Mallocator; class Foo{} void main(string[] args) { Foo foo = make!Foo(Mallocator.instance); dispose(Mallocator.instance, foo); } > Also, it seems many features of the language rely on GC. Is there a definitive list of those ? If you use DMD as compiler the switch -vgc can help to track GC allocations. |
Copyright © 1999-2021 by the D Language Foundation