More radical ideas about gc and reference counting

Jonathan M Davis via Digitalmars-d digitalmars-d at puremagic.com
Sat May 3 20:55:37 PDT 2014


On Sat, 03 May 2014 15:44:03 -0700
Andrei Alexandrescu via Digitalmars-d <digitalmars-d at puremagic.com>
wrote:

> On 5/3/14, 12:40 PM, Walter Bright wrote:
> > On 5/1/2014 7:59 AM, Andrei Alexandrescu wrote:
> >> If a class has at least one member with a
> >> destructor, the compiler might need to generate a destructor for
> >> the class.
> >
> > And in fact that's what dmd does.
>
> Which suggests a simple solution for calling destructors for structs
> and arrays:
>
> * Lower new for structs to return:
>
> new S;
> ->
> return &(new class { S member; }).member;
>
> * Lower array construction similarly.
>
> Then voila, the anonymous classes will destroy structs and arrays
> appropriately.

This might be a good approach, though I confess that it strikes me as rather
weird to wrap structs in classes like that. It also leaves open the question
of how to deal with structs that are newed directly rather than put in an
array. _Those_ really need to be destroyed properly as well. And unless
you're suggesting that S* effectively become a pointer to a struct within
an anonymous class in all cases, not only would this not work for structs
which were newed up directly on the heap, but I'd be worried about what would
happen when someone did something like &arr[5] with an array of structs. Would
they get a pointer to a class or a struct?

It was my understanding that some of what Rainer Schutze did with his precise
GC involved adding RTInfo which could make it possible to run struct
destructors for structs that were newed up directly on the heap. If that is
indeed the case, then I would think that we could use that for arrays of
structs as well.

- Jonathan M Davis


More information about the Digitalmars-d mailing list