Debian Codegraph @

Xpjproject/pjlib/src/pj/except.c

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