I can has @nogc and throw Exceptions?

Lodovico Giaretta via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Jul 13 04:39:11 PDT 2016


On Wednesday, 13 July 2016 at 00:57:38 UTC, Adam Sansier wrote:
> How about simply setting aside a 100kb of memory as a pool for 
> exceptions. Seems like a lot but still under 640kb, hell, even 
> 1MB would still be tiny.
>
> After all, it's not like exceptions are common or happen in 
> complex ways.
>
> Does anyone have a proper solution to this problem? I'd like 
> nogc exception handling with run-time generated args.

You shall use a static per-thread Region allocator[1] backed by 
Mallocator[2].
Then you just make[3] exceptions inside it and throw them.
So you can allocate and chain exceptions until you end the memory 
established on creation.
Whenever you don't need the exception chain anymore (i.e.: you 
catched them and program is back in "normal" mode, you just reset 
the region allocator, so you have all of your memory again, for 
the next exception chain).

[1] 
https://dlang.org/phobos/std_experimental_allocator_building_blocks_region.html
[2] 
https://dlang.org/phobos/std_experimental_allocator_mallocator.html
[3] https://dlang.org/phobos/std_experimental_allocator.html


More information about the Digitalmars-d-learn mailing list