D without a GC

Iain Buclaw ibuclaw at ubuntu.com
Mon Jan 3 05:57:14 PST 2011


== Quote from bearophile (bearophileHUGS at lycos.com)'s article
> Dmitry Olshansky:
> > As stated in this proposal they are quite useless, e.g. they are easily
> > implemented via mixin with alloca.
> Thank you for your comments. Here I have added some answers to your comments:
> http://d.puremagic.com/issues/show_bug.cgi?id=5348
> Bye,
> bearophile

Some thoughts to your proposal:

> void bar(int len) {
>     Foo* ptr = cast(Foo*)alloca(len * Foo.sizeof);
>     if (ptr == null)
>         throw new Exception("alloca failed");
>     Foo[] arr = ptr[0 .. len];
>     foreach (ref item; arr)
>         item = Foo.init;
>
>     // some code here
>     writeln(arr);
>
>     foreach (ref item; arr)
>         item.__dtor();
> }

1) Why call the dtors manually? Forgive me if I'm wrong, but iirc, alloca will
free the memory when the stack exits from the current frame level. :)

2) The GCC implementation is roughly equivalent to this:

{
  size_t len;
  void * saved_stack;
  int[len] * arr.2;
  int arr[len] [value-expr: *arr.2];

  saved_stack = __builtin_stack_save ();
  try
    {
      arr.2 = (int[len] *) __builtin_alloca (len * 4);
    }
  finally
    {
      __builtin_stack_restore (saved_stack);
    }
}

And any interaction with the VLA is done within the try block.

Being a very biased person, in an imaginary D implementation I'd favour some
similar sort of behaviour. :o)



More information about the Digitalmars-d mailing list