Allocator-aware @safe reference counting is still not possible
Atila Neves
atila.neves at gmail.com
Wed Feb 1 17:46:17 UTC 2023
On Saturday, 28 January 2023 at 15:56:54 UTC, Nick Treleaven
wrote:
> On Sunday, 22 January 2023 at 15:28:53 UTC, Atila Neves wrote:
>> [...]
>
> `isolated` would be nice, but for now we can model it with a
> struct so that this works:
> ```d
> class Mallocator : IAllocator
> {
> import core.stdc.stdlib : free, malloc;
>
> void* safeAllocate(size_t n) @trusted
> {
> return malloc(n);
> }
>
> void safeDeallocate(Isolated!(void*) ip) @trusted
> {
> ip.unwrap.free;
> }
> }
>
> void main()
> {
> IAllocator a = new Mallocator;
> scope m = a.safeAllocate(4);
> auto ip = (() @trusted =>
> assumeIsolated(a.safeAllocate(4)))();
> a.safeDeallocate(ip.move);
> assert(ip.unwrap == null);
> }
> ```
> Working code:
> https://github.com/ntrel/stuff/blob/master/typecons/isolated.d
>
> Isolated could go in std.typecons.
I don't understand how this presents a @safe interface.
More information about the Digitalmars-d
mailing list