Challenge: write a reference counted slice that works as much as possible like a built-in slice

Timon Gehr timon.gehr at gmx.ch
Fri Nov 12 21:14:28 UTC 2021


On 12.11.21 18:44, H. S. Teoh wrote:
> Yes.  So actually, this*could*  be made to work if the RC payload is
> only allowed to be allocated from an RC allocator.  The allocator would
> allocate n+8 bytes, for example, and return a pointer to offset 8 of the
> allocated block, which is cast to whatever type/qualifier(s) needed.
> Offset 0 would be the reference count.  The copy ctor would access the
> reference count as *(ptr-8), which is technically outside the
> const/immutable/whatever payload.  When the ref count reaches 0, the
> allocator knows to deallocate the block starting from (ptr-8), which is
> the start of the actual allocation.

You still need language support. Reaching mutable data through an 
immutable pointer violates transitivity assumptions.


More information about the Digitalmars-d mailing list