po: Update Finnish translation.
[wine.git] / dlls / msvcp90 / misc.c
blob103eca6f4a31494ad587327d313c1f105d97e6a7
1 /*
2 * Copyright 2010 Piotr Caban for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library 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 GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include <stdarg.h>
20 #include <limits.h>
21 #include <errno.h>
23 #include "msvcp90.h"
25 #include "wine/debug.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(msvcp);
29 #if _MSVCP_VER >= 110
30 /* error strings generated with glibc strerror */
31 static const char str_EPERM[] = "operation not permitted";
32 static const char str_ENOENT[] = "no such file or directory";
33 static const char str_ESRCH[] = "no such process";
34 static const char str_EINTR[] = "interrupted system call";
35 static const char str_EIO[] = "input/output error";
36 static const char str_ENXIO[] = "no such device or address";
37 static const char str_E2BIG[] = "argument list too long";
38 static const char str_ENOEXEC[] = "exec format error";
39 static const char str_EBADF[] = "bad file descriptor";
40 static const char str_ECHILD[] = "no child processes";
41 static const char str_EAGAIN[] = "resource temporarily unavailable";
42 static const char str_ENOMEM[] = "cannot allocate memory";
43 static const char str_EACCES[] = "permission denied";
44 static const char str_EFAULT[] = "bad address";
45 static const char str_EBUSY[] = "device or resource busy";
46 static const char str_EEXIST[] = "file exists";
47 static const char str_EXDEV[] = "invalid cross-device link";
48 static const char str_ENODEV[] = "no such device";
49 static const char str_ENOTDIR[] = "not a directory";
50 static const char str_EISDIR[] = "is a directory";
51 static const char str_EINVAL[] = "invalid argument";
52 static const char str_ENFILE[] = "too many open files in system";
53 static const char str_EMFILE[] = "too many open files";
54 static const char str_ENOTTY[] = "inappropriate ioctl for device";
55 static const char str_EFBIG[] = "file too large";
56 static const char str_ENOSPC[] = "no space left on device";
57 static const char str_ESPIPE[] = "illegal seek";
58 static const char str_EROFS[] = "read-only file system";
59 static const char str_EMLINK[] = "too many links";
60 static const char str_EPIPE[] = "broken pipe";
61 static const char str_EDOM[] = "numerical argument out of domain";
62 static const char str_ERANGE[] = "numerical result out of range";
63 static const char str_EDEADLK[] = "resource deadlock avoided";
64 static const char str_ENAMETOOLONG[] = "file name too long";
65 static const char str_ENOLCK[] = "no locks available";
66 static const char str_ENOSYS[] = "function not implemented";
67 static const char str_ENOTEMPTY[] = "directory not empty";
68 static const char str_EILSEQ[] = "invalid or incomplete multibyte or wide character";
69 static const char str_EADDRINUSE[] = "address already in use";
70 static const char str_EADDRNOTAVAIL[] = "cannot assign requested address";
71 static const char str_EAFNOSUPPORT[] = "address family not supported by protocol";
72 static const char str_EALREADY[] = "operation already in progress";
73 static const char str_EBADMSG[] = "not a data message";
74 static const char str_ECANCELED[] = "operation Canceled";
75 static const char str_ECONNABORTED[] = "software caused connection abort";
76 static const char str_ECONNREFUSED[] = "connection refused";
77 static const char str_ECONNRESET[] = "connection reset by peer";
78 static const char str_EDESTADDRREQ[] = "destination address required";
79 static const char str_EHOSTUNREACH[] = "no route to host";
80 static const char str_EIDRM[] = "identifier removed";
81 static const char str_EINPROGRESS[] = "operation now in progress";
82 static const char str_EISCONN[] = "transport endpoint is already connected";
83 static const char str_ELOOP[] = "too many symbolic links encountered";
84 static const char str_EMSGSIZE[] = "message too long";
85 static const char str_ENETDOWN[] = "network is down";
86 static const char str_ENETRESET[] = "network dropped connection because of reset";
87 static const char str_ENETUNREACH[] = "network is unreachable";
88 static const char str_ENOBUFS[] = "no buffer space available";
89 static const char str_ENODATA[] = "no data available";
90 static const char str_ENOLINK[] = "link has been severed";
91 static const char str_ENOMSG[] = "no message of desired type";
92 static const char str_ENOPROTOOPT[] = "protocol not available";
93 static const char str_ENOSR[] = "out of streams resources";
94 static const char str_ENOSTR[] = "device not a stream";
95 static const char str_ENOTCONN[] = "transport endpoint is not connected";
96 static const char str_ENOTRECOVERABLE[] = "state not recoverable";
97 static const char str_ENOTSOCK[] = "socket operation on non-socket";
98 static const char str_ENOTSUP[] = "not supported";
99 static const char str_EOPNOTSUPP[] = "operation not supported on transport endpoint";
100 static const char str_EOVERFLOW[] = "value too large for defined data type";
101 static const char str_EOWNERDEAD[] = "owner died";
102 static const char str_EPROTO[] = "protocol error";
103 static const char str_EPROTONOSUPPORT[] = "protocol not supported";
104 static const char str_EPROTOTYPE[] = "protocol wrong type for socket";
105 static const char str_ETIME[] = "timer expired";
106 static const char str_ETIMEDOUT[] = "connection timed out";
107 static const char str_ETXTBSY[] = "text file busy";
108 static const char str_EWOULDBLOCK[] = "operation would block";
110 static const struct {
111 int err;
112 const char *str;
113 } syserror_map[] =
115 {EPERM, str_EPERM},
116 {ENOENT, str_ENOENT},
117 {ESRCH, str_ESRCH},
118 {EINTR, str_EINTR},
119 {EIO, str_EIO},
120 {ENXIO, str_ENXIO},
121 {E2BIG, str_E2BIG},
122 {ENOEXEC, str_ENOEXEC},
123 {EBADF, str_EBADF},
124 {ECHILD, str_ECHILD},
125 {EAGAIN, str_EAGAIN},
126 {ENOMEM, str_ENOMEM},
127 {EACCES, str_EACCES},
128 {EFAULT, str_EFAULT},
129 {EBUSY, str_EBUSY},
130 {EEXIST, str_EEXIST},
131 {EXDEV, str_EXDEV},
132 {ENODEV, str_ENODEV},
133 {ENOTDIR, str_ENOTDIR},
134 {EISDIR, str_EISDIR},
135 {EINVAL, str_EINVAL},
136 {ENFILE, str_ENFILE},
137 {EMFILE, str_EMFILE},
138 {ENOTTY, str_ENOTTY},
139 {EFBIG, str_EFBIG},
140 {ENOSPC, str_ENOSPC},
141 {ESPIPE, str_ESPIPE},
142 {EROFS, str_EROFS},
143 {EMLINK, str_EMLINK},
144 {EPIPE, str_EPIPE},
145 {EDOM, str_EDOM},
146 {ERANGE, str_ERANGE},
147 {EDEADLK, str_EDEADLK},
148 {ENAMETOOLONG, str_ENAMETOOLONG},
149 {ENOLCK, str_ENOLCK},
150 {ENOSYS, str_ENOSYS},
151 {ENOTEMPTY, str_ENOTEMPTY},
152 {EILSEQ, str_EILSEQ},
153 {EADDRINUSE, str_EADDRINUSE},
154 {EADDRNOTAVAIL, str_EADDRNOTAVAIL},
155 {EAFNOSUPPORT, str_EAFNOSUPPORT},
156 {EALREADY, str_EALREADY},
157 {EBADMSG, str_EBADMSG},
158 {ECANCELED, str_ECANCELED},
159 {ECONNABORTED, str_ECONNABORTED},
160 {ECONNREFUSED, str_ECONNREFUSED},
161 {ECONNRESET, str_ECONNRESET},
162 {EDESTADDRREQ, str_EDESTADDRREQ},
163 {EHOSTUNREACH, str_EHOSTUNREACH},
164 {EIDRM, str_EIDRM},
165 {EINPROGRESS, str_EINPROGRESS},
166 {EISCONN, str_EISCONN},
167 {ELOOP, str_ELOOP},
168 {EMSGSIZE, str_EMSGSIZE},
169 {ENETDOWN, str_ENETDOWN},
170 {ENETRESET, str_ENETRESET},
171 {ENETUNREACH, str_ENETUNREACH},
172 {ENOBUFS, str_ENOBUFS},
173 {ENODATA, str_ENODATA},
174 {ENOLINK, str_ENOLINK},
175 {ENOMSG, str_ENOMSG},
176 {ENOPROTOOPT, str_ENOPROTOOPT},
177 {ENOSR, str_ENOSR},
178 {ENOSTR, str_ENOSTR},
179 {ENOTCONN, str_ENOTCONN},
180 {ENOTRECOVERABLE, str_ENOTRECOVERABLE},
181 {ENOTSOCK, str_ENOTSOCK},
182 {ENOTSUP, str_ENOTSUP},
183 {EOPNOTSUPP, str_EOPNOTSUPP},
184 {EOVERFLOW, str_EOVERFLOW},
185 {EOWNERDEAD, str_EOWNERDEAD},
186 {EPROTO, str_EPROTO},
187 {EPROTONOSUPPORT, str_EPROTONOSUPPORT},
188 {EPROTOTYPE, str_EPROTOTYPE},
189 {ETIME, str_ETIME},
190 {ETIMEDOUT, str_ETIMEDOUT},
191 {ETXTBSY, str_ETXTBSY},
192 {EWOULDBLOCK, str_EWOULDBLOCK},
194 #endif
196 #if _MSVCP_VER >= 140
197 static const struct {
198 int winerr;
199 int doserr;
200 } winerror_map[] =
202 {ERROR_INVALID_FUNCTION, ENOSYS}, {ERROR_FILE_NOT_FOUND, ENOENT},
203 {ERROR_PATH_NOT_FOUND, ENOENT}, {ERROR_TOO_MANY_OPEN_FILES, EMFILE},
204 {ERROR_ACCESS_DENIED, EACCES}, {ERROR_INVALID_HANDLE, EINVAL},
205 {ERROR_NOT_ENOUGH_MEMORY, ENOMEM}, {ERROR_INVALID_ACCESS, EACCES},
206 {ERROR_OUTOFMEMORY, ENOMEM}, {ERROR_INVALID_DRIVE, ENODEV},
207 {ERROR_CURRENT_DIRECTORY, EACCES}, {ERROR_NOT_SAME_DEVICE, EXDEV},
208 {ERROR_WRITE_PROTECT, EACCES}, {ERROR_BAD_UNIT, ENODEV},
209 {ERROR_NOT_READY, EAGAIN}, {ERROR_SEEK, EIO}, {ERROR_WRITE_FAULT, EIO},
210 {ERROR_READ_FAULT, EIO}, {ERROR_SHARING_VIOLATION, EACCES},
211 {ERROR_LOCK_VIOLATION, ENOLCK}, {ERROR_HANDLE_DISK_FULL, ENOSPC},
212 {ERROR_NOT_SUPPORTED, ENOTSUP}, {ERROR_DEV_NOT_EXIST, ENODEV},
213 {ERROR_FILE_EXISTS, EEXIST}, {ERROR_CANNOT_MAKE, EACCES},
214 {ERROR_INVALID_PARAMETER, EINVAL}, {ERROR_OPEN_FAILED, EIO},
215 {ERROR_BUFFER_OVERFLOW, ENAMETOOLONG}, {ERROR_DISK_FULL, ENOSPC},
216 {ERROR_INVALID_NAME, ENOENT}, {ERROR_NEGATIVE_SEEK, EINVAL},
217 {ERROR_BUSY_DRIVE, EBUSY}, {ERROR_DIR_NOT_EMPTY, ENOTEMPTY},
218 {ERROR_BUSY, EBUSY}, {ERROR_ALREADY_EXISTS, EEXIST},
219 {ERROR_LOCKED, ENOLCK}, {ERROR_DIRECTORY, EINVAL},
220 {ERROR_OPERATION_ABORTED, ECANCELED}, {ERROR_NOACCESS, EACCES},
221 {ERROR_CANTOPEN, EIO}, {ERROR_CANTREAD, EIO}, {ERROR_CANTWRITE, EIO},
222 {ERROR_RETRY, EAGAIN}, {ERROR_OPEN_FILES, EBUSY},
223 {ERROR_DEVICE_IN_USE, EBUSY}, {ERROR_REPARSE_TAG_INVALID, EINVAL},
224 {WSAEINTR, EINTR}, {WSAEBADF, EBADF}, {WSAEACCES, EACCES},
225 {WSAEFAULT, EFAULT}, {WSAEINVAL, EINVAL}, {WSAEMFILE, EMFILE},
226 {WSAEWOULDBLOCK, EWOULDBLOCK}, {WSAEINPROGRESS, EINPROGRESS},
227 {WSAEALREADY, EALREADY}, {WSAENOTSOCK, ENOTSOCK},
228 {WSAEDESTADDRREQ, EDESTADDRREQ}, {WSAEMSGSIZE, EMSGSIZE},
229 {WSAEPROTOTYPE, EPROTOTYPE}, {WSAENOPROTOOPT, ENOPROTOOPT},
230 {WSAEPROTONOSUPPORT, EPROTONOSUPPORT}, {WSAEOPNOTSUPP, EOPNOTSUPP},
231 {WSAEAFNOSUPPORT, EAFNOSUPPORT}, {WSAEADDRINUSE, EADDRINUSE},
232 {WSAEADDRNOTAVAIL, EADDRNOTAVAIL}, {WSAENETDOWN, ENETDOWN},
233 {WSAENETUNREACH, ENETUNREACH}, {WSAENETRESET, ENETRESET},
234 {WSAECONNABORTED, ECONNABORTED}, {WSAECONNRESET, ECONNRESET},
235 {WSAENOBUFS, ENOBUFS}, {WSAEISCONN, EISCONN}, {WSAENOTCONN, ENOTCONN},
236 {WSAETIMEDOUT, ETIMEDOUT}, {WSAECONNREFUSED, ECONNREFUSED},
237 {WSAENAMETOOLONG, ENAMETOOLONG}, {WSAEHOSTUNREACH, EHOSTUNREACH}
239 #endif
241 struct __Container_proxy;
243 typedef struct {
244 struct __Container_proxy *proxy;
245 } _Container_base12;
247 typedef struct __Iterator_base12 {
248 struct __Container_proxy *proxy;
249 struct __Iterator_base12 *next;
250 } _Iterator_base12;
252 typedef struct __Container_proxy {
253 const _Container_base12 *cont;
254 _Iterator_base12 *head;
255 } _Container_proxy;
257 /* ??0_Mutex@std@@QAE@XZ */
258 /* ??0_Mutex@std@@QEAA@XZ */
259 DEFINE_THISCALL_WRAPPER(mutex_ctor, 4)
260 mutex* __thiscall mutex_ctor(mutex *this)
262 CRITICAL_SECTION *cs = operator_new(sizeof(*cs));
263 InitializeCriticalSection(cs);
264 cs->DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": _Mutex critical section");
265 this->mutex = cs;
266 return this;
269 /* ??1_Mutex@std@@QAE@XZ */
270 /* ??1_Mutex@std@@QEAA@XZ */
271 DEFINE_THISCALL_WRAPPER(mutex_dtor, 4)
272 void __thiscall mutex_dtor(mutex *this)
274 ((CRITICAL_SECTION*)this->mutex)->DebugInfo->Spare[0] = 0;
275 DeleteCriticalSection(this->mutex);
276 operator_delete(this->mutex);
279 /* ?_Lock@_Mutex@std@@QAEXXZ */
280 /* ?_Lock@_Mutex@std@@QEAAXXZ */
281 DEFINE_THISCALL_WRAPPER(mutex_lock, 4)
282 void __thiscall mutex_lock(mutex *this)
284 EnterCriticalSection(this->mutex);
287 /* ?_Unlock@_Mutex@std@@QAEXXZ */
288 /* ?_Unlock@_Mutex@std@@QEAAXXZ */
289 DEFINE_THISCALL_WRAPPER(mutex_unlock, 4)
290 void __thiscall mutex_unlock(mutex *this)
292 LeaveCriticalSection(this->mutex);
295 /* ?_Mutex_Lock@_Mutex@std@@CAXPAV12@@Z */
296 /* ?_Mutex_Lock@_Mutex@std@@CAXPEAV12@@Z */
297 void CDECL mutex_mutex_lock(mutex *m)
299 mutex_lock(m);
302 /* ?_Mutex_Unlock@_Mutex@std@@CAXPAV12@@Z */
303 /* ?_Mutex_Unlock@_Mutex@std@@CAXPEAV12@@Z */
304 void CDECL mutex_mutex_unlock(mutex *m)
306 mutex_unlock(m);
309 /* ?_Mutex_ctor@_Mutex@std@@CAXPAV12@@Z */
310 /* ?_Mutex_ctor@_Mutex@std@@CAXPEAV12@@Z */
311 void CDECL mutex_mutex_ctor(mutex *m)
313 mutex_ctor(m);
316 /* ?_Mutex_dtor@_Mutex@std@@CAXPAV12@@Z */
317 /* ?_Mutex_dtor@_Mutex@std@@CAXPEAV12@@Z */
318 void CDECL mutex_mutex_dtor(mutex *m)
320 mutex_dtor(m);
323 static CRITICAL_SECTION lockit_cs[_MAX_LOCK];
325 static LONG init_locks;
326 static CRITICAL_SECTION init_locks_cs;
327 static CRITICAL_SECTION_DEBUG init_locks_cs_debug =
329 0, 0, &init_locks_cs,
330 { &init_locks_cs_debug.ProcessLocksList, &init_locks_cs_debug.ProcessLocksList },
331 0, 0, { (DWORD_PTR)(__FILE__ ": init_locks_cs") }
333 static CRITICAL_SECTION init_locks_cs = { &init_locks_cs_debug, -1, 0, 0, 0, 0 };
335 /* ?_Init_locks_ctor@_Init_locks@std@@CAXPAV12@@Z */
336 /* ?_Init_locks_ctor@_Init_locks@std@@CAXPEAV12@@Z */
337 void __cdecl _Init_locks__Init_locks_ctor(_Init_locks *this)
339 int i;
341 EnterCriticalSection(&init_locks_cs);
342 if (!init_locks)
344 for(i=0; i<_MAX_LOCK; i++)
346 InitializeCriticalSection(&lockit_cs[i]);
347 lockit_cs[i].DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": _Lockit critical section");
350 init_locks++;
351 LeaveCriticalSection(&init_locks_cs);
354 /* ??0_Init_locks@std@@QAE@XZ */
355 /* ??0_Init_locks@std@@QEAA@XZ */
356 DEFINE_THISCALL_WRAPPER(_Init_locks_ctor, 4)
357 _Init_locks* __thiscall _Init_locks_ctor(_Init_locks *this)
359 _Init_locks__Init_locks_ctor(this);
360 return this;
363 /* ?_Init_locks_dtor@_Init_locks@std@@CAXPAV12@@Z */
364 /* ?_Init_locks_dtor@_Init_locks@std@@CAXPEAV12@@Z */
365 void __cdecl _Init_locks__Init_locks_dtor(_Init_locks *this)
367 int i;
369 EnterCriticalSection(&init_locks_cs);
370 init_locks--;
371 if (!init_locks)
373 for(i=0; i<_MAX_LOCK; i++)
375 lockit_cs[i].DebugInfo->Spare[0] = 0;
376 DeleteCriticalSection(&lockit_cs[i]);
379 LeaveCriticalSection(&init_locks_cs);
382 /* ??1_Init_locks@std@@QAE@XZ */
383 /* ??1_Init_locks@std@@QEAA@XZ */
384 DEFINE_THISCALL_WRAPPER(_Init_locks_dtor, 4)
385 void __thiscall _Init_locks_dtor(_Init_locks *this)
387 _Init_locks__Init_locks_dtor(this);
390 #if _MSVCP_VER >= 70
391 static inline int get_locktype( _Lockit *lockit ) { return lockit->locktype; }
392 static inline void set_locktype( _Lockit *lockit, int type ) { lockit->locktype = type; }
393 #else
394 static inline int get_locktype( _Lockit *lockit ) { return 0; }
395 static inline void set_locktype( _Lockit *lockit, int type ) { }
396 #endif
398 /* ?_Lockit_ctor@_Lockit@std@@SAXH@Z */
399 void __cdecl _Lockit__Lockit_ctor_lock(int locktype)
401 EnterCriticalSection(&lockit_cs[locktype]);
404 /* ?_Lockit_ctor@_Lockit@std@@CAXPAV12@H@Z */
405 /* ?_Lockit_ctor@_Lockit@std@@CAXPEAV12@H@Z */
406 void __cdecl _Lockit__Lockit_ctor_locktype(_Lockit *lockit, int locktype)
408 set_locktype( lockit, locktype );
409 _Lockit__Lockit_ctor_lock(locktype);
412 /* ?_Lockit_ctor@_Lockit@std@@CAXPAV12@@Z */
413 /* ?_Lockit_ctor@_Lockit@std@@CAXPEAV12@@Z */
414 void __cdecl _Lockit__Lockit_ctor(_Lockit *lockit)
416 _Lockit__Lockit_ctor_locktype(lockit, 0);
419 /* ??0_Lockit@std@@QAE@H@Z */
420 /* ??0_Lockit@std@@QEAA@H@Z */
421 DEFINE_THISCALL_WRAPPER(_Lockit_ctor_locktype, 8)
422 _Lockit* __thiscall _Lockit_ctor_locktype(_Lockit *this, int locktype)
424 _Lockit__Lockit_ctor_locktype(this, locktype);
425 return this;
428 /* ??0_Lockit@std@@QAE@XZ */
429 /* ??0_Lockit@std@@QEAA@XZ */
430 DEFINE_THISCALL_WRAPPER(_Lockit_ctor, 4)
431 _Lockit* __thiscall _Lockit_ctor(_Lockit *this)
433 _Lockit__Lockit_ctor_locktype(this, 0);
434 return this;
437 /* ?_Lockit_dtor@_Lockit@std@@SAXH@Z */
438 void __cdecl _Lockit__Lockit_dtor_unlock(int locktype)
440 LeaveCriticalSection(&lockit_cs[locktype]);
443 /* ?_Lockit_dtor@_Lockit@std@@CAXPAV12@@Z */
444 /* ?_Lockit_dtor@_Lockit@std@@CAXPEAV12@@Z */
445 void __cdecl _Lockit__Lockit_dtor(_Lockit *lockit)
447 _Lockit__Lockit_dtor_unlock(get_locktype( lockit ));
450 /* ??1_Lockit@std@@QAE@XZ */
451 /* ??1_Lockit@std@@QEAA@XZ */
452 DEFINE_THISCALL_WRAPPER(_Lockit_dtor, 4)
453 void __thiscall _Lockit_dtor(_Lockit *this)
455 _Lockit__Lockit_dtor(this);
458 /* wctype */
459 unsigned short __cdecl wctype(const char *property)
461 static const struct {
462 const char *name;
463 unsigned short mask;
464 } properties[] = {
465 { "alnum", _DIGIT|_ALPHA },
466 { "alpha", _ALPHA },
467 { "cntrl", _CONTROL },
468 { "digit", _DIGIT },
469 { "graph", _DIGIT|_PUNCT|_ALPHA },
470 { "lower", _LOWER },
471 { "print", _DIGIT|_PUNCT|_BLANK|_ALPHA },
472 { "punct", _PUNCT },
473 { "space", _SPACE },
474 { "upper", _UPPER },
475 { "xdigit", _HEX }
477 unsigned int i;
479 for(i = 0; i < ARRAY_SIZE(properties); i++)
480 if(!strcmp(property, properties[i].name))
481 return properties[i].mask;
483 return 0;
486 typedef void (__cdecl *MSVCP_new_handler_func)(void);
487 static MSVCP_new_handler_func MSVCP_new_handler;
488 static int __cdecl new_handler_wrapper(size_t unused)
490 MSVCP_new_handler();
491 return 1;
494 /* ?set_new_handler@std@@YAP6AXXZP6AXXZ@Z */
495 MSVCP_new_handler_func __cdecl set_new_handler(MSVCP_new_handler_func new_handler)
497 MSVCP_new_handler_func old_handler = MSVCP_new_handler;
499 TRACE("%p\n", new_handler);
501 MSVCP_new_handler = new_handler;
502 MSVCRT_set_new_handler(new_handler ? new_handler_wrapper : NULL);
503 return old_handler;
506 /* ?set_new_handler@std@@YAP6AXXZH@Z */
507 MSVCP_new_handler_func __cdecl set_new_handler_reset(int unused)
509 return set_new_handler(NULL);
512 /* _Container_base0 is used by apps compiled without iterator checking
513 * (i.e. with _ITERATOR_DEBUG_LEVEL=0 ).
514 * It provides empty versions of methods used by visual c++'s stl's
515 * iterator checking.
516 * msvcr100 has to provide them in case apps are compiled with /Od
517 * or the optimizer fails to inline those (empty) calls.
520 /* ?_Orphan_all@_Container_base0@std@@QAEXXZ */
521 /* ?_Orphan_all@_Container_base0@std@@QEAAXXZ */
522 DEFINE_THISCALL_WRAPPER(Container_base0_Orphan_all, 4)
523 void __thiscall Container_base0_Orphan_all(void *this)
527 /* ?_Swap_all@_Container_base0@std@@QAEXAAU12@@Z */
528 /* ?_Swap_all@_Container_base0@std@@QEAAXAEAU12@@Z */
529 DEFINE_THISCALL_WRAPPER(Container_base0_Swap_all, 8)
530 void __thiscall Container_base0_Swap_all(void *this, void *that)
534 /* ??4_Container_base0@std@@QAEAAU01@ABU01@@Z */
535 /* ??4_Container_base0@std@@QEAAAEAU01@AEBU01@@Z */
536 DEFINE_THISCALL_WRAPPER(Container_base0_op_assign, 8)
537 void* __thiscall Container_base0_op_assign(void *this, const void *that)
539 return this;
542 /* ??0_Container_base12@std@@QAE@ABU01@@Z */
543 /* ??0_Container_base12@std@@QEAA@AEBU01@@Z */
544 DEFINE_THISCALL_WRAPPER(_Container_base12_copy_ctor, 8)
545 _Container_base12* __thiscall _Container_base12_copy_ctor(
546 _Container_base12 *this, _Container_base12 *that)
548 this->proxy = NULL;
549 return this;
552 /* ??0_Container_base12@std@@QAE@XZ */
553 /* ??0_Container_base12@std@@QEAA@XZ */
554 DEFINE_THISCALL_WRAPPER(_Container_base12_ctor, 4)
555 _Container_base12* __thiscall _Container_base12_ctor(_Container_base12 *this)
557 this->proxy = NULL;
558 return this;
561 /* ??1_Container_base12@std@@QAE@XZ */
562 /* ??1_Container_base12@std@@QEAA@XZ */
563 DEFINE_THISCALL_WRAPPER(_Container_base12_dtor, 4)
564 void __thiscall _Container_base12_dtor(_Container_base12 *this)
568 /* ??4_Container_base12@std@@QAEAAU01@ABU01@@Z */
569 /* ??4_Container_base12@std@@QEAAAEAU01@AEBU01@@ */
570 DEFINE_THISCALL_WRAPPER(_Container_base12_op_assign, 8)
571 _Container_base12* __thiscall _Container_base12_op_assign(
572 _Container_base12 *this, const _Container_base12 *that)
574 return this;
577 /* ?_Getpfirst@_Container_base12@std@@QBEPAPAU_Iterator_base12@2@XZ */
578 /* ?_Getpfirst@_Container_base12@std@@QEBAPEAPEAU_Iterator_base12@2@XZ */
579 DEFINE_THISCALL_WRAPPER(_Container_base12__Getpfirst, 4)
580 _Iterator_base12** __thiscall _Container_base12__Getpfirst(_Container_base12 *this)
582 return this->proxy ? &this->proxy->head : NULL;
585 /* ?_Orphan_all@_Container_base12@std@@QAEXXZ */
586 /* ?_Orphan_all@_Container_base12@std@@QEAAXXZ */
587 DEFINE_THISCALL_WRAPPER(_Container_base12__Orphan_all, 4)
588 void __thiscall _Container_base12__Orphan_all(_Container_base12 *this)
592 /* ?_Swap_all@_Container_base12@std@@QAEXAAU12@@Z */
593 /* ?_Swap_all@_Container_base12@std@@QEAAXAEAU12@@Z */
594 DEFINE_THISCALL_WRAPPER(_Container_base12__Swap_all, 8)
595 void __thiscall _Container_base12__Swap_all(
596 _Container_base12 *this, _Container_base12 *that)
598 _Container_proxy *tmp;
600 tmp = this->proxy;
601 this->proxy = that->proxy;
602 that->proxy = tmp;
604 if(this->proxy)
605 this->proxy->cont = this;
606 if(that->proxy)
607 that->proxy->cont = that;
610 #if _MSVCP_VER >= 110
612 #define SECSPERDAY 86400
613 /* 1601 to 1970 is 369 years plus 89 leap days */
614 #define SECS_1601_TO_1970 ((369 * 365 + 89) * (ULONGLONG)SECSPERDAY)
615 #define TICKSPERSEC 10000000
616 #define TICKS_1601_TO_1970 (SECS_1601_TO_1970 * TICKSPERSEC)
617 #define NANOSEC_PER_MILLISEC 1000000
618 #define MILLISEC_PER_SEC 1000
619 #define NANOSEC_PER_SEC (NANOSEC_PER_MILLISEC * MILLISEC_PER_SEC)
621 typedef int MSVCRT_long;
623 /* xtime */
624 typedef struct {
625 __time64_t sec;
626 MSVCRT_long nsec;
627 } xtime;
629 /* _Xtime_get_ticks */
630 LONGLONG __cdecl _Xtime_get_ticks(void)
632 FILETIME ft;
634 TRACE("\n");
636 GetSystemTimeAsFileTime(&ft);
637 return ((LONGLONG)ft.dwHighDateTime<<32) + ft.dwLowDateTime - TICKS_1601_TO_1970;
640 /* _xtime_get */
641 int __cdecl xtime_get(xtime* t, int unknown)
643 LONGLONG ticks;
645 TRACE("(%p)\n", t);
647 if(unknown != 1)
648 return 0;
650 ticks = _Xtime_get_ticks();
651 t->sec = ticks / TICKSPERSEC;
652 t->nsec = ticks % TICKSPERSEC * 100;
653 return 1;
656 /* _Xtime_diff_to_millis2 */
657 MSVCRT_long __cdecl _Xtime_diff_to_millis2(const xtime *t1, const xtime *t2)
659 LONGLONG diff_sec, diff_nsec;
661 TRACE("(%p, %p)\n", t1, t2);
663 diff_sec = t1->sec - t2->sec;
664 diff_nsec = t1->nsec - t2->nsec;
666 diff_sec += diff_nsec / NANOSEC_PER_SEC;
667 diff_nsec %= NANOSEC_PER_SEC;
668 if (diff_nsec < 0) {
669 diff_sec -= 1;
670 diff_nsec += NANOSEC_PER_SEC;
673 if (diff_sec<0 || (diff_sec==0 && diff_nsec<0))
674 return 0;
675 return diff_sec * MILLISEC_PER_SEC +
676 (diff_nsec + NANOSEC_PER_MILLISEC - 1) / NANOSEC_PER_MILLISEC;
679 /* _Xtime_diff_to_millis */
680 MSVCRT_long __cdecl _Xtime_diff_to_millis(const xtime *t)
682 xtime now;
684 TRACE("%p\n", t);
686 xtime_get(&now, 1);
687 return _Xtime_diff_to_millis2(t, &now);
689 #endif
691 #if _MSVCP_VER >= 90
692 unsigned int __cdecl _Random_device(void)
694 unsigned int ret;
696 TRACE("\n");
698 /* TODO: throw correct exception in case of failure */
699 if(rand_s(&ret))
700 throw_exception("random number generator failed\n");
701 return ret;
703 #endif
705 #if _MSVCP_VER >= 110
706 #define MTX_PLAIN 0x1
707 #define MTX_TRY 0x2
708 #define MTX_TIMED 0x4
709 #define MTX_RECURSIVE 0x100
710 #define MTX_LOCKED 3
711 typedef struct
713 DWORD flags;
714 cs cs;
715 DWORD thread_id;
716 DWORD count;
717 } *_Mtx_t;
719 #if _MSVCP_VER >= 140
720 typedef _Mtx_t _Mtx_arg_t;
721 #define MTX_T_FROM_ARG(m) (m)
722 #define MTX_T_TO_ARG(m) (m)
723 #else
724 typedef _Mtx_t *_Mtx_arg_t;
725 #define MTX_T_FROM_ARG(m) (*(m))
726 #define MTX_T_TO_ARG(m) (&(m))
727 #endif
729 void __cdecl _Mtx_init_in_situ(_Mtx_t mtx, int flags)
731 if(flags & ~(MTX_PLAIN | MTX_TRY | MTX_TIMED | MTX_RECURSIVE))
732 FIXME("unknown flags ignored: %x\n", flags);
734 mtx->flags = flags;
735 cs_init(&mtx->cs);
736 mtx->thread_id = -1;
737 mtx->count = 0;
740 int __cdecl _Mtx_init(_Mtx_t *mtx, int flags)
742 *mtx = operator_new(sizeof(**mtx));
743 _Mtx_init_in_situ(*mtx, flags);
744 return 0;
747 void __cdecl _Mtx_destroy_in_situ(_Mtx_t mtx)
749 cs_destroy(&mtx->cs);
752 void __cdecl _Mtx_destroy(_Mtx_arg_t mtx)
754 cs_destroy(&MTX_T_FROM_ARG(mtx)->cs);
755 operator_delete(MTX_T_FROM_ARG(mtx));
758 int __cdecl _Mtx_current_owns(_Mtx_arg_t mtx)
760 return MTX_T_FROM_ARG(mtx)->thread_id == GetCurrentThreadId();
763 int __cdecl _Mtx_lock(_Mtx_arg_t mtx)
765 if(MTX_T_FROM_ARG(mtx)->thread_id != GetCurrentThreadId()) {
766 cs_lock(&MTX_T_FROM_ARG(mtx)->cs);
767 MTX_T_FROM_ARG(mtx)->thread_id = GetCurrentThreadId();
768 }else if(!(MTX_T_FROM_ARG(mtx)->flags & MTX_RECURSIVE)
769 && MTX_T_FROM_ARG(mtx)->flags != MTX_PLAIN) {
770 return MTX_LOCKED;
773 MTX_T_FROM_ARG(mtx)->count++;
774 return 0;
777 int __cdecl _Mtx_unlock(_Mtx_arg_t mtx)
779 if(--MTX_T_FROM_ARG(mtx)->count)
780 return 0;
782 MTX_T_FROM_ARG(mtx)->thread_id = -1;
783 cs_unlock(&MTX_T_FROM_ARG(mtx)->cs);
784 return 0;
787 int __cdecl _Mtx_trylock(_Mtx_arg_t mtx)
789 if(MTX_T_FROM_ARG(mtx)->thread_id != GetCurrentThreadId()) {
790 if(!cs_trylock(&MTX_T_FROM_ARG(mtx)->cs))
791 return MTX_LOCKED;
792 MTX_T_FROM_ARG(mtx)->thread_id = GetCurrentThreadId();
793 }else if(!(MTX_T_FROM_ARG(mtx)->flags & MTX_RECURSIVE)
794 && MTX_T_FROM_ARG(mtx)->flags != MTX_PLAIN) {
795 return MTX_LOCKED;
798 MTX_T_FROM_ARG(mtx)->count++;
799 return 0;
802 void* __cdecl _Mtx_getconcrtcs(_Mtx_arg_t mtx)
804 return &MTX_T_FROM_ARG(mtx)->cs;
807 void __cdecl _Mtx_clear_owner(_Mtx_arg_t mtx)
809 _Mtx_t m = MTX_T_FROM_ARG(mtx);
810 m->thread_id = -1;
811 m->count--;
814 void __cdecl _Mtx_reset_owner(_Mtx_arg_t mtx)
816 _Mtx_t m = MTX_T_FROM_ARG(mtx);
817 m->thread_id = GetCurrentThreadId();
818 m->count++;
821 #define CND_TIMEDOUT 2
823 typedef struct
825 cv cv;
826 } *_Cnd_t;
828 #if _MSVCP_VER >= 140
829 typedef _Cnd_t _Cnd_arg_t;
830 #define CND_T_FROM_ARG(c) (c)
831 #define CND_T_TO_ARG(c) (c)
832 #else
833 typedef _Cnd_t *_Cnd_arg_t;
834 #define CND_T_FROM_ARG(c) (*(c))
835 #define CND_T_TO_ARG(c) (&(c))
836 #endif
838 void __cdecl _Cnd_init_in_situ(_Cnd_t cnd)
840 cv_init(&cnd->cv);
843 int __cdecl _Cnd_init(_Cnd_t *cnd)
845 *cnd = operator_new(sizeof(**cnd));
846 _Cnd_init_in_situ(*cnd);
847 return 0;
850 int __cdecl _Cnd_wait(_Cnd_arg_t cnd, _Mtx_arg_t mtx)
852 cv *cv = &CND_T_FROM_ARG(cnd)->cv;
853 _Mtx_t m = MTX_T_FROM_ARG(mtx);
855 _Mtx_clear_owner(mtx);
856 cv_wait(cv, &m->cs);
857 _Mtx_reset_owner(mtx);
858 return 0;
861 int __cdecl _Cnd_timedwait(_Cnd_arg_t cnd, _Mtx_arg_t mtx, const xtime *xt)
863 cv *cv = &CND_T_FROM_ARG(cnd)->cv;
864 _Mtx_t m = MTX_T_FROM_ARG(mtx);
865 bool r;
867 _Mtx_clear_owner(mtx);
868 r = cv_wait_for(cv, &m->cs, _Xtime_diff_to_millis(xt));
869 _Mtx_reset_owner(mtx);
870 return r ? 0 : CND_TIMEDOUT;
873 int __cdecl _Cnd_broadcast(_Cnd_arg_t cnd)
875 cv_notify_all(&CND_T_FROM_ARG(cnd)->cv);
876 return 0;
879 int __cdecl _Cnd_signal(_Cnd_arg_t cnd)
881 cv_notify_one(&CND_T_FROM_ARG(cnd)->cv);
882 return 0;
885 void __cdecl _Cnd_destroy_in_situ(_Cnd_t cnd)
887 _Cnd_broadcast(CND_T_TO_ARG(cnd));
888 cv_destroy(&cnd->cv);
891 void __cdecl _Cnd_destroy(_Cnd_arg_t cnd)
893 if(cnd) {
894 _Cnd_broadcast(cnd);
895 cv_destroy(&CND_T_FROM_ARG(cnd)->cv);
896 operator_delete(CND_T_FROM_ARG(cnd));
900 static struct {
901 int used;
902 int size;
904 struct _to_broadcast {
905 DWORD thread_id;
906 _Cnd_arg_t cnd;
907 _Mtx_arg_t mtx;
908 int *p;
909 } *to_broadcast;
910 } broadcast_at_thread_exit;
912 static CRITICAL_SECTION broadcast_at_thread_exit_cs;
913 static CRITICAL_SECTION_DEBUG broadcast_at_thread_exit_cs_debug =
915 0, 0, &broadcast_at_thread_exit_cs,
916 { &broadcast_at_thread_exit_cs_debug.ProcessLocksList, &broadcast_at_thread_exit_cs_debug.ProcessLocksList },
917 0, 0, { (DWORD_PTR)(__FILE__ ": broadcast_at_thread_exit_cs") }
919 static CRITICAL_SECTION broadcast_at_thread_exit_cs = { &broadcast_at_thread_exit_cs_debug, -1, 0, 0, 0, 0 };
921 void __cdecl _Cnd_register_at_thread_exit(_Cnd_arg_t cnd, _Mtx_arg_t mtx, int *p)
923 struct _to_broadcast *add;
925 TRACE("(%p %p %p)\n", cnd, mtx, p);
927 EnterCriticalSection(&broadcast_at_thread_exit_cs);
928 if(!broadcast_at_thread_exit.size) {
929 broadcast_at_thread_exit.to_broadcast = HeapAlloc(GetProcessHeap(),
930 0, 8*sizeof(broadcast_at_thread_exit.to_broadcast[0]));
931 if(!broadcast_at_thread_exit.to_broadcast) {
932 LeaveCriticalSection(&broadcast_at_thread_exit_cs);
933 return;
935 broadcast_at_thread_exit.size = 8;
936 } else if(broadcast_at_thread_exit.size == broadcast_at_thread_exit.used) {
937 add = HeapReAlloc(GetProcessHeap(), 0, broadcast_at_thread_exit.to_broadcast,
938 broadcast_at_thread_exit.size*2*sizeof(broadcast_at_thread_exit.to_broadcast[0]));
939 if(!add) {
940 LeaveCriticalSection(&broadcast_at_thread_exit_cs);
941 return;
943 broadcast_at_thread_exit.to_broadcast = add;
944 broadcast_at_thread_exit.size *= 2;
947 add = broadcast_at_thread_exit.to_broadcast + broadcast_at_thread_exit.used++;
948 add->thread_id = GetCurrentThreadId();
949 add->cnd = cnd;
950 add->mtx = mtx;
951 add->p = p;
952 LeaveCriticalSection(&broadcast_at_thread_exit_cs);
955 void __cdecl _Cnd_unregister_at_thread_exit(_Mtx_arg_t mtx)
957 int i;
959 TRACE("(%p)\n", mtx);
961 EnterCriticalSection(&broadcast_at_thread_exit_cs);
962 for(i=0; i<broadcast_at_thread_exit.used; i++) {
963 if(broadcast_at_thread_exit.to_broadcast[i].mtx != mtx)
964 continue;
966 memmove(broadcast_at_thread_exit.to_broadcast+i, broadcast_at_thread_exit.to_broadcast+i+1,
967 (broadcast_at_thread_exit.used-i-1)*sizeof(broadcast_at_thread_exit.to_broadcast[0]));
968 broadcast_at_thread_exit.used--;
969 i--;
971 LeaveCriticalSection(&broadcast_at_thread_exit_cs);
974 void __cdecl _Cnd_do_broadcast_at_thread_exit(void)
976 int i, id = GetCurrentThreadId();
978 TRACE("()\n");
980 EnterCriticalSection(&broadcast_at_thread_exit_cs);
981 for(i=0; i<broadcast_at_thread_exit.used; i++) {
982 if(broadcast_at_thread_exit.to_broadcast[i].thread_id != id)
983 continue;
985 _Mtx_unlock(broadcast_at_thread_exit.to_broadcast[i].mtx);
986 _Cnd_broadcast(broadcast_at_thread_exit.to_broadcast[i].cnd);
987 if(broadcast_at_thread_exit.to_broadcast[i].p)
988 *broadcast_at_thread_exit.to_broadcast[i].p = 1;
990 memmove(broadcast_at_thread_exit.to_broadcast+i, broadcast_at_thread_exit.to_broadcast+i+1,
991 (broadcast_at_thread_exit.used-i-1)*sizeof(broadcast_at_thread_exit.to_broadcast[0]));
992 broadcast_at_thread_exit.used--;
993 i--;
995 LeaveCriticalSection(&broadcast_at_thread_exit_cs);
998 #endif
1000 #if _MSVCP_VER == 100
1001 typedef struct {
1002 const vtable_ptr *vtable;
1003 } error_category;
1005 typedef struct {
1006 error_category base;
1007 const char *type;
1008 } custom_category;
1009 static custom_category iostream_category;
1011 DEFINE_RTTI_DATA0(error_category, 0, ".?AVerror_category@std@@")
1012 DEFINE_RTTI_DATA1(iostream_category, 0, &error_category_rtti_base_descriptor, ".?AV_Iostream_error_category@std@@")
1014 extern const vtable_ptr iostream_category_vtable;
1016 static void iostream_category_ctor(custom_category *this)
1018 this->base.vtable = &iostream_category_vtable;
1019 this->type = "iostream";
1022 DEFINE_THISCALL_WRAPPER(custom_category_vector_dtor, 8)
1023 custom_category* __thiscall custom_category_vector_dtor(custom_category *this, unsigned int flags)
1025 TRACE("(%p %x)\n", this, flags);
1026 if(flags & 2) {
1027 /* we have an array, with the number of elements stored before the first object */
1028 INT_PTR i, *ptr = (INT_PTR *)this-1;
1030 for(i=*ptr-1; i>=0; i--)
1031 operator_delete(ptr);
1032 } else {
1033 if(flags & 1)
1034 operator_delete(this);
1037 return this;
1040 DEFINE_THISCALL_WRAPPER(custom_category_name, 4)
1041 const char* __thiscall custom_category_name(const custom_category *this)
1043 return this->type;
1046 DEFINE_THISCALL_WRAPPER(custom_category_message, 12)
1047 basic_string_char* __thiscall custom_category_message(const custom_category *this,
1048 basic_string_char *ret, int err)
1050 return MSVCP_basic_string_char_ctor_cstr(ret, strerror(err));
1053 DEFINE_THISCALL_WRAPPER(custom_category_default_error_condition, 12)
1054 /*error_condition*/void* __thiscall custom_category_default_error_condition(
1055 custom_category *this, /*error_condition*/void *ret, int code)
1057 FIXME("(%p %p %x) stub\n", this, ret, code);
1058 return NULL;
1061 DEFINE_THISCALL_WRAPPER(custom_category_equivalent, 12)
1062 bool __thiscall custom_category_equivalent(const custom_category *this,
1063 int code, const /*error_condition*/void *condition)
1065 FIXME("(%p %x %p) stub\n", this, code, condition);
1066 return FALSE;
1069 DEFINE_THISCALL_WRAPPER(custom_category_equivalent_code, 12)
1070 bool __thiscall custom_category_equivalent_code(custom_category *this,
1071 const /*error_code*/void *code, int condition)
1073 FIXME("(%p %p %x) stub\n", this, code, condition);
1074 return FALSE;
1077 DEFINE_THISCALL_WRAPPER(iostream_category_message, 12)
1078 basic_string_char* __thiscall iostream_category_message(const custom_category *this,
1079 basic_string_char *ret, int err)
1081 if(err == 1) return MSVCP_basic_string_char_ctor_cstr(ret, "iostream error");
1082 return MSVCP_basic_string_char_ctor_cstr(ret, strerror(err));
1085 /* ?iostream_category@std@@YAABVerror_category@1@XZ */
1086 /* ?iostream_category@std@@YAAEBVerror_category@1@XZ */
1087 const error_category* __cdecl std_iostream_category(void)
1089 TRACE("()\n");
1090 return &iostream_category.base;
1093 static custom_category system_category;
1094 DEFINE_RTTI_DATA1(system_category, 0, &error_category_rtti_base_descriptor, ".?AV_System_error_category@std@@")
1096 extern const vtable_ptr system_category_vtable;
1098 static void system_category_ctor(custom_category *this)
1100 this->base.vtable = &system_category_vtable;
1101 this->type = "system";
1104 /* ?system_category@std@@YAABVerror_category@1@XZ */
1105 /* ?system_category@std@@YAAEBVerror_category@1@XZ */
1106 const error_category* __cdecl std_system_category(void)
1108 TRACE("()\n");
1109 return &system_category.base;
1112 static custom_category generic_category;
1113 DEFINE_RTTI_DATA1(generic_category, 0, &error_category_rtti_base_descriptor, ".?AV_Generic_error_category@std@@")
1115 extern const vtable_ptr generic_category_vtable;
1117 static void generic_category_ctor(custom_category *this)
1119 this->base.vtable = &generic_category_vtable;
1120 this->type = "generic";
1123 /* ?generic_category@std@@YAABVerror_category@1@XZ */
1124 /* ?generic_category@std@@YAAEBVerror_category@1@XZ */
1125 const error_category* __cdecl std_generic_category(void)
1127 TRACE("()\n");
1128 return &generic_category.base;
1130 #endif
1132 #if _MSVCP_VER >= 110
1133 static CRITICAL_SECTION call_once_cs;
1134 static CRITICAL_SECTION_DEBUG call_once_cs_debug =
1136 0, 0, &call_once_cs,
1137 { &call_once_cs_debug.ProcessLocksList, &call_once_cs_debug.ProcessLocksList },
1138 0, 0, { (DWORD_PTR)(__FILE__ ": call_once_cs") }
1140 static CRITICAL_SECTION call_once_cs = { &call_once_cs_debug, -1, 0, 0, 0, 0 };
1142 void __cdecl _Call_onceEx(int *once, void (__cdecl *func)(void*), void *argv)
1144 TRACE("%p %p %p\n", once, func, argv);
1146 EnterCriticalSection(&call_once_cs);
1147 if(!*once) {
1148 /* FIXME: handle exceptions */
1149 func(argv);
1150 *once = 1;
1152 LeaveCriticalSection(&call_once_cs);
1155 static void __cdecl call_once_func_wrapper(void *func)
1157 ((void (__cdecl*)(void))func)();
1160 void __cdecl _Call_once(int *once, void (__cdecl *func)(void))
1162 TRACE("%p %p\n", once, func);
1163 _Call_onceEx(once, call_once_func_wrapper, func);
1166 void __cdecl _Do_call(void *this)
1168 CALL_VTBL_FUNC(this, 0, void, (void*), (this));
1170 #endif
1172 #if _MSVCP_VER >= 110
1173 typedef struct
1175 HANDLE hnd;
1176 DWORD id;
1177 } _Thrd_t;
1179 typedef int (__cdecl *_Thrd_start_t)(void*);
1181 #define _THRD_ERROR 4
1183 int __cdecl _Thrd_equal(_Thrd_t a, _Thrd_t b)
1185 TRACE("(%p %lu %p %lu)\n", a.hnd, a.id, b.hnd, b.id);
1186 return a.id == b.id;
1189 int __cdecl _Thrd_lt(_Thrd_t a, _Thrd_t b)
1191 TRACE("(%p %lu %p %lu)\n", a.hnd, a.id, b.hnd, b.id);
1192 return a.id < b.id;
1195 void __cdecl _Thrd_sleep(const xtime *t)
1197 TRACE("(%p)\n", t);
1198 Sleep(_Xtime_diff_to_millis(t));
1201 void __cdecl _Thrd_yield(void)
1203 TRACE("()\n");
1204 Sleep(0);
1207 static _Thrd_t thread_current(void)
1209 _Thrd_t ret;
1211 if(DuplicateHandle(GetCurrentProcess(), GetCurrentThread(),
1212 GetCurrentProcess(), &ret.hnd, 0, FALSE, DUPLICATE_SAME_ACCESS)) {
1213 CloseHandle(ret.hnd);
1214 } else {
1215 ret.hnd = 0;
1217 ret.id = GetCurrentThreadId();
1219 TRACE("(%p %lu)\n", ret.hnd, ret.id);
1220 return ret;
1223 #ifndef __i386__
1224 _Thrd_t __cdecl _Thrd_current(void)
1226 return thread_current();
1228 #else
1229 ULONGLONG __cdecl _Thrd_current(void)
1231 union {
1232 _Thrd_t thr;
1233 ULONGLONG ull;
1234 } ret;
1236 C_ASSERT(sizeof(_Thrd_t) <= sizeof(ULONGLONG));
1238 ret.thr = thread_current();
1239 return ret.ull;
1241 #endif
1243 int __cdecl _Thrd_join(_Thrd_t thr, int *code)
1245 TRACE("(%p %lu %p)\n", thr.hnd, thr.id, code);
1246 if (WaitForSingleObject(thr.hnd, INFINITE))
1247 return _THRD_ERROR;
1249 if (code)
1250 GetExitCodeThread(thr.hnd, (DWORD *)code);
1252 CloseHandle(thr.hnd);
1253 return 0;
1256 int __cdecl _Thrd_start(_Thrd_t *thr, LPTHREAD_START_ROUTINE proc, void *arg)
1258 TRACE("(%p %p %p)\n", thr, proc, arg);
1259 thr->hnd = CreateThread(NULL, 0, proc, arg, 0, &thr->id);
1260 return thr->hnd ? 0 : _THRD_ERROR;
1263 typedef struct
1265 _Thrd_start_t proc;
1266 void *arg;
1267 } thread_proc_arg;
1269 static DWORD WINAPI thread_proc_wrapper(void *arg)
1271 thread_proc_arg wrapped_arg = *((thread_proc_arg*)arg);
1272 free(arg);
1273 return wrapped_arg.proc(wrapped_arg.arg);
1276 int __cdecl _Thrd_create(_Thrd_t *thr, _Thrd_start_t proc, void *arg)
1278 thread_proc_arg *wrapped_arg;
1279 int ret;
1281 TRACE("(%p %p %p)\n", thr, proc, arg);
1283 wrapped_arg = malloc(sizeof(*wrapped_arg));
1284 if(!wrapped_arg)
1285 return _THRD_ERROR; /* TODO: probably different error should be returned here */
1287 wrapped_arg->proc = proc;
1288 wrapped_arg->arg = arg;
1289 ret = _Thrd_start(thr, thread_proc_wrapper, wrapped_arg);
1290 if(ret) free(wrapped_arg);
1291 return ret;
1294 int __cdecl _Thrd_detach(_Thrd_t thr)
1296 return CloseHandle(thr.hnd) ? 0 : _THRD_ERROR;
1299 typedef struct
1301 const vtable_ptr *vtable;
1302 _Cnd_t cnd;
1303 _Mtx_t mtx;
1304 bool launched;
1305 } _Pad;
1307 DEFINE_RTTI_DATA0(_Pad, 0, ".?AV_Pad@std@@")
1309 /* ??_7_Pad@std@@6B@ */
1310 extern const vtable_ptr _Pad_vtable;
1312 unsigned int __cdecl _Thrd_hardware_concurrency(void)
1314 static unsigned int val = -1;
1316 TRACE("()\n");
1318 if(val == -1) {
1319 SYSTEM_INFO si;
1321 GetSystemInfo(&si);
1322 val = si.dwNumberOfProcessors;
1325 return val;
1328 unsigned int __cdecl _Thrd_id(void)
1330 TRACE("()\n");
1331 return GetCurrentThreadId();
1334 /* ??0_Pad@std@@QAE@XZ */
1335 /* ??0_Pad@std@@QEAA@XZ */
1336 DEFINE_THISCALL_WRAPPER(_Pad_ctor, 4)
1337 _Pad* __thiscall _Pad_ctor(_Pad *this)
1339 TRACE("(%p)\n", this);
1341 this->vtable = &_Pad_vtable;
1342 _Cnd_init(&this->cnd);
1343 _Mtx_init(&this->mtx, 0);
1344 this->launched = FALSE;
1345 _Mtx_lock(MTX_T_TO_ARG(this->mtx));
1346 return this;
1349 /* ??4_Pad@std@@QAEAAV01@ABV01@@Z */
1350 /* ??4_Pad@std@@QEAAAEAV01@AEBV01@@Z */
1351 DEFINE_THISCALL_WRAPPER(_Pad_op_assign, 8)
1352 _Pad* __thiscall _Pad_op_assign(_Pad *this, const _Pad *copy)
1354 TRACE("(%p %p)\n", this, copy);
1356 this->cnd = copy->cnd;
1357 this->mtx = copy->mtx;
1358 this->launched = copy->launched;
1359 return this;
1362 /* ??0_Pad@std@@QAE@ABV01@@Z */
1363 /* ??0_Pad@std@@QEAA@AEBV01@@Z */
1364 DEFINE_THISCALL_WRAPPER(_Pad_copy_ctor, 8)
1365 _Pad* __thiscall _Pad_copy_ctor(_Pad *this, const _Pad *copy)
1367 TRACE("(%p %p)\n", this, copy);
1369 this->vtable = &_Pad_vtable;
1370 return _Pad_op_assign(this, copy);
1373 /* ??1_Pad@std@@QAE@XZ */
1374 /* ??1_Pad@std@@QEAA@XZ */
1375 DEFINE_THISCALL_WRAPPER(_Pad_dtor, 4)
1376 void __thiscall _Pad_dtor(_Pad *this)
1378 TRACE("(%p)\n", this);
1380 _Mtx_unlock(MTX_T_TO_ARG(this->mtx));
1381 _Mtx_destroy(MTX_T_TO_ARG(this->mtx));
1382 _Cnd_destroy(CND_T_TO_ARG(this->cnd));
1385 DEFINE_THISCALL_WRAPPER(_Pad__Go, 4)
1386 #define call__Pad__Go(this) CALL_VTBL_FUNC(this, 0, unsigned int, (_Pad*), (this))
1387 unsigned int __thiscall _Pad__Go(_Pad *this)
1389 ERR("(%p) should not be called\n", this);
1390 return 0;
1393 static DWORD WINAPI launch_thread_proc(void *arg)
1395 _Pad *this = arg;
1396 return call__Pad__Go(this);
1399 /* ?_Launch@_Pad@std@@QAEXPAU_Thrd_imp_t@@@Z */
1400 /* ?_Launch@_Pad@std@@QEAAXPEAU_Thrd_imp_t@@@Z */
1401 DEFINE_THISCALL_WRAPPER(_Pad__Launch, 8)
1402 void __thiscall _Pad__Launch(_Pad *this, _Thrd_t *thr)
1404 TRACE("(%p %p)\n", this, thr);
1406 _Thrd_start(thr, launch_thread_proc, this);
1407 _Cnd_wait(CND_T_TO_ARG(this->cnd), MTX_T_TO_ARG(this->mtx));
1410 /* ?_Release@_Pad@std@@QAEXXZ */
1411 /* ?_Release@_Pad@std@@QEAAXXZ */
1412 DEFINE_THISCALL_WRAPPER(_Pad__Release, 4)
1413 void __thiscall _Pad__Release(_Pad *this)
1415 TRACE("(%p)\n", this);
1417 _Mtx_lock(MTX_T_TO_ARG(this->mtx));
1418 this->launched = TRUE;
1419 _Cnd_signal(CND_T_TO_ARG(this->cnd));
1420 _Mtx_unlock(MTX_T_TO_ARG(this->mtx));
1422 #endif
1424 /*********************************************************************
1425 * __crtInitializeCriticalSectionEx (MSVCP140.@)
1427 BOOL CDECL MSVCP__crtInitializeCriticalSectionEx(
1428 CRITICAL_SECTION *cs, DWORD spin_count, DWORD flags)
1430 TRACE("(%p %lx %lx)\n", cs, spin_count, flags);
1431 return InitializeCriticalSectionEx(cs, spin_count, flags);
1434 /*********************************************************************
1435 * __crtCreateEventExW (MSVCP140.@)
1437 HANDLE CDECL MSVCP__crtCreateEventExW(
1438 SECURITY_ATTRIBUTES *attribs, LPCWSTR name, DWORD flags, DWORD access)
1440 TRACE("(%p %s %#lx %#lx)\n", attribs, debugstr_w(name), flags, access);
1441 return CreateEventExW(attribs, name, flags, access);
1444 /*********************************************************************
1445 * __crtGetTickCount64 (MSVCP140.@)
1447 ULONGLONG CDECL MSVCP__crtGetTickCount64(void)
1449 return GetTickCount64();
1452 /*********************************************************************
1453 * __crtGetCurrentProcessorNumber (MSVCP140.@)
1455 DWORD CDECL MSVCP__crtGetCurrentProcessorNumber(void)
1457 return GetCurrentProcessorNumber();
1460 /*********************************************************************
1461 * __crtFlushProcessWriteBuffers (MSVCP140.@)
1463 VOID CDECL MSVCP__crtFlushProcessWriteBuffers(void)
1465 return FlushProcessWriteBuffers();
1468 /*********************************************************************
1469 * __crtCreateSemaphoreExW (MSVCP140.@)
1471 HANDLE CDECL MSVCP__crtCreateSemaphoreExW(
1472 SECURITY_ATTRIBUTES *attribs, LONG initial_count, LONG max_count, LPCWSTR name,
1473 DWORD flags, DWORD access)
1475 TRACE("(%p %ld %ld %s %#lx %#lx)\n", attribs, initial_count, max_count, debugstr_w(name),
1476 flags, access);
1477 return CreateSemaphoreExW(attribs, initial_count, max_count, name, flags, access);
1480 /*********************************************************************
1481 * __crtCreateThreadpoolTimer (MSVCP140.@)
1483 PTP_TIMER CDECL MSVCP__crtCreateThreadpoolTimer(PTP_TIMER_CALLBACK callback,
1484 PVOID userdata, TP_CALLBACK_ENVIRON *environment)
1486 TRACE("(%p %p %p)\n", callback, userdata, environment);
1487 return CreateThreadpoolTimer(callback, userdata, environment);
1490 /*********************************************************************
1491 * __crtCloseThreadpoolTimer (MSVCP140.@)
1493 VOID CDECL MSVCP__crtCloseThreadpoolTimer(TP_TIMER *timer)
1495 TRACE("(%p)\n", timer);
1496 CloseThreadpoolTimer(timer);
1499 /*********************************************************************
1500 * __crtSetThreadpoolTimer (MSVCP140.@)
1502 VOID CDECL MSVCP__crtSetThreadpoolTimer(TP_TIMER *timer,
1503 FILETIME *due_time, DWORD period, DWORD window_length)
1505 TRACE("(%p %p %#lx %#lx)\n", timer, due_time, period, window_length);
1506 return SetThreadpoolTimer(timer, due_time, period, window_length);
1509 /*********************************************************************
1510 * __crtWaitForThreadpoolTimerCallbacks (MSVCP140.@)
1512 VOID CDECL MSVCP__crtWaitForThreadpoolTimerCallbacks(TP_TIMER *timer, BOOL cancel)
1514 TRACE("(%p %d)\n", timer, cancel);
1515 WaitForThreadpoolTimerCallbacks(timer, cancel);
1518 /*********************************************************************
1519 * __crtCreateThreadpoolWait (MSVCP140.@)
1521 PTP_WAIT CDECL MSVCP__crtCreateThreadpoolWait(PTP_WAIT_CALLBACK callback,
1522 PVOID userdata, TP_CALLBACK_ENVIRON *environment)
1524 TRACE("(%p %p %p)\n", callback, userdata, environment);
1525 return CreateThreadpoolWait(callback, userdata, environment);
1528 /*********************************************************************
1529 * __crtCloseThreadpoolWait (MSVCP140.@)
1531 VOID CDECL MSVCP__crtCloseThreadpoolWait(TP_WAIT *wait)
1533 TRACE("(%p)\n", wait);
1534 CloseThreadpoolWait(wait);
1537 /*********************************************************************
1538 * __crtSetThreadpoolWait (MSVCP140.@)
1540 VOID CDECL MSVCP__crtSetThreadpoolWait(TP_WAIT *wait, HANDLE handle, FILETIME *due_time)
1542 TRACE("(%p %p %p)\n", wait, handle, due_time);
1543 return SetThreadpoolWait(wait, handle, due_time);
1546 /*********************************************************************
1547 * __crtFreeLibraryWhenCallbackReturns (MSVCP140.@)
1549 VOID CDECL MSVCP__crtFreeLibraryWhenCallbackReturns(PTP_CALLBACK_INSTANCE instance, HMODULE mod)
1551 TRACE("(%p %p)\n", instance, mod);
1552 FreeLibraryWhenCallbackReturns(instance, mod);
1555 /* ?_Execute_once@std@@YAHAAUonce_flag@1@P6GHPAX1PAPAX@Z1@Z */
1556 /* ?_Execute_once@std@@YAHAEAUonce_flag@1@P6AHPEAX1PEAPEAX@Z1@Z */
1557 BOOL __cdecl _Execute_once(INIT_ONCE *flag, PINIT_ONCE_FN func, void *param)
1559 return InitOnceExecuteOnce(flag, func, param, NULL);
1562 #if _MSVCP_VER >= 140
1563 LONGLONG __cdecl _Query_perf_counter(void)
1565 LARGE_INTEGER li;
1566 QueryPerformanceCounter(&li);
1567 return li.QuadPart;
1570 LONGLONG __cdecl _Query_perf_frequency(void)
1572 LARGE_INTEGER li;
1573 QueryPerformanceFrequency(&li);
1574 return li.QuadPart;
1576 #endif
1578 void __cdecl threads__Mtx_new(void **mtx)
1580 *mtx = operator_new(sizeof(CRITICAL_SECTION));
1581 InitializeCriticalSection(*mtx);
1584 void __cdecl threads__Mtx_delete(void *mtx)
1586 DeleteCriticalSection(mtx);
1589 void __cdecl threads__Mtx_lock(void *mtx)
1591 EnterCriticalSection(mtx);
1594 void __cdecl threads__Mtx_unlock(void *mtx)
1596 LeaveCriticalSection(mtx);
1599 #if _MSVCP_VER >= 110
1600 static LONG shared_ptr_lock;
1602 void __cdecl _Lock_shared_ptr_spin_lock(void)
1604 LONG l = 0;
1606 while(InterlockedCompareExchange(&shared_ptr_lock, 1, 0) != 0) {
1607 if(l++ == 1000) {
1608 Sleep(0);
1609 l = 0;
1614 void __cdecl _Unlock_shared_ptr_spin_lock(void)
1616 shared_ptr_lock = 0;
1618 #endif
1620 #if _MSVCP_VER >= 110 && _MSVCP_VER <= 120
1621 typedef struct {
1622 char dummy;
1623 } _Ph;
1625 /* ?_1@placeholders@std@@3V?$_Ph@$00@2@A */
1626 /* ?_20@placeholders@std@@3V?$_Ph@$0BE@@2@A */
1627 _Ph _Ph_1 = {0}, _Ph_2 = {0}, _Ph_3 = {0}, _Ph_4 = {0}, _Ph_5 = {0};
1628 _Ph _Ph_6 = {0}, _Ph_7 = {0}, _Ph_8 = {0}, _Ph_9 = {0}, _Ph_10 = {0};
1629 _Ph _Ph_11 = {0}, _Ph_12 = {0}, _Ph_13 = {0}, _Ph_14 = {0}, _Ph_15 = {0};
1630 _Ph _Ph_16 = {0}, _Ph_17 = {0}, _Ph_18 = {0}, _Ph_19 = {0}, _Ph_20 = {0};
1631 #endif
1633 #if _MSVCP_VER >= 110
1634 /* ?_Syserror_map@std@@YAPBDH@Z */
1635 /* ?_Syserror_map@std@@YAPEBDH@Z */
1636 const char* __cdecl _Syserror_map(int err)
1638 int i;
1640 TRACE("(%d)\n", err);
1642 for(i = 0; i < ARRAY_SIZE(syserror_map); i++)
1644 if(syserror_map[i].err == err)
1645 return syserror_map[i].str;
1647 #if _MSVCP_VER >= 140
1648 return "unknown error";
1649 #else
1650 return NULL;
1651 #endif
1653 #endif
1655 #if _MSVCP_VER >= 140
1656 /* ?_Winerror_message@std@@YAKKPADK@Z */
1657 /* ?_Winerror_message@std@@YAKKPEADK@Z */
1658 ULONG __cdecl _Winerror_message(ULONG err, char *buf, ULONG size)
1660 TRACE("(%lu %p %lu)\n", err, buf, size);
1662 return FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
1663 NULL, err, 0, buf, size, NULL);
1666 /* ?_Winerror_map@std@@YAHH@Z */
1667 int __cdecl _Winerror_map(int err)
1669 int low = 0, high = ARRAY_SIZE(winerror_map) - 1, mid;
1671 while(low <= high)
1673 mid = (low + high) / 2;
1675 if(err == winerror_map[mid].winerr)
1676 return winerror_map[mid].doserr;
1677 if(err > winerror_map[mid].winerr)
1678 low = mid + 1;
1679 else
1680 high = mid - 1;
1683 return 0;
1685 #endif
1687 #if _MSVCP_VER >= 100
1688 __ASM_BLOCK_BEGIN(misc_vtables)
1689 #if _MSVCP_VER == 100
1690 __ASM_VTABLE(iostream_category,
1691 VTABLE_ADD_FUNC(custom_category_vector_dtor)
1692 VTABLE_ADD_FUNC(custom_category_name)
1693 VTABLE_ADD_FUNC(iostream_category_message)
1694 VTABLE_ADD_FUNC(custom_category_default_error_condition)
1695 VTABLE_ADD_FUNC(custom_category_equivalent)
1696 VTABLE_ADD_FUNC(custom_category_equivalent_code));
1697 __ASM_VTABLE(system_category,
1698 VTABLE_ADD_FUNC(custom_category_vector_dtor)
1699 VTABLE_ADD_FUNC(custom_category_name)
1700 VTABLE_ADD_FUNC(custom_category_message)
1701 VTABLE_ADD_FUNC(custom_category_default_error_condition)
1702 VTABLE_ADD_FUNC(custom_category_equivalent)
1703 VTABLE_ADD_FUNC(custom_category_equivalent_code));
1704 __ASM_VTABLE(generic_category,
1705 VTABLE_ADD_FUNC(custom_category_vector_dtor)
1706 VTABLE_ADD_FUNC(custom_category_name)
1707 VTABLE_ADD_FUNC(custom_category_message)
1708 VTABLE_ADD_FUNC(custom_category_default_error_condition)
1709 VTABLE_ADD_FUNC(custom_category_equivalent)
1710 VTABLE_ADD_FUNC(custom_category_equivalent_code));
1711 #endif
1712 #if _MSVCP_VER >= 110
1713 __ASM_VTABLE(_Pad,
1714 VTABLE_ADD_FUNC(_Pad__Go));
1715 #endif
1716 __ASM_BLOCK_END
1718 void init_misc(void *base)
1720 #ifdef __x86_64__
1721 #if _MSVCP_VER == 100
1722 init_error_category_rtti(base);
1723 init_iostream_category_rtti(base);
1724 init_system_category_rtti(base);
1725 init_generic_category_rtti(base);
1726 #endif
1727 #if _MSVCP_VER >= 110
1728 init__Pad_rtti(base);
1729 #endif
1730 #endif
1732 #if _MSVCP_VER == 100
1733 iostream_category_ctor(&iostream_category);
1734 system_category_ctor(&system_category);
1735 generic_category_ctor(&generic_category);
1736 #endif
1739 void free_misc(void)
1741 #if _MSVCP_VER >= 110
1742 HeapFree(GetProcessHeap(), 0, broadcast_at_thread_exit.to_broadcast);
1743 #endif
1745 #endif