| 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 <kaz@ashi.footprints.net>
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
30#ifndef XCEPT_H
31#define XCEPT_H
32
33#include <glib.h>
34#include <setjmp.h>
35#include <stdlib.h>
36#include <stdarg.h>
37#include <assert.h>
38#include "ws_symbol_export.h"
39#include "ws_attributes.h"
40
41#define XCEPT_GROUP_ANY 0
42#define XCEPT_CODE_ANY 0
43#define XCEPT_BAD_ALLOC 1
44
45#ifdef __cplusplus
46 extern "C" {
47#endif
48
49 enum { except_no_call, except_call };
50
51 typedef struct {
52 unsigned long except_group;
53 unsigned long except_code;
54} except_id_t;
55
56 typedef struct {
57 except_id_t volatile except_id;
58 const char *volatile except_message;
59 void *volatile except_dyndata;
60} except_t;
61
62 struct except_cleanup {
63 void (*except_func)(void *);
64 void *except_context;
65};
66
67 struct except_catch {
68 const except_id_t *except_id;
69 size_t except_size;
70 except_t except_obj;
71 jmp_buf except_jmp;
72};
73
74 enum except_stacktype {
75 XCEPT_CLEANUP, XCEPT_CATCHER
76};
77
85 struct except_stacknode {
86 struct except_stacknode *except_down;
87 enum except_stacktype except_type;
88 union {
89 struct except_catch *except_catcher;
90 struct except_cleanup *except_cleanup;
91 } except_info;
92};
93
94/* private functions made external so they can be used in macros */
95WS_DLL_PUBLIC void except_setup_clean(struct except_stacknode *,
96 struct except_cleanup *, void (*)(void *), void *);
97WS_DLL_PUBLIC void except_setup_try(struct except_stacknode *,
98 struct except_catch *, const except_id_t [], size_t);
99WS_DLL_PUBLIC struct except_stacknode *except_pop(void);
100
101/* public interface functions */
102WS_DLL_PUBLIC int except_init(void);
103WS_DLL_PUBLIC void except_deinit(void);
104WS_DLL_PUBLIC WS_NORETURN void except_rethrow(except_t *);
105WS_DLL_PUBLIC WS_NORETURN void except_throw(long, long, const char *);
106WS_DLL_PUBLIC WS_NORETURN void except_throwd(long, long, const char *, void *);
107WS_DLL_PUBLIC WS_NORETURN void except_vthrowf(long group, long code, const char *fmt, va_list vl);
108WS_DLL_PUBLIC WS_NORETURN void except_throwf(long, long, const char *, ...)
109 G_GNUC_PRINTF(3, 4);
110WS_DLL_PUBLIC void (*except_unhandled_catcher(void (*)(except_t *)))(except_t *);
111extern unsigned long except_code(except_t *);
112extern unsigned long except_group(except_t *);
113extern const char *except_message(except_t *);
114extern void *except_data(except_t *);
115WS_DLL_PUBLIC void *except_take_data(except_t *);
116WS_DLL_PUBLIC void except_set_allocator(void *(*)(size_t), void (*)(void *));
117WS_DLL_PUBLIC void *except_alloc(size_t);
118WS_DLL_PUBLIC void except_free(void *);
119
120/* Functions to be used in a last resort when things go badly wrong; e.g.,
121 * Lua uses setjmp and longjmp for its own error handling, and Lua errors
122 * can longjmp past the ENDTRY so that the function that created the current
123 * top node has exited and the node (and the jmp_buf) is no longer valid
124 * (having been created on the stack) so we can't run the handlers or even
125 * traverse the exception stack. It's better to do this than crash. */
126const struct except_stacknode *except_get_top(void);
127 void except_set_top(struct except_stacknode *node);
128
129#define except_code(E) ((E)->except_id.except_code)
130#define except_group(E) ((E)->except_id.except_group)
131#define except_message(E) ((E)->except_message)
132#define except_data(E) ((E)->except_dyndata)
133
134#ifdef __cplusplus
135}
136#endif
137
138/*
139 * void except_cleanup_push(void (*)(void *), void *);
140 * void except_cleanup_pop(int);
141 * void except_checked_cleanup_pop(void (*)(void *), int);
142 * void except_try_push(const except_id_t [], size_t, except_t **);
143 * void except_try_pop(void);
144 */
145
146#define except_cleanup_push(F, C) \
147 { \
148 struct except_stacknode except_sn; \
149 struct except_cleanup except_cl; \
150 except_setup_clean(&except_sn, &except_cl, F, C)
151
152#define except_cleanup_pop(E) \
153 except_pop(); \
154 if (E) \
155 except_cl.except_func(except_cl.except_context); \
156 }
157
158#define except_checked_cleanup_pop(F, E) \
159 except_pop(); \
160 assert (except_cl.except_func == (F)); \
161 if (E) \
162 except_cl.except_func(except_cl.except_context); \
163 }
164
165
166/* --- Variants to allow nesting of except_cleanup_push w/o "shadowing" variables */
167#define except_cleanup_push_pfx(pfx, F, C) \
168 { \
169 struct except_stacknode pfx##_except_sn; \
170 struct except_cleanup pfx##_except_cl; \
171 except_setup_clean(&pfx##_except_sn, &pfx##_except_cl, F, C)
172
173#define except_cleanup_pop_pfx(pfx, E) \
174 except_pop(); \
175 if (E) \
176 pfx##_except_cl.except_func(pfx##_except_cl.except_context);\
177 }
178
179#define except_checked_cleanup_pop_pfx(pfx, F, E) \
180 except_pop(); \
181 assert (pfx##_except_cl.except_func == (F)); \
182 if (E) \
183 pfx##_except_cl.except_func(pfx##_except_cl.except_context);\
184 }
185/* ---------- */
186
187
188#define except_try_push(ID, NUM, PPE) \
189 { \
190 struct except_stacknode except_sn; \
191 struct except_catch except_ch; \
192 except_setup_try(&except_sn, &except_ch, ID, NUM); \
193 if (setjmp(except_ch.except_jmp)) \
194 *(PPE) = &except_ch.except_obj; \
195 else \
196 *(PPE) = 0
197
198#define except_try_pop() \
199 except_free(except_ch.except_obj.except_dyndata); \
200 except_pop(); \
201 }
202
203#endif /* XCEPT_H */
204
205/*
206 * Editor modelines - https://www.wireshark.org/tools/modelines.html
207 *
208 * Local variables:
209 * c-basic-offset: 4
210 * tab-width: 8
211 * indent-tabs-mode: nil
212 * End:
213 *
214 * vi: set shiftwidth=4 tabstop=8 expandtab:
215 * :indentSize=4:tabSize=8:noTabs=true:
216 */
except_catch
Definition except.h:67
except_cleanup
Definition except.h:62
except_id_t
Definition except.h:51
except_stacknode
Represents a node in the exception handling stack.
Definition except.h:85
except_stacknode::except_cleanup
struct except_cleanup * except_cleanup
Definition except.h:90
except_stacknode::except_type
enum except_stacktype except_type
Definition except.h:87
except_stacknode::except_down
struct except_stacknode * except_down
Definition except.h:86
except_stacknode::except_catcher
struct except_catch * except_catcher
Definition except.h:89
except_t
Definition except.h:56