List of Phobos functions that allocate memory?

Marco Leise Marco.Leise at gmx.de
Sat Feb 8 20:38:59 PST 2014


Am Sat, 08 Feb 2014 11:33:51 +0000
schrieb "Jakob Ovrum" <jakobovrum at gmail.com>:

> On Saturday, 8 February 2014 at 11:27:27 UTC, Jonathan M Davis 
> wrote:
> > On Saturday, February 08, 2014 11:17:25 Jakob Ovrum wrote:
> >> On Saturday, 8 February 2014 at 11:05:38 UTC, Dmitry Olshansky
> >> wrote:>
> >> 
> >> > If both are thread-local and cached I see no problem 
> >> > whatsoever.
> >> > The thing is the current "default" of creating exception is
> >> > AWFUL.
> >> > And D stands for sane defaults and the simple path being good
> >> > last time I checked.
> >> 
> >> How is it not a problem? XException's fields (message, location
> >> etc) would be overwritten by the latest throw site, and its
> >> `next` field would point to itself.
> >
> > Then we have multiple of them, or we new up another one when a 
> > second one is
> > needed. Even if it were only the first exception which avoided 
> > the allocation,
> > it would be a big gain, and in most cases, you're only going to 
> > get a single
> > exception, or the exceptions will be of different types.
> >
> > - Jonathan M Davis
> 
> Yes, I'm sure there is a cool solution, I'm just pointing out 
> that it's not as simple as statically allocating.
> 
> I think it would be a nice exercise to compose such a solution 
> with std.allocator.

Yes, it doesn't seem feasible otherwise. Since you can call
functions recursively you could potentially chain exceptions
from the same line of code several times.

  catch (Exception e)
  {
      staticException.line = __LINE__;
      staticException.file = __FILE__;
      staticException.next = e;  // e.next is staticException
      throw staticException;
  }

You'd have to flag staticException as "in use" and spawn a new
instance every time you need another one of the same type.
Since there is no way to reset that flag automatically when
the last user goes out of scope (i.e. ref counting), that's
not even an option.

Preallocated exceptions only work if you are confident your
exception wont be recursively thrown and thereby chained to
itself. Granted, the majority of code, but really too much
cognitive load when writing exception handling code.

-- 
Marco



More information about the Digitalmars-d mailing list