except.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
  3. * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
  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. static long thread_local_id = -1;
  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. #if !defined(PJ_EXCEPTION_USE_WIN32_SEH) || PJ_EXCEPTION_USE_WIN32_SEH==0
  36. PJ_DEF(void) pj_throw_exception_(int exception_id)
  37. {
  38. struct pj_exception_state_t *handler;
  39. handler = (struct pj_exception_state_t*)
  40. pj_thread_local_get(thread_local_id);
  41. if (handler == NULL) {
  42. PJ_LOG(1,("except.c", "!!!FATAL: unhandled exception %s!\n",
  43. pj_exception_id_name(exception_id)));
  44. pj_assert(handler != NULL);
  45. /* This will crash the system! */
  46. }
  47. pj_pop_exception_handler_(handler);
  48. pj_longjmp(handler->state, exception_id);
  49. }
  50. static void exception_cleanup(void)
  51. {
  52. if (thread_local_id != -1) {
  53. pj_thread_local_free(thread_local_id);
  54. thread_local_id = -1;
  55. }
  56. #if defined(PJ_HAS_EXCEPTION_NAMES) && PJ_HAS_EXCEPTION_NAMES != 0
  57. {
  58. unsigned i;
  59. for (i=0; i<PJ_MAX_EXCEPTION_ID; ++i)
  60. exception_id_names[i] = NULL;
  61. }
  62. #else
  63. last_exception_id = 1;
  64. #endif
  65. }
  66. PJ_DEF(void) pj_push_exception_handler_(struct pj_exception_state_t *rec)
  67. {
  68. struct pj_exception_state_t *parent_handler = NULL;
  69. if (thread_local_id == -1) {
  70. pj_thread_local_alloc(&thread_local_id);
  71. pj_assert(thread_local_id != -1);
  72. pj_atexit(&exception_cleanup);
  73. }
  74. parent_handler = (struct pj_exception_state_t *)
  75. pj_thread_local_get(thread_local_id);
  76. rec->prev = parent_handler;
  77. pj_thread_local_set(thread_local_id, rec);
  78. }
  79. PJ_DEF(void) pj_pop_exception_handler_(struct pj_exception_state_t *rec)
  80. {
  81. struct pj_exception_state_t *handler;
  82. handler = (struct pj_exception_state_t *)
  83. pj_thread_local_get(thread_local_id);
  84. if (handler && handler==rec) {
  85. pj_thread_local_set(thread_local_id, handler->prev);
  86. }
  87. }
  88. #endif
  89. #if defined(PJ_HAS_EXCEPTION_NAMES) && PJ_HAS_EXCEPTION_NAMES != 0
  90. PJ_DEF(pj_status_t) pj_exception_id_alloc( const char *name,
  91. pj_exception_id_t *id)
  92. {
  93. unsigned i;
  94. pj_enter_critical_section();
  95. /*
  96. * Start from 1 (not 0)!!!
  97. * Exception 0 is reserved for normal path of setjmp()!!!
  98. */
  99. for (i=1; i<PJ_MAX_EXCEPTION_ID; ++i) {
  100. if (exception_id_names[i] == NULL) {
  101. exception_id_names[i] = name;
  102. *id = i;
  103. pj_leave_critical_section();
  104. return PJ_SUCCESS;
  105. }
  106. }
  107. pj_leave_critical_section();
  108. return PJ_ETOOMANY;
  109. }
  110. PJ_DEF(pj_status_t) pj_exception_id_free( pj_exception_id_t id )
  111. {
  112. /*
  113. * Start from 1 (not 0)!!!
  114. * Exception 0 is reserved for normal path of setjmp()!!!
  115. */
  116. PJ_ASSERT_RETURN(id>0 && id<PJ_MAX_EXCEPTION_ID, PJ_EINVAL);
  117. pj_enter_critical_section();
  118. exception_id_names[id] = NULL;
  119. pj_leave_critical_section();
  120. return PJ_SUCCESS;
  121. }
  122. PJ_DEF(const char*) pj_exception_id_name(pj_exception_id_t id)
  123. {
  124. static char unknown_name[32];
  125. /*
  126. * Start from 1 (not 0)!!!
  127. * Exception 0 is reserved for normal path of setjmp()!!!
  128. */
  129. PJ_ASSERT_RETURN(id>0 && id<PJ_MAX_EXCEPTION_ID, "<Invalid ID>");
  130. if (exception_id_names[id] == NULL) {
  131. pj_ansi_snprintf(unknown_name, sizeof(unknown_name),
  132. "exception %d", id);
  133. return unknown_name;
  134. }
  135. return exception_id_names[id];
  136. }
  137. #else /* PJ_HAS_EXCEPTION_NAMES */
  138. PJ_DEF(pj_status_t) pj_exception_id_alloc( const char *name,
  139. pj_exception_id_t *id)
  140. {
  141. PJ_ASSERT_RETURN(last_exception_id < PJ_MAX_EXCEPTION_ID-1, PJ_ETOOMANY);
  142. *id = last_exception_id++;
  143. return PJ_SUCCESS;
  144. }
  145. PJ_DEF(pj_status_t) pj_exception_id_free( pj_exception_id_t id )
  146. {
  147. return PJ_SUCCESS;
  148. }
  149. PJ_DEF(const char*) pj_exception_id_name(pj_exception_id_t id)
  150. {
  151. return "";
  152. }
  153. #endif /* PJ_HAS_EXCEPTION_NAMES */