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 | |
| 26 | |
| 27 | #if defined(PJ_HAS_EXCEPTION_NAMES) && PJ_HAS_EXCEPTION_NAMES != 0 |
| 28 | static const char *exception_id_names[PJ_MAX_EXCEPTION_ID]; |
| 29 | #else |
| 30 | /* |
| 31 | * Start from 1 (not 0)!!! |
| 32 | * Exception 0 is reserved for normal path of setjmp()!!! |
| 33 | */ |
| 34 | static int last_exception_id = 1; |
| 35 | #endif /* PJ_HAS_EXCEPTION_NAMES */ |
| 36 | |
| 37 | |
| 38 | #if defined(PJ_HAS_EXCEPTION_NAMES) && PJ_HAS_EXCEPTION_NAMES != 0 |
| 39 | PJ_DEF(pj_status_t) pj_exception_id_alloc( const char *name, |
| 40 | pj_exception_id_t *id) |
| 41 | { |
| 42 | unsigned i; |
| 43 | |
| 44 | pj_enter_critical_section(); |
| 45 | |
| 46 | /* |
| 47 | * Start from 1 (not 0)!!! |
| 48 | * Exception 0 is reserved for normal path of setjmp()!!! |
| 49 | */ |
| 50 | for (i=1; i<PJ_MAX_EXCEPTION_ID; ++i) { |
| 51 | if (exception_id_names[i] == NULL) { |
| 52 | exception_id_names[i] = name; |
| 53 | *id = i; |
| 54 | pj_leave_critical_section(); |
| 55 | return PJ_SUCCESS; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | pj_leave_critical_section(); |
| 60 | return PJ_ETOOMANY; |
| 61 | } |
| 62 | |
| 63 | PJ_DEF(pj_status_t) pj_exception_id_free( pj_exception_id_t id ) |
| 64 | { |
| 65 | /* |
| 66 | * Start from 1 (not 0)!!! |
| 67 | * Exception 0 is reserved for normal path of setjmp()!!! |
| 68 | */ |
| 69 | PJ_ASSERT_RETURN(id>0 && id<PJ_MAX_EXCEPTION_ID, PJ_EINVAL); |
| 70 | |
| 71 | pj_enter_critical_section(); |
| 72 | exception_id_names[id] = NULL; |
| 73 | pj_leave_critical_section(); |
| 74 | |
| 75 | return PJ_SUCCESS; |
| 76 | |
| 77 | } |
| 78 | |
| 79 | PJ_DEF(const char*) pj_exception_id_name(pj_exception_id_t id) |
| 80 | { |
| 81 | /* |
| 82 | * Start from 1 (not 0)!!! |
| 83 | * Exception 0 is reserved for normal path of setjmp()!!! |
| 84 | */ |
| 85 | PJ_ASSERT_RETURN(id>0 && id<PJ_MAX_EXCEPTION_ID, "<Invalid ID>"); |
| 86 | |
| 87 | if (exception_id_names[id] == NULL) |
| 88 | return "<Unallocated ID>"; |
| 89 | |
| 90 | return exception_id_names[id]; |
| 91 | } |
| 92 | |
| 93 | #else /* PJ_HAS_EXCEPTION_NAMES */ |
| 94 | PJ_DEF(pj_status_t) pj_exception_id_alloc( const char *name, |
| 95 | pj_exception_id_t *id) |
| 96 | { |
| 97 | PJ_ASSERT_RETURN(last_exception_id < PJ_MAX_EXCEPTION_ID-1, PJ_ETOOMANY); |
| 98 | |
| 99 | *id = last_exception_id++; |
| 100 | return PJ_SUCCESS; |
| 101 | } |
| 102 | |
| 103 | PJ_DEF(pj_status_t) pj_exception_id_free( pj_exception_id_t id ) |
| 104 | { |
| 105 | return PJ_SUCCESS; |
| 106 | } |
| 107 | |
| 108 | PJ_DEF(const char*) pj_exception_id_name(pj_exception_id_t id) |
| 109 | { |
| 110 | return ""; |
| 111 | } |
| 112 | |
| 113 | #endif /* PJ_HAS_EXCEPTION_NAMES */ |
| 114 | |
| 115 | |
| 116 | |