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

H. S. Teoh hsteoh at quickfur.ath.cx
Fri Nov 12 17:44:39 UTC 2021


On Fri, Nov 12, 2021 at 05:23:33PM +0000, deadalnix via Digitalmars-d wrote:
> On Friday, 12 November 2021 at 17:01:57 UTC, H. S. Teoh wrote:
> > As I said elsewhere, it's a different level of abstraction. The GC
> > operates at a lower level of abstraction, and therefore does "dirty"
> > things like casting to/from immutable, pointer arithmetic, etc.. But
> > the interface it presents to user code forms a higher level of
> > abstraction where these low-level manipulations are hidden away
> > under the hood.
[...]
> I think it is fair to expect any RC system to be able to operate at
> that same level.

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.


T

-- 
Tech-savvy: euphemism for nerdy.


More information about the Digitalmars-d mailing list