try block portability?

Sean Kelly sean at f4.ca
Thu Mar 8 08:44:14 PST 2007


Davidl at 126.com wrote:
> 
> i've seen reactos hacker suffer from PSEH thingie.
> and they use a macro to implement PSEH try block.
> the PSEH try block would looks like
> _PSEH_TRY
> {
> .....
> }
> _PSEH_EXCEPT
> {
> ....
> }
> _PSEH_END
> 
> this is a good idea since all try block code implemented
> in C. and it's more portable than
> try
> {
> }
> except
> {
> }
> 
> but it's very difficult to ensure it's correct. and also the
> binary generated is dummy and low efficient, cause the idea
> of it is implemented roughly as following
> 
> for(;;)
> {
> if not firsttimevisit
> {
>   ... try  code
>   break;
> }
> else
> {
>    if (setjmp(label1))
>    label1:
>    {
>       ... finally code
>    }
>    else
>    {
>       continue;
>    }
> }
> }
> 
> any idea of meta-programming of implementing this try block
> stuff? or need some compiler support?

This could probably be sorted out with some clever programming, but I'm 
not sure it's a good idea.  Depending on the platform, longjmp may or 
may not execute dtors and such as it moves its way up the stack.  If it 
doesn't, the app could leak resources.  If it does, you're doing the 
same thing as normal exception handling and gain nothing.  If 
performance is really a concern, it might be worth investigating the 
exception handing mechanism itself and see if that can be optimized. 
Win32 uses SEH, and I think Win64 uses some new method.  And exception 
handling in Unix is largely hand-coded into phobos (see internal/deh.d).


Sean



More information about the Digitalmars-d mailing list