Challenge: write a reference counted slice that works as much as possible like a built-in slice
Andrei Alexandrescu
SeeWebsiteForEmail at erdani.com
Fri Nov 12 22:09:28 UTC 2021
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.
More information about the Digitalmars-d
mailing list