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