2 * Portable Exception Handling for ANSI C.
3 * Copyright (C) 1999 Kaz Kylheku <kaz@ashi.footprints.net>
5 * Free Software License:
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.
19 * Modified to support throwing an exception with a null message pointer,
20 * and to have the message not be const (as we generate messages with
21 * "g_strdup_sprintf()", which means they need to be freed; using
22 * a null message means that we don't have to use a special string
23 * for exceptions with no message, and don't have to worry about
41 #include "exceptions.h"
44 #define XCEPT_BUFFER_SIZE 1024
46 #ifdef KAZLIB_POSIX_THREADS
50 static pthread_mutex_t init_mtx = PTHREAD_MUTEX_INITIALIZER;
51 static int init_counter;
52 static pthread_key_t top_key;
53 static pthread_key_t uh_key;
54 static pthread_key_t alloc_key;
55 static pthread_key_t dealloc_key;
56 static void unhandled_catcher(except_t *);
58 #define get_top() ((struct except_stacknode *) pthread_getspecific(top_key))
59 #define set_top(T) (pthread_setspecific(top_key, (T)), (void)((T) == (struct except_stacknode *) 0))
60 #define set_catcher(C) (pthread_setspecific(uh_key, (void *) (C)), (void)((C) == (void (*)(except_t *)) 0))
61 #define set_alloc(A) (pthread_setspecific(alloc_key, (void *) (A)), (void)((A) == (void *(*)(size_t)) 0))
62 #define set_dealloc(D) (pthread_setspecific(dealloc_key, (void *) (D)), (void)((D) == (void (*)(void *)) 0))
64 static void (*get_catcher(void))(except_t *)
66 void (*catcher)(except_t *) = (void (*)(except_t *)) pthread_getspecific(uh_key);
67 return (catcher == 0) ? unhandled_catcher : catcher;
70 static void *(*get_alloc(void))(size_t)
72 void *(*alloc)(size_t) = (void *(*)(size_t)) pthread_getspecific(alloc_key);
73 return (alloc == 0) ? malloc : alloc;
76 static void (*get_dealloc(void))(void *)
78 void (*dealloc)(void *) = (void (*)(void *)) pthread_getspecific(dealloc_key);
79 return (dealloc == 0) ? free : dealloc;
86 pthread_mutex_lock(&init_mtx);
88 assert (init_counter < INT_MAX);
90 if (init_counter++ == 0) {
91 int top_ok = (pthread_key_create(&top_key, 0) == 0);
92 int uh_ok = (pthread_key_create(&uh_key, 0) == 0);
93 int alloc_ok = (pthread_key_create(&alloc_key, 0) == 0);
94 int dealloc_ok = (pthread_key_create(&dealloc_key, 0) == 0);
96 if (!top_ok || !uh_ok || !alloc_ok || !dealloc_ok) {
100 pthread_key_delete(top_key);
102 pthread_key_delete(uh_key);
104 pthread_key_delete(alloc_key);
106 pthread_key_delete(dealloc_key);
110 pthread_mutex_unlock(&init_mtx);
115 void except_deinit(void)
117 pthread_mutex_lock(&init_mtx);
119 assert (init_counter > 0);
121 if (--init_counter == 0) {
122 pthread_key_delete(top_key);
123 pthread_key_delete(uh_key);
124 pthread_key_delete(alloc_key);
125 pthread_key_delete(dealloc_key);
128 pthread_mutex_unlock(&init_mtx);
131 #else /* no thread support */
133 static int init_counter;
134 static void unhandled_catcher(except_t *);
135 static void (*uh_catcher_ptr)(except_t *) = unhandled_catcher;
136 /* We need this 'size_t' cast due to a glitch in GLib where g_malloc was prototyped
137 * as 'gpointer g_malloc (gulong n_bytes)'. This was later fixed to the correct prototype
138 * 'gpointer g_malloc (gsize n_bytes)'. In Wireshark we use the latter prototype
139 * throughout the code. We can get away with this even with older versions of GLib by
140 * adding a '(void *(*)(size_t))' cast whenever we refer to g_malloc. The only platform
141 * supported by Wireshark where this isn't safe (sizeof size_t != sizeof gulong) is Win64.
142 * However, we _always_ bundle the newest version of GLib on this platform so
143 * the size_t issue doesn't exists here. Pheew.. */
144 static void *(*allocator)(size_t) = (void *(*)(size_t)) g_malloc;
145 static void (*deallocator)(void *) = g_free;
146 static struct except_stacknode *stack_top;
148 #define get_top() (stack_top)
149 #define set_top(T) (stack_top = (T))
150 #define get_catcher() (uh_catcher_ptr)
151 #define set_catcher(C) (uh_catcher_ptr = (C))
152 #define get_alloc() (allocator)
153 #define set_alloc(A) (allocator = (A))
154 #define get_dealloc() (deallocator)
155 #define set_dealloc(D) (deallocator = (D))
157 int except_init(void)
159 assert (init_counter < INT_MAX);
164 void except_deinit(void)
166 assert (init_counter > 0);
173 static int match(const volatile except_id_t *thrown, const except_id_t *caught)
175 int group_match = (caught->except_group == XCEPT_GROUP_ANY ||
176 caught->except_group == thrown->except_group);
177 int code_match = (caught->except_code == XCEPT_CODE_ANY ||
178 caught->except_code == thrown->except_code);
180 return group_match && code_match;
183 G_GNUC_NORETURN WS_MSVC_NORETURN static void do_throw(except_t *except)
185 struct except_stacknode *top;
187 assert (except->except_id.except_group != 0 &&
188 except->except_id.except_code != 0);
190 for (top = get_top(); top != 0; top = top->except_down) {
191 if (top->except_type == XCEPT_CLEANUP) {
192 top->except_info.except_cleanup->except_func(top->except_info.except_cleanup->except_context);
194 struct except_catch *catcher = top->except_info.except_catcher;
195 const except_id_t *pi = catcher->except_id;
198 assert (top->except_type == XCEPT_CATCHER);
199 except_free(catcher->except_obj.except_dyndata);
201 for (i = 0; i < catcher->except_size; pi++, i++) {
202 if (match(&except->except_id, pi)) {
203 catcher->except_obj = *except;
205 longjmp(catcher->except_jmp, 1);
212 get_catcher()(except); /* unhandled exception */
216 static void unhandled_catcher(except_t *except)
218 if (except->except_message == NULL) {
219 fprintf(stderr, "Unhandled exception (group=%lu, code=%lu)\n",
220 except->except_id.except_group,
221 except->except_id.except_code);
223 fprintf(stderr, "Unhandled exception (\"%s\", group=%lu, code=%lu)\n",
224 except->except_message, except->except_id.except_group,
225 except->except_id.except_code);
230 static void stack_push(struct except_stacknode *node)
232 node->except_down = get_top();
236 void except_setup_clean(struct except_stacknode *esn,
237 struct except_cleanup *ecl, void (*cleanf)(void *), void *context)
239 esn->except_type = XCEPT_CLEANUP;
240 ecl->except_func = cleanf;
241 ecl->except_context = context;
242 esn->except_info.except_cleanup = ecl;
246 void except_setup_try(struct except_stacknode *esn,
247 struct except_catch *ech, const except_id_t id[], size_t size)
250 ech->except_size = size;
251 ech->except_obj.except_dyndata = 0;
252 esn->except_type = XCEPT_CATCHER;
253 esn->except_info.except_catcher = ech;
257 struct except_stacknode *except_pop(void)
259 struct except_stacknode *top = get_top();
260 set_top(top->except_down);
264 G_GNUC_NORETURN WS_MSVC_NORETURN void except_rethrow(except_t *except)
266 struct except_stacknode *top = get_top();
268 assert (top->except_type == XCEPT_CATCHER);
269 assert (&top->except_info.except_catcher->except_obj == except);
270 set_top(top->except_down);
274 G_GNUC_NORETURN WS_MSVC_NORETURN void except_throw(long group, long code, const char *msg)
278 except.except_id.except_group = group;
279 except.except_id.except_code = code;
280 except.except_message = msg;
281 except.except_dyndata = 0;
284 if (code == DissectorError && IsDebuggerPresent()) {
292 G_GNUC_NORETURN WS_MSVC_NORETURN void except_throwd(long group, long code, const char *msg, void *data)
296 except.except_id.except_group = group;
297 except.except_id.except_code = code;
298 except.except_message = msg;
299 except.except_dyndata = data;
305 * XXX - should we use g_strdup_sprintf() here, so we're not limited by
306 * XCEPT_BUFFER_SIZE? We could then just use this to generate formatted
309 G_GNUC_NORETURN WS_MSVC_NORETURN void except_throwf(long group, long code, const char *fmt, ...)
311 char *buf = (char *)except_alloc(XCEPT_BUFFER_SIZE);
315 g_vsnprintf(buf, XCEPT_BUFFER_SIZE, fmt, vl);
317 except_throwd(group, code, buf, buf);
320 void (*except_unhandled_catcher(void (*new_catcher)(except_t *)))(except_t *)
322 void (*old_catcher)(except_t *) = get_catcher();
323 set_catcher(new_catcher);
329 #undef except_message
332 unsigned long except_code(except_t *ex)
334 return ex->except_id.except_code;
337 unsigned long except_group(except_t *ex)
339 return ex->except_id.except_group;
342 const char *except_message(except_t *ex)
344 return ex->except_message;
347 void *except_data(except_t *ex)
349 return ex->except_dyndata;
352 void *except_take_data(except_t *ex)
354 void *data = ex->except_dyndata;
355 ex->except_dyndata = 0;
359 void except_set_allocator(void *(*alloc)(size_t), void (*dealloc)(void *))
362 set_dealloc(dealloc);
365 void *except_alloc(size_t size)
367 void *ptr = get_alloc()(size);
370 except_throw(XCEPT_BAD_ALLOC, 0, "out of memory");
374 void except_free(void *ptr)
379 #ifdef KAZLIB_TEST_MAIN
382 static void cleanup(void *arg)
384 printf("cleanup(\"%s\") called\n", (char *) arg);
387 static void bottom_level(void)
390 printf("throw exception? "); fflush(stdout);
391 fgets(buf, sizeof buf, stdin);
393 if (buf[0] >= 0 && (buf[0] == 'Y' || buf[0] == 'y'))
394 except_throw(1, 1, "nasty exception");
397 static void top_level(void)
399 except_cleanup_push(cleanup, "argument");
401 except_cleanup_pop(0);
404 int main(int argc, char **argv)
406 static const except_id_t catch[] = { { 1, 1 }, { 1, 2 } };
411 * Nested exception ``try blocks''
415 except_try_push(catch, 2, &ex);
418 except_try_push(catch, 2, &ex);
423 msg = except_message(ex);
425 printf("caught exception (inner): s=%lu, c=%lu\n",
426 except_group(ex), except_code(ex));
428 printf("caught exception (inner): \"%s\", s=%lu, c=%lu\n",
429 msg, except_group(ex), except_code(ex));
436 msg = except_message(ex);
438 printf("caught exception (outer): s=%lu, c=%lu\n",
439 except_group(ex), except_code(ex));
441 printf("caught exception (outer): \"%s\", s=%lu, c=%lu\n",
442 except_message(ex), except_group(ex), except_code(ex));
446 except_throw(99, 99, "exception in main");
454 * Editor modelines - http://www.wireshark.org/tools/modelines.html
459 * indent-tabs-mode: nil
462 * vi: set shiftwidth=4 tabstop=8 expandtab:
463 * :indentSize=4:tabSize=8:noTabs=true: