Reference counted containers prototype
Andrei Alexandrescu
SeeWebsiteForEmail at erdani.org
Mon Dec 26 09:25:10 PST 2011
Hello,
I've been playing with a new approach to reference counting, in
particular for the containers library.
A small prototype is at http://pastebin.com/WnSQY1Jw. The prototype
features a simple doubly-linked list implementation DListImpl. That is
not supposed to be manipulated directly (or it might, in case the user
wants a simple garbage collected implementation - this is a point in the
discussion).
DListImpl has only a couple of primitives implemented. The only
interesting points that it tries to make are:
(a) the presence of the dispose() primitive, which deallocates all
memory and brings the object back to its .init state
(b) the presence of the dup() primitive, which creates a full-blown
duplicate of the object.
The interesting part of the sample is RefImpl, which has a couple of
quite interesting details:
(a) All interaction with the held object is done via opDispatch. In fact
opDispatch can be engineered to statically enforce no reference to the
held object escapes.
(b) A const/immutable call against a non-existing is silently converted
into a call against a default-initialized object.
(c) If a call works and returns the same type for a non-const and a
const object, then the const version is preferred. This is to reduce the
number of calls to ensureUnique().
Destroy.
Andrei
More information about the Digitalmars-d
mailing list