Tristan Matthews | 0a329cc | 2013-07-17 13:20:14 -0400 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
| 3 | * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com) |
| 4 | * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 | */ |
| 20 | #include <pj/except.h> |
| 21 | #include <pj/os.h> |
| 22 | #include <pj/assert.h> |
| 23 | #include <pj/log.h> |
| 24 | #include <pj/errno.h> |
| 25 | #include <pj/string.h> |
| 26 | |
| 27 | static long thread_local_id = -1; |
| 28 | |
| 29 | #if defined(PJ_HAS_EXCEPTION_NAMES) && PJ_HAS_EXCEPTION_NAMES != 0 |
| 30 | static const char *exception_id_names[PJ_MAX_EXCEPTION_ID]; |
| 31 | #else |
| 32 | /* |
| 33 | * Start from 1 (not 0)!!! |
| 34 | * Exception 0 is reserved for normal path of setjmp()!!! |
| 35 | */ |
| 36 | static int last_exception_id = 1; |
| 37 | #endif /* PJ_HAS_EXCEPTION_NAMES */ |
| 38 | |
| 39 | |
| 40 | #if !defined(PJ_EXCEPTION_USE_WIN32_SEH) || PJ_EXCEPTION_USE_WIN32_SEH==0 |
| 41 | PJ_DEF(void) pj_throw_exception_(int exception_id) |
| 42 | { |
| 43 | struct pj_exception_state_t *handler; |
| 44 | |
| 45 | handler = (struct pj_exception_state_t*) |
| 46 | pj_thread_local_get(thread_local_id); |
| 47 | if (handler == NULL) { |
| 48 | PJ_LOG(1,("except.c", "!!!FATAL: unhandled exception %s!\n", |
| 49 | pj_exception_id_name(exception_id))); |
| 50 | pj_assert(handler != NULL); |
| 51 | /* This will crash the system! */ |
| 52 | } |
| 53 | pj_pop_exception_handler_(handler); |
| 54 | pj_longjmp(handler->state, exception_id); |
| 55 | } |
| 56 | |
| 57 | static void exception_cleanup(void) |
| 58 | { |
| 59 | if (thread_local_id != -1) { |
| 60 | pj_thread_local_free(thread_local_id); |
| 61 | thread_local_id = -1; |
| 62 | } |
| 63 | |
| 64 | #if defined(PJ_HAS_EXCEPTION_NAMES) && PJ_HAS_EXCEPTION_NAMES != 0 |
| 65 | { |
| 66 | unsigned i; |
| 67 | for (i=0; i<PJ_MAX_EXCEPTION_ID; ++i) |
| 68 | exception_id_names[i] = NULL; |
| 69 | } |
| 70 | #else |
| 71 | last_exception_id = 1; |
| 72 | #endif |
| 73 | } |
| 74 | |
| 75 | PJ_DEF(void) pj_push_exception_handler_(struct pj_exception_state_t *rec) |
| 76 | { |
| 77 | struct pj_exception_state_t *parent_handler = NULL; |
| 78 | |
| 79 | if (thread_local_id == -1) { |
| 80 | pj_thread_local_alloc(&thread_local_id); |
| 81 | pj_assert(thread_local_id != -1); |
| 82 | pj_atexit(&exception_cleanup); |
| 83 | } |
| 84 | parent_handler = (struct pj_exception_state_t *) |
| 85 | pj_thread_local_get(thread_local_id); |
| 86 | rec->prev = parent_handler; |
| 87 | pj_thread_local_set(thread_local_id, rec); |
| 88 | } |
| 89 | |
| 90 | PJ_DEF(void) pj_pop_exception_handler_(struct pj_exception_state_t *rec) |
| 91 | { |
| 92 | struct pj_exception_state_t *handler; |
| 93 | |
| 94 | handler = (struct pj_exception_state_t *) |
| 95 | pj_thread_local_get(thread_local_id); |
| 96 | if (handler && handler==rec) { |
| 97 | pj_thread_local_set(thread_local_id, handler->prev); |
| 98 | } |
| 99 | } |
| 100 | #endif |
| 101 | |
| 102 | #if defined(PJ_HAS_EXCEPTION_NAMES) && PJ_HAS_EXCEPTION_NAMES != 0 |
| 103 | PJ_DEF(pj_status_t) pj_exception_id_alloc( const char *name, |
| 104 | pj_exception_id_t *id) |
| 105 | { |
| 106 | unsigned i; |
| 107 | |
| 108 | pj_enter_critical_section(); |
| 109 | |
| 110 | /* |
| 111 | * Start from 1 (not 0)!!! |
| 112 | * Exception 0 is reserved for normal path of setjmp()!!! |
| 113 | */ |
| 114 | for (i=1; i<PJ_MAX_EXCEPTION_ID; ++i) { |
| 115 | if (exception_id_names[i] == NULL) { |
| 116 | exception_id_names[i] = name; |
| 117 | *id = i; |
| 118 | pj_leave_critical_section(); |
| 119 | return PJ_SUCCESS; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | pj_leave_critical_section(); |
| 124 | return PJ_ETOOMANY; |
| 125 | } |
| 126 | |
| 127 | PJ_DEF(pj_status_t) pj_exception_id_free( pj_exception_id_t id ) |
| 128 | { |
| 129 | /* |
| 130 | * Start from 1 (not 0)!!! |
| 131 | * Exception 0 is reserved for normal path of setjmp()!!! |
| 132 | */ |
| 133 | PJ_ASSERT_RETURN(id>0 && id<PJ_MAX_EXCEPTION_ID, PJ_EINVAL); |
| 134 | |
| 135 | pj_enter_critical_section(); |
| 136 | exception_id_names[id] = NULL; |
| 137 | pj_leave_critical_section(); |
| 138 | |
| 139 | return PJ_SUCCESS; |
| 140 | |
| 141 | } |
| 142 | |
| 143 | PJ_DEF(const char*) pj_exception_id_name(pj_exception_id_t id) |
| 144 | { |
| 145 | static char unknown_name[32]; |
| 146 | |
| 147 | /* |
| 148 | * Start from 1 (not 0)!!! |
| 149 | * Exception 0 is reserved for normal path of setjmp()!!! |
| 150 | */ |
| 151 | PJ_ASSERT_RETURN(id>0 && id<PJ_MAX_EXCEPTION_ID, "<Invalid ID>"); |
| 152 | |
| 153 | if (exception_id_names[id] == NULL) { |
| 154 | pj_ansi_snprintf(unknown_name, sizeof(unknown_name), |
| 155 | "exception %d", id); |
| 156 | return unknown_name; |
| 157 | } |
| 158 | |
| 159 | return exception_id_names[id]; |
| 160 | } |
| 161 | |
| 162 | #else /* PJ_HAS_EXCEPTION_NAMES */ |
| 163 | PJ_DEF(pj_status_t) pj_exception_id_alloc( const char *name, |
| 164 | pj_exception_id_t *id) |
| 165 | { |
| 166 | PJ_ASSERT_RETURN(last_exception_id < PJ_MAX_EXCEPTION_ID-1, PJ_ETOOMANY); |
| 167 | |
| 168 | *id = last_exception_id++; |
| 169 | return PJ_SUCCESS; |
| 170 | } |
| 171 | |
| 172 | PJ_DEF(pj_status_t) pj_exception_id_free( pj_exception_id_t id ) |
| 173 | { |
| 174 | return PJ_SUCCESS; |
| 175 | } |
| 176 | |
| 177 | PJ_DEF(const char*) pj_exception_id_name(pj_exception_id_t id) |
| 178 | { |
| 179 | return ""; |
| 180 | } |
| 181 | |
| 182 | #endif /* PJ_HAS_EXCEPTION_NAMES */ |
| 183 | |
| 184 | |
| 185 | |