Wireshark 4.7.0
The Wireshark network protocol analyzer
Loading...
Searching...
No Matches
except.h
Go to the documentation of this file.
1/*
2 * Portable Exception Handling for ANSI C.
3 * Copyright (C) 1999 Kaz Kylheku <[email protected]>
4 *
5 * Free Software License:
6 *
7 * All rights are reserved by the author, with the following exceptions:
8 * Permission is granted to freely reproduce and distribute this software,
9 * possibly in exchange for a fee, provided that this copyright notice appears
10 * intact. Permission is also granted to adapt this software to produce
11 * derivative works, as long as the modified versions carry this copyright
12 * notice and additional notices stating that the work has been modified.
13 * This source code may be translated into executable form and incorporated
14 * into proprietary software; there is no requirement for such software to
15 * contain a copyright notice related to this source.
16 *
17 */
18
29#pragma once
30#include <glib.h>
31#include <setjmp.h>
32#include <stdlib.h>
33#include <stdarg.h>
34#include <assert.h>
35#include "ws_symbol_export.h"
36#include "ws_attributes.h"
37
38#define XCEPT_GROUP_ANY 0
39#define XCEPT_CODE_ANY 0
40#define XCEPT_BAD_ALLOC 1
41
42#ifdef __cplusplus
43extern "C" {
44#endif
45
46enum { except_no_call, except_call };
47
48typedef struct {
49 unsigned long except_group;
50 unsigned long except_code;
52
53typedef struct {
54 except_id_t volatile except_id;
55 const char *volatile except_message;
56 void *volatile except_dyndata;
57} except_t;
58
60 void (*except_func)(void *);
61 void *except_context;
62};
63
65 const except_id_t *except_id;
66 size_t except_size;
67 except_t except_obj;
68 jmp_buf except_jmp;
69};
70
71enum except_stacktype {
72 XCEPT_CLEANUP, XCEPT_CATCHER
73};
74
84 enum except_stacktype except_type;
85 union {
88 } except_info;
89};
90
91/* private functions made external so they can be used in macros */
92
96WS_DLL_PUBLIC void except_setup_clean(struct except_stacknode *,
97 struct except_cleanup *, void (*)(void *), void *);
98
102WS_DLL_PUBLIC void except_setup_try(struct except_stacknode *,
103 struct except_catch *, const except_id_t [], size_t);
104
109WS_DLL_PUBLIC struct except_stacknode *except_pop(void);
110
111/* public interface functions */
117WS_DLL_PUBLIC int except_init(void);
118
122WS_DLL_PUBLIC void except_deinit(void);
123
129WS_DLL_PUBLIC WS_NORETURN void except_rethrow(except_t * except);
130
137WS_DLL_PUBLIC WS_NORETURN void except_throw(long group, long code, const char *msg);
138
139WS_DLL_PUBLIC WS_NORETURN void except_throwd(long, long, const char *, void *);
140WS_DLL_PUBLIC WS_NORETURN void except_vthrowf(long group, long code, const char *fmt, va_list vl);
141WS_DLL_PUBLIC WS_NORETURN void except_throwf(long, long, const char *, ...)
142 G_GNUC_PRINTF(3, 4);
143WS_DLL_PUBLIC void (*except_unhandled_catcher(void (*)(except_t *)))(except_t *);
144extern unsigned long except_code(except_t *);
145extern unsigned long except_group(except_t *);
146extern const char *except_message(except_t *);
147extern void *except_data(except_t *);
148
155WS_DLL_PUBLIC void *except_take_data(except_t * ex);
156
157WS_DLL_PUBLIC void except_set_allocator(void *(*)(size_t), void (*)(void *));
158WS_DLL_PUBLIC void *except_alloc(size_t);
159
165WS_DLL_PUBLIC void except_free(void * ptr);
166
167/* Functions to be used in a last resort when things go badly wrong; e.g.,
168 * Lua uses setjmp and longjmp for its own error handling, and Lua errors
169 * can longjmp past the ENDTRY so that the function that created the current
170 * top node has exited and the node (and the jmp_buf) is no longer valid
171 * (having been created on the stack) so we can't run the handlers or even
172 * traverse the exception stack. It's better to do this than crash. */
173
179const struct except_stacknode *except_get_top(void);
180
186void except_set_top(struct except_stacknode *node);
187
188#define except_code(E) ((E)->except_id.except_code)
189#define except_group(E) ((E)->except_id.except_group)
190#define except_message(E) ((E)->except_message)
191#define except_data(E) ((E)->except_dyndata)
192
193#ifdef __cplusplus
194}
195#endif
196
197/*
198 * void except_cleanup_push(void (*)(void *), void *);
199 * void except_cleanup_pop(int);
200 * void except_checked_cleanup_pop(void (*)(void *), int);
201 * void except_try_push(const except_id_t [], size_t, except_t **);
202 * void except_try_pop(void);
203 */
204
205#define except_cleanup_push(F, C) \
206 { \
207 struct except_stacknode except_sn; \
208 struct except_cleanup except_cl; \
209 except_setup_clean(&except_sn, &except_cl, F, C)
210
211#define except_cleanup_pop(E) \
212 except_pop(); \
213 if (E) \
214 except_cl.except_func(except_cl.except_context); \
215 }
216
217#define except_checked_cleanup_pop(F, E) \
218 except_pop(); \
219 assert (except_cl.except_func == (F)); \
220 if (E) \
221 except_cl.except_func(except_cl.except_context); \
222 }
223
224
225/* --- Variants to allow nesting of except_cleanup_push w/o "shadowing" variables */
226#define except_cleanup_push_pfx(pfx, F, C) \
227 { \
228 struct except_stacknode pfx##_except_sn; \
229 struct except_cleanup pfx##_except_cl; \
230 except_setup_clean(&pfx##_except_sn, &pfx##_except_cl, F, C)
231
232#define except_cleanup_pop_pfx(pfx, E) \
233 except_pop(); \
234 if (E) \
235 pfx##_except_cl.except_func(pfx##_except_cl.except_context);\
236 }
237
238#define except_checked_cleanup_pop_pfx(pfx, F, E) \
239 except_pop(); \
240 assert (pfx##_except_cl.except_func == (F)); \
241 if (E) \
242 pfx##_except_cl.except_func(pfx##_except_cl.except_context);\
243 }
244/* ---------- */
245
246
247#define except_try_push(ID, NUM, PPE) \
248 { \
249 struct except_stacknode except_sn; \
250 struct except_catch except_ch; \
251 except_setup_try(&except_sn, &except_ch, ID, NUM); \
252 if (setjmp(except_ch.except_jmp)) \
253 *(PPE) = &except_ch.except_obj; \
254 else \
255 *(PPE) = 0
256
257#define except_try_pop() \
258 except_free(except_ch.except_obj.except_dyndata); \
259 except_pop(); \
260 }
261
262/*
263 * Editor modelines - https://www.wireshark.org/tools/modelines.html
264 *
265 * Local variables:
266 * c-basic-offset: 4
267 * tab-width: 8
268 * indent-tabs-mode: nil
269 * End:
270 *
271 * vi: set shiftwidth=4 tabstop=8 expandtab:
272 * :indentSize=4:tabSize=8:noTabs=true:
273 */
WS_DLL_PUBLIC struct except_stacknode * except_pop(void)
Pop the top node from the exception stack.
Definition except.c:270
WS_DLL_PUBLIC void except_setup_try(struct except_stacknode *, struct except_catch *, const except_id_t[], size_t)
Set up a try block for exception handling.
Definition except.c:259
WS_DLL_PUBLIC void * except_take_data(except_t *ex)
Take data from an exception object.
Definition except.c:374
const struct except_stacknode * except_get_top(void)
Get the top node from the exception stack.
Definition except.c:175
WS_DLL_PUBLIC void except_setup_clean(struct except_stacknode *, struct except_cleanup *, void(*)(void *), void *)
Set up a cleanup handler for exception handling.
Definition except.c:249
WS_DLL_PUBLIC WS_NORETURN void except_throw(long group, long code, const char *msg)
Throw an exception with a message and optional data.
Definition except.c:288
WS_DLL_PUBLIC int except_init(void)
Initialize the exception handling system.
Definition except.c:160
WS_DLL_PUBLIC void except_deinit(void)
Deinitialize the exception handling system.
Definition except.c:167
WS_DLL_PUBLIC WS_NORETURN void except_rethrow(except_t *except)
Rethrow an exception.
Definition except.c:278
WS_DLL_PUBLIC void except_free(void *ptr)
Frees memory allocated for an exception node.
Definition except.c:396
void except_set_top(struct except_stacknode *node)
Set the top node of an exception stack.
Definition except.c:180
Definition except.h:64
Definition except.h:59
Definition except.h:48
Represents a node in the exception handling stack.
Definition except.h:82
struct except_cleanup * except_cleanup
Definition except.h:87
enum except_stacktype except_type
Definition except.h:84
struct except_stacknode * except_down
Definition except.h:83
struct except_catch * except_catcher
Definition except.h:86
Definition except.h:53