AVR-LibC  2.2.0
Standard C library for AVR-GCC
 

AVR-LibC Documentation

Logo

AVR-LibC Development Pages

Main Page

User Manual

Library Reference

FAQ

Example Projects

File List

Loading...
Searching...
No Matches
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$ */
32
33#ifndef __SETJMP_H_
34#define __SETJMP_H_ 1
35
36#ifdef __cplusplus
37extern "C" {
38#endif
39
40/*
41 jmp_buf:
42 offset size description
43 0 16/2 call-saved registers (r2-r17)
44 (AVR_TINY arch has only 2 call saved registers (r18,r19))
45 16/2 2 frame pointer (r29:r28)
46 18/4 2 stack pointer (SPH:SPL)
47 20/6 1 status register (SREG)
48 21/7 2/3 return address (PC) (2 bytes used for <=128Kw flash)
49 23/24/9 = total size (AVR_TINY arch always has 2 bytes PC)
50 */
51
52#if !defined(__DOXYGEN__)
53
54#if defined(__AVR_TINY__)
55# define _JBLEN 9
56#elif defined(__AVR_3_BYTE_PC__) && __AVR_3_BYTE_PC__
57# define _JBLEN 24
58#else
59# define _JBLEN 23
60#endif
61typedef struct _jmp_buf { unsigned char _jb[_JBLEN]; } jmp_buf[1];
62
63#endif /* not __DOXYGEN__ */
64
65/** \file */
66/** \defgroup setjmp <setjmp.h>: Non-local goto
67
68 While the C language has the dreaded \c goto statement, it can only be
69 used to jump to a label in the same (local) function. In order to jump
70 directly to another (non-local) function, the C library provides the
71 #setjmp and #longjmp functions. setjmp and longjmp are useful for
72 dealing with errors and interrupts encountered in a low-level subroutine
73 of a program.
74
75 \note #setjmp and #longjmp make programs hard to understand and maintain.
76 If possible, an alternative should be used.
77
78 \note longjmp can destroy changes made to global register
79 variables (see \ref faq_regbind).
80
81 For a very detailed discussion of setjmp/longjmp, see Chapter 7 of
82 <em>Advanced Programming in the UNIX Environment</em>, by W. Richard
83 Stevens.
84
85 Example:
86
87 \code
88 #include <setjmp.h>
89
90 jmp_buf env;
91
92 int main (void)
93 {
94 if (setjmp (env))
95 {
96 // Handle error ...
97 }
98
99 while (1)
100 {
101 // Main processing loop which calls foo() somewhere ...
102 }
103 }
104
105 void foo (void)
106 {
107 // blah, blah, blah ...
108
109 if (err)
110 {
111 longjmp (env, 1);
112 }
113 }
114 \endcode */
115
116#ifndef __DOXYGEN__
117#ifndef __ATTR_NORETURN__
118#define __ATTR_NORETURN__ __attribute__((__noreturn__))
119#endif
120#endif /* ! DOXYGEN */
121
122/** \ingroup setjmp
123 \brief Save stack context for non-local goto.
124
125 \code #include <setjmp.h>\endcode
126
127 setjmp() saves the stack context/environment in \e __jmpb for later use by
128 longjmp(). The stack context will be invalidated if the function which
129 called setjmp() returns.
130
131 \param __jmpb Variable of type \c jmp_buf which holds the stack
132 information such that the environment can be restored.
133
134 \returns setjmp() returns 0 if returning directly, and
135 non-zero when returning from longjmp() using the saved context. */
136
137extern int setjmp(jmp_buf __jmpb);
138
139/** \ingroup setjmp
140 \brief Non-local jump to a saved stack context.
141
142 \code #include <setjmp.h>\endcode
143
144 longjmp() restores the environment saved by the last call of setjmp() with
145 the corresponding \e __jmpb argument. After longjmp() is completed,
146 program execution continues as if the corresponding call of setjmp() had
147 just returned the value \e __ret.
148
149 \note longjmp() cannot cause 0 to be returned. If longjmp() is invoked
150 with a second argument of 0, 1 will be returned instead.
151
152 \param __jmpb Information saved by a previous call to setjmp().
153 \param __ret Value to return to the caller of setjmp().
154
155 \returns This function never returns. */
156
157extern void longjmp(jmp_buf __jmpb, int __ret) __ATTR_NORETURN__;
158
159#ifdef __cplusplus
160}
161#endif
162
163#endif /* !__SETJMP_H_ */
int setjmp(jmp_buf __jmpb)
Save stack context for non-local goto.
void longjmp(jmp_buf __jmpb, int __ret)
Non-local jump to a saved stack context.