Setjmp Library
The Setjmp library contains functions and types definitions for bypassing the normal function call and return discipline.
jmp_buf is an array of unsigned int type suitable for holding information needed to restore a calling environment.
Type declaration is contained in the sejmp.h
header file which can be found in the include
folder of the compiler.
Library Routines
Setjmp
Prototype |
int Setjmp(jmp_buf env); |
---|---|
Description |
This function saves calling position for a later use by Longjmp. |
Parameters |
|
Returns |
|
Requires |
Nothing. |
Example |
jmp_buf buf; ... Setjmp(buf); |
Notes |
None. |
Longjmp
Prototype |
void Longjmp(jmp_buf env, int val); |
---|---|
Description |
Restores calling environment saved in |
Parameters |
|
Returns |
Nothing. |
Requires |
Invocation of |
Example |
jmp_buf buf; ... Longjmp(buf, 2); |
Notes |
None. |
Library Example
This example demonstrates function cross calling using the Setjmp
and Longjmp
functions.
When called, Setjmp
saves its calling environment in its jmp_buf argument for a later use by Longjmp
.
Longjmp
, on the other hand, restores the environment saved by the most recent invocation of Setjmp
with the corresponding jmp_buf argument.
#include <Setjmp.h> jmp_buf buf; // Note: Program flow diagrams are indexed according // to the sequence of execution void func33(){ // 2<------------| // | Delay_ms(1000); // | // | asm nop; // | Longjmp(buf, 2); // 3---------------->| asm nop; // | | // | | } // | | // | | void func(){ // 1<--------| | | // | | | portb = 3; // | | | if (Setjmp(buf) == 2) // 3<----------------| portb = 1; // 4-->| | | else // | | | func33(); // 2------------>| // | | // 4<--| | } // 5----->| | // | | void main() { // | | // | | PORTB = 0; // | | TRISB = 0; // | | // | | asm nop; // | | // | | func(); // 1-------->| // | asm nop; // 5<-----| Delay_ms(1000); PORTB = 0xFFFF; }
What do you think about this topic ? Send us feedback!