Debian Codegraph @

vendor/c_exception/README.md

raw
1 CException - Lightweight exception library for C
2 ================================================
3
4 [![CException Build Status](https://api.travis-ci.org/ThrowTheSwitch/CException.png?branch=master)](https://travis-ci.org/ThrowTheSwitch/CException)
5
6 _This Documentation Is Released Under a Creative Commons 3.0 Attribution Share-Alike License_
7
8 CException is simple exception handling in C. It is significantly faster than full-blown C++ exception handling
9 but loses some flexibility. It is portable to any platform supporting `setjmp`/`longjmp`.
10
11 Getting Started
12 ================
13
14 The simplest way to get started is to just grab the code and pull it into your project:
15
16 ```
17 git clone https://github.com/throwtheswitch/cexception.git
18 ```
19
20 If you want to contribute to this project, you'll also need to have Ruby and Ceedling installed to run the unit tests.
21
22 Usage
23 =====
24
25 ### So what's it good for?
26
27 Mostly error handling. Passing errors down a long chain of function calls gets ugly. Sometimes really ugly.
28 So what if you could just specify certain places where you want to handle errors, and all your errors were
29 transferred there? Let's try a lame example:
30
31 CException uses C standard library functions setjmp and longjmp to operate. As long as the target system
32 has these two functions defined, this library should be useable with very little configuration. It even
33 supports environments where multiple program flows are in use, such as real-time operating systems...
34 we started this project for use in embedded systems... but it obviously can be used for larger systems too.
35
36 ### Error Handling with CException:
37
38 ```
39 void functionC(void) {
40 //do some stuff
41 if (there_was_a_problem)
42 Throw(ERR_BAD_BREATH);
43 //this stuff never gets called because of error
44 }
45 ```
46
47 There are about a gajillion exception frameworks using a similar setjmp/longjmp method out there... and there
48 will probably be more in the future. Unfortunately, when we started our last embedded project, all those that
49 existed either (a) did not support multiple tasks (therefore multiple stacks) or (b) were way more complex
50 than we really wanted. CException was born.
51
52 Why?
53 ====
54
55 ### It's ANSI C
56
57 ...and it beats passing error codes around.
58
59 ### You want something simple...
60
61 CException throws a single id. You can define those ID's to be whatever you like.
62 You might even choose which type that number is for your project. But that's as far as it goes. We weren't interested
63 in passing objects or structs or strings... just simple error codes. Fast. Easy to Use. Easy to Understand.
64
65 ### Performance...
66
67 CException can be configured for single tasking or multitasking. In single tasking, there is
68 very little overhead past the setjmp/longjmp calls (which are already fast). In multitasking, your only additional
69 overhead is the time it takes you to determine a unique task id (0 to num_tasks).
70
71 How?
72 ====
73
74 Code that is to be protected are wrapped in `Try { }` blocks. The code inside the Try block is _protected_,
75 meaning that if any Throws occur, program control is directly transferred to the start of the Catch block.
76 The Catch block immediately follows the Try block. It's ignored if no errors have occurred.
77
78 A numerical exception ID is included with Throw, and is passed into the Catch block. This allows you to handle
79 errors differently or to report which error has occurred... or maybe it just makes debugging easier so you
80 know where the problem was Thrown.
81
82 Throws can occur from anywhere inside the Try block, directly in the function you're testing or even within
83 function calls (nested as deeply as you like). There can be as many Throws as you like, just remember that
84 execution of the guts of your Try block ends as soon as the first Throw is triggered. Once you throw, you're
85 transferred to the Catch block. A silly example:
86
87 ```
88 void SillyExampleWhichPrintsZeroThroughFive(void) {
89 CEXCEPTION_T e;
90 int i;
91 while (i = 0; i < 6; i++) {
92 Try {
93 Throw(i);
94 //This spot is never reached
95 }
96 Catch(e) {
97 printf(“%i “, e);
98 }
99 }
100 }
101 ```
102
103 Limitations
104 ===========
105
106 This library was made to be as fast as possible, and provide basic exception handling. It is not a full-blown
107 exception library like C++. Because of this, there are a few limitations that should be observed in order to
108 successfully utilize this library:
109
110 ### Return & Goto
111
112 Do not directly `return` from within a `Try` block, nor `goto` into or out of a `Try` block.
113 The `Try` macro allocates some local memory and alters a global pointer. These are cleaned up at the
114 top of the `Catch` macro. Gotos and returns would bypass some of these steps, resulting in memory leaks
115 or unpredictable behavior.
116
117 ### Local Variables
118
119 If (a) you change local (stack) variables within your `Try` block, and (b) wish to make use of the updated
120 values after an exception is thrown, those variables should be made `volatile`.
121
122 Note that this is ONLY for locals and ONLY when you need access to them after a `Throw`.
123
124 Compilers optimize (and thank goodness they do). There is no way to guarantee that the actual memory
125 location was updated and not just a register unless the variable is marked volatile.
126
127 ### Memory Management
128
129 Memory which is `malloc`'d within a `Try` block is not automatically released when an error is thrown. This
130 will sometimes be desirable, and othertimes may not. It will be the responsibility of the code you put in
131 the `Catch` block to perform this kind of cleanup.
132
133 There's just no easy way to track `malloc`'d memory, etc., without replacing or wrapping `malloc`
134 calls or something like that. This is a lightweight framework, so these options were not desirable.
135
136 CException API
137 ==============
138
139 ### `Try { ... }`
140
141 `Try` is a macro which starts a protected block. It MUST be followed by a pair of braces or a single
142 protected line (similar to an 'if'), enclosing the data that is to be protected. It MUST be followed by
143 a `Catch` block (don't worry, you'll get compiler errors to let you know if you mess any of that up).
144
145 The `Try` block is your protected block. It contains your main program flow, where you can ignore errors
146 (other than a quick `Throw` call). You may nest multiple `Try` blocks if you want to handle errors at
147 multiple levels, and you can even rethrow an error from within a nested `Catch`.
148
149 ### `Catch(e) { }`
150
151 `Catch` is a macro which ends the `Try` block and starts the error handling block. The `Catch` block
152 is executed if and only if an exception was thrown while within the `Try` block. This error was thrown
153 by a `Throw` call somewhere within `Try` (or within a function called within `Try`, or a function called
154 by a function called within `Try`... you get the idea.).
155
156 `Catch` receives a single id of type `CEXCEPTION_T` which you can ignore or use to handle the error in
157 some way. You may throw errors from within Catches, but they will be caught by a `Try` wrapping the `Catch`,
158 not the one immediately preceeding.
159
160 ### `Throw(e)`
161
162 `Throw` is the method used to throw an error. `Throw`s should only occur from within a protected
163 (`Try`...`Catch`) block, though it may easily be nested many function calls deep without an impact
164 on performance or functionality. `Throw` takes a single argument, which is an exception id which will be
165 passed to `Catch` as the reason for the error. If you wish to _re-throw_ an error, this can be done by
166 calling `Throw(e)` with the error code you just caught. It _IS_ valid to throw from a `Catch` block.
167
168 ### `ExitTry()`
169
170 `ExitTry` is a method used to immediately exit your current Try block but NOT treat this as an error. Don't
171 run the Catch. Just start executing from after the Catch as if nothing had happened.
172
173 Configuration
174 =============
175
176 CException is a mostly portable library. It has one universal dependency, plus some macros which are required if
177 working in a multi-tasking environment.
178
179 The standard C library setjmp must be available. Since this is part of the standard library, it's all good.
180
181 If working in a multitasking environment, you need a stack frame for each task. Therefore, you must define
182 methods for obtaining an index into an array of frames and to get the overall number of id's are required. If
183 the OS supports a method to retrieve task ID's, and those tasks are number 0, 1, 2... you are in an ideal
184 situation. Otherwise, a more creative mapping function may be required. Note that this function is likely to
185 be called twice for each protected block and once during a throw. This is the only added overhead in the system.
186
187 You have options for configuring the library, if the defaults aren't good enough for you. You can add defines
188 at the command prompt directly. You can always include a configuration file before including `CException.h`.
189 You can make sure `CEXCEPTION_USE_CONFIG_FILE` is defined, which will force make CException look for
190 `CExceptionConfig.h`, where you can define whatever you like. However you do it, you can override any or
191 all of the following:
192
193 ### `CEXCEPTION_T`
194
195 Set this to the type you want your exception id's to be. Defaults to an 'unsigned int'.
196
197 ### `CEXCEPTION_NONE`
198
199 Set this to a number which will never be an exception id in your system. Defaults to `0x5a5a5a5a`.
200
201 ### `CEXCEPTION_GET_ID`
202
203 If in a multi-tasking environment, this should be set to be a call to the function described in #2 above.
204 It defaults to just return 0 all the time (good for single tasking environments, not so good otherwise).
205
206 ### `CEXCEPTION_NUM_ID`
207
208 If in a multi-tasking environment, this should be set to the number of ID's required (usually the number
209 of tasks in the system). Defaults to 1 (good for single tasking environments or systems where you will only
210 use this from one task).
211
212 ### `CEXCEPTION_NO_CATCH_HANDLER (id)`
213
214 This macro can be optionally specified. It allows you to specify code to be called when a Throw is made
215 outside of Try...Catch protection. Consider this the emergency fallback plan for when something has gone
216 terribly wrong.
217
218 ### And More!
219
220 You may also want to include any header files which will commonly be needed by the rest of your application
221 where it uses exception handling here. For example, OS header files or exception codes would be useful.
222
223 Finally, there are some hook macros which you can implement to inject your own target-specific code in
224 particular places. It is a rare instance where you will need these, but they are here if you need them:
225
226 * `CEXCEPTION_HOOK_START_TRY` - called immediately before the Try block
227 * `CEXCEPTION_HOOK_HAPPY_TRY` - called immediately after the Try block if no exception was thrown
228 * `CEXCEPTION_HOOK_AFTER_TRY` - called immediately after the Try block OR before an exception is caught
229 * `CEXCEPTION_HOOK_START_CATCH` - called immediately before the catch
230
231 Testing
232 =======
233
234 If you want to validate that CException works with your tools or that it works with your custom
235 configuration, you may want to run the included test suite. This is the test suite (along with real
236 projects we've used it on) that we use to make sure that things actually work the way we claim.
237 The test suite makes use of Ceedling, which uses the Unity Test Framework. It will require a native C compiler.
238 The example makefile and rakefile both use gcc.
239
240 License
241 =======
242
243 *This software is licensed under the MIT License:
244 Copyright (c) 2007-2019 Mark VanderVoord*
245
246 *Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
247 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.*
248
249 *_THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE._*