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

rikki cattermole rikki at cattermole.co.nz
Fri Nov 12 02:02:49 UTC 2021


On 12/11/2021 2:49 PM, Greg Strong wrote:
> On Friday, 12 November 2021 at 01:34:05 UTC, rikki cattermole wrote:
>> - Any escape hatch must acknowledge that memory lifetimes is not the 
>> same thing as the memory containing data of a given type.
> 
> I'm sorry, can you expound on this one a little more?  I don't follow.

So the reference count needs to be heap allocated.
The value of a given type must be heap allocated as well.

These two memory addresses need not be next to each other.

I.e.

struct ReferenceCount {
	shared(int) count;
}

struct Node(Type) {
	Type value;
}

struct Reference(Type) {
	private {
		Node* data;
		ReferenceCount* refCount;
	}
}

Rather than:

struct Reference(Type) {
	private	Storage!type* storage;
}

struct Storage(Type) {
	Type value;
	shared(int) refCount;
}

The idea is that the value may be in read only memory, but the reference 
cannot be due to requirement of mutation.


More information about the Digitalmars-d mailing list