GC.addRange multiple times

vitamin vit at vit.vit
Wed Feb 3 16:57:47 UTC 2021


Is it valid if I call GC.addRange with same or overlaping range?
Example:

https://run.dlang.io/is/a5a0Ag

import core.memory;
import std.experimental.allocator.mallocator;
import core.lifetime;

class Bar{
}

struct Foo{
     Bar bar;

     this(Bar bar){
         this.bar = bar;
     }
}


void main(){
     void[] buffer = Mallocator.instance.allocate(Foo.sizeof * 10);
     scope(exit){
	GC.removeRange(buffer.ptr);
         Mallocator.instance.deallocate(buffer);
     }

     GC.addRange(buffer.ptr, Foo.sizeof);
     emplace(cast(Foo*)buffer.ptr, new Bar);

     GC.addRange(buffer.ptr, Foo.sizeof*2);  //Overlaps previous 
range, is it OK?
     emplace(cast(Foo*)(buffer.ptr + Foo.sizeof), new Bar);
}



More information about the Digitalmars-d-learn mailing list