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.
16 * $Id: except.c,v 1.1 2000/05/11 08:14:49 gram Exp $
27 #define XCEPT_BUFFER_SIZE 1024
30 static const char rcsid[] = "$Id: except.c,v 1.1 2000/05/11 08:14:49 gram Exp $";
33 #define group except_group
34 #define code except_code
36 #define message except_message
37 #define dyndata except_dyndata
38 #define func except_func
39 #define context except_context
41 #define size except_size
42 #define obj except_obj
43 #define jmp except_jmp
44 #define down except_down
45 #define type except_type
46 #define catcher except_catcher
47 #define cleanup except_cleanup
48 #define info except_info
50 #ifdef KAZLIB_POSIX_THREADS
54 static pthread_mutex_t init_mtx = PTHREAD_MUTEX_INITIALIZER;
55 static int init_counter;
56 static pthread_key_t top_key;
57 static pthread_key_t uh_key;
58 static pthread_key_t alloc_key;
59 static pthread_key_t dealloc_key;
60 static void unhandled_catcher(except_t *);
62 #define get_top() ((struct except_stacknode *) pthread_getspecific(top_key))
63 #define set_top(T) (pthread_setspecific(top_key, (T)), (void)((T) == (struct except_stacknode *) 0))
64 #define set_catcher(C) (pthread_setspecific(uh_key, (void *) (C)), (void)((C) == (void (*)(except_t *)) 0))
65 #define set_alloc(A) (pthread_setspecific(alloc_key, (void *) (A)), (void)((A) == (void *(*)(size_t)) 0))
66 #define set_dealloc(D) (pthread_setspecific(dealloc_key, (void *) (D)), (void)((D) == (void (*)(void *)) 0))
68 static void (*get_catcher(void))(except_t *)
70 void (*catcher)(except_t *) = (void (*)(except_t *)) pthread_getspecific(uh_key);
71 return (catcher == 0) ? unhandled_catcher : catcher;
74 static void *(*get_alloc(void))(size_t)
76 void *(*alloc)(size_t) = (void *(*)(size_t)) pthread_getspecific(alloc_key);
77 return (alloc == 0) ? malloc : alloc;
80 static void (*get_dealloc(void))(void *)
82 void (*dealloc)(void *) = (void (*)(void *)) pthread_getspecific(dealloc_key);
83 return (dealloc == 0) ? free : dealloc;
90 pthread_mutex_lock(&init_mtx);
92 assert (init_counter < INT_MAX);
94 if (init_counter++ == 0) {
95 int top_ok = (pthread_key_create(&top_key, 0) == 0);
96 int uh_ok = (pthread_key_create(&uh_key, 0) == 0);
97 int alloc_ok = (pthread_key_create(&alloc_key, 0) == 0);
98 int dealloc_ok = (pthread_key_create(&dealloc_key, 0) == 0);
100 if (!top_ok || !uh_ok || !alloc_ok || !dealloc_ok) {
104 pthread_key_delete(top_key);
106 pthread_key_delete(uh_key);
108 pthread_key_delete(alloc_key);
110 pthread_key_delete(dealloc_key);
114 pthread_mutex_unlock(&init_mtx);
119 void except_deinit(void)
121 pthread_mutex_lock(&init_mtx);
123 assert (init_counter > 0);
125 if (--init_counter == 0) {
126 pthread_key_delete(top_key);
127 pthread_key_delete(uh_key);
128 pthread_key_delete(alloc_key);
129 pthread_key_delete(dealloc_key);
132 pthread_mutex_unlock(&init_mtx);
135 #else /* no thread support */
137 static int init_counter;
138 static void unhandled_catcher(except_t *);
139 static void (*uh_catcher_ptr)(except_t *) = unhandled_catcher;
140 static void *(*allocator)(size_t) = malloc;
141 static void (*deallocator)(void *) = free;
142 static struct except_stacknode *stack_top;
144 #define get_top() (stack_top)
145 #define set_top(T) (stack_top = (T))
146 #define get_catcher() (uh_catcher_ptr)
147 #define set_catcher(C) (uh_catcher_ptr = (C))
148 #define get_alloc() (allocator)
149 #define set_alloc(A) (allocator = (A))
150 #define get_dealloc() (deallocator)
151 #define set_dealloc(D) (deallocator = (D))
153 int except_init(void)
155 assert (init_counter < INT_MAX);
160 void except_deinit(void)
162 assert (init_counter > 0);
169 static int match(const except_id_t *thrown, const except_id_t *caught)
171 int group_match = (caught->group == XCEPT_GROUP_ANY || caught->group == thrown->group);
172 int code_match = (caught->code == XCEPT_CODE_ANY || caught->code == thrown->code);
174 return group_match && code_match;
177 static void do_throw(except_t *except)
179 struct except_stacknode *top;
181 assert (except->id.group != 0 && except->id.code != 0);
183 for (top = get_top(); top != 0; top = top->down) {
184 if (top->type == XCEPT_CLEANUP) {
185 top->info.cleanup->func(top->info.cleanup->context);
187 struct except_catch *catcher = top->info.catcher;
188 const except_id_t *pi = catcher->id;
191 assert (top->type == XCEPT_CATCHER);
192 except_free(catcher->obj.dyndata);
194 for (i = 0; i < catcher->size; pi++, i++) {
195 if (match(&except->id, pi)) {
196 catcher->obj = *except;
198 longjmp(catcher->jmp, 1);
205 get_catcher()(except); /* unhandled exception */
209 static void unhandled_catcher(except_t *except)
211 fprintf(stderr, "Unhandled exception (\"%s\", group=%ld, code=%ld)\n",
212 except->message, except->id.group, except->id.code);
216 static void stack_push(struct except_stacknode *node)
218 node->down = get_top();
222 void except_setup_clean(struct except_stacknode *esn,
223 struct except_cleanup *ecl, void (*cleanf)(void *), void *context)
225 esn->type = XCEPT_CLEANUP;
227 ecl->context = context;
228 esn->info.cleanup = ecl;
232 void except_setup_try(struct except_stacknode *esn,
233 struct except_catch *ech, const except_id_t id[], size_t size)
237 ech->obj.dyndata = 0;
238 esn->type = XCEPT_CATCHER;
239 esn->info.catcher = ech;
243 struct except_stacknode *except_pop(void)
245 struct except_stacknode *top = get_top();
250 void except_rethrow(except_t *except)
252 struct except_stacknode *top = get_top();
254 assert (top->type == XCEPT_CATCHER);
255 assert (&top->info.catcher->obj == except);
260 void except_throw(long group, long code, const char *msg)
264 except.id.group = group;
265 except.id.code = code;
266 except.message = msg;
272 void except_throwd(long group, long code, const char *msg, void *data)
276 except.id.group = group;
277 except.id.code = code;
278 except.message = msg;
279 except.dyndata = data;
284 void except_throwf(long group, long code, const char *fmt, ...)
286 char *buf = except_alloc(XCEPT_BUFFER_SIZE);
290 vsprintf(buf, fmt, vl);
292 except_throwd(group, code, buf, buf);
295 void (*except_unhandled_catcher(void (*new_catcher)(except_t *)))(except_t *)
297 void (*old_catcher)(except_t *) = get_catcher();
298 set_catcher(new_catcher);
304 #undef except_message
307 unsigned long except_code(except_t *ex)
312 unsigned long except_group(except_t *ex)
317 const char *except_message(except_t *ex)
322 void *except_data(except_t *ex)
327 void *except_take_data(except_t *ex)
329 void *data = ex->dyndata;
334 void except_set_allocator(void *(*alloc)(size_t), void (*dealloc)(void *))
337 set_dealloc(dealloc);
340 void *except_alloc(size_t size)
342 void *ptr = get_alloc()(size);
345 except_throw(XCEPT_BAD_ALLOC, 0, "out of memory");
349 void except_free(void *ptr)
354 #ifdef KAZLIB_TEST_MAIN
359 static void cleanup(void *arg)
361 printf("cleanup(\"%s\") called\n", (char *) arg);
364 static void bottom_level(void)
367 printf("throw exception? "); fflush(stdout);
368 fgets(buf, sizeof buf, stdin);
370 if (toupper(buf[0]) == 'Y')
371 except_throw(1, 1, "nasty exception");
374 static void top_level(void)
376 except_cleanup_push(cleanup, "argument");
378 except_cleanup_pop(0);
381 int main(int argc, char **argv)
383 static const except_id_t catch[] = { { 1, 1 }, { 1, 2 } };
387 * Nested exception ``try blocks''
391 except_try_push(catch, 2, &ex);
394 except_try_push(catch, 2, &ex);
399 printf("caught exception (inner): \"%s\", s=%ld, c=%ld\n",
400 except_message(ex), except_group(ex), except_code(ex));
406 printf("caught exception (outer): \"%s\", s=%ld, c=%ld\n",
407 except_message(ex), except_group(ex), except_code(ex));
410 except_throw(99, 99, "exception in main");