wined3d: Use wined3d_bit_scan() in context_preload_textures().
[wine.git] / dlls / msvcp90 / misc.c
blob2d309911f10baa3bc93d08dc5d6d6a5aa0f33223
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 "windef.h"
26 #include "winbase.h"
27 #include "winternl.h"
28 #include "wine/debug.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(msvcp);
32 #if _MSVCP_VER >= 110
33 /* error strings generated with glibc strerror */
34 static const char str_EPERM[] = "operation not permitted";
35 static const char str_ENOENT[] = "no such file or directory";
36 static const char str_ESRCH[] = "no such process";
37 static const char str_EINTR[] = "interrupted system call";
38 static const char str_EIO[] = "input/output error";
39 static const char str_ENXIO[] = "no such device or address";
40 static const char str_E2BIG[] = "argument list too long";
41 static const char str_ENOEXEC[] = "exec format error";
42 static const char str_EBADF[] = "bad file descriptor";
43 static const char str_ECHILD[] = "no child processes";
44 static const char str_EAGAIN[] = "resource temporarily unavailable";
45 static const char str_ENOMEM[] = "cannot allocate memory";
46 static const char str_EACCES[] = "permission denied";
47 static const char str_EFAULT[] = "bad address";
48 static const char str_EBUSY[] = "device or resource busy";
49 static const char str_EEXIST[] = "file exists";
50 static const char str_EXDEV[] = "invalid cross-device link";
51 static const char str_ENODEV[] = "no such device";
52 static const char str_ENOTDIR[] = "not a directory";
53 static const char str_EISDIR[] = "is a directory";
54 static const char str_EINVAL[] = "invalid argument";
55 static const char str_ENFILE[] = "too many open files in system";
56 static const char str_EMFILE[] = "too many open files";
57 static const char str_ENOTTY[] = "inappropriate ioctl for device";
58 static const char str_EFBIG[] = "file too large";
59 static const char str_ENOSPC[] = "no space left on device";
60 static const char str_ESPIPE[] = "illegal seek";
61 static const char str_EROFS[] = "read-only file system";
62 static const char str_EMLINK[] = "too many links";
63 static const char str_EPIPE[] = "broken pipe";
64 static const char str_EDOM[] = "numerical argument out of domain";
65 static const char str_ERANGE[] = "numerical result out of range";
66 static const char str_EDEADLK[] = "resource deadlock avoided";
67 static const char str_ENAMETOOLONG[] = "file name too long";
68 static const char str_ENOLCK[] = "no locks available";
69 static const char str_ENOSYS[] = "function not implemented";
70 static const char str_ENOTEMPTY[] = "directory not empty";
71 static const char str_EILSEQ[] = "invalid or incomplete multibyte or wide character";
72 static const char str_EADDRINUSE[] = "address already in use";
73 static const char str_EADDRNOTAVAIL[] = "cannot assign requested address";
74 static const char str_EAFNOSUPPORT[] = "address family not supported by protocol";
75 static const char str_EALREADY[] = "operation already in progress";
76 static const char str_EBADMSG[] = "not a data message";
77 static const char str_ECANCELED[] = "operation Canceled";
78 static const char str_ECONNABORTED[] = "software caused connection abort";
79 static const char str_ECONNREFUSED[] = "connection refused";
80 static const char str_ECONNRESET[] = "connection reset by peer";
81 static const char str_EDESTADDRREQ[] = "destination address required";
82 static const char str_EHOSTUNREACH[] = "no route to host";
83 static const char str_EIDRM[] = "identifier removed";
84 static const char str_EINPROGRESS[] = "operation now in progress";
85 static const char str_EISCONN[] = "transport endpoint is already connected";
86 static const char str_ELOOP[] = "too many symbolic links encountered";
87 static const char str_EMSGSIZE[] = "message too long";
88 static const char str_ENETDOWN[] = "network is down";
89 static const char str_ENETRESET[] = "network dropped connection because of reset";
90 static const char str_ENETUNREACH[] = "network is unreachable";
91 static const char str_ENOBUFS[] = "no buffer space available";
92 static const char str_ENODATA[] = "no data available";
93 static const char str_ENOLINK[] = "link has been severed";
94 static const char str_ENOMSG[] = "no message of desired type";
95 static const char str_ENOPROTOOPT[] = "protocol not available";
96 static const char str_ENOSR[] = "out of streams resources";
97 static const char str_ENOSTR[] = "device not a stream";
98 static const char str_ENOTCONN[] = "transport endpoint is not connected";
99 static const char str_ENOTRECOVERABLE[] = "state not recoverable";
100 static const char str_ENOTSOCK[] = "socket operation on non-socket";
101 static const char str_ENOTSUP[] = "not supported";
102 static const char str_EOPNOTSUPP[] = "operation not supported on transport endpoint";
103 static const char str_EOVERFLOW[] = "value too large for defined data type";
104 static const char str_EOWNERDEAD[] = "owner died";
105 static const char str_EPROTO[] = "protocol error";
106 static const char str_EPROTONOSUPPORT[] = "protocol not supported";
107 static const char str_EPROTOTYPE[] = "protocol wrong type for socket";
108 static const char str_ETIME[] = "timer expired";
109 static const char str_ETIMEDOUT[] = "connection timed out";
110 static const char str_ETXTBSY[] = "text file busy";
111 static const char str_EWOULDBLOCK[] = "operation would block";
113 static const struct {
114 int err;
115 const char *str;
116 } syserror_map[] =
118 {EPERM, str_EPERM},
119 {ENOENT, str_ENOENT},
120 {ESRCH, str_ESRCH},
121 {EINTR, str_EINTR},
122 {EIO, str_EIO},
123 {ENXIO, str_ENXIO},
124 {E2BIG, str_E2BIG},
125 {ENOEXEC, str_ENOEXEC},
126 {EBADF, str_EBADF},
127 {ECHILD, str_ECHILD},
128 {EAGAIN, str_EAGAIN},
129 {ENOMEM, str_ENOMEM},
130 {EACCES, str_EACCES},
131 {EFAULT, str_EFAULT},
132 {EBUSY, str_EBUSY},
133 {EEXIST, str_EEXIST},
134 {EXDEV, str_EXDEV},
135 {ENODEV, str_ENODEV},
136 {ENOTDIR, str_ENOTDIR},
137 {EISDIR, str_EISDIR},
138 {EINVAL, str_EINVAL},
139 {ENFILE, str_ENFILE},
140 {EMFILE, str_EMFILE},
141 {ENOTTY, str_ENOTTY},
142 {EFBIG, str_EFBIG},
143 {ENOSPC, str_ENOSPC},
144 {ESPIPE, str_ESPIPE},
145 {EROFS, str_EROFS},
146 {EMLINK, str_EMLINK},
147 {EPIPE, str_EPIPE},
148 {EDOM, str_EDOM},
149 {ERANGE, str_ERANGE},
150 {EDEADLK, str_EDEADLK},
151 {ENAMETOOLONG, str_ENAMETOOLONG},
152 {ENOLCK, str_ENOLCK},
153 {ENOSYS, str_ENOSYS},
154 {ENOTEMPTY, str_ENOTEMPTY},
155 {EILSEQ, str_EILSEQ},
156 {EADDRINUSE, str_EADDRINUSE},
157 {EADDRNOTAVAIL, str_EADDRNOTAVAIL},
158 {EAFNOSUPPORT, str_EAFNOSUPPORT},
159 {EALREADY, str_EALREADY},
160 {EBADMSG, str_EBADMSG},
161 {ECANCELED, str_ECANCELED},
162 {ECONNABORTED, str_ECONNABORTED},
163 {ECONNREFUSED, str_ECONNREFUSED},
164 {ECONNRESET, str_ECONNRESET},
165 {EDESTADDRREQ, str_EDESTADDRREQ},
166 {EHOSTUNREACH, str_EHOSTUNREACH},
167 {EIDRM, str_EIDRM},
168 {EINPROGRESS, str_EINPROGRESS},
169 {EISCONN, str_EISCONN},
170 {ELOOP, str_ELOOP},
171 {EMSGSIZE, str_EMSGSIZE},
172 {ENETDOWN, str_ENETDOWN},
173 {ENETRESET, str_ENETRESET},
174 {ENETUNREACH, str_ENETUNREACH},
175 {ENOBUFS, str_ENOBUFS},
176 {ENODATA, str_ENODATA},
177 {ENOLINK, str_ENOLINK},
178 {ENOMSG, str_ENOMSG},
179 {ENOPROTOOPT, str_ENOPROTOOPT},
180 {ENOSR, str_ENOSR},
181 {ENOSTR, str_ENOSTR},
182 {ENOTCONN, str_ENOTCONN},
183 {ENOTRECOVERABLE, str_ENOTRECOVERABLE},
184 {ENOTSOCK, str_ENOTSOCK},
185 {ENOTSUP, str_ENOTSUP},
186 {EOPNOTSUPP, str_EOPNOTSUPP},
187 {EOVERFLOW, str_EOVERFLOW},
188 {EOWNERDEAD, str_EOWNERDEAD},
189 {EPROTO, str_EPROTO},
190 {EPROTONOSUPPORT, str_EPROTONOSUPPORT},
191 {EPROTOTYPE, str_EPROTOTYPE},
192 {ETIME, str_ETIME},
193 {ETIMEDOUT, str_ETIMEDOUT},
194 {ETXTBSY, str_ETXTBSY},
195 {EWOULDBLOCK, str_EWOULDBLOCK},
197 #endif
199 #if _MSVCP_VER >= 140
200 static const struct {
201 int winerr;
202 int doserr;
203 } winerror_map[] =
205 {ERROR_INVALID_FUNCTION, ENOSYS}, {ERROR_FILE_NOT_FOUND, ENOENT},
206 {ERROR_PATH_NOT_FOUND, ENOENT}, {ERROR_TOO_MANY_OPEN_FILES, EMFILE},
207 {ERROR_ACCESS_DENIED, EACCES}, {ERROR_INVALID_HANDLE, EINVAL},
208 {ERROR_NOT_ENOUGH_MEMORY, ENOMEM}, {ERROR_INVALID_ACCESS, EACCES},
209 {ERROR_OUTOFMEMORY, ENOMEM}, {ERROR_INVALID_DRIVE, ENODEV},
210 {ERROR_CURRENT_DIRECTORY, EACCES}, {ERROR_NOT_SAME_DEVICE, EXDEV},
211 {ERROR_WRITE_PROTECT, EACCES}, {ERROR_BAD_UNIT, ENODEV},
212 {ERROR_NOT_READY, EAGAIN}, {ERROR_SEEK, EIO}, {ERROR_WRITE_FAULT, EIO},
213 {ERROR_READ_FAULT, EIO}, {ERROR_SHARING_VIOLATION, EACCES},
214 {ERROR_LOCK_VIOLATION, ENOLCK}, {ERROR_HANDLE_DISK_FULL, ENOSPC},
215 {ERROR_NOT_SUPPORTED, ENOTSUP}, {ERROR_DEV_NOT_EXIST, ENODEV},
216 {ERROR_FILE_EXISTS, EEXIST}, {ERROR_CANNOT_MAKE, EACCES},
217 {ERROR_INVALID_PARAMETER, EINVAL}, {ERROR_OPEN_FAILED, EIO},
218 {ERROR_BUFFER_OVERFLOW, ENAMETOOLONG}, {ERROR_DISK_FULL, ENOSPC},
219 {ERROR_INVALID_NAME, ENOENT}, {ERROR_NEGATIVE_SEEK, EINVAL},
220 {ERROR_BUSY_DRIVE, EBUSY}, {ERROR_DIR_NOT_EMPTY, ENOTEMPTY},
221 {ERROR_BUSY, EBUSY}, {ERROR_ALREADY_EXISTS, EEXIST},
222 {ERROR_LOCKED, ENOLCK}, {ERROR_DIRECTORY, EINVAL},
223 {ERROR_OPERATION_ABORTED, ECANCELED}, {ERROR_NOACCESS, EACCES},
224 {ERROR_CANTOPEN, EIO}, {ERROR_CANTREAD, EIO}, {ERROR_CANTWRITE, EIO},
225 {ERROR_RETRY, EAGAIN}, {ERROR_OPEN_FILES, EBUSY},
226 {ERROR_DEVICE_IN_USE, EBUSY}, {ERROR_REPARSE_TAG_INVALID, EINVAL},
227 {WSAEINTR, EINTR}, {WSAEBADF, EBADF}, {WSAEACCES, EACCES},
228 {WSAEFAULT, EFAULT}, {WSAEINVAL, EINVAL}, {WSAEMFILE, EMFILE},
229 {WSAEWOULDBLOCK, EWOULDBLOCK}, {WSAEINPROGRESS, EINPROGRESS},
230 {WSAEALREADY, EALREADY}, {WSAENOTSOCK, ENOTSOCK},
231 {WSAEDESTADDRREQ, EDESTADDRREQ}, {WSAEMSGSIZE, EMSGSIZE},
232 {WSAEPROTOTYPE, EPROTOTYPE}, {WSAENOPROTOOPT, ENOPROTOOPT},
233 {WSAEPROTONOSUPPORT, EPROTONOSUPPORT}, {WSAEOPNOTSUPP, EOPNOTSUPP},
234 {WSAEAFNOSUPPORT, EAFNOSUPPORT}, {WSAEADDRINUSE, EADDRINUSE},
235 {WSAEADDRNOTAVAIL, EADDRNOTAVAIL}, {WSAENETDOWN, ENETDOWN},
236 {WSAENETUNREACH, ENETUNREACH}, {WSAENETRESET, ENETRESET},
237 {WSAECONNABORTED, ECONNABORTED}, {WSAECONNRESET, ECONNRESET},
238 {WSAENOBUFS, ENOBUFS}, {WSAEISCONN, EISCONN}, {WSAENOTCONN, ENOTCONN},
239 {WSAETIMEDOUT, ETIMEDOUT}, {WSAECONNREFUSED, ECONNREFUSED},
240 {WSAENAMETOOLONG, ENAMETOOLONG}, {WSAEHOSTUNREACH, EHOSTUNREACH}
242 #endif
244 struct __Container_proxy;
246 typedef struct {
247 struct __Container_proxy *proxy;
248 } _Container_base12;
250 typedef struct __Iterator_base12 {
251 struct __Container_proxy *proxy;
252 struct __Iterator_base12 *next;
253 } _Iterator_base12;
255 typedef struct __Container_proxy {
256 const _Container_base12 *cont;
257 _Iterator_base12 *head;
258 } _Container_proxy;
260 /* ??0_Mutex@std@@QAE@XZ */
261 /* ??0_Mutex@std@@QEAA@XZ */
262 DEFINE_THISCALL_WRAPPER(mutex_ctor, 4)
263 mutex* __thiscall mutex_ctor(mutex *this)
265 CRITICAL_SECTION *cs = operator_new(sizeof(*cs));
266 InitializeCriticalSection(cs);
267 cs->DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": _Mutex critical section");
268 this->mutex = cs;
269 return this;
272 /* ??1_Mutex@std@@QAE@XZ */
273 /* ??1_Mutex@std@@QEAA@XZ */
274 DEFINE_THISCALL_WRAPPER(mutex_dtor, 4)
275 void __thiscall mutex_dtor(mutex *this)
277 ((CRITICAL_SECTION*)this->mutex)->DebugInfo->Spare[0] = 0;
278 DeleteCriticalSection(this->mutex);
279 operator_delete(this->mutex);
282 /* ?_Lock@_Mutex@std@@QAEXXZ */
283 /* ?_Lock@_Mutex@std@@QEAAXXZ */
284 DEFINE_THISCALL_WRAPPER(mutex_lock, 4)
285 void __thiscall mutex_lock(mutex *this)
287 EnterCriticalSection(this->mutex);
290 /* ?_Unlock@_Mutex@std@@QAEXXZ */
291 /* ?_Unlock@_Mutex@std@@QEAAXXZ */
292 DEFINE_THISCALL_WRAPPER(mutex_unlock, 4)
293 void __thiscall mutex_unlock(mutex *this)
295 LeaveCriticalSection(this->mutex);
298 /* ?_Mutex_Lock@_Mutex@std@@CAXPAV12@@Z */
299 /* ?_Mutex_Lock@_Mutex@std@@CAXPEAV12@@Z */
300 void CDECL mutex_mutex_lock(mutex *m)
302 mutex_lock(m);
305 /* ?_Mutex_Unlock@_Mutex@std@@CAXPAV12@@Z */
306 /* ?_Mutex_Unlock@_Mutex@std@@CAXPEAV12@@Z */
307 void CDECL mutex_mutex_unlock(mutex *m)
309 mutex_unlock(m);
312 /* ?_Mutex_ctor@_Mutex@std@@CAXPAV12@@Z */
313 /* ?_Mutex_ctor@_Mutex@std@@CAXPEAV12@@Z */
314 void CDECL mutex_mutex_ctor(mutex *m)
316 mutex_ctor(m);
319 /* ?_Mutex_dtor@_Mutex@std@@CAXPAV12@@Z */
320 /* ?_Mutex_dtor@_Mutex@std@@CAXPEAV12@@Z */
321 void CDECL mutex_mutex_dtor(mutex *m)
323 mutex_dtor(m);
326 static CRITICAL_SECTION lockit_cs[_MAX_LOCK];
328 static LONG init_locks;
329 static CRITICAL_SECTION init_locks_cs;
330 static CRITICAL_SECTION_DEBUG init_locks_cs_debug =
332 0, 0, &init_locks_cs,
333 { &init_locks_cs_debug.ProcessLocksList, &init_locks_cs_debug.ProcessLocksList },
334 0, 0, { (DWORD_PTR)(__FILE__ ": init_locks_cs") }
336 static CRITICAL_SECTION init_locks_cs = { &init_locks_cs_debug, -1, 0, 0, 0, 0 };
338 /* ?_Init_locks_ctor@_Init_locks@std@@CAXPAV12@@Z */
339 /* ?_Init_locks_ctor@_Init_locks@std@@CAXPEAV12@@Z */
340 void __cdecl _Init_locks__Init_locks_ctor(_Init_locks *this)
342 int i;
344 EnterCriticalSection(&init_locks_cs);
345 if (!init_locks)
347 for(i=0; i<_MAX_LOCK; i++)
349 InitializeCriticalSection(&lockit_cs[i]);
350 lockit_cs[i].DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": _Lockit critical section");
353 init_locks++;
354 LeaveCriticalSection(&init_locks_cs);
357 /* ??0_Init_locks@std@@QAE@XZ */
358 /* ??0_Init_locks@std@@QEAA@XZ */
359 DEFINE_THISCALL_WRAPPER(_Init_locks_ctor, 4)
360 _Init_locks* __thiscall _Init_locks_ctor(_Init_locks *this)
362 _Init_locks__Init_locks_ctor(this);
363 return this;
366 /* ?_Init_locks_dtor@_Init_locks@std@@CAXPAV12@@Z */
367 /* ?_Init_locks_dtor@_Init_locks@std@@CAXPEAV12@@Z */
368 void __cdecl _Init_locks__Init_locks_dtor(_Init_locks *this)
370 int i;
372 EnterCriticalSection(&init_locks_cs);
373 init_locks--;
374 if (!init_locks)
376 for(i=0; i<_MAX_LOCK; i++)
378 lockit_cs[i].DebugInfo->Spare[0] = 0;
379 DeleteCriticalSection(&lockit_cs[i]);
382 LeaveCriticalSection(&init_locks_cs);
385 /* ??1_Init_locks@std@@QAE@XZ */
386 /* ??1_Init_locks@std@@QEAA@XZ */
387 DEFINE_THISCALL_WRAPPER(_Init_locks_dtor, 4)
388 void __thiscall _Init_locks_dtor(_Init_locks *this)
390 _Init_locks__Init_locks_dtor(this);
393 #if _MSVCP_VER >= 70
394 static inline int get_locktype( _Lockit *lockit ) { return lockit->locktype; }
395 static inline void set_locktype( _Lockit *lockit, int type ) { lockit->locktype = type; }
396 #else
397 static inline int get_locktype( _Lockit *lockit ) { return 0; }
398 static inline void set_locktype( _Lockit *lockit, int type ) { }
399 #endif
401 /* ?_Lockit_ctor@_Lockit@std@@SAXH@Z */
402 void __cdecl _Lockit__Lockit_ctor_lock(int locktype)
404 EnterCriticalSection(&lockit_cs[locktype]);
407 /* ?_Lockit_ctor@_Lockit@std@@CAXPAV12@H@Z */
408 /* ?_Lockit_ctor@_Lockit@std@@CAXPEAV12@H@Z */
409 void __cdecl _Lockit__Lockit_ctor_locktype(_Lockit *lockit, int locktype)
411 set_locktype( lockit, locktype );
412 _Lockit__Lockit_ctor_lock(locktype);
415 /* ?_Lockit_ctor@_Lockit@std@@CAXPAV12@@Z */
416 /* ?_Lockit_ctor@_Lockit@std@@CAXPEAV12@@Z */
417 void __cdecl _Lockit__Lockit_ctor(_Lockit *lockit)
419 _Lockit__Lockit_ctor_locktype(lockit, 0);
422 /* ??0_Lockit@std@@QAE@H@Z */
423 /* ??0_Lockit@std@@QEAA@H@Z */
424 DEFINE_THISCALL_WRAPPER(_Lockit_ctor_locktype, 8)
425 _Lockit* __thiscall _Lockit_ctor_locktype(_Lockit *this, int locktype)
427 _Lockit__Lockit_ctor_locktype(this, locktype);
428 return this;
431 /* ??0_Lockit@std@@QAE@XZ */
432 /* ??0_Lockit@std@@QEAA@XZ */
433 DEFINE_THISCALL_WRAPPER(_Lockit_ctor, 4)
434 _Lockit* __thiscall _Lockit_ctor(_Lockit *this)
436 _Lockit__Lockit_ctor_locktype(this, 0);
437 return this;
440 /* ?_Lockit_dtor@_Lockit@std@@SAXH@Z */
441 void __cdecl _Lockit__Lockit_dtor_unlock(int locktype)
443 LeaveCriticalSection(&lockit_cs[locktype]);
446 /* ?_Lockit_dtor@_Lockit@std@@CAXPAV12@@Z */
447 /* ?_Lockit_dtor@_Lockit@std@@CAXPEAV12@@Z */
448 void __cdecl _Lockit__Lockit_dtor(_Lockit *lockit)
450 _Lockit__Lockit_dtor_unlock(get_locktype( lockit ));
453 /* ??1_Lockit@std@@QAE@XZ */
454 /* ??1_Lockit@std@@QEAA@XZ */
455 DEFINE_THISCALL_WRAPPER(_Lockit_dtor, 4)
456 void __thiscall _Lockit_dtor(_Lockit *this)
458 _Lockit__Lockit_dtor(this);
461 /* wctype */
462 unsigned short __cdecl wctype(const char *property)
464 static const struct {
465 const char *name;
466 unsigned short mask;
467 } properties[] = {
468 { "alnum", _DIGIT|_ALPHA },
469 { "alpha", _ALPHA },
470 { "cntrl", _CONTROL },
471 { "digit", _DIGIT },
472 { "graph", _DIGIT|_PUNCT|_ALPHA },
473 { "lower", _LOWER },
474 { "print", _DIGIT|_PUNCT|_BLANK|_ALPHA },
475 { "punct", _PUNCT },
476 { "space", _SPACE },
477 { "upper", _UPPER },
478 { "xdigit", _HEX }
480 unsigned int i;
482 for(i = 0; i < ARRAY_SIZE(properties); i++)
483 if(!strcmp(property, properties[i].name))
484 return properties[i].mask;
486 return 0;
489 typedef void (__cdecl *MSVCP_new_handler_func)(void);
490 static MSVCP_new_handler_func MSVCP_new_handler;
491 static int __cdecl new_handler_wrapper(size_t unused)
493 MSVCP_new_handler();
494 return 1;
497 /* ?set_new_handler@std@@YAP6AXXZP6AXXZ@Z */
498 MSVCP_new_handler_func __cdecl set_new_handler(MSVCP_new_handler_func new_handler)
500 MSVCP_new_handler_func old_handler = MSVCP_new_handler;
502 TRACE("%p\n", new_handler);
504 MSVCP_new_handler = new_handler;
505 MSVCRT_set_new_handler(new_handler ? new_handler_wrapper : NULL);
506 return old_handler;
509 /* ?set_new_handler@std@@YAP6AXXZH@Z */
510 MSVCP_new_handler_func __cdecl set_new_handler_reset(int unused)
512 return set_new_handler(NULL);
515 /* _Container_base0 is used by apps compiled without iterator checking
516 * (i.e. with _ITERATOR_DEBUG_LEVEL=0 ).
517 * It provides empty versions of methods used by visual c++'s stl's
518 * iterator checking.
519 * msvcr100 has to provide them in case apps are compiled with /Od
520 * or the optimizer fails to inline those (empty) calls.
523 /* ?_Orphan_all@_Container_base0@std@@QAEXXZ */
524 /* ?_Orphan_all@_Container_base0@std@@QEAAXXZ */
525 DEFINE_THISCALL_WRAPPER(Container_base0_Orphan_all, 4)
526 void __thiscall Container_base0_Orphan_all(void *this)
530 /* ?_Swap_all@_Container_base0@std@@QAEXAAU12@@Z */
531 /* ?_Swap_all@_Container_base0@std@@QEAAXAEAU12@@Z */
532 DEFINE_THISCALL_WRAPPER(Container_base0_Swap_all, 8)
533 void __thiscall Container_base0_Swap_all(void *this, void *that)
537 /* ??4_Container_base0@std@@QAEAAU01@ABU01@@Z */
538 /* ??4_Container_base0@std@@QEAAAEAU01@AEBU01@@Z */
539 DEFINE_THISCALL_WRAPPER(Container_base0_op_assign, 8)
540 void* __thiscall Container_base0_op_assign(void *this, const void *that)
542 return this;
545 /* ??0_Container_base12@std@@QAE@ABU01@@Z */
546 /* ??0_Container_base12@std@@QEAA@AEBU01@@Z */
547 DEFINE_THISCALL_WRAPPER(_Container_base12_copy_ctor, 8)
548 _Container_base12* __thiscall _Container_base12_copy_ctor(
549 _Container_base12 *this, _Container_base12 *that)
551 this->proxy = NULL;
552 return this;
555 /* ??0_Container_base12@std@@QAE@XZ */
556 /* ??0_Container_base12@std@@QEAA@XZ */
557 DEFINE_THISCALL_WRAPPER(_Container_base12_ctor, 4)
558 _Container_base12* __thiscall _Container_base12_ctor(_Container_base12 *this)
560 this->proxy = NULL;
561 return this;
564 /* ??1_Container_base12@std@@QAE@XZ */
565 /* ??1_Container_base12@std@@QEAA@XZ */
566 DEFINE_THISCALL_WRAPPER(_Container_base12_dtor, 4)
567 void __thiscall _Container_base12_dtor(_Container_base12 *this)
571 /* ??4_Container_base12@std@@QAEAAU01@ABU01@@Z */
572 /* ??4_Container_base12@std@@QEAAAEAU01@AEBU01@@ */
573 DEFINE_THISCALL_WRAPPER(_Container_base12_op_assign, 8)
574 _Container_base12* __thiscall _Container_base12_op_assign(
575 _Container_base12 *this, const _Container_base12 *that)
577 return this;
580 /* ?_Getpfirst@_Container_base12@std@@QBEPAPAU_Iterator_base12@2@XZ */
581 /* ?_Getpfirst@_Container_base12@std@@QEBAPEAPEAU_Iterator_base12@2@XZ */
582 DEFINE_THISCALL_WRAPPER(_Container_base12__Getpfirst, 4)
583 _Iterator_base12** __thiscall _Container_base12__Getpfirst(_Container_base12 *this)
585 return this->proxy ? &this->proxy->head : NULL;
588 /* ?_Orphan_all@_Container_base12@std@@QAEXXZ */
589 /* ?_Orphan_all@_Container_base12@std@@QEAAXXZ */
590 DEFINE_THISCALL_WRAPPER(_Container_base12__Orphan_all, 4)
591 void __thiscall _Container_base12__Orphan_all(_Container_base12 *this)
595 /* ?_Swap_all@_Container_base12@std@@QAEXAAU12@@Z */
596 /* ?_Swap_all@_Container_base12@std@@QEAAXAEAU12@@Z */
597 DEFINE_THISCALL_WRAPPER(_Container_base12__Swap_all, 8)
598 void __thiscall _Container_base12__Swap_all(
599 _Container_base12 *this, _Container_base12 *that)
601 _Container_proxy *tmp;
603 tmp = this->proxy;
604 this->proxy = that->proxy;
605 that->proxy = tmp;
607 if(this->proxy)
608 this->proxy->cont = this;
609 if(that->proxy)
610 that->proxy->cont = that;
613 #if _MSVCP_VER >= 110
615 #define SECSPERDAY 86400
616 /* 1601 to 1970 is 369 years plus 89 leap days */
617 #define SECS_1601_TO_1970 ((369 * 365 + 89) * (ULONGLONG)SECSPERDAY)
618 #define TICKSPERSEC 10000000
619 #define TICKS_1601_TO_1970 (SECS_1601_TO_1970 * TICKSPERSEC)
620 #define NANOSEC_PER_MILLISEC 1000000
621 #define MILLISEC_PER_SEC 1000
622 #define NANOSEC_PER_SEC (NANOSEC_PER_MILLISEC * MILLISEC_PER_SEC)
624 typedef int MSVCRT_long;
626 /* xtime */
627 typedef struct {
628 __time64_t sec;
629 MSVCRT_long nsec;
630 } xtime;
632 /* _Xtime_get_ticks */
633 LONGLONG __cdecl _Xtime_get_ticks(void)
635 FILETIME ft;
637 TRACE("\n");
639 GetSystemTimeAsFileTime(&ft);
640 return ((LONGLONG)ft.dwHighDateTime<<32) + ft.dwLowDateTime - TICKS_1601_TO_1970;
643 /* _xtime_get */
644 int __cdecl xtime_get(xtime* t, int unknown)
646 LONGLONG ticks;
648 TRACE("(%p)\n", t);
650 if(unknown != 1)
651 return 0;
653 ticks = _Xtime_get_ticks();
654 t->sec = ticks / TICKSPERSEC;
655 t->nsec = ticks % TICKSPERSEC * 100;
656 return 1;
659 /* _Xtime_diff_to_millis2 */
660 MSVCRT_long __cdecl _Xtime_diff_to_millis2(const xtime *t1, const xtime *t2)
662 LONGLONG diff_sec, diff_nsec;
664 TRACE("(%p, %p)\n", t1, t2);
666 diff_sec = t1->sec - t2->sec;
667 diff_nsec = t1->nsec - t2->nsec;
669 diff_sec += diff_nsec / NANOSEC_PER_SEC;
670 diff_nsec %= NANOSEC_PER_SEC;
671 if (diff_nsec < 0) {
672 diff_sec -= 1;
673 diff_nsec += NANOSEC_PER_SEC;
676 if (diff_sec<0 || (diff_sec==0 && diff_nsec<0))
677 return 0;
678 return diff_sec * MILLISEC_PER_SEC +
679 (diff_nsec + NANOSEC_PER_MILLISEC - 1) / NANOSEC_PER_MILLISEC;
682 /* _Xtime_diff_to_millis */
683 MSVCRT_long __cdecl _Xtime_diff_to_millis(const xtime *t)
685 xtime now;
687 TRACE("%p\n", t);
689 xtime_get(&now, 1);
690 return _Xtime_diff_to_millis2(t, &now);
692 #endif
694 #if _MSVCP_VER >= 90
695 unsigned int __cdecl _Random_device(void)
697 unsigned int ret;
699 TRACE("\n");
701 /* TODO: throw correct exception in case of failure */
702 if(rand_s(&ret))
703 throw_exception("random number generator failed\n");
704 return ret;
706 #endif
708 #if _MSVCP_VER >= 110
709 #ifdef __ASM_USE_THISCALL_WRAPPER
711 extern void *call_thiscall_func;
712 __ASM_GLOBAL_FUNC(call_thiscall_func,
713 "popl %eax\n\t"
714 "popl %edx\n\t"
715 "popl %ecx\n\t"
716 "pushl %eax\n\t"
717 "jmp *%edx\n\t")
719 #define call_func1(func,this) ((void* (WINAPI*)(void*,void*))&call_thiscall_func)(func,this)
721 #else /* __i386__ */
723 #define call_func1(func,this) func(this)
725 #endif /* __i386__ */
727 #define MTX_PLAIN 0x1
728 #define MTX_TRY 0x2
729 #define MTX_TIMED 0x4
730 #define MTX_RECURSIVE 0x100
731 #define MTX_LOCKED 3
732 typedef struct
734 DWORD flags;
735 critical_section cs;
736 DWORD thread_id;
737 DWORD count;
738 } *_Mtx_t;
740 #if _MSVCP_VER >= 140
741 typedef _Mtx_t _Mtx_arg_t;
742 #define MTX_T_FROM_ARG(m) (m)
743 #define MTX_T_TO_ARG(m) (m)
744 #else
745 typedef _Mtx_t *_Mtx_arg_t;
746 #define MTX_T_FROM_ARG(m) (*(m))
747 #define MTX_T_TO_ARG(m) (&(m))
748 #endif
750 void __cdecl _Mtx_init_in_situ(_Mtx_t mtx, int flags)
752 if(flags & ~(MTX_PLAIN | MTX_TRY | MTX_TIMED | MTX_RECURSIVE))
753 FIXME("unknown flags ignored: %x\n", flags);
755 mtx->flags = flags;
756 call_func1(critical_section_ctor, &mtx->cs);
757 mtx->thread_id = -1;
758 mtx->count = 0;
761 int __cdecl _Mtx_init(_Mtx_t *mtx, int flags)
763 *mtx = operator_new(sizeof(**mtx));
764 _Mtx_init_in_situ(*mtx, flags);
765 return 0;
768 void __cdecl _Mtx_destroy_in_situ(_Mtx_t mtx)
770 call_func1(critical_section_dtor, &mtx->cs);
773 void __cdecl _Mtx_destroy(_Mtx_arg_t mtx)
775 call_func1(critical_section_dtor, &MTX_T_FROM_ARG(mtx)->cs);
776 operator_delete(MTX_T_FROM_ARG(mtx));
779 int __cdecl _Mtx_current_owns(_Mtx_arg_t mtx)
781 return MTX_T_FROM_ARG(mtx)->thread_id == GetCurrentThreadId();
784 int __cdecl _Mtx_lock(_Mtx_arg_t mtx)
786 if(MTX_T_FROM_ARG(mtx)->thread_id != GetCurrentThreadId()) {
787 call_func1(critical_section_lock, &MTX_T_FROM_ARG(mtx)->cs);
788 MTX_T_FROM_ARG(mtx)->thread_id = GetCurrentThreadId();
789 }else if(!(MTX_T_FROM_ARG(mtx)->flags & MTX_RECURSIVE)
790 && MTX_T_FROM_ARG(mtx)->flags != MTX_PLAIN) {
791 return MTX_LOCKED;
794 MTX_T_FROM_ARG(mtx)->count++;
795 return 0;
798 int __cdecl _Mtx_unlock(_Mtx_arg_t mtx)
800 if(--MTX_T_FROM_ARG(mtx)->count)
801 return 0;
803 MTX_T_FROM_ARG(mtx)->thread_id = -1;
804 call_func1(critical_section_unlock, &MTX_T_FROM_ARG(mtx)->cs);
805 return 0;
808 int __cdecl _Mtx_trylock(_Mtx_arg_t mtx)
810 if(MTX_T_FROM_ARG(mtx)->thread_id != GetCurrentThreadId()) {
811 if(!call_func1(critical_section_trylock, &MTX_T_FROM_ARG(mtx)->cs))
812 return MTX_LOCKED;
813 MTX_T_FROM_ARG(mtx)->thread_id = GetCurrentThreadId();
814 }else if(!(MTX_T_FROM_ARG(mtx)->flags & MTX_RECURSIVE)
815 && MTX_T_FROM_ARG(mtx)->flags != MTX_PLAIN) {
816 return MTX_LOCKED;
819 MTX_T_FROM_ARG(mtx)->count++;
820 return 0;
823 critical_section* __cdecl _Mtx_getconcrtcs(_Mtx_arg_t mtx)
825 return &MTX_T_FROM_ARG(mtx)->cs;
828 static inline LONG interlocked_dec_if_nonzero( LONG *dest )
830 LONG val, tmp;
831 for (val = *dest;; val = tmp)
833 if (!val || (tmp = InterlockedCompareExchange( dest, val - 1, val )) == val)
834 break;
836 return val;
839 #define CND_TIMEDOUT 2
841 typedef struct
843 CONDITION_VARIABLE cv;
844 } *_Cnd_t;
846 #if _MSVCP_VER >= 140
847 typedef _Cnd_t _Cnd_arg_t;
848 #define CND_T_FROM_ARG(c) (c)
849 #define CND_T_TO_ARG(c) (c)
850 #else
851 typedef _Cnd_t *_Cnd_arg_t;
852 #define CND_T_FROM_ARG(c) (*(c))
853 #define CND_T_TO_ARG(c) (&(c))
854 #endif
856 static HANDLE keyed_event;
858 void __cdecl _Cnd_init_in_situ(_Cnd_t cnd)
860 InitializeConditionVariable(&cnd->cv);
862 if(!keyed_event) {
863 HANDLE event;
865 NtCreateKeyedEvent(&event, GENERIC_READ|GENERIC_WRITE, NULL, 0);
866 if(InterlockedCompareExchangePointer(&keyed_event, event, NULL) != NULL)
867 NtClose(event);
871 int __cdecl _Cnd_init(_Cnd_t *cnd)
873 *cnd = operator_new(sizeof(**cnd));
874 _Cnd_init_in_situ(*cnd);
875 return 0;
878 int __cdecl _Cnd_wait(_Cnd_arg_t cnd, _Mtx_arg_t mtx)
880 CONDITION_VARIABLE *cv = &CND_T_FROM_ARG(cnd)->cv;
882 InterlockedExchangeAdd( (LONG *)&cv->Ptr, 1 );
883 _Mtx_unlock(mtx);
885 NtWaitForKeyedEvent(keyed_event, &cv->Ptr, FALSE, NULL);
887 _Mtx_lock(mtx);
888 return 0;
891 int __cdecl _Cnd_timedwait(_Cnd_arg_t cnd, _Mtx_arg_t mtx, const xtime *xt)
893 CONDITION_VARIABLE *cv = &CND_T_FROM_ARG(cnd)->cv;
894 LARGE_INTEGER timeout;
895 NTSTATUS status;
897 InterlockedExchangeAdd( (LONG *)&cv->Ptr, 1 );
898 _Mtx_unlock(mtx);
900 timeout.QuadPart = (ULONGLONG)(ULONG)_Xtime_diff_to_millis(xt) * -10000;
901 status = NtWaitForKeyedEvent(keyed_event, &cv->Ptr, FALSE, &timeout);
902 if (status)
904 if (!interlocked_dec_if_nonzero( (LONG *)&cv->Ptr ))
905 status = NtWaitForKeyedEvent( keyed_event, &cv->Ptr, FALSE, NULL );
908 _Mtx_lock(mtx);
909 return status ? CND_TIMEDOUT : 0;
912 int __cdecl _Cnd_broadcast(_Cnd_arg_t cnd)
914 CONDITION_VARIABLE *cv = &CND_T_FROM_ARG(cnd)->cv;
915 LONG val = InterlockedExchange( (LONG *)&cv->Ptr, 0 );
916 while (val-- > 0)
917 NtReleaseKeyedEvent( keyed_event, &cv->Ptr, FALSE, NULL );
918 return 0;
921 int __cdecl _Cnd_signal(_Cnd_arg_t cnd)
923 CONDITION_VARIABLE *cv = &CND_T_FROM_ARG(cnd)->cv;
924 if (interlocked_dec_if_nonzero( (LONG *)&cv->Ptr ))
925 NtReleaseKeyedEvent( keyed_event, &cv->Ptr, FALSE, NULL );
926 return 0;
929 void __cdecl _Cnd_destroy_in_situ(_Cnd_t cnd)
931 _Cnd_broadcast(CND_T_TO_ARG(cnd));
934 void __cdecl _Cnd_destroy(_Cnd_arg_t cnd)
936 if(cnd) {
937 _Cnd_broadcast(cnd);
938 operator_delete(CND_T_FROM_ARG(cnd));
942 static struct {
943 int used;
944 int size;
946 struct _to_broadcast {
947 DWORD thread_id;
948 _Cnd_arg_t cnd;
949 _Mtx_arg_t mtx;
950 int *p;
951 } *to_broadcast;
952 } broadcast_at_thread_exit;
954 static CRITICAL_SECTION broadcast_at_thread_exit_cs;
955 static CRITICAL_SECTION_DEBUG broadcast_at_thread_exit_cs_debug =
957 0, 0, &broadcast_at_thread_exit_cs,
958 { &broadcast_at_thread_exit_cs_debug.ProcessLocksList, &broadcast_at_thread_exit_cs_debug.ProcessLocksList },
959 0, 0, { (DWORD_PTR)(__FILE__ ": broadcast_at_thread_exit_cs") }
961 static CRITICAL_SECTION broadcast_at_thread_exit_cs = { &broadcast_at_thread_exit_cs_debug, -1, 0, 0, 0, 0 };
963 void __cdecl _Cnd_register_at_thread_exit(_Cnd_arg_t cnd, _Mtx_arg_t mtx, int *p)
965 struct _to_broadcast *add;
967 TRACE("(%p %p %p)\n", cnd, mtx, p);
969 EnterCriticalSection(&broadcast_at_thread_exit_cs);
970 if(!broadcast_at_thread_exit.size) {
971 broadcast_at_thread_exit.to_broadcast = HeapAlloc(GetProcessHeap(),
972 0, 8*sizeof(broadcast_at_thread_exit.to_broadcast[0]));
973 if(!broadcast_at_thread_exit.to_broadcast) {
974 LeaveCriticalSection(&broadcast_at_thread_exit_cs);
975 return;
977 broadcast_at_thread_exit.size = 8;
978 } else if(broadcast_at_thread_exit.size == broadcast_at_thread_exit.used) {
979 add = HeapReAlloc(GetProcessHeap(), 0, broadcast_at_thread_exit.to_broadcast,
980 broadcast_at_thread_exit.size*2*sizeof(broadcast_at_thread_exit.to_broadcast[0]));
981 if(!add) {
982 LeaveCriticalSection(&broadcast_at_thread_exit_cs);
983 return;
985 broadcast_at_thread_exit.to_broadcast = add;
986 broadcast_at_thread_exit.size *= 2;
989 add = broadcast_at_thread_exit.to_broadcast + broadcast_at_thread_exit.used++;
990 add->thread_id = GetCurrentThreadId();
991 add->cnd = cnd;
992 add->mtx = mtx;
993 add->p = p;
994 LeaveCriticalSection(&broadcast_at_thread_exit_cs);
997 void __cdecl _Cnd_unregister_at_thread_exit(_Mtx_arg_t mtx)
999 int i;
1001 TRACE("(%p)\n", mtx);
1003 EnterCriticalSection(&broadcast_at_thread_exit_cs);
1004 for(i=0; i<broadcast_at_thread_exit.used; i++) {
1005 if(broadcast_at_thread_exit.to_broadcast[i].mtx != mtx)
1006 continue;
1008 memmove(broadcast_at_thread_exit.to_broadcast+i, broadcast_at_thread_exit.to_broadcast+i+1,
1009 (broadcast_at_thread_exit.used-i-1)*sizeof(broadcast_at_thread_exit.to_broadcast[0]));
1010 broadcast_at_thread_exit.used--;
1011 i--;
1013 LeaveCriticalSection(&broadcast_at_thread_exit_cs);
1016 void __cdecl _Cnd_do_broadcast_at_thread_exit(void)
1018 int i, id = GetCurrentThreadId();
1020 TRACE("()\n");
1022 EnterCriticalSection(&broadcast_at_thread_exit_cs);
1023 for(i=0; i<broadcast_at_thread_exit.used; i++) {
1024 if(broadcast_at_thread_exit.to_broadcast[i].thread_id != id)
1025 continue;
1027 _Mtx_unlock(broadcast_at_thread_exit.to_broadcast[i].mtx);
1028 _Cnd_broadcast(broadcast_at_thread_exit.to_broadcast[i].cnd);
1029 if(broadcast_at_thread_exit.to_broadcast[i].p)
1030 *broadcast_at_thread_exit.to_broadcast[i].p = 1;
1032 memmove(broadcast_at_thread_exit.to_broadcast+i, broadcast_at_thread_exit.to_broadcast+i+1,
1033 (broadcast_at_thread_exit.used-i-1)*sizeof(broadcast_at_thread_exit.to_broadcast[0]));
1034 broadcast_at_thread_exit.used--;
1035 i--;
1037 LeaveCriticalSection(&broadcast_at_thread_exit_cs);
1040 #endif
1042 #if _MSVCP_VER == 100
1043 typedef struct {
1044 const vtable_ptr *vtable;
1045 } error_category;
1047 typedef struct {
1048 error_category base;
1049 const char *type;
1050 } custom_category;
1051 static custom_category iostream_category;
1053 DEFINE_RTTI_DATA0(error_category, 0, ".?AVerror_category@std@@")
1054 DEFINE_RTTI_DATA1(iostream_category, 0, &error_category_rtti_base_descriptor, ".?AV_Iostream_error_category@std@@")
1056 extern const vtable_ptr iostream_category_vtable;
1058 static void iostream_category_ctor(custom_category *this)
1060 this->base.vtable = &iostream_category_vtable;
1061 this->type = "iostream";
1064 DEFINE_THISCALL_WRAPPER(custom_category_vector_dtor, 8)
1065 custom_category* __thiscall custom_category_vector_dtor(custom_category *this, unsigned int flags)
1067 TRACE("(%p %x)\n", this, flags);
1068 if(flags & 2) {
1069 /* we have an array, with the number of elements stored before the first object */
1070 INT_PTR i, *ptr = (INT_PTR *)this-1;
1072 for(i=*ptr-1; i>=0; i--)
1073 operator_delete(ptr);
1074 } else {
1075 if(flags & 1)
1076 operator_delete(this);
1079 return this;
1082 DEFINE_THISCALL_WRAPPER(custom_category_name, 4)
1083 const char* __thiscall custom_category_name(const custom_category *this)
1085 return this->type;
1088 DEFINE_THISCALL_WRAPPER(custom_category_message, 12)
1089 basic_string_char* __thiscall custom_category_message(const custom_category *this,
1090 basic_string_char *ret, int err)
1092 return MSVCP_basic_string_char_ctor_cstr(ret, strerror(err));
1095 DEFINE_THISCALL_WRAPPER(custom_category_default_error_condition, 12)
1096 /*error_condition*/void* __thiscall custom_category_default_error_condition(
1097 custom_category *this, /*error_condition*/void *ret, int code)
1099 FIXME("(%p %p %x) stub\n", this, ret, code);
1100 return NULL;
1103 DEFINE_THISCALL_WRAPPER(custom_category_equivalent, 12)
1104 bool __thiscall custom_category_equivalent(const custom_category *this,
1105 int code, const /*error_condition*/void *condition)
1107 FIXME("(%p %x %p) stub\n", this, code, condition);
1108 return FALSE;
1111 DEFINE_THISCALL_WRAPPER(custom_category_equivalent_code, 12)
1112 bool __thiscall custom_category_equivalent_code(custom_category *this,
1113 const /*error_code*/void *code, int condition)
1115 FIXME("(%p %p %x) stub\n", this, code, condition);
1116 return FALSE;
1119 DEFINE_THISCALL_WRAPPER(iostream_category_message, 12)
1120 basic_string_char* __thiscall iostream_category_message(const custom_category *this,
1121 basic_string_char *ret, int err)
1123 if(err == 1) return MSVCP_basic_string_char_ctor_cstr(ret, "iostream error");
1124 return MSVCP_basic_string_char_ctor_cstr(ret, strerror(err));
1127 /* ?iostream_category@std@@YAABVerror_category@1@XZ */
1128 /* ?iostream_category@std@@YAAEBVerror_category@1@XZ */
1129 const error_category* __cdecl std_iostream_category(void)
1131 TRACE("()\n");
1132 return &iostream_category.base;
1135 static custom_category system_category;
1136 DEFINE_RTTI_DATA1(system_category, 0, &error_category_rtti_base_descriptor, ".?AV_System_error_category@std@@")
1138 extern const vtable_ptr system_category_vtable;
1140 static void system_category_ctor(custom_category *this)
1142 this->base.vtable = &system_category_vtable;
1143 this->type = "system";
1146 /* ?system_category@std@@YAABVerror_category@1@XZ */
1147 /* ?system_category@std@@YAAEBVerror_category@1@XZ */
1148 const error_category* __cdecl std_system_category(void)
1150 TRACE("()\n");
1151 return &system_category.base;
1154 static custom_category generic_category;
1155 DEFINE_RTTI_DATA1(generic_category, 0, &error_category_rtti_base_descriptor, ".?AV_Generic_error_category@std@@")
1157 extern const vtable_ptr generic_category_vtable;
1159 static void generic_category_ctor(custom_category *this)
1161 this->base.vtable = &generic_category_vtable;
1162 this->type = "generic";
1165 /* ?generic_category@std@@YAABVerror_category@1@XZ */
1166 /* ?generic_category@std@@YAAEBVerror_category@1@XZ */
1167 const error_category* __cdecl std_generic_category(void)
1169 TRACE("()\n");
1170 return &generic_category.base;
1172 #endif
1174 #if _MSVCP_VER >= 110
1175 static CRITICAL_SECTION call_once_cs;
1176 static CRITICAL_SECTION_DEBUG call_once_cs_debug =
1178 0, 0, &call_once_cs,
1179 { &call_once_cs_debug.ProcessLocksList, &call_once_cs_debug.ProcessLocksList },
1180 0, 0, { (DWORD_PTR)(__FILE__ ": call_once_cs") }
1182 static CRITICAL_SECTION call_once_cs = { &call_once_cs_debug, -1, 0, 0, 0, 0 };
1184 void __cdecl _Call_onceEx(int *once, void (__cdecl *func)(void*), void *argv)
1186 TRACE("%p %p %p\n", once, func, argv);
1188 EnterCriticalSection(&call_once_cs);
1189 if(!*once) {
1190 /* FIXME: handle exceptions */
1191 func(argv);
1192 *once = 1;
1194 LeaveCriticalSection(&call_once_cs);
1197 static void __cdecl call_once_func_wrapper(void *func)
1199 ((void (__cdecl*)(void))func)();
1202 void __cdecl _Call_once(int *once, void (__cdecl *func)(void))
1204 TRACE("%p %p\n", once, func);
1205 _Call_onceEx(once, call_once_func_wrapper, func);
1208 void __cdecl _Do_call(void *this)
1210 CALL_VTBL_FUNC(this, 0, void, (void*), (this));
1212 #endif
1214 #if _MSVCP_VER >= 110
1215 typedef struct
1217 HANDLE hnd;
1218 DWORD id;
1219 } _Thrd_t;
1221 typedef int (__cdecl *_Thrd_start_t)(void*);
1223 #define _THRD_ERROR 4
1225 int __cdecl _Thrd_equal(_Thrd_t a, _Thrd_t b)
1227 TRACE("(%p %u %p %u)\n", a.hnd, a.id, b.hnd, b.id);
1228 return a.id == b.id;
1231 int __cdecl _Thrd_lt(_Thrd_t a, _Thrd_t b)
1233 TRACE("(%p %u %p %u)\n", a.hnd, a.id, b.hnd, b.id);
1234 return a.id < b.id;
1237 void __cdecl _Thrd_sleep(const xtime *t)
1239 TRACE("(%p)\n", t);
1240 Sleep(_Xtime_diff_to_millis(t));
1243 void __cdecl _Thrd_yield(void)
1245 TRACE("()\n");
1246 Sleep(0);
1249 static _Thrd_t thread_current(void)
1251 _Thrd_t ret;
1253 if(DuplicateHandle(GetCurrentProcess(), GetCurrentThread(),
1254 GetCurrentProcess(), &ret.hnd, 0, FALSE, DUPLICATE_SAME_ACCESS)) {
1255 CloseHandle(ret.hnd);
1256 } else {
1257 ret.hnd = 0;
1259 ret.id = GetCurrentThreadId();
1261 TRACE("(%p %u)\n", ret.hnd, ret.id);
1262 return ret;
1265 #ifndef __i386__
1266 _Thrd_t __cdecl _Thrd_current(void)
1268 return thread_current();
1270 #else
1271 ULONGLONG __cdecl _Thrd_current(void)
1273 union {
1274 _Thrd_t thr;
1275 ULONGLONG ull;
1276 } ret;
1278 C_ASSERT(sizeof(_Thrd_t) <= sizeof(ULONGLONG));
1280 ret.thr = thread_current();
1281 return ret.ull;
1283 #endif
1285 int __cdecl _Thrd_join(_Thrd_t thr, int *code)
1287 TRACE("(%p %u %p)\n", thr.hnd, thr.id, code);
1288 if (WaitForSingleObject(thr.hnd, INFINITE))
1289 return _THRD_ERROR;
1291 if (code)
1292 GetExitCodeThread(thr.hnd, (DWORD *)code);
1294 CloseHandle(thr.hnd);
1295 return 0;
1298 int __cdecl _Thrd_start(_Thrd_t *thr, LPTHREAD_START_ROUTINE proc, void *arg)
1300 TRACE("(%p %p %p)\n", thr, proc, arg);
1301 thr->hnd = CreateThread(NULL, 0, proc, arg, 0, &thr->id);
1302 return thr->hnd ? 0 : _THRD_ERROR;
1305 typedef struct
1307 _Thrd_start_t proc;
1308 void *arg;
1309 } thread_proc_arg;
1311 static DWORD WINAPI thread_proc_wrapper(void *arg)
1313 thread_proc_arg wrapped_arg = *((thread_proc_arg*)arg);
1314 free(arg);
1315 return wrapped_arg.proc(wrapped_arg.arg);
1318 int __cdecl _Thrd_create(_Thrd_t *thr, _Thrd_start_t proc, void *arg)
1320 thread_proc_arg *wrapped_arg;
1321 int ret;
1323 TRACE("(%p %p %p)\n", thr, proc, arg);
1325 wrapped_arg = malloc(sizeof(*wrapped_arg));
1326 if(!wrapped_arg)
1327 return _THRD_ERROR; /* TODO: probably different error should be returned here */
1329 wrapped_arg->proc = proc;
1330 wrapped_arg->arg = arg;
1331 ret = _Thrd_start(thr, thread_proc_wrapper, wrapped_arg);
1332 if(ret) free(wrapped_arg);
1333 return ret;
1336 int __cdecl _Thrd_detach(_Thrd_t thr)
1338 return CloseHandle(thr.hnd) ? 0 : _THRD_ERROR;
1341 typedef struct
1343 const vtable_ptr *vtable;
1344 _Cnd_t cnd;
1345 _Mtx_t mtx;
1346 bool launched;
1347 } _Pad;
1349 DEFINE_RTTI_DATA0(_Pad, 0, ".?AV_Pad@std@@")
1351 /* ??_7_Pad@std@@6B@ */
1352 extern const vtable_ptr _Pad_vtable;
1354 unsigned int __cdecl _Thrd_hardware_concurrency(void)
1356 static unsigned int val = -1;
1358 TRACE("()\n");
1360 if(val == -1) {
1361 SYSTEM_INFO si;
1363 GetSystemInfo(&si);
1364 val = si.dwNumberOfProcessors;
1367 return val;
1370 unsigned int __cdecl _Thrd_id(void)
1372 TRACE("()\n");
1373 return GetCurrentThreadId();
1376 /* ??0_Pad@std@@QAE@XZ */
1377 /* ??0_Pad@std@@QEAA@XZ */
1378 DEFINE_THISCALL_WRAPPER(_Pad_ctor, 4)
1379 _Pad* __thiscall _Pad_ctor(_Pad *this)
1381 TRACE("(%p)\n", this);
1383 this->vtable = &_Pad_vtable;
1384 _Cnd_init(&this->cnd);
1385 _Mtx_init(&this->mtx, 0);
1386 this->launched = FALSE;
1387 _Mtx_lock(MTX_T_TO_ARG(this->mtx));
1388 return this;
1391 /* ??4_Pad@std@@QAEAAV01@ABV01@@Z */
1392 /* ??4_Pad@std@@QEAAAEAV01@AEBV01@@Z */
1393 DEFINE_THISCALL_WRAPPER(_Pad_op_assign, 8)
1394 _Pad* __thiscall _Pad_op_assign(_Pad *this, const _Pad *copy)
1396 TRACE("(%p %p)\n", this, copy);
1398 this->cnd = copy->cnd;
1399 this->mtx = copy->mtx;
1400 this->launched = copy->launched;
1401 return this;
1404 /* ??0_Pad@std@@QAE@ABV01@@Z */
1405 /* ??0_Pad@std@@QEAA@AEBV01@@Z */
1406 DEFINE_THISCALL_WRAPPER(_Pad_copy_ctor, 8)
1407 _Pad* __thiscall _Pad_copy_ctor(_Pad *this, const _Pad *copy)
1409 TRACE("(%p %p)\n", this, copy);
1411 this->vtable = &_Pad_vtable;
1412 return _Pad_op_assign(this, copy);
1415 /* ??1_Pad@std@@QAE@XZ */
1416 /* ??1_Pad@std@@QEAA@XZ */
1417 DEFINE_THISCALL_WRAPPER(_Pad_dtor, 4)
1418 void __thiscall _Pad_dtor(_Pad *this)
1420 TRACE("(%p)\n", this);
1422 _Mtx_unlock(MTX_T_TO_ARG(this->mtx));
1423 _Mtx_destroy(MTX_T_TO_ARG(this->mtx));
1424 _Cnd_destroy(CND_T_TO_ARG(this->cnd));
1427 DEFINE_THISCALL_WRAPPER(_Pad__Go, 4)
1428 #define call__Pad__Go(this) CALL_VTBL_FUNC(this, 0, unsigned int, (_Pad*), (this))
1429 unsigned int __thiscall _Pad__Go(_Pad *this)
1431 ERR("(%p) should not be called\n", this);
1432 return 0;
1435 static DWORD WINAPI launch_thread_proc(void *arg)
1437 _Pad *this = arg;
1438 return call__Pad__Go(this);
1441 /* ?_Launch@_Pad@std@@QAEXPAU_Thrd_imp_t@@@Z */
1442 /* ?_Launch@_Pad@std@@QEAAXPEAU_Thrd_imp_t@@@Z */
1443 DEFINE_THISCALL_WRAPPER(_Pad__Launch, 8)
1444 void __thiscall _Pad__Launch(_Pad *this, _Thrd_t *thr)
1446 TRACE("(%p %p)\n", this, thr);
1448 _Thrd_start(thr, launch_thread_proc, this);
1449 _Cnd_wait(CND_T_TO_ARG(this->cnd), MTX_T_TO_ARG(this->mtx));
1452 /* ?_Release@_Pad@std@@QAEXXZ */
1453 /* ?_Release@_Pad@std@@QEAAXXZ */
1454 DEFINE_THISCALL_WRAPPER(_Pad__Release, 4)
1455 void __thiscall _Pad__Release(_Pad *this)
1457 TRACE("(%p)\n", this);
1459 _Mtx_lock(MTX_T_TO_ARG(this->mtx));
1460 this->launched = TRUE;
1461 _Cnd_signal(CND_T_TO_ARG(this->cnd));
1462 _Mtx_unlock(MTX_T_TO_ARG(this->mtx));
1464 #endif
1466 /*********************************************************************
1467 * __crtInitializeCriticalSectionEx (MSVCP140.@)
1469 BOOL CDECL MSVCP__crtInitializeCriticalSectionEx(
1470 CRITICAL_SECTION *cs, DWORD spin_count, DWORD flags)
1472 TRACE("(%p %x %x)\n", cs, spin_count, flags);
1473 return InitializeCriticalSectionEx(cs, spin_count, flags);
1476 /*********************************************************************
1477 * __crtCreateEventExW (MSVCP140.@)
1479 HANDLE CDECL MSVCP__crtCreateEventExW(
1480 SECURITY_ATTRIBUTES *attribs, LPCWSTR name, DWORD flags, DWORD access)
1482 TRACE("(%p %s 0x%08x 0x%08x)\n", attribs, debugstr_w(name), flags, access);
1483 return CreateEventExW(attribs, name, flags, access);
1486 /*********************************************************************
1487 * __crtGetTickCount64 (MSVCP140.@)
1489 ULONGLONG CDECL MSVCP__crtGetTickCount64(void)
1491 return GetTickCount64();
1494 /*********************************************************************
1495 * __crtGetCurrentProcessorNumber (MSVCP140.@)
1497 DWORD CDECL MSVCP__crtGetCurrentProcessorNumber(void)
1499 return GetCurrentProcessorNumber();
1502 /*********************************************************************
1503 * __crtFlushProcessWriteBuffers (MSVCP140.@)
1505 VOID CDECL MSVCP__crtFlushProcessWriteBuffers(void)
1507 return FlushProcessWriteBuffers();
1510 /*********************************************************************
1511 * __crtCreateSemaphoreExW (MSVCP140.@)
1513 HANDLE CDECL MSVCP__crtCreateSemaphoreExW(
1514 SECURITY_ATTRIBUTES *attribs, LONG initial_count, LONG max_count, LPCWSTR name,
1515 DWORD flags, DWORD access)
1517 TRACE("(%p %d %d %s 0x%08x 0x%08x)\n", attribs, initial_count, max_count, debugstr_w(name),
1518 flags, access);
1519 return CreateSemaphoreExW(attribs, initial_count, max_count, name, flags, access);
1522 /*********************************************************************
1523 * __crtCreateThreadpoolTimer (MSVCP140.@)
1525 PTP_TIMER CDECL MSVCP__crtCreateThreadpoolTimer(PTP_TIMER_CALLBACK callback,
1526 PVOID userdata, TP_CALLBACK_ENVIRON *environment)
1528 TRACE("(%p %p %p)\n", callback, userdata, environment);
1529 return CreateThreadpoolTimer(callback, userdata, environment);
1532 /*********************************************************************
1533 * __crtCloseThreadpoolTimer (MSVCP140.@)
1535 VOID CDECL MSVCP__crtCloseThreadpoolTimer(TP_TIMER *timer)
1537 TRACE("(%p)\n", timer);
1538 CloseThreadpoolTimer(timer);
1541 /*********************************************************************
1542 * __crtSetThreadpoolTimer (MSVCP140.@)
1544 VOID CDECL MSVCP__crtSetThreadpoolTimer(TP_TIMER *timer,
1545 FILETIME *due_time, DWORD period, DWORD window_length)
1547 TRACE("(%p %p 0x%08x 0x%08x)\n", timer, due_time, period, window_length);
1548 return SetThreadpoolTimer(timer, due_time, period, window_length);
1551 /*********************************************************************
1552 * __crtWaitForThreadpoolTimerCallbacks (MSVCP140.@)
1554 VOID CDECL MSVCP__crtWaitForThreadpoolTimerCallbacks(TP_TIMER *timer, BOOL cancel)
1556 TRACE("(%p %d)\n", timer, cancel);
1557 WaitForThreadpoolTimerCallbacks(timer, cancel);
1560 /*********************************************************************
1561 * __crtCreateThreadpoolWait (MSVCP140.@)
1563 PTP_WAIT CDECL MSVCP__crtCreateThreadpoolWait(PTP_WAIT_CALLBACK callback,
1564 PVOID userdata, TP_CALLBACK_ENVIRON *environment)
1566 TRACE("(%p %p %p)\n", callback, userdata, environment);
1567 return CreateThreadpoolWait(callback, userdata, environment);
1570 /*********************************************************************
1571 * __crtCloseThreadpoolWait (MSVCP140.@)
1573 VOID CDECL MSVCP__crtCloseThreadpoolWait(TP_WAIT *wait)
1575 TRACE("(%p)\n", wait);
1576 CloseThreadpoolWait(wait);
1579 /*********************************************************************
1580 * __crtSetThreadpoolWait (MSVCP140.@)
1582 VOID CDECL MSVCP__crtSetThreadpoolWait(TP_WAIT *wait, HANDLE handle, FILETIME *due_time)
1584 TRACE("(%p %p %p)\n", wait, handle, due_time);
1585 return SetThreadpoolWait(wait, handle, due_time);
1588 /*********************************************************************
1589 * __crtFreeLibraryWhenCallbackReturns (MSVCP140.@)
1591 VOID CDECL MSVCP__crtFreeLibraryWhenCallbackReturns(PTP_CALLBACK_INSTANCE instance, HMODULE mod)
1593 TRACE("(%p %p)\n", instance, mod);
1594 FreeLibraryWhenCallbackReturns(instance, mod);
1597 /* ?_Execute_once@std@@YAHAAUonce_flag@1@P6GHPAX1PAPAX@Z1@Z */
1598 /* ?_Execute_once@std@@YAHAEAUonce_flag@1@P6AHPEAX1PEAPEAX@Z1@Z */
1599 BOOL __cdecl _Execute_once(INIT_ONCE *flag, PINIT_ONCE_FN func, void *param)
1601 return InitOnceExecuteOnce(flag, func, param, NULL);
1604 #if _MSVCP_VER >= 140
1605 LONGLONG __cdecl _Query_perf_counter(void)
1607 LARGE_INTEGER li;
1608 QueryPerformanceCounter(&li);
1609 return li.QuadPart;
1612 LONGLONG __cdecl _Query_perf_frequency(void)
1614 LARGE_INTEGER li;
1615 QueryPerformanceFrequency(&li);
1616 return li.QuadPart;
1618 #endif
1620 void __cdecl threads__Mtx_new(void **mtx)
1622 *mtx = operator_new(sizeof(CRITICAL_SECTION));
1623 InitializeCriticalSection(*mtx);
1626 void __cdecl threads__Mtx_delete(void *mtx)
1628 DeleteCriticalSection(mtx);
1631 void __cdecl threads__Mtx_lock(void *mtx)
1633 EnterCriticalSection(mtx);
1636 void __cdecl threads__Mtx_unlock(void *mtx)
1638 LeaveCriticalSection(mtx);
1641 #if _MSVCP_VER >= 110
1642 static LONG shared_ptr_lock;
1644 void __cdecl _Lock_shared_ptr_spin_lock(void)
1646 LONG l = 0;
1648 while(InterlockedCompareExchange(&shared_ptr_lock, 1, 0) != 0) {
1649 if(l++ == 1000) {
1650 Sleep(0);
1651 l = 0;
1656 void __cdecl _Unlock_shared_ptr_spin_lock(void)
1658 shared_ptr_lock = 0;
1660 #endif
1662 #if _MSVCP_VER >= 110 && _MSVCP_VER <= 120
1663 typedef struct {
1664 char dummy;
1665 } _Ph;
1667 /* ?_1@placeholders@std@@3V?$_Ph@$00@2@A */
1668 /* ?_20@placeholders@std@@3V?$_Ph@$0BE@@2@A */
1669 _Ph _Ph_1 = {0}, _Ph_2 = {0}, _Ph_3 = {0}, _Ph_4 = {0}, _Ph_5 = {0};
1670 _Ph _Ph_6 = {0}, _Ph_7 = {0}, _Ph_8 = {0}, _Ph_9 = {0}, _Ph_10 = {0};
1671 _Ph _Ph_11 = {0}, _Ph_12 = {0}, _Ph_13 = {0}, _Ph_14 = {0}, _Ph_15 = {0};
1672 _Ph _Ph_16 = {0}, _Ph_17 = {0}, _Ph_18 = {0}, _Ph_19 = {0}, _Ph_20 = {0};
1673 #endif
1675 #if _MSVCP_VER >= 110
1676 /* ?_Syserror_map@std@@YAPBDH@Z */
1677 /* ?_Syserror_map@std@@YAPEBDH@Z */
1678 const char* __cdecl _Syserror_map(int err)
1680 int i;
1682 TRACE("(%d)\n", err);
1684 for(i = 0; i < ARRAY_SIZE(syserror_map); i++)
1686 if(syserror_map[i].err == err)
1687 return syserror_map[i].str;
1689 #if _MSVCP_VER >= 140
1690 return "unknown error";
1691 #else
1692 return NULL;
1693 #endif
1695 #endif
1697 #if _MSVCP_VER >= 140
1698 /* ?_Winerror_message@std@@YAKKPADK@Z */
1699 /* ?_Winerror_message@std@@YAKKPEADK@Z */
1700 ULONG __cdecl _Winerror_message(ULONG err, char *buf, ULONG size)
1702 TRACE("(%u %p %u)\n", err, buf, size);
1704 return FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
1705 NULL, err, 0, buf, size, NULL);
1708 /* ?_Winerror_map@std@@YAHH@Z */
1709 int __cdecl _Winerror_map(int err)
1711 int low = 0, high = ARRAY_SIZE(winerror_map) - 1, mid;
1713 while(low <= high)
1715 mid = (low + high) / 2;
1717 if(err == winerror_map[mid].winerr)
1718 return winerror_map[mid].doserr;
1719 if(err > winerror_map[mid].winerr)
1720 low = mid + 1;
1721 else
1722 high = mid - 1;
1725 return 0;
1727 #endif
1729 #if _MSVCP_VER >= 100
1730 __ASM_BLOCK_BEGIN(misc_vtables)
1731 #if _MSVCP_VER == 100
1732 __ASM_VTABLE(iostream_category,
1733 VTABLE_ADD_FUNC(custom_category_vector_dtor)
1734 VTABLE_ADD_FUNC(custom_category_name)
1735 VTABLE_ADD_FUNC(iostream_category_message)
1736 VTABLE_ADD_FUNC(custom_category_default_error_condition)
1737 VTABLE_ADD_FUNC(custom_category_equivalent)
1738 VTABLE_ADD_FUNC(custom_category_equivalent_code));
1739 __ASM_VTABLE(system_category,
1740 VTABLE_ADD_FUNC(custom_category_vector_dtor)
1741 VTABLE_ADD_FUNC(custom_category_name)
1742 VTABLE_ADD_FUNC(custom_category_message)
1743 VTABLE_ADD_FUNC(custom_category_default_error_condition)
1744 VTABLE_ADD_FUNC(custom_category_equivalent)
1745 VTABLE_ADD_FUNC(custom_category_equivalent_code));
1746 __ASM_VTABLE(generic_category,
1747 VTABLE_ADD_FUNC(custom_category_vector_dtor)
1748 VTABLE_ADD_FUNC(custom_category_name)
1749 VTABLE_ADD_FUNC(custom_category_message)
1750 VTABLE_ADD_FUNC(custom_category_default_error_condition)
1751 VTABLE_ADD_FUNC(custom_category_equivalent)
1752 VTABLE_ADD_FUNC(custom_category_equivalent_code));
1753 #endif
1754 #if _MSVCP_VER >= 110
1755 __ASM_VTABLE(_Pad,
1756 VTABLE_ADD_FUNC(_Pad__Go));
1757 #endif
1758 __ASM_BLOCK_END
1760 void init_misc(void *base)
1762 #ifdef __x86_64__
1763 #if _MSVCP_VER == 100
1764 init_error_category_rtti(base);
1765 init_iostream_category_rtti(base);
1766 init_system_category_rtti(base);
1767 init_generic_category_rtti(base);
1768 #endif
1769 #if _MSVCP_VER >= 110
1770 init__Pad_rtti(base);
1771 #endif
1772 #endif
1774 #if _MSVCP_VER == 100
1775 iostream_category_ctor(&iostream_category);
1776 system_category_ctor(&system_category);
1777 generic_category_ctor(&generic_category);
1778 #endif
1781 void free_misc(void)
1783 #if _MSVCP_VER >= 110
1784 if(keyed_event)
1785 NtClose(keyed_event);
1786 HeapFree(GetProcessHeap(), 0, broadcast_at_thread_exit.to_broadcast);
1787 #endif
1789 #endif