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
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
49};
50
51
55typedef struct {
56 unsigned long except_group;
57 unsigned long except_code;
59
60
64typedef struct {
66 const char *volatile except_message;
67 void *volatile except_dyndata;
68} except_t;
69
70
75 void (*except_func)(void *);
77};
78
79
89
90
98
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
278void 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 */
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 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
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 void(*)(except_t *) except_unhandled_catcher(void(*new_catcher)(except_t *))
Sets the unhandled exception catcher.
Definition except.h:198
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_throwd(long group, long code, const char *msg, void *data)
Throw an exception with a detailed message and data.
Definition except.c:306
WS_DLL_PUBLIC WS_NORETURN void except_rethrow(except_t *except)
Rethrow an exception.
Definition except.c:278
@ except_no_call
Definition except.h:47
@ except_call
Definition except.h:48
WS_DLL_PUBLIC void except_set_allocator(void *(*alloc)(size_t), void(*dealloc)(void *))
Sets custom memory allocation and deallocation functions for exception handling.
Definition except.c:381
WS_DLL_PUBLIC void * except_alloc(size_t size)
Allocates memory for an exception object.
Definition except.c:387
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
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
except_stacktype
Discriminator tag for entries on the exception handler stack.
Definition except.h:94
@ XCEPT_CATCHER
Definition except.h:96
@ XCEPT_CLEANUP
Definition except.h:95
A catch block descriptor associating a set of exception IDs with a longjmp target.
Definition except.h:83
except_t except_obj
Definition except.h:86
const except_id_t * except_id
Definition except.h:84
size_t except_size
Definition except.h:85
jmp_buf except_jmp
Definition except.h:87
A cleanup handler registered to run when a scope is exited due to an exception.
Definition except.h:74
void * except_context
Definition except.h:76
void(* except_func)(void *)
Definition except.h:75
Identifies an exception class by group and code.
Definition except.h:55
unsigned long except_group
Definition except.h:56
unsigned long except_code
Definition except.h:57
Represents a node in the exception handling stack.
Definition except.h:106
struct except_cleanup * except_cleanup
Definition except.h:111
enum except_stacktype except_type
Definition except.h:108
struct except_stacknode * except_down
Definition except.h:107
struct except_catch * except_catcher
Definition except.h:110
Represents a thrown exception instance, including its identity, message, and dynamic data.
Definition except.h:64
except_id_t volatile except_id
Definition except.h:65
const char *volatile except_message
Definition except.h:66
void *volatile except_dyndata
Definition except.h:67