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
36 * \author Bernie Innocenti <bernie@codewiz.org>
38 #ifndef MWARE_EXCEPT_H
39 #define MWARE_EXCEPT_H
41 #include <cfg/debug.h>
45 #define EXCEPT_CONTEXTS 8
48 * A stack of jump buffers used to record try sites
49 * so they can be reached from throw sites.
51 * The stack contains return points for each nested
52 * context. jmp_buf's are pushed into the stack at
53 * try points and popped out when the try block ends
54 * normally or when an exception is thrown.
56 extern jmp_buf except_stack[EXCEPT_CONTEXTS];
57 extern int except_top;
59 #define PUSH_EXCEPT (ASSERT(except_top < EXCEPT_CONTEXTS), setjmp(except_stack[except_top++]))
60 #define POP_EXCEPT (ASSERT(except_top > 0), --except_top)
61 #define DO_EXCEPT (ASSERT(except_top > 0), longjmp(except_stack[--except_top], true))
64 * Jump buffer to use when throwing an exception or aborting an operation
66 * User code can throw exceptions like this:
69 * void a_function_throwing_exceptions(void)
71 * if (some_error_condition)
76 * Catching exceptions (brackets are optional):
81 * void a_function_catching_an_exception(void)
85 * printf("Entered try block\n");
86 * a_function_throwing_exceptions();
87 * printf("Survived execution of critical code\n");
91 * printf("Exception caught!\n");
97 * Simple syntax when you don't need to do anything when catching an excaption:
101 * printf("Entered try block\n");
102 * a_function_throwing_exceptions();
103 * printf("Survived execution of critical code\n");
107 * You also need to declare the exception stack once in
108 * your global declarations:
113 #define TRY if (PUSH_EXCEPT) { {
114 #define TRY_END } POP_EXCEPT; }
115 #define CATCH } POP_EXCEPT; } else {
117 #define THROW DO_EXCEPT
120 #define EXCEPT_DEFINE \
121 jmp_buf except_stack[EXCEPT_CONTEXTS]; \
124 #endif /* MWARE_EXCEPT_H */