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
28 #include "wine/debug.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(msvcp
);
31 #define SECSPERDAY 86400
32 /* 1601 to 1970 is 369 years plus 89 leap days */
33 #define SECS_1601_TO_1970 ((369 * 365 + 89) * (ULONGLONG)SECSPERDAY)
34 #define TICKSPERSEC 10000000
35 #define TICKS_1601_TO_1970 (SECS_1601_TO_1970 * TICKSPERSEC)
36 #define NANOSEC_PER_MILLISEC 1000000
37 #define MILLISEC_PER_SEC 1000
39 struct __Container_proxy
;
42 struct __Container_proxy
*proxy
;
45 typedef struct __Iterator_base12
{
46 struct __Container_proxy
*proxy
;
47 struct __Iterator_base12
*next
;
50 typedef struct __Container_proxy
{
51 const _Container_base12
*cont
;
52 _Iterator_base12
*head
;
55 /* ??0_Mutex@std@@QAE@XZ */
56 /* ??0_Mutex@std@@QEAA@XZ */
57 DEFINE_THISCALL_WRAPPER(mutex_ctor
, 4)
58 mutex
* __thiscall
mutex_ctor(mutex
*this)
60 CRITICAL_SECTION
*cs
= MSVCRT_operator_new(sizeof(*cs
));
62 ERR("Out of memory\n");
63 throw_exception(EXCEPTION_BAD_ALLOC
, NULL
);
66 InitializeCriticalSection(cs
);
67 cs
->DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": _Mutex critical section");
72 /* ??1_Mutex@std@@QAE@XZ */
73 /* ??1_Mutex@std@@QEAA@XZ */
74 DEFINE_THISCALL_WRAPPER(mutex_dtor
, 4)
75 void __thiscall
mutex_dtor(mutex
*this)
77 ((CRITICAL_SECTION
*)this->mutex
)->DebugInfo
->Spare
[0] = 0;
78 DeleteCriticalSection(this->mutex
);
79 MSVCRT_operator_delete(this->mutex
);
82 /* ?_Lock@_Mutex@std@@QAEXXZ */
83 /* ?_Lock@_Mutex@std@@QEAAXXZ */
84 DEFINE_THISCALL_WRAPPER(mutex_lock
, 4)
85 void __thiscall
mutex_lock(mutex
*this)
87 EnterCriticalSection(this->mutex
);
90 /* ?_Unlock@_Mutex@std@@QAEXXZ */
91 /* ?_Unlock@_Mutex@std@@QEAAXXZ */
92 DEFINE_THISCALL_WRAPPER(mutex_unlock
, 4)
93 void __thiscall
mutex_unlock(mutex
*this)
95 LeaveCriticalSection(this->mutex
);
98 /* ?_Mutex_Lock@_Mutex@std@@CAXPAV12@@Z */
99 /* ?_Mutex_Lock@_Mutex@std@@CAXPEAV12@@Z */
100 void CDECL
mutex_mutex_lock(mutex
*m
)
105 /* ?_Mutex_Unlock@_Mutex@std@@CAXPAV12@@Z */
106 /* ?_Mutex_Unlock@_Mutex@std@@CAXPEAV12@@Z */
107 void CDECL
mutex_mutex_unlock(mutex
*m
)
112 /* ?_Mutex_ctor@_Mutex@std@@CAXPAV12@@Z */
113 /* ?_Mutex_ctor@_Mutex@std@@CAXPEAV12@@Z */
114 void CDECL
mutex_mutex_ctor(mutex
*m
)
119 /* ?_Mutex_dtor@_Mutex@std@@CAXPAV12@@Z */
120 /* ?_Mutex_dtor@_Mutex@std@@CAXPEAV12@@Z */
121 void CDECL
mutex_mutex_dtor(mutex
*m
)
126 static CRITICAL_SECTION lockit_cs
[_MAX_LOCK
];
128 /* ?_Lockit_ctor@_Lockit@std@@SAXH@Z */
129 void __cdecl
_Lockit_init(int locktype
) {
130 InitializeCriticalSection(&lockit_cs
[locktype
]);
131 lockit_cs
[locktype
].DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": _Lockit critical section");
134 /* ?_Lockit_dtor@_Lockit@std@@SAXH@Z */
135 void __cdecl
_Lockit_free(int locktype
)
137 lockit_cs
[locktype
].DebugInfo
->Spare
[0] = 0;
138 DeleteCriticalSection(&lockit_cs
[locktype
]);
141 void init_lockit(void) {
144 for(i
=0; i
<_MAX_LOCK
; i
++)
148 void free_lockit(void) {
151 for(i
=0; i
<_MAX_LOCK
; i
++)
155 /* ?_Lockit_ctor@_Lockit@std@@CAXPAV12@H@Z */
156 /* ?_Lockit_ctor@_Lockit@std@@CAXPEAV12@H@Z */
157 void __cdecl
_Lockit__Lockit_ctor_locktype(_Lockit
*lockit
, int locktype
)
159 lockit
->locktype
= locktype
;
160 EnterCriticalSection(&lockit_cs
[locktype
]);
163 /* ?_Lockit_ctor@_Lockit@std@@CAXPAV12@@Z */
164 /* ?_Lockit_ctor@_Lockit@std@@CAXPEAV12@@Z */
165 void __cdecl
_Lockit__Lockit_ctor(_Lockit
*lockit
)
167 _Lockit__Lockit_ctor_locktype(lockit
, 0);
170 /* ??0_Lockit@std@@QAE@H@Z */
171 /* ??0_Lockit@std@@QEAA@H@Z */
172 DEFINE_THISCALL_WRAPPER(_Lockit_ctor_locktype
, 8)
173 _Lockit
* __thiscall
_Lockit_ctor_locktype(_Lockit
*this, int locktype
)
175 _Lockit__Lockit_ctor_locktype(this, locktype
);
179 /* ??0_Lockit@std@@QAE@XZ */
180 /* ??0_Lockit@std@@QEAA@XZ */
181 DEFINE_THISCALL_WRAPPER(_Lockit_ctor
, 4)
182 _Lockit
* __thiscall
_Lockit_ctor(_Lockit
*this)
184 _Lockit__Lockit_ctor_locktype(this, 0);
188 /* ?_Lockit_dtor@_Lockit@std@@CAXPAV12@@Z */
189 /* ?_Lockit_dtor@_Lockit@std@@CAXPEAV12@@Z */
190 void __cdecl
_Lockit__Lockit_dtor(_Lockit
*lockit
)
192 LeaveCriticalSection(&lockit_cs
[lockit
->locktype
]);
195 /* ??1_Lockit@std@@QAE@XZ */
196 /* ??1_Lockit@std@@QEAA@XZ */
197 DEFINE_THISCALL_WRAPPER(_Lockit_dtor
, 4)
198 void __thiscall
_Lockit_dtor(_Lockit
*this)
200 _Lockit__Lockit_dtor(this);
204 unsigned short __cdecl
wctype(const char *property
)
206 static const struct {
210 { "alnum", _DIGIT
|_ALPHA
},
212 { "cntrl", _CONTROL
},
214 { "graph", _DIGIT
|_PUNCT
|_ALPHA
},
216 { "print", _DIGIT
|_PUNCT
|_BLANK
|_ALPHA
},
224 for(i
=0; i
<sizeof(properties
)/sizeof(properties
[0]); i
++)
225 if(!strcmp(property
, properties
[i
].name
))
226 return properties
[i
].mask
;
231 typedef void (__cdecl
*MSVCP_new_handler_func
)(void);
232 static MSVCP_new_handler_func MSVCP_new_handler
;
233 static int __cdecl
new_handler_wrapper(MSVCP_size_t unused
)
239 /* ?set_new_handler@std@@YAP6AXXZP6AXXZ@Z */
240 MSVCP_new_handler_func __cdecl
set_new_handler(MSVCP_new_handler_func new_handler
)
242 MSVCP_new_handler_func old_handler
= MSVCP_new_handler
;
244 TRACE("%p\n", new_handler
);
246 MSVCP_new_handler
= new_handler
;
247 MSVCRT_set_new_handler(new_handler
? new_handler_wrapper
: NULL
);
251 /* ?set_new_handler@std@@YAP6AXXZH@Z */
252 MSVCP_new_handler_func __cdecl
set_new_handler_reset(int unused
)
254 return set_new_handler(NULL
);
257 /* _Container_base0 is used by apps compiled without iterator checking
258 * (i.e. with _ITERATOR_DEBUG_LEVEL=0 ).
259 * It provides empty versions of methods used by visual c++'s stl's
261 * msvcr100 has to provide them in case apps are compiled with /Od
262 * or the optimizer fails to inline those (empty) calls.
265 /* ?_Orphan_all@_Container_base0@std@@QAEXXZ */
266 /* ?_Orphan_all@_Container_base0@std@@QEAAXXZ */
267 DEFINE_THISCALL_WRAPPER(Container_base0_Orphan_all
, 4)
268 void __thiscall
Container_base0_Orphan_all(void *this)
272 /* ?_Swap_all@_Container_base0@std@@QAEXAAU12@@Z */
273 /* ?_Swap_all@_Container_base0@std@@QEAAXAEAU12@@Z */
274 DEFINE_THISCALL_WRAPPER(Container_base0_Swap_all
, 8)
275 void __thiscall
Container_base0_Swap_all(void *this, void *that
)
279 /* ??4_Container_base0@std@@QAEAAU01@ABU01@@Z */
280 /* ??4_Container_base0@std@@QEAAAEAU01@AEBU01@@Z */
281 DEFINE_THISCALL_WRAPPER(Container_base0_op_assign
, 8)
282 void* __thiscall
Container_base0_op_assign(void *this, const void *that
)
287 /* ??0_Container_base12@std@@QAE@ABU01@@Z */
288 /* ??0_Container_base12@std@@QEAA@AEBU01@@Z */
289 DEFINE_THISCALL_WRAPPER(_Container_base12_copy_ctor
, 8)
290 _Container_base12
* __thiscall
_Container_base12_copy_ctor(
291 _Container_base12
*this, _Container_base12
*that
)
297 /* ??0_Container_base12@std@@QAE@XZ */
298 /* ??0_Container_base12@std@@QEAA@XZ */
299 DEFINE_THISCALL_WRAPPER(_Container_base12_ctor
, 4)
300 _Container_base12
* __thiscall
_Container_base12_ctor(_Container_base12
*this)
306 /* ??1_Container_base12@std@@QAE@XZ */
307 /* ??1_Container_base12@std@@QEAA@XZ */
308 DEFINE_THISCALL_WRAPPER(_Container_base12_dtor
, 4)
309 void __thiscall
_Container_base12_dtor(_Container_base12
*this)
313 /* ??4_Container_base12@std@@QAEAAU01@ABU01@@Z */
314 /* ??4_Container_base12@std@@QEAAAEAU01@AEBU01@@ */
315 DEFINE_THISCALL_WRAPPER(_Container_base12_op_assign
, 8)
316 _Container_base12
* __thiscall
_Container_base12_op_assign(
317 _Container_base12
*this, const _Container_base12
*that
)
322 /* ?_Getpfirst@_Container_base12@std@@QBEPAPAU_Iterator_base12@2@XZ */
323 /* ?_Getpfirst@_Container_base12@std@@QEBAPEAPEAU_Iterator_base12@2@XZ */
324 DEFINE_THISCALL_WRAPPER(_Container_base12__Getpfirst
, 4)
325 _Iterator_base12
** __thiscall
_Container_base12__Getpfirst(_Container_base12
*this)
327 return this->proxy
? &this->proxy
->head
: NULL
;
330 /* ?_Orphan_all@_Container_base12@std@@QAEXXZ */
331 /* ?_Orphan_all@_Container_base12@std@@QEAAXXZ */
332 DEFINE_THISCALL_WRAPPER(_Container_base12__Orphan_all
, 4)
333 void __thiscall
_Container_base12__Orphan_all(_Container_base12
*this)
337 /* ?_Swap_all@_Container_base12@std@@QAEXAAU12@@Z */
338 /* ?_Swap_all@_Container_base12@std@@QEAAXAEAU12@@Z */
339 DEFINE_THISCALL_WRAPPER(_Container_base12__Swap_all
, 8)
340 void __thiscall
_Container_base12__Swap_all(
341 _Container_base12
*this, _Container_base12
*that
)
343 _Container_proxy
*tmp
;
346 this->proxy
= that
->proxy
;
350 this->proxy
->cont
= this;
352 that
->proxy
->cont
= that
;
355 #if _MSVCP_VER >= 110
356 typedef int MSVCRT_long
;
364 /* _Xtime_get_ticks */
365 LONGLONG __cdecl
_Xtime_get_ticks(void)
371 GetSystemTimeAsFileTime(&ft
);
372 return ((LONGLONG
)ft
.dwHighDateTime
<<32) + ft
.dwLowDateTime
- TICKS_1601_TO_1970
;
376 int __cdecl
xtime_get(xtime
* t
, int unknown
)
385 ticks
= _Xtime_get_ticks();
386 t
->sec
= ticks
/ TICKSPERSEC
;
387 t
->nsec
= ticks
% TICKSPERSEC
* 100;
391 /* _Xtime_diff_to_millis2 */
392 MSVCRT_long __cdecl
_Xtime_diff_to_millis2(xtime
*t1
, xtime
*t2
)
395 MSVCRT_long diff_nsec
, ret
;
397 TRACE("(%p, %p)\n", t1
, t2
);
399 diff_sec
= t1
->sec
- t2
->sec
;
400 diff_nsec
= t1
->nsec
- t2
->nsec
;
401 ret
= diff_sec
* MILLISEC_PER_SEC
+
402 (diff_nsec
+ NANOSEC_PER_MILLISEC
- 1) / NANOSEC_PER_MILLISEC
;
403 return ret
> 0 ? ret
: 0;
406 /* _Xtime_diff_to_millis */
407 MSVCRT_long __cdecl
_Xtime_diff_to_millis(xtime
*t
)
414 return _Xtime_diff_to_millis2(t
, &now
);
419 unsigned int __cdecl
_Random_device(void)
425 /* TODO: throw correct exception in case of failure */
427 throw_exception(EXCEPTION
, "random number generator failed\n");
432 #if _MSVCP_VER >= 110
433 #if defined(__i386__) && !defined(__arm__)
435 #define THISCALL(func) __thiscall_ ## func
436 #define __thiscall __stdcall
437 #define DEFINE_THISCALL_WRAPPER(func,args) \
438 extern void THISCALL(func)(void); \
439 __ASM_GLOBAL_FUNC(__thiscall_ ## func, \
443 "jmp " __ASM_NAME(#func) __ASM_STDCALL(args) )
445 extern void *call_thiscall_func
;
446 __ASM_GLOBAL_FUNC(call_thiscall_func
,
453 #define call_func1(func,this) ((void* (WINAPI*)(void*,void*))&call_thiscall_func)(func,this)
457 #define __thiscall __cdecl
458 #define DEFINE_THISCALL_WRAPPER(func,args) /* nothing */
460 #define call_func1(func,this) func(this)
462 #endif /* __i386__ */
464 #define MTX_MULTI_LOCK 0x100
474 int __cdecl
_Mtx_init(_Mtx_t
*mtx
, int flags
)
476 if(flags
& ~MTX_MULTI_LOCK
)
477 FIXME("unknown flags ignorred: %x\n", flags
);
479 *mtx
= MSVCRT_operator_new(sizeof(**mtx
));
480 (*mtx
)->flags
= flags
;
481 call_func1(critical_section_ctor
, &(*mtx
)->cs
);
482 (*mtx
)->thread_id
= -1;
487 void __cdecl
_Mtx_destroy(_Mtx_t
*mtx
)
489 call_func1(critical_section_dtor
, &(*mtx
)->cs
);
490 MSVCRT_operator_delete(*mtx
);
493 int __cdecl
_Mtx_lock(_Mtx_t
*mtx
)
495 if((*mtx
)->thread_id
!= GetCurrentThreadId()) {
496 call_func1(critical_section_lock
, &(*mtx
)->cs
);
497 (*mtx
)->thread_id
= GetCurrentThreadId();
498 }else if(!((*mtx
)->flags
& MTX_MULTI_LOCK
)) {
506 int __cdecl
_Mtx_unlock(_Mtx_t
*mtx
)
511 (*mtx
)->thread_id
= -1;
512 call_func1(critical_section_unlock
, &(*mtx
)->cs
);
516 int __cdecl
_Mtx_trylock(_Mtx_t
*mtx
)
518 if((*mtx
)->thread_id
!= GetCurrentThreadId()) {
519 if(!call_func1(critical_section_trylock
, &(*mtx
)->cs
))
521 (*mtx
)->thread_id
= GetCurrentThreadId();
522 }else if(!((*mtx
)->flags
& MTX_MULTI_LOCK
)) {
530 critical_section
* __cdecl
_Mtx_getconcrtcs(_Mtx_t
*mtx
)
536 #if _MSVCP_VER == 100
538 const vtable_ptr
*vtable
;
545 static custom_category iostream_category
;
547 DEFINE_RTTI_DATA0(error_category
, 0, ".?AVerror_category@std@@")
548 DEFINE_RTTI_DATA1(iostream_category
, 0, &error_category_rtti_base_descriptor
, ".?AV_Iostream_error_category@std@@")
550 extern const vtable_ptr MSVCP_iostream_category_vtable
;
552 static void iostream_category_ctor(custom_category
*this)
554 this->base
.vtable
= &MSVCP_iostream_category_vtable
;
555 this->type
= "iostream";
558 DEFINE_THISCALL_WRAPPER(custom_category_vector_dtor
, 8)
559 custom_category
* __thiscall
custom_category_vector_dtor(custom_category
*this, unsigned int flags
)
561 TRACE("(%p %x)\n", this, flags
);
563 /* we have an array, with the number of elements stored before the first object */
564 INT_PTR i
, *ptr
= (INT_PTR
*)this-1;
566 for(i
=*ptr
-1; i
>=0; i
--)
567 MSVCRT_operator_delete(ptr
);
570 MSVCRT_operator_delete(this);
576 DEFINE_THISCALL_WRAPPER(custom_category_name
, 4)
577 const char* __thiscall
custom_category_name(const custom_category
*this)
582 DEFINE_THISCALL_WRAPPER(custom_category_message
, 12)
583 basic_string_char
* __thiscall
custom_category_message(const custom_category
*this,
584 basic_string_char
*ret
, int err
)
586 if(err
== 1) return MSVCP_basic_string_char_ctor_cstr(ret
, "iostream error");
587 return MSVCP_basic_string_char_ctor_cstr(ret
, strerror(err
));
590 DEFINE_THISCALL_WRAPPER(custom_category_default_error_condition
, 12)
591 /*error_condition*/void* __thiscall
custom_category_default_error_condition(
592 custom_category
*this, /*error_condition*/void *ret
, int code
)
594 FIXME("(%p %p %x) stub\n", this, ret
, code
);
598 DEFINE_THISCALL_WRAPPER(custom_category_equivalent
, 12)
599 MSVCP_bool __thiscall
custom_category_equivalent(const custom_category
*this,
600 int code
, const /*error_condition*/void *condition
)
602 FIXME("(%p %x %p) stub\n", this, code
, condition
);
606 DEFINE_THISCALL_WRAPPER(custom_category_equivalent_code
, 12)
607 MSVCP_bool __thiscall
custom_category_equivalent_code(custom_category
*this,
608 const /*error_code*/void *code
, int condition
)
610 FIXME("(%p %p %x) stub\n", this, code
, condition
);
614 /* ?iostream_category@std@@YAABVerror_category@1@XZ */
615 /* ?iostream_category@std@@YAAEBVerror_category@1@XZ */
616 const error_category
* __cdecl
std_iostream_category(void)
619 return &iostream_category
.base
;
623 void __asm_dummy_vtables(void) {
625 __ASM_VTABLE(iostream_category
,
626 VTABLE_ADD_FUNC(custom_category_vector_dtor
)
627 VTABLE_ADD_FUNC(custom_category_name
)
628 VTABLE_ADD_FUNC(custom_category_message
)
629 VTABLE_ADD_FUNC(custom_category_default_error_condition
)
630 VTABLE_ADD_FUNC(custom_category_equivalent
)
631 VTABLE_ADD_FUNC(custom_category_equivalent_code
));
637 void init_misc(void *base
)
640 #if _MSVCP_VER == 100
641 init_error_category_rtti(base
);
642 init_iostream_category_rtti(base
);
646 #if _MSVCP_VER == 100
647 iostream_category_ctor(&iostream_category
);