$treeview $search $mathjax $extrastylesheet
avr-libc  2.0.0
$projectbrief
$projectbrief
$searchbox

AVR Libc Home Page

AVRs

AVR Libc Development Pages

Main Page

User Manual

Library Reference

FAQ

Example Projects

setjmp.h

Go to the documentation of this file.
00001 /* Copyright (c) 2002,2007 Marek Michalkiewicz
00002    All rights reserved.
00003 
00004    Redistribution and use in source and binary forms, with or without
00005    modification, are permitted provided that the following conditions are met:
00006 
00007    * Redistributions of source code must retain the above copyright
00008      notice, this list of conditions and the following disclaimer.
00009 
00010    * Redistributions in binary form must reproduce the above copyright
00011      notice, this list of conditions and the following disclaimer in
00012      the documentation and/or other materials provided with the
00013      distribution.
00014 
00015    * Neither the name of the copyright holders nor the names of
00016      contributors may be used to endorse or promote products derived
00017      from this software without specific prior written permission.
00018 
00019   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00020   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00021   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00022   ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00023   LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00024   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00025   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00026   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00027   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00028   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00029   POSSIBILITY OF SUCH DAMAGE. */
00030 
00031 /* $Id$ */
00032 
00033 #ifndef __SETJMP_H_
00034 #define __SETJMP_H_ 1
00035 
00036 #ifdef __cplusplus
00037 extern "C" {
00038 #endif
00039 
00040 /*
00041    jmp_buf:
00042     offset  size    description
00043      0  16/2    call-saved registers (r2-r17)
00044                 (AVR_TINY arch has only 2 call saved registers (r18,r19))
00045     16/2     2  frame pointer (r29:r28)
00046     18/4     2  stack pointer (SPH:SPL)
00047     20/6     1  status register (SREG)
00048     21/7     2/3    return address (PC) (2 bytes used for <=128Kw flash)
00049     23/24/9 = total size (AVR_TINY arch always has 2 bytes PC)
00050  */
00051 
00052 #if !defined(__DOXYGEN__)
00053 
00054 #if defined(__AVR_TINY__)
00055 # define _JBLEN  9
00056 #elif   defined(__AVR_3_BYTE_PC__) && __AVR_3_BYTE_PC__
00057 # define _JBLEN  24
00058 #else
00059 # define _JBLEN  23
00060 #endif
00061 typedef struct _jmp_buf { unsigned char _jb[_JBLEN]; } jmp_buf[1];
00062 
00063 #endif /* not __DOXYGEN__ */
00064 
00065 /** \file */
00066 /** \defgroup setjmp <setjmp.h>: Non-local goto
00067 
00068     While the C language has the dreaded \c goto statement, it can only be
00069     used to jump to a label in the same (local) function.  In order to jump
00070     directly to another (non-local) function, the C library provides the
00071     setjmp() and longjmp() functions.  setjmp() and longjmp() are useful for
00072     dealing with errors and interrupts encountered in a low-level subroutine
00073     of a program.
00074 
00075     \note setjmp() and longjmp() make programs hard to understand and maintain.
00076     If possible, an alternative should be used.
00077 
00078     \note longjmp() can destroy changes made to global register
00079     variables (see \ref faq_regbind).
00080 
00081     For a very detailed discussion of setjmp()/longjmp(), see Chapter 7 of
00082     <em>Advanced Programming in the UNIX Environment</em>, by W. Richard
00083     Stevens.
00084 
00085     Example:
00086 
00087     \code
00088     #include <setjmp.h>
00089 
00090     jmp_buf env;
00091 
00092     int main (void)
00093     {
00094         if (setjmp (env))
00095         {
00096             ... handle error ...
00097         }
00098 
00099         while (1)
00100         {
00101            ... main processing loop which calls foo() some where ...
00102         }
00103     }
00104 
00105     ...
00106 
00107     void foo (void)
00108     {
00109         ... blah, blah, blah ...
00110 
00111         if (err)
00112         {
00113             longjmp (env, 1);
00114         }
00115     }
00116     \endcode */
00117 
00118 #if !(defined(__ATTR_NORETURN__) || defined(__DOXYGEN__))
00119 #define __ATTR_NORETURN__ __attribute__((__noreturn__))
00120 #endif
00121 
00122 /** \ingroup setjmp
00123     \brief Save stack context for non-local goto.
00124 
00125     \code #include <setjmp.h>\endcode
00126 
00127     setjmp() saves the stack context/environment in \e __jmpb for later use by
00128     longjmp().  The stack context will be invalidated if the function which
00129     called setjmp() returns.
00130 
00131     \param __jmpb Variable of type \c jmp_buf which holds the stack
00132     information such that the environment can be restored.
00133 
00134     \returns setjmp() returns 0 if returning directly, and
00135     non-zero when returning from longjmp() using the saved context. */
00136 
00137 extern int setjmp(jmp_buf __jmpb);
00138 
00139 /** \ingroup setjmp
00140     \brief Non-local jump to a saved stack context.
00141 
00142     \code #include <setjmp.h>\endcode
00143 
00144     longjmp() restores the environment saved by the last call of setjmp() with
00145     the corresponding \e __jmpb argument.  After longjmp() is completed,
00146     program execution continues as if the corresponding call of setjmp() had
00147     just returned the value \e __ret.
00148 
00149     \note longjmp() cannot cause 0 to be returned.  If longjmp() is invoked
00150     with a second argument of 0, 1 will be returned instead.
00151 
00152     \param __jmpb Information saved by a previous call to setjmp().
00153     \param __ret  Value to return to the caller of setjmp().
00154 
00155     \returns This function never returns. */
00156 
00157 extern void longjmp(jmp_buf __jmpb, int __ret) __ATTR_NORETURN__;
00158 
00159 #ifdef __cplusplus
00160 }
00161 #endif
00162 
00163 #endif  /* !__SETJMP_H_ */
 All Data Structures Files Functions Variables Typedefs Enumerations Defines