"Marven Lee" <
marv...@gmail.com> wrote in message
news:9pl89c...@mid.individual.net...
> As I was skimming over the thread on exception propagation
> in C it dawned on me that I have never used setjmp() & longjmp().
I haven't used them in a long time. I haven't used them for anything
serious. AIR, they can be awkard to use correctly. Basically, using
longjmp() "resets" the program to a known location, i.e., by "exiting"
setjmp().
> So does setjmp() merely store the register state in the jmp_buf
> or is there some compiler magic going on?
They are both custom implementations for the current C compiler. From the
ones I've looked at *just* the registers needed to resume are saved into the
jump buffer. But, you also need to have the correct parameters on the stack
when you resume. IIRC, that's why you usually use them up a level or two of
nested procedures ... I.e., the local stack for the current procedure gets
cleared out.
> I'd like to know if it's possible to write a custom version
> of setjmp()/longjmp().
1) Does your C compiler support inline assembly?
2) Is this only for one C compiler?
If both are yes, I'd say: "Yes." If multiple C compilers, you'll need to
write one for each. If inline assembly support is a "No", then you
may have some work ahead.
> [ideas]
...
> So is it possible to do a custom setjmp/longjmp or does the
> compiler use some internal trickery that I am not aware of,
> perhaps due to optimizing variables on the stack ?
>
The ones I've seen *DO NOT* save stack state. It's up to the programmer
to use setjmp()/longjmp() in a safe manner.
Summary from Harbison & Steele, "C: A Reference Manual", 3rd, 1991:
a) setjmp() saves caller's environment in an implementation defined array
b) calling longjmp() causes it to return at setjmp()
c) longjmp() passes a status value to setjmp()
d) some implementations allow zero as a status
e) and some prohibit zero returning one instead
f) static variables are preserved
g) for ANSI C, automatic variables local to setjmp() only have their correct
values if i) they weren't modified ii) they are marked as volatile
h) ANSI C restricts the syntax use of setjmp() to four situations (I've not
posted them)
i) ANSI C requires longjmp() to work correctly within non-nested signal
handlers
j) the behavior is undefined if setjmp() wasn't called prior to longjmp()
k) the behavior is undefined if a longjmp() wasn't called prior to exiting
the function that contains setjmp()
The manpages in Posix environments and ISO C drafts should have info too.
Rod Pemberton