<s>
setjmp.h	B-Language
is	O
a	O
header	B-Language
defined	O
in	O
the	B-Language
C	I-Language
standard	I-Language
library	I-Language
to	O
provide	O
"	O
non-local	O
jumps	O
"	O
:	O
control	O
flow	O
that	O
deviates	O
from	O
the	O
usual	O
subroutine	O
call	O
and	O
return	O
sequence	O
.	O
</s>
<s>
The	O
complementary	O
functions	O
setjmp	B-Language
and	O
longjmp	B-Language
provide	O
this	O
functionality	O
.	O
</s>
<s>
A	O
typical	O
use	O
of	O
setjmp/longjmp	B-Language
is	O
implementation	O
of	O
an	O
exception	B-General_Concept
mechanism	I-General_Concept
that	O
exploits	O
the	O
ability	O
of	O
longjmp	B-Language
to	O
reestablish	O
program	O
or	O
thread	O
state	O
,	O
even	O
across	O
multiple	O
levels	O
of	O
function	O
calls	O
.	O
</s>
<s>
A	O
less	O
common	O
use	O
of	O
setjmp	B-Language
is	O
to	O
create	O
syntax	O
similar	O
to	O
coroutines	O
.	O
</s>
<s>
setjmp(jmp_buf env )	O
Sets	O
up	O
the	O
local	O
jmp_buf	O
buffer	B-General_Concept
and	O
initializes	O
it	O
for	O
the	O
jump	O
.	O
</s>
<s>
This	O
routineISO	O
C	O
states	O
that	O
setjmp	B-Language
must	O
be	O
implemented	O
as	O
a	O
macro	O
,	O
but	O
POSIX	O
explicitly	O
states	O
that	O
it	O
is	O
undefined	B-Language
whether	O
setjmp	B-Language
is	O
a	O
macro	O
or	O
a	O
function	O
.	O
</s>
<s>
saves	O
the	O
program	O
's	O
calling	O
environment	O
in	O
the	O
environment	O
buffer	B-General_Concept
specified	O
by	O
the	O
env	O
argument	O
for	O
later	O
use	O
by	O
longjmp	B-Language
.	O
</s>
<s>
If	O
the	O
return	O
is	O
from	O
a	O
direct	O
invocation	O
,	O
setjmp	B-Language
returns	O
0	O
.	O
</s>
<s>
If	O
the	O
return	O
is	O
from	O
a	O
call	O
to	O
longjmp	B-Language
,	O
setjmp	B-Language
returns	O
a	O
nonzero	O
value	O
.	O
</s>
<s>
longjmp(jmp_buf env, int value )	O
Restores	O
the	O
context	O
of	O
the	O
environment	O
buffer	B-General_Concept
env	O
that	O
was	O
saved	O
by	O
invocation	O
of	O
the	O
setjmp	B-Language
routine	O
in	O
the	O
same	O
invocation	O
of	O
the	O
program	O
.	O
</s>
<s>
Invoking	O
longjmp	B-Language
from	O
a	O
nested	O
signal	B-Operating_System
handler	I-Operating_System
is	O
undefined	B-Language
.	O
</s>
<s>
The	O
value	O
specified	O
by	O
value	O
is	O
passed	O
from	O
longjmp	B-Language
to	O
setjmp	B-Language
.	O
</s>
<s>
After	O
longjmp	B-Language
is	O
completed	O
,	O
program	O
execution	O
continues	O
as	O
if	O
the	O
corresponding	O
invocation	O
of	O
setjmp	B-Language
had	O
just	O
returned	O
.	O
</s>
<s>
If	O
the	O
value	O
passed	O
to	O
longjmp	B-Language
is	O
0	O
,	O
setjmp	B-Language
will	O
behave	O
as	O
if	O
it	O
had	O
returned	O
1	O
;	O
otherwise	O
,	O
it	O
will	O
behave	O
as	O
if	O
it	O
had	O
returned	O
value	O
.	O
</s>
<s>
setjmp	B-Language
saves	O
the	O
current	O
environment	O
(	O
the	O
program	O
state	O
)	O
,	O
at	O
some	O
point	O
of	O
program	O
execution	O
,	O
into	O
a	O
platform-specific	O
data	O
structure	O
(	O
jmp_buf	O
)	O
that	O
can	O
be	O
used	O
at	O
some	O
later	O
point	O
of	O
program	O
execution	O
by	O
longjmp	B-Language
to	O
restore	O
the	O
program	O
state	O
to	O
that	O
saved	O
by	O
setjmp	B-Language
into	O
jmp_buf	O
.	O
</s>
<s>
This	O
process	O
can	O
be	O
imagined	O
to	O
be	O
a	O
"	O
jump	O
"	O
back	O
to	O
the	O
point	O
of	O
program	O
execution	O
where	O
setjmp	B-Language
saved	O
the	O
environment	O
.	O
</s>
<s>
The	O
(	O
apparent	O
)	O
return	B-Language
value	I-Language
from	O
setjmp	B-Language
indicates	O
whether	O
control	O
reached	O
that	O
point	O
normally	O
(	O
zero	O
)	O
or	O
from	O
a	O
call	O
to	O
longjmp	B-Language
(	O
nonzero	O
)	O
.	O
</s>
<s>
POSIX.1	O
does	O
not	O
specify	O
whether	O
setjmp	B-Language
and	O
longjmp	B-Language
save	O
and	O
restore	O
the	O
current	O
set	O
of	O
blocked	O
signals	B-Operating_System
;	O
if	O
a	O
program	O
employs	O
signal	B-Operating_System
handling	I-Operating_System
it	O
should	O
use	O
POSIX	O
's	O
sigsetjmp/siglongjmp	O
.	O
</s>
<s>
jmp_buf	O
An	O
array	O
type	O
,	O
such	O
as	O
struct	O
__jmp_buf_tag[1],This	O
is	O
the	O
type	O
used	O
by	O
the	O
GNU	B-Language
C	I-Language
Library	I-Language
,	O
version	O
2.7	O
suitable	O
for	O
holding	O
the	O
information	O
needed	O
to	O
restore	O
a	O
calling	O
environment	O
.	O
</s>
<s>
The	O
C99	B-Language
Rationale	O
describes	O
jmp_buf	O
as	O
being	O
an	O
array	O
type	O
for	O
backward	B-General_Concept
compatibility	I-General_Concept
;	O
existing	O
code	O
refers	O
to	O
jmp_buf	O
storage	O
locations	O
by	O
name	O
(	O
without	O
the	O
&	O
address-of	O
operator	O
)	O
,	O
which	O
is	O
only	O
possible	O
for	O
array	O
types	O
.	O
</s>
<s>
When	O
a	O
"	O
non-local	O
goto	O
"	O
is	O
executed	O
via	O
setjmp/longjmp	B-Language
in	O
C++	B-Language
,	O
normal	O
"	O
stack	O
unwinding	O
"	O
does	O
not	O
occur	O
.	O
</s>
<s>
This	O
could	O
include	O
closing	O
file	B-Application
descriptors	I-Application
,	O
flushing	O
buffers	B-General_Concept
,	O
or	O
freeing	O
heap-allocated	B-General_Concept
memory	I-General_Concept
.	O
</s>
<s>
If	O
the	O
function	O
in	O
which	O
setjmp	B-Language
was	O
called	O
returns	O
,	O
it	O
is	O
no	O
longer	O
possible	O
to	O
safely	O
use	O
longjmp	B-Language
with	O
the	O
corresponding	O
jmp_buf	O
object	O
.	O
</s>
<s>
Calling	O
longjmp	B-Language
restores	O
the	O
stack	O
pointer	O
,	O
which	O
—	O
because	O
the	O
function	O
returned	O
—	O
would	O
point	O
to	O
a	O
non-existent	O
and	O
potentially	O
overwritten	O
or	O
corrupted	O
stack	O
frame	O
.	O
</s>
<s>
Similarly	O
,	O
C99	B-Language
does	O
not	O
require	O
that	O
longjmp	B-Language
preserve	O
the	O
current	O
stack	O
frame	O
.	O
</s>
<s>
This	O
means	O
that	O
jumping	O
into	O
a	O
function	O
which	O
was	O
exited	O
via	O
a	O
call	O
to	O
longjmp	B-Language
is	O
undefined	B-Language
.	O
</s>
<s>
However	O
,	O
most	O
implementations	O
of	O
longjmp	B-Language
leave	O
the	O
stack	O
frame	O
intact	O
,	O
allowing	O
setjmp	B-Language
and	O
longjmp	B-Language
to	O
be	O
used	O
to	O
jump	O
back-and-forth	O
between	O
two	O
or	O
more	O
functions	O
—	O
a	O
feature	O
exploited	O
for	O
multitasking	O
.	O
</s>
<s>
The	O
example	O
below	O
shows	O
the	O
basic	O
idea	O
of	O
setjmp	B-Language
.	O
</s>
<s>
"	O
main	O
"	O
gets	O
printed	O
as	O
the	O
conditional	O
statement	O
if	O
(	O
!	O
setjmp(buf )	O
)	O
is	O
executed	O
a	O
second	O
time	O
.	O
</s>
<s>
In	O
this	O
example	O
,	O
setjmp	B-Language
is	O
used	O
to	O
bracket	O
exception	B-General_Concept
handling	I-General_Concept
,	O
like	O
try	O
in	O
some	O
other	O
languages	O
.	O
</s>
<s>
The	O
call	O
to	O
longjmp	B-Language
is	O
analogous	O
to	O
a	O
throw	O
statement	O
,	O
allowing	O
an	O
exception	O
to	O
return	O
an	O
error	O
status	O
directly	O
to	O
the	O
setjmp	B-Language
.	O
</s>
<s>
The	O
following	O
code	O
adheres	O
to	O
the	O
1999	B-Language
ISO	I-Language
C	I-Language
standard	O
and	O
Single	O
UNIX	O
Specification	O
by	O
invoking	O
setjmp	B-Language
in	O
a	O
limited	O
range	O
of	O
contexts	O
:	O
</s>
<s>
Following	O
these	O
rules	O
can	O
make	O
it	O
easier	O
for	O
the	O
implementation	O
to	O
create	O
the	O
environment	O
buffer	B-General_Concept
,	O
which	O
can	O
be	O
a	O
sensitive	O
operation	O
.	O
</s>
<s>
More	O
general	O
use	O
of	O
setjmp	B-Language
can	O
cause	O
undefined	B-Language
behaviour	I-Language
,	O
such	O
as	O
corruption	O
of	O
local	O
variables	O
;	O
conforming	O
compilers	O
and	O
environments	O
are	O
not	O
required	O
to	O
protect	O
or	O
even	O
warn	O
against	O
such	O
usage	O
.	O
</s>
<s>
However	O
,	O
slightly	O
more	O
sophisticated	O
idioms	O
such	O
as	O
switch	O
( ( 	O
exception_type	O
=	O
setjmp(env )	O
)	O
)	O
{	O
}	O
are	O
common	O
in	O
literature	O
and	O
practice	O
,	O
and	O
remain	O
relatively	O
portable	O
.	O
</s>
<s>
A	O
simple	O
conforming	O
methodology	O
is	O
presented	O
below	O
,	O
where	O
an	O
additional	O
variable	O
is	O
maintained	O
along	O
with	O
the	O
state	O
buffer	B-General_Concept
.	O
</s>
<s>
This	O
variable	O
could	O
be	O
elaborated	O
into	O
a	O
structure	O
incorporating	O
the	O
buffer	B-General_Concept
itself	O
.	O
</s>
<s>
In	O
a	O
more	O
modern-looking	O
example	O
,	O
the	O
usual	O
"	O
try	O
"	O
block	O
would	O
be	O
implemented	O
as	O
a	O
setjmp	B-Language
(	O
with	O
some	O
preparation	O
code	O
for	O
multilevel	O
jumps	O
,	O
as	O
seen	O
in	O
)	O
,	O
the	O
"	O
throw	O
"	O
as	O
longjmp	B-Language
with	O
the	O
optional	O
parameter	O
as	O
the	O
exception	O
,	O
and	O
the	O
"	O
catch	O
"	O
as	O
the	O
"	O
else	O
"	O
block	O
under	O
"	O
try	O
"	O
.	O
</s>
<s>
C99	B-Language
provides	O
that	O
longjmp	B-Language
is	O
guaranteed	O
to	O
work	O
only	O
when	O
the	O
destination	O
is	O
a	O
calling	O
function	O
,	O
i.e.	O
,	O
that	O
the	O
destination	O
scope	O
is	O
guaranteed	O
to	O
be	O
intact	O
.	O
</s>
<s>
Jumping	O
to	O
a	O
function	O
that	O
has	O
already	O
terminated	O
by	O
return	O
or	O
longjmp	B-Language
is	O
undefined	B-Language
.	O
</s>
<s>
However	O
,	O
most	O
implementations	O
of	O
longjmp	B-Language
do	O
not	O
specifically	O
destroy	O
local	O
variables	O
when	O
performing	O
the	O
jump	O
.	O
</s>
<s>
Since	O
the	O
context	O
survives	O
until	O
its	O
local	O
variables	O
are	O
erased	O
,	O
it	O
could	O
actually	O
be	O
restored	O
by	O
setjmp	B-Language
.	O
</s>
<s>
In	O
many	O
environments	O
(	O
such	O
as	O
and	O
)	O
,	O
idioms	O
such	O
as	O
if( 	O
!	O
setjmp(child_env )	O
)	O
longjmp(caller_env )	O
;	O
can	O
allow	O
a	O
called	O
function	O
to	O
effectively	O
pause-and-resume	O
at	O
a	O
setjmp	B-Language
.	O
</s>
<s>
This	O
is	O
exploited	O
by	O
thread	O
libraries	O
to	O
provide	O
cooperative	B-Operating_System
multitasking	I-Operating_System
facilities	O
without	O
using	O
setcontext	B-Operating_System
or	O
other	O
fiber	B-Operating_System
facilities	O
.	O
</s>
<s>
Considering	O
that	O
setjmp	B-Language
to	O
a	O
child	O
function	O
will	O
generally	O
work	O
unless	O
sabotaged	O
,	O
and	O
setcontext	B-Operating_System
,	O
as	O
part	O
of	O
POSIX	O
,	O
is	O
not	O
required	O
to	O
be	O
provided	O
by	O
C	O
implementations	O
,	O
this	O
mechanism	O
may	O
be	O
portable	O
where	O
the	O
setcontext	B-Operating_System
alternative	O
fails	O
.	O
</s>
<s>
Since	O
no	O
exception	O
will	O
be	O
generated	O
upon	O
overflow	O
of	O
one	O
of	O
the	O
multiple	O
stacks	O
in	O
such	O
a	O
mechanism	O
,	O
it	O
is	O
essential	O
to	O
overestimate	O
the	O
space	O
required	O
for	O
each	O
context	O
,	O
including	O
the	O
one	O
containing	O
main( )	O
and	O
including	O
space	O
for	O
any	O
signal	B-Operating_System
handlers	I-Operating_System
that	O
might	O
interrupt	O
regular	O
execution	O
.	O
</s>
