msvc-inval.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /* Invalid parameter handler for MSVC runtime libraries.
  2. Copyright (C) 2011-2012 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as published by
  5. the Free Software Foundation; either version 2, or (at your option)
  6. any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License along
  12. with this program; if not, write to the Free Software Foundation,
  13. Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
  14. #include <config.h>
  15. /* Specification. */
  16. #include "msvc-inval.h"
  17. #if HAVE_MSVC_INVALID_PARAMETER_HANDLER \
  18. && !(MSVC_INVALID_PARAMETER_HANDLING == SANE_LIBRARY_HANDLING)
  19. /* Get _invalid_parameter_handler type and _set_invalid_parameter_handler
  20. declaration. */
  21. # include <stdlib.h>
  22. # if MSVC_INVALID_PARAMETER_HANDLING == DEFAULT_HANDLING
  23. static void cdecl
  24. gl_msvc_invalid_parameter_handler (const wchar_t *expression,
  25. const wchar_t *function,
  26. const wchar_t *file,
  27. unsigned int line,
  28. uintptr_t dummy)
  29. {
  30. }
  31. # else
  32. /* Get declarations of the native Windows API functions. */
  33. # define WIN32_LEAN_AND_MEAN
  34. # include <windows.h>
  35. # if defined _MSC_VER
  36. static void cdecl
  37. gl_msvc_invalid_parameter_handler (const wchar_t *expression,
  38. const wchar_t *function,
  39. const wchar_t *file,
  40. unsigned int line,
  41. uintptr_t dummy)
  42. {
  43. RaiseException (STATUS_GNULIB_INVALID_PARAMETER, 0, 0, NULL);
  44. }
  45. # else
  46. /* An index to thread-local storage. */
  47. static DWORD tls_index;
  48. static int tls_initialized /* = 0 */;
  49. /* Used as a fallback only. */
  50. static struct gl_msvc_inval_per_thread not_per_thread;
  51. struct gl_msvc_inval_per_thread *
  52. gl_msvc_inval_current (void)
  53. {
  54. if (!tls_initialized)
  55. {
  56. tls_index = TlsAlloc ();
  57. tls_initialized = 1;
  58. }
  59. if (tls_index == TLS_OUT_OF_INDEXES)
  60. /* TlsAlloc had failed. */
  61. return &not_per_thread;
  62. else
  63. {
  64. struct gl_msvc_inval_per_thread *pointer =
  65. (struct gl_msvc_inval_per_thread *) TlsGetValue (tls_index);
  66. if (pointer == NULL)
  67. {
  68. /* First call. Allocate a new 'struct gl_msvc_inval_per_thread'. */
  69. pointer =
  70. (struct gl_msvc_inval_per_thread *)
  71. malloc (sizeof (struct gl_msvc_inval_per_thread));
  72. if (pointer == NULL)
  73. /* Could not allocate memory. Use the global storage. */
  74. pointer = &not_per_thread;
  75. TlsSetValue (tls_index, pointer);
  76. }
  77. return pointer;
  78. }
  79. }
  80. static void cdecl
  81. gl_msvc_invalid_parameter_handler (const wchar_t *expression,
  82. const wchar_t *function,
  83. const wchar_t *file,
  84. unsigned int line,
  85. uintptr_t dummy)
  86. {
  87. struct gl_msvc_inval_per_thread *current = gl_msvc_inval_current ();
  88. if (current->restart_valid)
  89. longjmp (current->restart, 1);
  90. else
  91. /* An invalid parameter notification from outside the gnulib code.
  92. Give the caller a chance to intervene. */
  93. RaiseException (STATUS_GNULIB_INVALID_PARAMETER, 0, 0, NULL);
  94. }
  95. # endif
  96. # endif
  97. static int gl_msvc_inval_initialized /* = 0 */;
  98. void
  99. gl_msvc_inval_ensure_handler (void)
  100. {
  101. if (gl_msvc_inval_initialized == 0)
  102. {
  103. _set_invalid_parameter_handler (gl_msvc_invalid_parameter_handler);
  104. gl_msvc_inval_initialized = 1;
  105. }
  106. }
  107. #endif