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
  • env: buffer suitable for holding information needed for restoring calling environment
Returns
  • 0 if the return is from direct invocation
  • nonzero value if the return is from a call to Longjmp (this value will be set by the Longjmp routine)
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 env buffer by the most recent invocation of Setjmp. If there has been no such invocation, or the function containing the invocation of Setjmp has terminated in the interim, the behavior is undefined.

Parameters
  • env: buffer holding the information saved by the corresponding Setjmp invocation
  • val: value to be returned by the corresponding Setjmp function
Returns

Nothing.

Requires

Invocation of Longjmp must occur before return from the function in which Setjmp was called encounters.

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.

Copy Code To ClipboardCopy Code To Clipboard
#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;
}
Copyright (c) 2002-2018 mikroElektronika. All rights reserved.
What do you think about this topic ? Send us feedback!
Want more examples and libraries? 
Find them on LibStock - A place for the code