| Wireshark 4.7.3 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
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
43 extern "C" {
44#endif
45
47 enum { except_no_call,
48 except_call
49};
50
51
55 typedef struct {
56 unsigned long except_group;
57 unsigned long except_code;
58} except_id_t;
59
60
64 typedef struct {
65 except_id_t volatile except_id;
66 const char *volatile except_message;
67 void *volatile except_dyndata;
68} except_t;
69
70
74 struct except_cleanup {
75 void (*except_func)(void *);
76 void *except_context;
77};
78
79
83 struct except_catch {
84 const except_id_t *except_id;
85 size_t except_size;
86 except_t except_obj;
87 jmp_buf except_jmp;
88};
89
90
94 enum except_stacktype {
95 XCEPT_CLEANUP,
96 XCEPT_CATCHER
97};
98
106 struct except_stacknode {
107 struct except_stacknode *except_down;
108 enum except_stacktype except_type;
109 union {
110 struct except_catch *except_catcher;
111 struct except_cleanup *except_cleanup;
112 } except_info;
113};
114
115/* private functions made external so they can be used in macros */
116
120WS_DLL_PUBLIC void except_setup_clean(struct except_stacknode *,
121 struct except_cleanup *, void (*)(void *), void *);
122
126WS_DLL_PUBLIC void except_setup_try(struct except_stacknode *,
127 struct except_catch *, const except_id_t [], size_t);
128
133WS_DLL_PUBLIC struct except_stacknode *except_pop(void);
134
135/* public interface functions */
141WS_DLL_PUBLIC int except_init(void);
142
146WS_DLL_PUBLIC void except_deinit(void);
147
153WS_DLL_PUBLIC WS_NORETURN void except_rethrow(except_t * except);
154
161WS_DLL_PUBLIC WS_NORETURN void except_throw(long group, long code, const char *msg);
162
171WS_DLL_PUBLIC WS_NORETURN void except_throwd(long group, long code, const char *msg, void *data);
172
181WS_DLL_PUBLIC WS_NORETURN void except_vthrowf(long group, long code, const char *fmt, va_list vl);
182
190WS_DLL_PUBLIC WS_NORETURN void except_throwf(long group, long code, const char *fmt, ...)
191 G_GNUC_PRINTF(3, 4);
192
198WS_DLL_PUBLIC void (*except_unhandled_catcher(void (*new_catcher)(except_t *)))(except_t *);
199
205extern unsigned long except_code(except_t *ex);
206
212extern unsigned long except_group(except_t *ex);
213
219extern const char *except_message(except_t *ex);
220
226extern void *except_data(except_t *ex);
227
234WS_DLL_PUBLIC void *except_take_data(except_t * ex);
235
242WS_DLL_PUBLIC void except_set_allocator(void *(*alloc)(size_t), void (*dealloc)(void *));
243
250WS_DLL_PUBLIC void *except_alloc(size_t size);
251
257WS_DLL_PUBLIC void except_free(void * ptr);
258
259/* Functions to be used in a last resort when things go badly wrong; e.g.,
260 * Lua uses setjmp and longjmp for its own error handling, and Lua errors
261 * can longjmp past the ENDTRY so that the function that created the current
262 * top node has exited and the node (and the jmp_buf) is no longer valid
263 * (having been created on the stack) so we can't run the handlers or even
264 * traverse the exception stack. It's better to do this than crash. */
265
271const struct except_stacknode *except_get_top(void);
272
278 void except_set_top(struct except_stacknode *node);
279
280#define except_code(E) ((E)->except_id.except_code)
281#define except_group(E) ((E)->except_id.except_group)
282#define except_message(E) ((E)->except_message)
283#define except_data(E) ((E)->except_dyndata)
284
285#ifdef __cplusplus
286}
287#endif
288
289/*
290 * void except_cleanup_push(void (*)(void *), void *);
291 * void except_cleanup_pop(int);
292 * void except_checked_cleanup_pop(void (*)(void *), int);
293 * void except_try_push(const except_id_t [], size_t, except_t **);
294 * void except_try_pop(void);
295 */
296
297#define except_cleanup_push(F, C) \
298 { \
299 struct except_stacknode except_sn; \
300 struct except_cleanup except_cl; \
301 except_setup_clean(&except_sn, &except_cl, F, C)
302
303#define except_cleanup_pop(E) \
304 except_pop(); \
305 if (E) \
306 except_cl.except_func(except_cl.except_context); \
307 }
308
309#define except_checked_cleanup_pop(F, E) \
310 except_pop(); \
311 assert (except_cl.except_func == (F)); \
312 if (E) \
313 except_cl.except_func(except_cl.except_context); \
314 }
315
316
317/* --- Variants to allow nesting of except_cleanup_push w/o "shadowing" variables */
318#define except_cleanup_push_pfx(pfx, F, C) \
319 { \
320 struct except_stacknode pfx##_except_sn; \
321 struct except_cleanup pfx##_except_cl; \
322 except_setup_clean(&pfx##_except_sn, &pfx##_except_cl, F, C)
323
324#define except_cleanup_pop_pfx(pfx, E) \
325 except_pop(); \
326 if (E) \
327 pfx##_except_cl.except_func(pfx##_except_cl.except_context);\
328 }
329
330#define except_checked_cleanup_pop_pfx(pfx, F, E) \
331 except_pop(); \
332 assert (pfx##_except_cl.except_func == (F)); \
333 if (E) \
334 pfx##_except_cl.except_func(pfx##_except_cl.except_context);\
335 }
336/* ---------- */
337
338
339#define except_try_push(ID, NUM, PPE) \
340 { \
341 struct except_stacknode except_sn; \
342 struct except_catch except_ch; \
343 except_setup_try(&except_sn, &except_ch, ID, NUM); \
344 if (setjmp(except_ch.except_jmp)) \
345 *(PPE) = &except_ch.except_obj; \
346 else \
347 *(PPE) = 0
348
349#define except_try_pop() \
350 except_free(except_ch.except_obj.except_dyndata); \
351 except_pop(); \
352 }
353
354/*
355 * Editor modelines - https://www.wireshark.org/tools/modelines.html
356 *
357 * Local variables:
358 * c-basic-offset: 4
359 * tab-width: 8
360 * indent-tabs-mode: nil
361 * End:
362 *
363 * vi: set shiftwidth=4 tabstop=8 expandtab:
364 * :indentSize=4:tabSize=8:noTabs=true:
365 */
except_pop
WS_DLL_PUBLIC struct except_stacknode * except_pop(void)
Pop the top node from the exception stack.
Definition except.c:270
except_setup_try
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
except_vthrowf
WS_DLL_PUBLIC WS_NORETURN void except_vthrowf(long group, long code, const char *fmt, va_list vl)
Throw an exception with a formatted message.
Definition except.c:323
except_setup_clean
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
except_no_call
@ except_no_call
Definition except.h:47
except_call
@ except_call
Definition except.h:48
except_throw
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
except_init
WS_DLL_PUBLIC int except_init(void)
Initialize the exception handling system.
Definition except.c:160
except_deinit
WS_DLL_PUBLIC void except_deinit(void)
Deinitialize the exception handling system.
Definition except.c:167
except_throwd
WS_DLL_PUBLIC WS_NORETURN void except_throwd(long group, long code, const char *msg, void *data)
Throw an exception with a detailed message and data.
Definition except.c:306
except_rethrow
WS_DLL_PUBLIC WS_NORETURN void except_rethrow(except_t *except)
Rethrow an exception.
Definition except.c:278
except_throwf
WS_DLL_PUBLIC WS_NORETURN void except_throwf(long group, long code, const char *fmt,...)
Throws an exception with a formatted message.
Definition except.c:332
except_stacktype
except_stacktype
Discriminator tag for entries on the exception handler stack.
Definition except.h:94
XCEPT_CATCHER
@ XCEPT_CATCHER
Definition except.h:96
XCEPT_CLEANUP
@ XCEPT_CLEANUP
Definition except.h:95
except_catch
A catch block descriptor associating a set of exception IDs with a longjmp target.
Definition except.h:83
except_catch::except_obj
except_t except_obj
Definition except.h:86
except_catch::except_id
const except_id_t * except_id
Definition except.h:84
except_catch::except_size
size_t except_size
Definition except.h:85
except_catch::except_jmp
jmp_buf except_jmp
Definition except.h:87
except_cleanup
A cleanup handler registered to run when a scope is exited due to an exception.
Definition except.h:74
except_cleanup::except_context
void * except_context
Definition except.h:76
except_cleanup::except_func
void(* except_func)(void *)
Definition except.h:75
except_id_t
Identifies an exception class by group and code.
Definition except.h:55
except_id_t::except_group
unsigned long except_group
Definition except.h:56
except_id_t::except_code
unsigned long except_code
Definition except.h:57
except_stacknode
Represents a node in the exception handling stack.
Definition except.h:106
except_stacknode::except_cleanup
struct except_cleanup * except_cleanup
Definition except.h:111
except_stacknode::except_type
enum except_stacktype except_type
Definition except.h:108
except_stacknode::except_down
struct except_stacknode * except_down
Definition except.h:107
except_stacknode::except_catcher
struct except_catch * except_catcher
Definition except.h:110
except_t
Represents a thrown exception instance, including its identity, message, and dynamic data.
Definition except.h:64
except_t::except_id
except_id_t volatile except_id
Definition except.h:65
except_t::except_message
const char *volatile except_message
Definition except.h:66
except_t::except_dyndata
void *volatile except_dyndata
Definition except.h:67