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