struct std.experimental.allocator.building_blocks.region.Region should have its postblit disabled, at least when it has a non-empty destructor, i.e. when it frees its store.
Consider the following code:
import std.experimental.allocator;
import std.experimental.allocator.mallocator;
import std.experimental.allocator.building_blocks.region;
import std.stdio: writeln;
void foo(Alloc)(Alloc alloc)
{
writeln("alloc disposed...");
}
void main()
{
auto alloc = Region!Mallocator(Mallocator.instance.allocate(128));
auto ptr = alloc.make!int(3);
writeln("*ptr = ", *ptr) // prints 3
foo(alloc);
writeln("*ptr = ", *ptr) // ops... invalid memory...
// ops... alloc destructor trying to free again the block already freed by foo
}
Comment #1 by stojkovic.igor — 2017-09-03T14:12:54Z
Would it work if we allow copying in this case but reserve first few bytes from allocated memory to count references to it so we can only free it when count says there are no more references to it. I think we would just need to increment count on postBlit and decrement it in destructor and opAssign. Something like what I did here in my implementation of SharedRegion:
https://github.com/igor84/dngin/blob/master/source/util/allocators.d#L176
Comment #2 by alphaglosined — 2022-12-17T13:21:18Z
Alternatively, it should support moving rather than copying.
This allows other allocator data structures like free lists to move it into their own memory (which is quite a useful trick).
Comment #3 by robert.schadek — 2024-12-01T16:27:24Z