ws2_32: Accept shouldn't fail when addrlen32 is NULL.
[wine.git] / dlls / msvcp90 / misc.c
blob7418fd2cb7611b1e2f13ba382f1944dbfe54855a
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 "config.h"
21 #include <stdarg.h>
22 #include <limits.h>
24 #include "msvcp90.h"
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winternl.h"
29 #include "wine/debug.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(msvcp);
32 struct __Container_proxy;
34 typedef struct {
35 struct __Container_proxy *proxy;
36 } _Container_base12;
38 typedef struct __Iterator_base12 {
39 struct __Container_proxy *proxy;
40 struct __Iterator_base12 *next;
41 } _Iterator_base12;
43 typedef struct __Container_proxy {
44 const _Container_base12 *cont;
45 _Iterator_base12 *head;
46 } _Container_proxy;
48 /* ??0_Mutex@std@@QAE@XZ */
49 /* ??0_Mutex@std@@QEAA@XZ */
50 DEFINE_THISCALL_WRAPPER(mutex_ctor, 4)
51 mutex* __thiscall mutex_ctor(mutex *this)
53 CRITICAL_SECTION *cs = MSVCRT_operator_new(sizeof(*cs));
54 if(!cs) {
55 ERR("Out of memory\n");
56 throw_exception(EXCEPTION_BAD_ALLOC, NULL);
59 InitializeCriticalSection(cs);
60 cs->DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": _Mutex critical section");
61 this->mutex = cs;
62 return this;
65 /* ??1_Mutex@std@@QAE@XZ */
66 /* ??1_Mutex@std@@QEAA@XZ */
67 DEFINE_THISCALL_WRAPPER(mutex_dtor, 4)
68 void __thiscall mutex_dtor(mutex *this)
70 ((CRITICAL_SECTION*)this->mutex)->DebugInfo->Spare[0] = 0;
71 DeleteCriticalSection(this->mutex);
72 MSVCRT_operator_delete(this->mutex);
75 /* ?_Lock@_Mutex@std@@QAEXXZ */
76 /* ?_Lock@_Mutex@std@@QEAAXXZ */
77 DEFINE_THISCALL_WRAPPER(mutex_lock, 4)
78 void __thiscall mutex_lock(mutex *this)
80 EnterCriticalSection(this->mutex);
83 /* ?_Unlock@_Mutex@std@@QAEXXZ */
84 /* ?_Unlock@_Mutex@std@@QEAAXXZ */
85 DEFINE_THISCALL_WRAPPER(mutex_unlock, 4)
86 void __thiscall mutex_unlock(mutex *this)
88 LeaveCriticalSection(this->mutex);
91 /* ?_Mutex_Lock@_Mutex@std@@CAXPAV12@@Z */
92 /* ?_Mutex_Lock@_Mutex@std@@CAXPEAV12@@Z */
93 void CDECL mutex_mutex_lock(mutex *m)
95 mutex_lock(m);
98 /* ?_Mutex_Unlock@_Mutex@std@@CAXPAV12@@Z */
99 /* ?_Mutex_Unlock@_Mutex@std@@CAXPEAV12@@Z */
100 void CDECL mutex_mutex_unlock(mutex *m)
102 mutex_unlock(m);
105 /* ?_Mutex_ctor@_Mutex@std@@CAXPAV12@@Z */
106 /* ?_Mutex_ctor@_Mutex@std@@CAXPEAV12@@Z */
107 void CDECL mutex_mutex_ctor(mutex *m)
109 mutex_ctor(m);
112 /* ?_Mutex_dtor@_Mutex@std@@CAXPAV12@@Z */
113 /* ?_Mutex_dtor@_Mutex@std@@CAXPEAV12@@Z */
114 void CDECL mutex_mutex_dtor(mutex *m)
116 mutex_dtor(m);
119 static CRITICAL_SECTION lockit_cs[_MAX_LOCK];
121 #if _MSVCP_VER >= 70
122 static inline int get_locktype( _Lockit *lockit ) { return lockit->locktype; }
123 static inline void set_locktype( _Lockit *lockit, int type ) { lockit->locktype = type; }
124 #else
125 static inline int get_locktype( _Lockit *lockit ) { return 0; }
126 static inline void set_locktype( _Lockit *lockit, int type ) { }
127 #endif
129 /* ?_Lockit_ctor@_Lockit@std@@SAXH@Z */
130 void __cdecl _Lockit_init(int locktype) {
131 InitializeCriticalSection(&lockit_cs[locktype]);
132 lockit_cs[locktype].DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": _Lockit critical section");
135 /* ?_Lockit_dtor@_Lockit@std@@SAXH@Z */
136 void __cdecl _Lockit_free(int locktype)
138 lockit_cs[locktype].DebugInfo->Spare[0] = 0;
139 DeleteCriticalSection(&lockit_cs[locktype]);
142 void init_lockit(void) {
143 int i;
145 for(i=0; i<_MAX_LOCK; i++)
146 _Lockit_init(i);
149 void free_lockit(void) {
150 int i;
152 for(i=0; i<_MAX_LOCK; i++)
153 _Lockit_free(i);
156 /* ?_Lockit_ctor@_Lockit@std@@CAXPAV12@H@Z */
157 /* ?_Lockit_ctor@_Lockit@std@@CAXPEAV12@H@Z */
158 void __cdecl _Lockit__Lockit_ctor_locktype(_Lockit *lockit, int locktype)
160 set_locktype( lockit, locktype );
161 EnterCriticalSection(&lockit_cs[locktype]);
164 /* ?_Lockit_ctor@_Lockit@std@@CAXPAV12@@Z */
165 /* ?_Lockit_ctor@_Lockit@std@@CAXPEAV12@@Z */
166 void __cdecl _Lockit__Lockit_ctor(_Lockit *lockit)
168 _Lockit__Lockit_ctor_locktype(lockit, 0);
171 /* ??0_Lockit@std@@QAE@H@Z */
172 /* ??0_Lockit@std@@QEAA@H@Z */
173 DEFINE_THISCALL_WRAPPER(_Lockit_ctor_locktype, 8)
174 _Lockit* __thiscall _Lockit_ctor_locktype(_Lockit *this, int locktype)
176 _Lockit__Lockit_ctor_locktype(this, locktype);
177 return this;
180 /* ??0_Lockit@std@@QAE@XZ */
181 /* ??0_Lockit@std@@QEAA@XZ */
182 DEFINE_THISCALL_WRAPPER(_Lockit_ctor, 4)
183 _Lockit* __thiscall _Lockit_ctor(_Lockit *this)
185 _Lockit__Lockit_ctor_locktype(this, 0);
186 return this;
189 /* ?_Lockit_dtor@_Lockit@std@@CAXPAV12@@Z */
190 /* ?_Lockit_dtor@_Lockit@std@@CAXPEAV12@@Z */
191 void __cdecl _Lockit__Lockit_dtor(_Lockit *lockit)
193 LeaveCriticalSection(&lockit_cs[get_locktype( lockit )]);
196 /* ??1_Lockit@std@@QAE@XZ */
197 /* ??1_Lockit@std@@QEAA@XZ */
198 DEFINE_THISCALL_WRAPPER(_Lockit_dtor, 4)
199 void __thiscall _Lockit_dtor(_Lockit *this)
201 _Lockit__Lockit_dtor(this);
204 /* wctype */
205 unsigned short __cdecl wctype(const char *property)
207 static const struct {
208 const char *name;
209 unsigned short mask;
210 } properties[] = {
211 { "alnum", _DIGIT|_ALPHA },
212 { "alpha", _ALPHA },
213 { "cntrl", _CONTROL },
214 { "digit", _DIGIT },
215 { "graph", _DIGIT|_PUNCT|_ALPHA },
216 { "lower", _LOWER },
217 { "print", _DIGIT|_PUNCT|_BLANK|_ALPHA },
218 { "punct", _PUNCT },
219 { "space", _SPACE },
220 { "upper", _UPPER },
221 { "xdigit", _HEX }
223 unsigned int i;
225 for(i=0; i<sizeof(properties)/sizeof(properties[0]); i++)
226 if(!strcmp(property, properties[i].name))
227 return properties[i].mask;
229 return 0;
232 typedef void (__cdecl *MSVCP_new_handler_func)(void);
233 static MSVCP_new_handler_func MSVCP_new_handler;
234 static int __cdecl new_handler_wrapper(MSVCP_size_t unused)
236 MSVCP_new_handler();
237 return 1;
240 /* ?set_new_handler@std@@YAP6AXXZP6AXXZ@Z */
241 MSVCP_new_handler_func __cdecl set_new_handler(MSVCP_new_handler_func new_handler)
243 MSVCP_new_handler_func old_handler = MSVCP_new_handler;
245 TRACE("%p\n", new_handler);
247 MSVCP_new_handler = new_handler;
248 MSVCRT_set_new_handler(new_handler ? new_handler_wrapper : NULL);
249 return old_handler;
252 /* ?set_new_handler@std@@YAP6AXXZH@Z */
253 MSVCP_new_handler_func __cdecl set_new_handler_reset(int unused)
255 return set_new_handler(NULL);
258 /* _Container_base0 is used by apps compiled without iterator checking
259 * (i.e. with _ITERATOR_DEBUG_LEVEL=0 ).
260 * It provides empty versions of methods used by visual c++'s stl's
261 * iterator checking.
262 * msvcr100 has to provide them in case apps are compiled with /Od
263 * or the optimizer fails to inline those (empty) calls.
266 /* ?_Orphan_all@_Container_base0@std@@QAEXXZ */
267 /* ?_Orphan_all@_Container_base0@std@@QEAAXXZ */
268 DEFINE_THISCALL_WRAPPER(Container_base0_Orphan_all, 4)
269 void __thiscall Container_base0_Orphan_all(void *this)
273 /* ?_Swap_all@_Container_base0@std@@QAEXAAU12@@Z */
274 /* ?_Swap_all@_Container_base0@std@@QEAAXAEAU12@@Z */
275 DEFINE_THISCALL_WRAPPER(Container_base0_Swap_all, 8)
276 void __thiscall Container_base0_Swap_all(void *this, void *that)
280 /* ??4_Container_base0@std@@QAEAAU01@ABU01@@Z */
281 /* ??4_Container_base0@std@@QEAAAEAU01@AEBU01@@Z */
282 DEFINE_THISCALL_WRAPPER(Container_base0_op_assign, 8)
283 void* __thiscall Container_base0_op_assign(void *this, const void *that)
285 return this;
288 /* ??0_Container_base12@std@@QAE@ABU01@@Z */
289 /* ??0_Container_base12@std@@QEAA@AEBU01@@Z */
290 DEFINE_THISCALL_WRAPPER(_Container_base12_copy_ctor, 8)
291 _Container_base12* __thiscall _Container_base12_copy_ctor(
292 _Container_base12 *this, _Container_base12 *that)
294 this->proxy = NULL;
295 return this;
298 /* ??0_Container_base12@std@@QAE@XZ */
299 /* ??0_Container_base12@std@@QEAA@XZ */
300 DEFINE_THISCALL_WRAPPER(_Container_base12_ctor, 4)
301 _Container_base12* __thiscall _Container_base12_ctor(_Container_base12 *this)
303 this->proxy = NULL;
304 return this;
307 /* ??1_Container_base12@std@@QAE@XZ */
308 /* ??1_Container_base12@std@@QEAA@XZ */
309 DEFINE_THISCALL_WRAPPER(_Container_base12_dtor, 4)
310 void __thiscall _Container_base12_dtor(_Container_base12 *this)
314 /* ??4_Container_base12@std@@QAEAAU01@ABU01@@Z */
315 /* ??4_Container_base12@std@@QEAAAEAU01@AEBU01@@ */
316 DEFINE_THISCALL_WRAPPER(_Container_base12_op_assign, 8)
317 _Container_base12* __thiscall _Container_base12_op_assign(
318 _Container_base12 *this, const _Container_base12 *that)
320 return this;
323 /* ?_Getpfirst@_Container_base12@std@@QBEPAPAU_Iterator_base12@2@XZ */
324 /* ?_Getpfirst@_Container_base12@std@@QEBAPEAPEAU_Iterator_base12@2@XZ */
325 DEFINE_THISCALL_WRAPPER(_Container_base12__Getpfirst, 4)
326 _Iterator_base12** __thiscall _Container_base12__Getpfirst(_Container_base12 *this)
328 return this->proxy ? &this->proxy->head : NULL;
331 /* ?_Orphan_all@_Container_base12@std@@QAEXXZ */
332 /* ?_Orphan_all@_Container_base12@std@@QEAAXXZ */
333 DEFINE_THISCALL_WRAPPER(_Container_base12__Orphan_all, 4)
334 void __thiscall _Container_base12__Orphan_all(_Container_base12 *this)
338 /* ?_Swap_all@_Container_base12@std@@QAEXAAU12@@Z */
339 /* ?_Swap_all@_Container_base12@std@@QEAAXAEAU12@@Z */
340 DEFINE_THISCALL_WRAPPER(_Container_base12__Swap_all, 8)
341 void __thiscall _Container_base12__Swap_all(
342 _Container_base12 *this, _Container_base12 *that)
344 _Container_proxy *tmp;
346 tmp = this->proxy;
347 this->proxy = that->proxy;
348 that->proxy = tmp;
350 if(this->proxy)
351 this->proxy->cont = this;
352 if(that->proxy)
353 that->proxy->cont = that;
356 #if _MSVCP_VER >= 110
358 #define SECSPERDAY 86400
359 /* 1601 to 1970 is 369 years plus 89 leap days */
360 #define SECS_1601_TO_1970 ((369 * 365 + 89) * (ULONGLONG)SECSPERDAY)
361 #define TICKSPERSEC 10000000
362 #define TICKS_1601_TO_1970 (SECS_1601_TO_1970 * TICKSPERSEC)
363 #define NANOSEC_PER_MILLISEC 1000000
364 #define MILLISEC_PER_SEC 1000
366 typedef int MSVCRT_long;
368 /* xtime */
369 typedef struct {
370 __time64_t sec;
371 MSVCRT_long nsec;
372 } xtime;
374 /* _Xtime_get_ticks */
375 LONGLONG __cdecl _Xtime_get_ticks(void)
377 FILETIME ft;
379 TRACE("\n");
381 GetSystemTimeAsFileTime(&ft);
382 return ((LONGLONG)ft.dwHighDateTime<<32) + ft.dwLowDateTime - TICKS_1601_TO_1970;
385 /* _xtime_get */
386 int __cdecl xtime_get(xtime* t, int unknown)
388 LONGLONG ticks;
390 TRACE("(%p)\n", t);
392 if(unknown != 1)
393 return 0;
395 ticks = _Xtime_get_ticks();
396 t->sec = ticks / TICKSPERSEC;
397 t->nsec = ticks % TICKSPERSEC * 100;
398 return 1;
401 /* _Xtime_diff_to_millis2 */
402 MSVCRT_long __cdecl _Xtime_diff_to_millis2(const xtime *t1, const xtime *t2)
404 __time64_t diff_sec;
405 MSVCRT_long diff_nsec, ret;
407 TRACE("(%p, %p)\n", t1, t2);
409 diff_sec = t1->sec - t2->sec;
410 diff_nsec = t1->nsec - t2->nsec;
411 ret = diff_sec * MILLISEC_PER_SEC +
412 (diff_nsec + NANOSEC_PER_MILLISEC - 1) / NANOSEC_PER_MILLISEC;
413 return ret > 0 ? ret : 0;
416 /* _Xtime_diff_to_millis */
417 MSVCRT_long __cdecl _Xtime_diff_to_millis(const xtime *t)
419 xtime now;
421 TRACE("%p\n", t);
423 xtime_get(&now, 1);
424 return _Xtime_diff_to_millis2(t, &now);
426 #endif
428 #if _MSVCP_VER >= 90
429 unsigned int __cdecl _Random_device(void)
431 unsigned int ret;
433 TRACE("\n");
435 /* TODO: throw correct exception in case of failure */
436 if(rand_s(&ret))
437 throw_exception(EXCEPTION, "random number generator failed\n");
438 return ret;
440 #endif
442 #if _MSVCP_VER >= 110
443 #if defined(__i386__) && !defined(__arm__)
445 extern void *call_thiscall_func;
446 __ASM_GLOBAL_FUNC(call_thiscall_func,
447 "popl %eax\n\t"
448 "popl %edx\n\t"
449 "popl %ecx\n\t"
450 "pushl %eax\n\t"
451 "jmp *%edx\n\t")
453 #define call_func1(func,this) ((void* (WINAPI*)(void*,void*))&call_thiscall_func)(func,this)
455 #else /* __i386__ */
457 #define call_func1(func,this) func(this)
459 #endif /* __i386__ */
461 #define MTX_MULTI_LOCK 0x100
462 #define MTX_LOCKED 3
463 typedef struct
465 DWORD flags;
466 critical_section cs;
467 DWORD thread_id;
468 DWORD count;
469 } *_Mtx_t;
471 #if _MSVCP_VER >= 140
472 typedef _Mtx_t _Mtx_arg_t;
473 #define MTX_T_FROM_ARG(m) (m)
474 #define MTX_T_TO_ARG(m) (m)
475 #else
476 typedef _Mtx_t *_Mtx_arg_t;
477 #define MTX_T_FROM_ARG(m) (*(m))
478 #define MTX_T_TO_ARG(m) (&(m))
479 #endif
481 void __cdecl _Mtx_init_in_situ(_Mtx_t mtx, int flags)
483 if(flags & ~MTX_MULTI_LOCK)
484 FIXME("unknown flags ignored: %x\n", flags);
486 mtx->flags = flags;
487 call_func1(critical_section_ctor, &mtx->cs);
488 mtx->thread_id = -1;
489 mtx->count = 0;
492 int __cdecl _Mtx_init(_Mtx_t *mtx, int flags)
494 *mtx = MSVCRT_operator_new(sizeof(**mtx));
495 _Mtx_init_in_situ(*mtx, flags);
496 return 0;
499 void __cdecl _Mtx_destroy_in_situ(_Mtx_t mtx)
501 call_func1(critical_section_dtor, &mtx->cs);
504 void __cdecl _Mtx_destroy(_Mtx_arg_t mtx)
506 call_func1(critical_section_dtor, &MTX_T_FROM_ARG(mtx)->cs);
507 MSVCRT_operator_delete(MTX_T_FROM_ARG(mtx));
510 int __cdecl _Mtx_current_owns(_Mtx_arg_t mtx)
512 return MTX_T_FROM_ARG(mtx)->thread_id == GetCurrentThreadId();
515 int __cdecl _Mtx_lock(_Mtx_arg_t mtx)
517 if(MTX_T_FROM_ARG(mtx)->thread_id != GetCurrentThreadId()) {
518 call_func1(critical_section_lock, &MTX_T_FROM_ARG(mtx)->cs);
519 MTX_T_FROM_ARG(mtx)->thread_id = GetCurrentThreadId();
520 }else if(!(MTX_T_FROM_ARG(mtx)->flags & MTX_MULTI_LOCK)) {
521 return MTX_LOCKED;
524 MTX_T_FROM_ARG(mtx)->count++;
525 return 0;
528 int __cdecl _Mtx_unlock(_Mtx_arg_t mtx)
530 if(--MTX_T_FROM_ARG(mtx)->count)
531 return 0;
533 MTX_T_FROM_ARG(mtx)->thread_id = -1;
534 call_func1(critical_section_unlock, &MTX_T_FROM_ARG(mtx)->cs);
535 return 0;
538 int __cdecl _Mtx_trylock(_Mtx_arg_t mtx)
540 if(MTX_T_FROM_ARG(mtx)->thread_id != GetCurrentThreadId()) {
541 if(!call_func1(critical_section_trylock, &MTX_T_FROM_ARG(mtx)->cs))
542 return MTX_LOCKED;
543 MTX_T_FROM_ARG(mtx)->thread_id = GetCurrentThreadId();
544 }else if(!(MTX_T_FROM_ARG(mtx)->flags & MTX_MULTI_LOCK)) {
545 return MTX_LOCKED;
548 MTX_T_FROM_ARG(mtx)->count++;
549 return 0;
552 critical_section* __cdecl _Mtx_getconcrtcs(_Mtx_arg_t mtx)
554 return &MTX_T_FROM_ARG(mtx)->cs;
557 static inline LONG interlocked_dec_if_nonzero( LONG *dest )
559 LONG val, tmp;
560 for (val = *dest;; val = tmp)
562 if (!val || (tmp = InterlockedCompareExchange( dest, val - 1, val )) == val)
563 break;
565 return val;
568 #define CND_TIMEDOUT 2
570 typedef struct
572 CONDITION_VARIABLE cv;
573 } *_Cnd_t;
575 #if _MSVCP_VER >= 140
576 typedef _Cnd_t _Cnd_arg_t;
577 #define CND_T_FROM_ARG(c) (c)
578 #define CND_T_TO_ARG(c) (c)
579 #else
580 typedef _Cnd_t *_Cnd_arg_t;
581 #define CND_T_FROM_ARG(c) (*(c))
582 #define CND_T_TO_ARG(c) (&(c))
583 #endif
585 static HANDLE keyed_event;
587 int __cdecl _Cnd_init(_Cnd_t *cnd)
589 *cnd = MSVCRT_operator_new(sizeof(**cnd));
590 InitializeConditionVariable(&(*cnd)->cv);
592 if(!keyed_event) {
593 HANDLE event;
595 NtCreateKeyedEvent(&event, GENERIC_READ|GENERIC_WRITE, NULL, 0);
596 if(InterlockedCompareExchangePointer(&keyed_event, event, NULL) != NULL)
597 NtClose(event);
600 return 0;
603 int __cdecl _Cnd_wait(_Cnd_arg_t cnd, _Mtx_arg_t mtx)
605 CONDITION_VARIABLE *cv = &CND_T_FROM_ARG(cnd)->cv;
607 InterlockedExchangeAdd( (LONG *)&cv->Ptr, 1 );
608 _Mtx_unlock(mtx);
610 NtWaitForKeyedEvent(keyed_event, &cv->Ptr, FALSE, NULL);
612 _Mtx_lock(mtx);
613 return 0;
616 int __cdecl _Cnd_timedwait(_Cnd_arg_t cnd, _Mtx_arg_t mtx, const xtime *xt)
618 CONDITION_VARIABLE *cv = &CND_T_FROM_ARG(cnd)->cv;
619 LARGE_INTEGER timeout;
620 NTSTATUS status;
622 InterlockedExchangeAdd( (LONG *)&cv->Ptr, 1 );
623 _Mtx_unlock(mtx);
625 timeout.QuadPart = (ULONGLONG)_Xtime_diff_to_millis(xt) * -10000;
626 status = NtWaitForKeyedEvent(keyed_event, &cv->Ptr, FALSE, &timeout);
627 if (status)
629 if (!interlocked_dec_if_nonzero( (LONG *)&cv->Ptr ))
630 status = NtWaitForKeyedEvent( keyed_event, &cv->Ptr, FALSE, NULL );
633 _Mtx_lock(mtx);
634 return status ? CND_TIMEDOUT : 0;
637 int __cdecl _Cnd_broadcast(_Cnd_arg_t cnd)
639 CONDITION_VARIABLE *cv = &CND_T_FROM_ARG(cnd)->cv;
640 LONG val = InterlockedExchange( (LONG *)&cv->Ptr, 0 );
641 while (val-- > 0)
642 NtReleaseKeyedEvent( keyed_event, &cv->Ptr, FALSE, NULL );
643 return 0;
646 int __cdecl _Cnd_signal(_Cnd_arg_t cnd)
648 CONDITION_VARIABLE *cv = &CND_T_FROM_ARG(cnd)->cv;
649 if (interlocked_dec_if_nonzero( (LONG *)&cv->Ptr ))
650 NtReleaseKeyedEvent( keyed_event, &cv->Ptr, FALSE, NULL );
651 return 0;
654 void __cdecl _Cnd_destroy(_Cnd_arg_t cnd)
656 if(cnd) {
657 _Cnd_broadcast(cnd);
658 MSVCRT_operator_delete(CND_T_FROM_ARG(cnd));
661 #endif
663 #if _MSVCP_VER == 100
664 typedef struct {
665 const vtable_ptr *vtable;
666 } error_category;
668 typedef struct {
669 error_category base;
670 const char *type;
671 } custom_category;
672 static custom_category iostream_category;
674 DEFINE_RTTI_DATA0(error_category, 0, ".?AVerror_category@std@@")
675 DEFINE_RTTI_DATA1(iostream_category, 0, &error_category_rtti_base_descriptor, ".?AV_Iostream_error_category@std@@")
677 extern const vtable_ptr MSVCP_iostream_category_vtable;
679 static void iostream_category_ctor(custom_category *this)
681 this->base.vtable = &MSVCP_iostream_category_vtable;
682 this->type = "iostream";
685 DEFINE_THISCALL_WRAPPER(custom_category_vector_dtor, 8)
686 custom_category* __thiscall custom_category_vector_dtor(custom_category *this, unsigned int flags)
688 TRACE("(%p %x)\n", this, flags);
689 if(flags & 2) {
690 /* we have an array, with the number of elements stored before the first object */
691 INT_PTR i, *ptr = (INT_PTR *)this-1;
693 for(i=*ptr-1; i>=0; i--)
694 MSVCRT_operator_delete(ptr);
695 } else {
696 if(flags & 1)
697 MSVCRT_operator_delete(this);
700 return this;
703 DEFINE_THISCALL_WRAPPER(custom_category_name, 4)
704 const char* __thiscall custom_category_name(const custom_category *this)
706 return this->type;
709 DEFINE_THISCALL_WRAPPER(custom_category_message, 12)
710 basic_string_char* __thiscall custom_category_message(const custom_category *this,
711 basic_string_char *ret, int err)
713 return MSVCP_basic_string_char_ctor_cstr(ret, strerror(err));
716 DEFINE_THISCALL_WRAPPER(custom_category_default_error_condition, 12)
717 /*error_condition*/void* __thiscall custom_category_default_error_condition(
718 custom_category *this, /*error_condition*/void *ret, int code)
720 FIXME("(%p %p %x) stub\n", this, ret, code);
721 return NULL;
724 DEFINE_THISCALL_WRAPPER(custom_category_equivalent, 12)
725 MSVCP_bool __thiscall custom_category_equivalent(const custom_category *this,
726 int code, const /*error_condition*/void *condition)
728 FIXME("(%p %x %p) stub\n", this, code, condition);
729 return FALSE;
732 DEFINE_THISCALL_WRAPPER(custom_category_equivalent_code, 12)
733 MSVCP_bool __thiscall custom_category_equivalent_code(custom_category *this,
734 const /*error_code*/void *code, int condition)
736 FIXME("(%p %p %x) stub\n", this, code, condition);
737 return FALSE;
740 DEFINE_THISCALL_WRAPPER(iostream_category_message, 12)
741 basic_string_char* __thiscall iostream_category_message(const custom_category *this,
742 basic_string_char *ret, int err)
744 if(err == 1) return MSVCP_basic_string_char_ctor_cstr(ret, "iostream error");
745 return MSVCP_basic_string_char_ctor_cstr(ret, strerror(err));
748 /* ?iostream_category@std@@YAABVerror_category@1@XZ */
749 /* ?iostream_category@std@@YAAEBVerror_category@1@XZ */
750 const error_category* __cdecl std_iostream_category(void)
752 TRACE("()\n");
753 return &iostream_category.base;
756 static custom_category system_category;
757 DEFINE_RTTI_DATA1(system_category, 0, &error_category_rtti_base_descriptor, ".?AV_System_error_category@std@@")
759 extern const vtable_ptr MSVCP_system_category_vtable;
761 static void system_category_ctor(custom_category *this)
763 this->base.vtable = &MSVCP_system_category_vtable;
764 this->type = "system";
767 /* ?system_category@std@@YAABVerror_category@1@XZ */
768 /* ?system_category@std@@YAAEBVerror_category@1@XZ */
769 const error_category* __cdecl std_system_category(void)
771 TRACE("()\n");
772 return &system_category.base;
775 static custom_category generic_category;
776 DEFINE_RTTI_DATA1(generic_category, 0, &error_category_rtti_base_descriptor, ".?AV_Generic_error_category@std@@")
778 extern const vtable_ptr MSVCP_generic_category_vtable;
780 static void generic_category_ctor(custom_category *this)
782 this->base.vtable = &MSVCP_generic_category_vtable;
783 this->type = "generic";
786 /* ?generic_category@std@@YAABVerror_category@1@XZ */
787 /* ?generic_category@std@@YAAEBVerror_category@1@XZ */
788 const error_category* __cdecl std_generic_category(void)
790 TRACE("()\n");
791 return &generic_category.base;
793 #endif
795 #if _MSVCP_VER >= 110
796 static CRITICAL_SECTION call_once_cs;
797 static CRITICAL_SECTION_DEBUG call_once_cs_debug =
799 0, 0, &call_once_cs,
800 { &call_once_cs_debug.ProcessLocksList, &call_once_cs_debug.ProcessLocksList },
801 0, 0, { (DWORD_PTR)(__FILE__ ": call_once_cs") }
803 static CRITICAL_SECTION call_once_cs = { &call_once_cs_debug, -1, 0, 0, 0, 0 };
805 void __cdecl _Call_onceEx(int *once, void (__cdecl *func)(void*), void *argv)
807 TRACE("%p %p %p\n", once, func, argv);
809 EnterCriticalSection(&call_once_cs);
810 if(!*once) {
811 /* FIXME: handle exceptions */
812 func(argv);
813 *once = 1;
815 LeaveCriticalSection(&call_once_cs);
818 static void __cdecl call_once_func_wrapper(void *func)
820 ((void (__cdecl*)(void))func)();
823 void __cdecl _Call_once(int *once, void (__cdecl *func)(void))
825 TRACE("%p %p\n", once, func);
826 _Call_onceEx(once, call_once_func_wrapper, func);
829 void __cdecl _Do_call(void *this)
831 CALL_VTBL_FUNC(this, 0, void, (void*), (this));
833 #endif
835 #if _MSVCP_VER >= 110
836 typedef struct
838 HANDLE hnd;
839 DWORD id;
840 } _Thrd_t;
842 typedef int (__cdecl *_Thrd_start_t)(void*);
844 #define _THRD_ERROR 4
846 int __cdecl _Thrd_equal(_Thrd_t a, _Thrd_t b)
848 TRACE("(%p %u %p %u)\n", a.hnd, a.id, b.hnd, b.id);
849 return a.id == b.id;
852 int __cdecl _Thrd_lt(_Thrd_t a, _Thrd_t b)
854 TRACE("(%p %u %p %u)\n", a.hnd, a.id, b.hnd, b.id);
855 return a.id < b.id;
858 void __cdecl _Thrd_sleep(const xtime *t)
860 TRACE("(%p)\n", t);
861 Sleep(_Xtime_diff_to_millis(t));
864 void __cdecl _Thrd_yield(void)
866 TRACE("()\n");
867 Sleep(0);
870 static _Thrd_t thread_current(void)
872 _Thrd_t ret;
874 if(DuplicateHandle(GetCurrentProcess(), GetCurrentThread(),
875 GetCurrentProcess(), &ret.hnd, 0, FALSE, DUPLICATE_SAME_ACCESS)) {
876 CloseHandle(ret.hnd);
877 } else {
878 ret.hnd = 0;
880 ret.id = GetCurrentThreadId();
882 TRACE("(%p %u)\n", ret.hnd, ret.id);
883 return ret;
886 #ifndef __i386__
887 _Thrd_t __cdecl _Thrd_current(void)
889 return thread_current();
891 #else
892 ULONGLONG __cdecl _Thrd_current(void)
894 union {
895 _Thrd_t thr;
896 ULONGLONG ull;
897 } ret;
899 C_ASSERT(sizeof(_Thrd_t) <= sizeof(ULONGLONG));
901 ret.thr = thread_current();
902 return ret.ull;
904 #endif
906 int __cdecl _Thrd_join(_Thrd_t thr, int *code)
908 TRACE("(%p %u %p)\n", thr.hnd, thr.id, code);
909 if (WaitForSingleObject(thr.hnd, INFINITE))
910 return _THRD_ERROR;
912 if (code)
913 GetExitCodeThread(thr.hnd, (DWORD *)code);
915 CloseHandle(thr.hnd);
916 return 0;
919 int __cdecl _Thrd_start(_Thrd_t *thr, LPTHREAD_START_ROUTINE proc, void *arg)
921 TRACE("(%p %p %p)\n", thr, proc, arg);
922 thr->hnd = CreateThread(NULL, 0, proc, arg, 0, &thr->id);
923 return thr->hnd ? 0 : _THRD_ERROR;
926 typedef struct
928 _Thrd_start_t proc;
929 void *arg;
930 } thread_proc_arg;
932 static DWORD WINAPI thread_proc_wrapper(void *arg)
934 thread_proc_arg wrapped_arg = *((thread_proc_arg*)arg);
935 free(arg);
936 return wrapped_arg.proc(wrapped_arg.arg);
939 int __cdecl _Thrd_create(_Thrd_t *thr, _Thrd_start_t proc, void *arg)
941 thread_proc_arg *wrapped_arg;
942 int ret;
944 TRACE("(%p %p %p)\n", thr, proc, arg);
946 wrapped_arg = malloc(sizeof(*wrapped_arg));
947 if(!wrapped_arg)
948 return _THRD_ERROR; /* TODO: probably different error should be returned here */
950 wrapped_arg->proc = proc;
951 wrapped_arg->arg = arg;
952 ret = _Thrd_start(thr, thread_proc_wrapper, wrapped_arg);
953 if(ret) free(wrapped_arg);
954 return ret;
957 int __cdecl _Thrd_detach(_Thrd_t thr)
959 return CloseHandle(thr.hnd) ? 0 : _THRD_ERROR;
962 typedef struct
964 const vtable_ptr *vtable;
965 _Cnd_t cnd;
966 _Mtx_t mtx;
967 MSVCP_bool launched;
968 } _Pad;
970 DEFINE_RTTI_DATA0(_Pad, 0, ".?AV_Pad@std@@")
972 /* ??_7_Pad@std@@6B@ */
973 extern const vtable_ptr MSVCP__Pad_vtable;
975 unsigned int __cdecl _Thrd_hardware_concurrency(void)
977 static unsigned int val = -1;
979 TRACE("()\n");
981 if(val == -1) {
982 SYSTEM_INFO si;
984 GetSystemInfo(&si);
985 val = si.dwNumberOfProcessors;
988 return val;
991 /* ??0_Pad@std@@QAE@XZ */
992 /* ??0_Pad@std@@QEAA@XZ */
993 DEFINE_THISCALL_WRAPPER(_Pad_ctor, 4)
994 _Pad* __thiscall _Pad_ctor(_Pad *this)
996 TRACE("(%p)\n", this);
998 this->vtable = &MSVCP__Pad_vtable;
999 _Cnd_init(&this->cnd);
1000 _Mtx_init(&this->mtx, 0);
1001 this->launched = FALSE;
1002 _Mtx_lock(MTX_T_TO_ARG(this->mtx));
1003 return this;
1006 /* ??4_Pad@std@@QAEAAV01@ABV01@@Z */
1007 /* ??4_Pad@std@@QEAAAEAV01@AEBV01@@Z */
1008 DEFINE_THISCALL_WRAPPER(_Pad_op_assign, 8)
1009 _Pad* __thiscall _Pad_op_assign(_Pad *this, const _Pad *copy)
1011 TRACE("(%p %p)\n", this, copy);
1013 this->cnd = copy->cnd;
1014 this->mtx = copy->mtx;
1015 this->launched = copy->launched;
1016 return this;
1019 /* ??0_Pad@std@@QAE@ABV01@@Z */
1020 /* ??0_Pad@std@@QEAA@AEBV01@@Z */
1021 DEFINE_THISCALL_WRAPPER(_Pad_copy_ctor, 8)
1022 _Pad* __thiscall _Pad_copy_ctor(_Pad *this, const _Pad *copy)
1024 TRACE("(%p %p)\n", this, copy);
1026 this->vtable = &MSVCP__Pad_vtable;
1027 return _Pad_op_assign(this, copy);
1030 /* ??1_Pad@std@@QAE@XZ */
1031 /* ??1_Pad@std@@QEAA@XZ */
1032 DEFINE_THISCALL_WRAPPER(_Pad_dtor, 4)
1033 void __thiscall _Pad_dtor(_Pad *this)
1035 TRACE("(%p)\n", this);
1037 _Mtx_unlock(MTX_T_TO_ARG(this->mtx));
1038 _Mtx_destroy(MTX_T_TO_ARG(this->mtx));
1039 _Cnd_destroy(CND_T_TO_ARG(this->cnd));
1042 DEFINE_THISCALL_WRAPPER(_Pad__Go, 4)
1043 #define call__Pad__Go(this) CALL_VTBL_FUNC(this, 0, unsigned int, (_Pad*), (this))
1044 unsigned int __thiscall _Pad__Go(_Pad *this)
1046 ERR("(%p) should not be called\n", this);
1047 return 0;
1050 static DWORD WINAPI launch_thread_proc(void *arg)
1052 _Pad *this = arg;
1053 return call__Pad__Go(this);
1056 /* ?_Launch@_Pad@std@@QAEXPAU_Thrd_imp_t@@@Z */
1057 /* ?_Launch@_Pad@std@@QEAAXPEAU_Thrd_imp_t@@@Z */
1058 DEFINE_THISCALL_WRAPPER(_Pad__Launch, 8)
1059 void __thiscall _Pad__Launch(_Pad *this, _Thrd_t *thr)
1061 TRACE("(%p %p)\n", this, thr);
1063 _Thrd_start(thr, launch_thread_proc, this);
1064 _Cnd_wait(CND_T_TO_ARG(this->cnd), MTX_T_TO_ARG(this->mtx));
1067 /* ?_Release@_Pad@std@@QAEXXZ */
1068 /* ?_Release@_Pad@std@@QEAAXXZ */
1069 DEFINE_THISCALL_WRAPPER(_Pad__Release, 4)
1070 void __thiscall _Pad__Release(_Pad *this)
1072 TRACE("(%p)\n", this);
1074 _Mtx_lock(MTX_T_TO_ARG(this->mtx));
1075 this->launched = TRUE;
1076 _Cnd_signal(CND_T_TO_ARG(this->cnd));
1077 _Mtx_unlock(MTX_T_TO_ARG(this->mtx));
1079 #endif
1081 #ifndef __GNUC__
1082 void __asm_dummy_vtables(void) {
1083 #endif
1084 #if _MSVCP_VER == 100
1085 __ASM_VTABLE(iostream_category,
1086 VTABLE_ADD_FUNC(custom_category_vector_dtor)
1087 VTABLE_ADD_FUNC(custom_category_name)
1088 VTABLE_ADD_FUNC(iostream_category_message)
1089 VTABLE_ADD_FUNC(custom_category_default_error_condition)
1090 VTABLE_ADD_FUNC(custom_category_equivalent)
1091 VTABLE_ADD_FUNC(custom_category_equivalent_code));
1092 __ASM_VTABLE(system_category,
1093 VTABLE_ADD_FUNC(custom_category_vector_dtor)
1094 VTABLE_ADD_FUNC(custom_category_name)
1095 VTABLE_ADD_FUNC(custom_category_message)
1096 VTABLE_ADD_FUNC(custom_category_default_error_condition)
1097 VTABLE_ADD_FUNC(custom_category_equivalent)
1098 VTABLE_ADD_FUNC(custom_category_equivalent_code));
1099 __ASM_VTABLE(generic_category,
1100 VTABLE_ADD_FUNC(custom_category_vector_dtor)
1101 VTABLE_ADD_FUNC(custom_category_name)
1102 VTABLE_ADD_FUNC(custom_category_message)
1103 VTABLE_ADD_FUNC(custom_category_default_error_condition)
1104 VTABLE_ADD_FUNC(custom_category_equivalent)
1105 VTABLE_ADD_FUNC(custom_category_equivalent_code));
1106 #endif
1107 #if _MSVCP_VER >= 110
1108 __ASM_VTABLE(_Pad,
1109 VTABLE_ADD_FUNC(_Pad__Go));
1110 #endif
1111 #ifndef __GNUC__
1113 #endif
1115 /*********************************************************************
1116 * __crtInitializeCriticalSectionEx (MSVCP140.@)
1118 BOOL CDECL MSVCP__crtInitializeCriticalSectionEx(
1119 CRITICAL_SECTION *cs, DWORD spin_count, DWORD flags)
1121 TRACE("(%p %x %x)\n", cs, spin_count, flags);
1122 return InitializeCriticalSectionEx(cs, spin_count, flags);
1125 /*********************************************************************
1126 * __crtCreateEventExW (MSVCP140.@)
1128 HANDLE CDECL MSVCP__crtCreateEventExW(
1129 SECURITY_ATTRIBUTES *attribs, LPCWSTR name, DWORD flags, DWORD access)
1131 TRACE("(%p %s 0x%08x 0x%08x)\n", attribs, debugstr_w(name), flags, access);
1132 return CreateEventExW(attribs, name, flags, access);
1135 /*********************************************************************
1136 * __crtGetTickCount64 (MSVCP140.@)
1138 ULONGLONG CDECL MSVCP__crtGetTickCount64(void)
1140 return GetTickCount64();
1143 /*********************************************************************
1144 * __crtCreateSemaphoreExW (MSVCP140.@)
1146 HANDLE CDECL MSVCP__crtCreateSemaphoreExW(
1147 SECURITY_ATTRIBUTES *attribs, LONG initial_count, LONG max_count, LPCWSTR name,
1148 DWORD flags, DWORD access)
1150 TRACE("(%p %d %d %s 0x%08x 0x%08x)\n", attribs, initial_count, max_count, debugstr_w(name),
1151 flags, access);
1152 return CreateSemaphoreExW(attribs, initial_count, max_count, name, flags, access);
1155 void init_misc(void *base)
1157 #ifdef __x86_64__
1158 #if _MSVCP_VER == 100
1159 init_error_category_rtti(base);
1160 init_iostream_category_rtti(base);
1161 init_system_category_rtti(base);
1162 init_generic_category_rtti(base);
1163 #endif
1164 #if _MSVCP_VER >= 110
1165 init__Pad_rtti(base);
1166 #endif
1167 #endif
1169 #if _MSVCP_VER == 100
1170 iostream_category_ctor(&iostream_category);
1171 system_category_ctor(&system_category);
1172 generic_category_ctor(&generic_category);
1173 #endif
1176 void free_misc(void)
1178 #if _MSVCP_VER >= 110
1179 if(keyed_event)
1180 NtClose(keyed_event);
1181 #endif
1184 #if _MSVCP_VER >= 140
1185 LONGLONG __cdecl _Query_perf_counter(void)
1187 LARGE_INTEGER li;
1188 QueryPerformanceCounter(&li);
1189 return li.QuadPart;
1192 LONGLONG __cdecl _Query_perf_frequency(void)
1194 LARGE_INTEGER li;
1195 QueryPerformanceFrequency(&li);
1196 return li.QuadPart;
1198 #endif