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

Timon Gehr timon.gehr at gmx.ch
Sat Nov 13 03:15:15 UTC 2021


On 12.11.21 23:09, Andrei Alexandrescu wrote:
> On 2021-11-12 16:14, Timon Gehr wrote:
>> 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.
> 
> Indeed, but not indirectly. You can soundly access a pointer to mutable 
> data if you use the immutable pointer as a key in a global hashtable.

Actually, you can't.

On 08.11.21 22:42, Andrei Alexandrescu wrote:
> - work in pure code just like T[] does



More information about the Digitalmars-d mailing list