4 * This file is part of BeRTOS.
6 * Bertos is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * As a special exception, you may use this file as part of a free software
21 * library without restriction. Specifically, if other files instantiate
22 * templates or use macros or inline functions from this file, or you compile
23 * this file and link it with other files to produce an executable, this
24 * file does not by itself cause the resulting executable to be covered by
25 * the GNU General Public License. This exception does not however
26 * invalidate any other reasons why the executable file might be covered by
27 * the GNU General Public License.
29 * Copyright 2006 Develer S.r.l. (http://www.develer.com/)
30 * Copyright 1999 Bernie Innocenti <bernie@codewiz.org>
34 * \brief C++-like structured exception handling for C programs
37 * \author Bernie Innocenti <bernie@codewiz.org>
39 #ifndef MWARE_EXCEPT_H
40 #define MWARE_EXCEPT_H
42 #include <cfg/debug.h>
46 #define EXCEPT_CONTEXTS 8
49 * A stack of jump buffers used to record try sites
50 * so they can be reached from throw sites.
52 * The stack contains return points for each nested
53 * context. jmp_buf's are pushed into the stack at
54 * try points and popped out when the try block ends
55 * normally or when an exception is thrown.
57 extern jmp_buf except_stack[EXCEPT_CONTEXTS];
58 extern int except_top;
60 #define PUSH_EXCEPT (ASSERT(except_top < EXCEPT_CONTEXTS), setjmp(except_stack[except_top++]))
61 #define POP_EXCEPT (ASSERT(except_top > 0), --except_top)
62 #define DO_EXCEPT (ASSERT(except_top > 0), longjmp(except_stack[--except_top], true))
65 * Jump buffer to use when throwing an exception or aborting an operation
67 * User code can throw exceptions like this:
70 * void a_function_throwing_exceptions(void)
72 * if (some_error_condition)
77 * Catching exceptions (brackets are optional):
82 * void a_function_catching_an_exception(void)
86 * printf("Entered try block\n");
87 * a_function_throwing_exceptions();
88 * printf("Survived execution of critical code\n");
92 * printf("Exception caught!\n");
98 * Simple syntax when you don't need to do anything when catching an excaption:
102 * printf("Entered try block\n");
103 * a_function_throwing_exceptions();
104 * printf("Survived execution of critical code\n");
108 * You also need to declare the exception stack once in
109 * your global declarations:
114 #define TRY if (PUSH_EXCEPT) { {
115 #define TRY_END } POP_EXCEPT; }
116 #define CATCH } POP_EXCEPT; } else {
118 #define THROW DO_EXCEPT
121 #define EXCEPT_DEFINE \
122 jmp_buf except_stack[EXCEPT_CONTEXTS]; \
125 #endif /* MWARE_EXCEPT_H */