AVR-LibC  2.3.0git
Standard C library for AVR-GCC
 

AVR-LibC Documen­tation

AVR-LibC Development Pages

Main Page

User Manual

Library Refe­rence

FAQ

Example Projects

File List

Index

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