Main Page | Alphabetical List | Class List | File List | Class Members | File Members

setjmp.h

Go to the documentation of this file.
00001 /*--------------------------------------------------------
00002  * /
00003  * / apeNEXT setjmp functions for nlcc
00004  * /
00005  * / $Id: setjmp.h,v 1.14 2005/10/27 20:04:45 morinl Exp $
00006  * /
00007  * / ISO/IEC 9899:1999 (E) Standard
00008  * / ctype  <setjmp.h>
00009  * /
00010  * / IMPLEMENTATION for APEnext: possible?         (setjmp.h)
00011  * /---------------------------------------------------------------------------------------------*/
00012 
00013 #ifndef _SETJMP_H
00014 #define _SETJMP_H
00015 
00016 #include <nlibc.h>
00017 
00018 /*------------------------------------------------------------------------------------------------
00019  * / NAME   
00020  * /           setjmp.h - stack environment declarations 
00021  * / 
00022  * / SYNOPSIS
00023  * /           #include <setjmp.h>
00024  * / 
00025  * / DESCRIPTION
00026  * /           The <setjmp.h> header contains the type definitions for array 
00027  * /           types jmp_buf and sigjmp_buf. 
00028  * /
00029  * / RETURN VALUE
00030  * /           
00031  * / 
00032  * / Notes
00033  * /           
00034  * / 
00035  * / IMPLEMENTATION for APEnext:  possible         (setjmp.h) 
00036  * / --------------------------------------------------------------------------------------------*/
00037 
00038 
00039 /*---------------------------------------------------------------------------------------------
00040      This header file is useful for dealing with errors and interrupts
00041      encountered in a low-level subroutine of a program.
00042 
00043      All varieties of setjmp save their stack environment in env for later use by all 
00044      varieties of longjmp.  If the return is from a direct invocation, all
00045      setjmps return the value 0. If the return is from a call to any of the
00046      longjmps, all setjmp routines return a nonzero value.
00047 
00048      All longjmps restore the environment saved by the last call of setjmp
00049      with the corresponding env argument.  After the longjmp is completed,
00050      program execution continues as if the corresponding call of setjmp (which
00051      must not itself have returned in the interim) had just returned the value
00052      val.  longjmps cannot cause setjmps to return the value 0.  If a longjmp
00053      is invoked with a second argument of 0, all versions of setjmp will
00054      return 1.  At the time of the second return from a setjmp, external and
00055      static variables have values as of the time longjmp is called (see
00056      example).  The values of register and automatic variables are undefined.
00057      Register or automatic variables whose value must be relied upon must be
00058      declared as volatile.
00059 
00060      Example:
00061      #include <setjmp.h>
00062      jmp_buf env;
00063      int i = 0;
00064      main ()
00065      {    if (setjmp(env) != 0) {
00066           (void) printf("2nd return from setjmp: i = %d\n", i);
00067           exit(0);
00068          }
00069          (void) printf("1st return from setjmp: i = %d\n", i);
00070          i = 1;
00071          g(); // NOTREACHED
00072      }
00073      g()
00074      {         longjmp(env, 1);  // NOTREACHED
00075      }
00076 
00077      The program's output is:
00078           1st return from setjmp: i = 0
00079           2nd return from setjmp: i = 1
00080 
00081      Another example:
00082           #include<setjmp.h>
00083           #include<stdio.h>
00084           void some_function(jmp_buf);
00085           int main(void)
00086           {
00087           int value;
00088           jmp_buf environment_buffer;
00089 
00090           value=setjmp(environment_buffer);
00091           if(value!=0)
00092           {  printf("Reached this point from a longjmp with value=%d.\n",value);  exit(0);  }
00093           printf("Calling function.\n");
00094           some_function(environment_buffer);
00095           return 0;
00096           }
00097           void some_function(jmp_buf env_buf)
00098           { longjmp(env_buf,5); }
00099 
00100           The output from this program should be:
00101           Calling function.
00102           Reached this point from a longjmp with value=5.
00103 
00104 */
00105 
00106 /*------------------------------------------------------------------------------------------------
00107  * / NAME   
00108  * /           jmp_buf - typedef
00109  * / 
00110  * / SYNOPSIS
00111  * /           #include <setjmp.h>
00112  * /
00113  * /           typedef a-type jmp_buf 
00114  * / 
00115  * / DESCRIPTION
00116  * /           This type is the array type a-type of an object that you declare to hold the 
00117  * /           context information stored by setjmp (browse below) and accessed by longjmp.  
00118  * /
00119  * / RETURN VALUE
00120  * /           
00121  * / 
00122  * / Notes
00123  * /           
00124  * / 
00125  * / IMPLEMENTATION for APEnext:  possible         (jump_buf) 
00126  * / --------------------------------------------------------------------------------------------*/
00127 typedef int jmp_buf[2];
00128 
00129 /*------------------------------------------------------------------------------------------------
00130  * / NAME     
00131  * /           setjmp - set jump point for a non-local goto 
00132  * /  
00133  * / SYNOPSIS
00134  * /           #include <setjmp.h>
00135  * /
00136  * /           int setjmp(jmp_buf env)
00137  * / 
00138  * / DESCRIPTION
00139  * /            Saves the environment into the variable environment. 
00140  * / 
00141  * / RETURN VALUE
00142  * /           Non-zero when  the point in the sourcecode was reached by a longjmp
00143  * /           Zero indicating the environment  has been saved
00144  * / 
00145  * / Notes
00146  * /           
00147  * / 
00148  * / IMPLEMENTATION for APEnext:  possible         (setjmp) 
00149  * / --------------------------------------------------------------------------------------------*/
00150 #ifndef __HAS_MAIN
00151 extern int setjmp(jmp_buf env);
00152 #else
00153 #if defined(_uses_setjmp_setjmp_h) || !defined(__cflow_processed)
00154 int setjmp(jmp_buf env)
00155 {
00156   return -1;
00157 }
00158 #endif
00159 #endif // Has Main
00160 
00161 
00162 /*------------------------------------------------------------------------------------------------
00163  * / NAME   
00164  * /           longjmp - environment restored
00165  * / 
00166  * / SYNOPSIS
00167  * /           #include <setjmp.h>
00168  * /
00169  * /           void longjmp(jmp_buf env, int val) 
00170  * / 
00171  * / DESCRIPTION
00172  * /           longjmp() function causes the environment to be restored from a setjmp call 
00173  * /           where the environment variable had been saved. It causes execution to goto 
00174  * /           the setjmp location as if setjmp had returned the value of the variable value. 
00175  * /           The variable value cannot be zero. However, if zero is passed, then 1 is replaced. 
00176  * /           If the function where setjmp was called has terminated, then the results are undefined. 
00177  * / 
00178  * / RETURN VALUE
00179  * /           None
00180  * / 
00181  * / Notes
00182  * /           
00183  * / 
00184  * / IMPLEMENTATION for APEnext:  possible?         (longjmp) 
00185  * / --------------------------------------------------------------------------------------------*/
00186 #ifndef __HAS_MAIN
00187 extern void longjmp(jmp_buf env, int val);
00188 #else
00189 #if defined(_uses_longjmp_setjmp_h) || !defined(__cflow_processed)
00190 void longjmp(jmp_buf env, int val)
00191 {
00192 }
00193 #endif // longjmp
00194 #endif // Has Main
00195 
00196 #endif /* ifndef _SETJMP_H  */
00197 

Generated on Fri Jul 14 10:51:31 2006 for nlibc by doxygen 1.3.5