|
On Feb 15, 2005, at 16:48, David Given wrote:
On Tuesday 15 February 2005 16:18, David Jones wrote: [...]In particular when longjmp is invoked it is allowed to trash any local variable declared in the function that called setjmp (the one to which we are jumping), unless that variable is declared volatile.You are, of course, absolutely right.(It turns out that gcc *does* have special knowledge of setjmp(), but only enough to warn you about non-volatile variables --- it doesn't stack andunstack stuff for you, which is really irritating because the code I'm working on right now requires it. Does anyone know any workarounds?)
1) declare your stuff volatile. May slow down too much, so perhaps try...
2) only call setjmp from within a wrapper function. It doesn't have any locals, so none to trash. This is just a trick to get the compiler to emit the necessary stacking and unstacking (which will happen when you call the wrapper function).
A closer re-reading (encouraged by a dim spark in my memory) shows that longjmp _does_ preserve locals, but only if they have not changed between the setjmp and the longjmp. In other words if you haven't changed the value of a local between the setjmp and the longjmp then you're okay. Thinking about it, it would be madness if this wasn't true. Note that other than code in the function that calls setjmp, the only other way to change the value of the local in the function that called setjmp is if you takes its address, and you presumably know if you do that; so the amount of code you have to examine is relatively small.
David Jones