AVR Libc Home Page AVRs AVR Libc Development Pages
Main Page User Manual Library Reference FAQ Alphabetical Index Example Projects

setjmp.h
Go to the documentation of this file.
1 /* Copyright (c) 2002,2007 Marek Michalkiewicz
2  All rights reserved.
3 
4  Redistribution and use in source and binary forms, with or without
5  modification, are permitted provided that the following conditions are met:
6 
7  * Redistributions of source code must retain the above copyright
8  notice, this list of conditions and the following disclaimer.
9 
10  * Redistributions in binary form must reproduce the above copyright
11  notice, this list of conditions and the following disclaimer in
12  the documentation and/or other materials provided with the
13  distribution.
14 
15  * Neither the name of the copyright holders nor the names of
16  contributors may be used to endorse or promote products derived
17  from this software without specific prior written permission.
18 
19  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  POSSIBILITY OF SUCH DAMAGE. */
30 
31 /* $Id: setjmp.h 1656 2008-03-24 11:29:55Z dmix $ */
32 
33 #ifndef __SETJMP_H_
34 #define __SETJMP_H_ 1
35 
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39 
40 /*
41  jmp_buf:
42  offset size description
43  0 16 call-saved registers (r2-r17)
44  16 2 frame pointer (r29:r28)
45  18 2 stack pointer (SPH:SPL)
46  20 1 status register (SREG)
47  21 2/3 return address (PC) (2 bytes used for <=128Kw flash)
48  23/24 = total size
49  */
50 
51 #if !defined(__DOXYGEN__)
52 
53 #if defined(__AVR_3_BYTE_PC__) && __AVR_3_BYTE_PC__
54 # define _JBLEN 24
55 #else
56 # define _JBLEN 23
57 #endif
58 typedef struct _jmp_buf { unsigned char _jb[_JBLEN]; } jmp_buf[1];
59 
60 #endif /* not __DOXYGEN__ */
61 
62 /** \file */
63 /** \defgroup setjmp <setjmp.h>: Non-local goto
64 
65  While the C language has the dreaded \c goto statement, it can only be
66  used to jump to a label in the same (local) function. In order to jump
67  directly to another (non-local) function, the C library provides the
68  setjmp() and longjmp() functions. setjmp() and longjmp() are useful for
69  dealing with errors and interrupts encountered in a low-level subroutine
70  of a program.
71 
72  \note setjmp() and longjmp() make programs hard to understand and maintain.
73  If possible, an alternative should be used.
74 
75  \note longjmp() can destroy changes made to global register
76  variables (see \ref faq_regbind).
77 
78  For a very detailed discussion of setjmp()/longjmp(), see Chapter 7 of
79  <em>Advanced Programming in the UNIX Environment</em>, by W. Richard
80  Stevens.
81 
82  Example:
83 
84  \code
85  #include <setjmp.h>
86 
87  jmp_buf env;
88 
89  int main (void)
90  {
91  if (setjmp (env))
92  {
93  ... handle error ...
94  }
95 
96  while (1)
97  {
98  ... main processing loop which calls foo() some where ...
99  }
100  }
101 
102  ...
103 
104  void foo (void)
105  {
106  ... blah, blah, blah ...
107 
108  if (err)
109  {
110  longjmp (env, 1);
111  }
112  }
113  \endcode */
114 
115 #ifndef __ATTR_NORETURN__
116 #define __ATTR_NORETURN__ __attribute__((__noreturn__))
117 #endif
118 
119 /** \ingroup setjmp
120  \brief Save stack context for non-local goto.
121 
122  \code #include <setjmp.h>\endcode
123 
124  setjmp() saves the stack context/environment in \e __jmpb for later use by
125  longjmp(). The stack context will be invalidated if the function which
126  called setjmp() returns.
127 
128  \param __jmpb Variable of type \c jmp_buf which holds the stack
129  information such that the environment can be restored.
130 
131  \returns setjmp() returns 0 if returning directly, and
132  non-zero when returning from longjmp() using the saved context. */
133 
134 extern int setjmp(jmp_buf __jmpb);
135 
136 /** \ingroup setjmp
137  \brief Non-local jump to a saved stack context.
138 
139  \code #include <setjmp.h>\endcode
140 
141  longjmp() restores the environment saved by the last call of setjmp() with
142  the corresponding \e __jmpb argument. After longjmp() is completed,
143  program execution continues as if the corresponding call of setjmp() had
144  just returned the value \e __ret.
145 
146  \note longjmp() cannot cause 0 to be returned. If longjmp() is invoked
147  with a second argument of 0, 1 will be returned instead.
148 
149  \param __jmpb Information saved by a previous call to setjmp().
150  \param __ret Value to return to the caller of setjmp().
151 
152  \returns This function never returns. */
153 
154 extern void longjmp(jmp_buf __jmpb, int __ret) __ATTR_NORETURN__;
155 
156 #ifdef __cplusplus
157 }
158 #endif
159 
160 #endif /* !__SETJMP_H_ */
int setjmp(jmp_buf __jmpb)
Save stack context for non-local goto.
void longjmp(jmp_buf __jmpb, int __ret) __ATTR_NORETURN__
Non-local jump to a saved stack context.

Automatically generated by Doxygen 1.8.7 on Tue Aug 12 2014.