1 /* Invalid parameter handler for MSVC runtime libraries.
2 Copyright (C) 2011-2022 Free Software Foundation, Inc.
4 This file is free software: you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2.1 of the
7 License, or (at your option) any later version.
9 This file 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.
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
20 #include "msvc-inval.h"
22 #if HAVE_MSVC_INVALID_PARAMETER_HANDLER \
23 && !(MSVC_INVALID_PARAMETER_HANDLING == SANE_LIBRARY_HANDLING)
25 /* Get _invalid_parameter_handler type and _set_invalid_parameter_handler
29 # if MSVC_INVALID_PARAMETER_HANDLING == DEFAULT_HANDLING
32 gl_msvc_invalid_parameter_handler (const wchar_t *expression
,
33 const wchar_t *function
,
42 /* Get declarations of the native Windows API functions. */
43 # define WIN32_LEAN_AND_MEAN
49 gl_msvc_invalid_parameter_handler (const wchar_t *expression
,
50 const wchar_t *function
,
55 RaiseException (STATUS_GNULIB_INVALID_PARAMETER
, 0, 0, NULL
);
60 /* An index to thread-local storage. */
61 static DWORD tls_index
;
62 static int tls_initialized
/* = 0 */;
64 /* Used as a fallback only. */
65 static struct gl_msvc_inval_per_thread not_per_thread
;
67 struct gl_msvc_inval_per_thread
*
68 gl_msvc_inval_current (void)
72 tls_index
= TlsAlloc ();
75 if (tls_index
== TLS_OUT_OF_INDEXES
)
76 /* TlsAlloc had failed. */
77 return ¬_per_thread
;
80 struct gl_msvc_inval_per_thread
*pointer
=
81 (struct gl_msvc_inval_per_thread
*) TlsGetValue (tls_index
);
84 /* First call. Allocate a new 'struct gl_msvc_inval_per_thread'. */
86 (struct gl_msvc_inval_per_thread
*)
87 malloc (sizeof (struct gl_msvc_inval_per_thread
));
89 /* Could not allocate memory. Use the global storage. */
90 pointer
= ¬_per_thread
;
91 TlsSetValue (tls_index
, pointer
);
98 gl_msvc_invalid_parameter_handler (const wchar_t *expression
,
99 const wchar_t *function
,
104 struct gl_msvc_inval_per_thread
*current
= gl_msvc_inval_current ();
105 if (current
->restart_valid
)
106 longjmp (current
->restart
, 1);
108 /* An invalid parameter notification from outside the gnulib code.
109 Give the caller a chance to intervene. */
110 RaiseException (STATUS_GNULIB_INVALID_PARAMETER
, 0, 0, NULL
);
117 static int gl_msvc_inval_initialized
/* = 0 */;
120 gl_msvc_inval_ensure_handler (void)
122 if (gl_msvc_inval_initialized
== 0)
124 _set_invalid_parameter_handler (gl_msvc_invalid_parameter_handler
);
125 gl_msvc_inval_initialized
= 1;