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 struct __Container_proxy
;
34 struct __Container_proxy
*proxy
;
37 typedef struct __Iterator_base12
{
38 struct __Container_proxy
*proxy
;
39 struct __Iterator_base12
*next
;
42 typedef struct __Container_proxy
{
43 const _Container_base12
*cont
;
44 _Iterator_base12
*head
;
47 /* ??0_Mutex@std@@QAE@XZ */
48 /* ??0_Mutex@std@@QEAA@XZ */
49 DEFINE_THISCALL_WRAPPER(mutex_ctor
, 4)
50 mutex
* __thiscall
mutex_ctor(mutex
*this)
52 CRITICAL_SECTION
*cs
= MSVCRT_operator_new(sizeof(*cs
));
54 ERR("Out of memory\n");
55 throw_exception(EXCEPTION_BAD_ALLOC
, NULL
);
58 InitializeCriticalSection(cs
);
59 cs
->DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": _Mutex critical section");
64 /* ??1_Mutex@std@@QAE@XZ */
65 /* ??1_Mutex@std@@QEAA@XZ */
66 DEFINE_THISCALL_WRAPPER(mutex_dtor
, 4)
67 void __thiscall
mutex_dtor(mutex
*this)
69 ((CRITICAL_SECTION
*)this->mutex
)->DebugInfo
->Spare
[0] = 0;
70 DeleteCriticalSection(this->mutex
);
71 MSVCRT_operator_delete(this->mutex
);
74 /* ?_Lock@_Mutex@std@@QAEXXZ */
75 /* ?_Lock@_Mutex@std@@QEAAXXZ */
76 DEFINE_THISCALL_WRAPPER(mutex_lock
, 4)
77 void __thiscall
mutex_lock(mutex
*this)
79 EnterCriticalSection(this->mutex
);
82 /* ?_Unlock@_Mutex@std@@QAEXXZ */
83 /* ?_Unlock@_Mutex@std@@QEAAXXZ */
84 DEFINE_THISCALL_WRAPPER(mutex_unlock
, 4)
85 void __thiscall
mutex_unlock(mutex
*this)
87 LeaveCriticalSection(this->mutex
);
90 /* ?_Mutex_Lock@_Mutex@std@@CAXPAV12@@Z */
91 /* ?_Mutex_Lock@_Mutex@std@@CAXPEAV12@@Z */
92 void CDECL
mutex_mutex_lock(mutex
*m
)
97 /* ?_Mutex_Unlock@_Mutex@std@@CAXPAV12@@Z */
98 /* ?_Mutex_Unlock@_Mutex@std@@CAXPEAV12@@Z */
99 void CDECL
mutex_mutex_unlock(mutex
*m
)
104 /* ?_Mutex_ctor@_Mutex@std@@CAXPAV12@@Z */
105 /* ?_Mutex_ctor@_Mutex@std@@CAXPEAV12@@Z */
106 void CDECL
mutex_mutex_ctor(mutex
*m
)
111 /* ?_Mutex_dtor@_Mutex@std@@CAXPAV12@@Z */
112 /* ?_Mutex_dtor@_Mutex@std@@CAXPEAV12@@Z */
113 void CDECL
mutex_mutex_dtor(mutex
*m
)
118 static CRITICAL_SECTION lockit_cs
[_MAX_LOCK
];
121 static inline int get_locktype( _Lockit
*lockit
) { return lockit
->locktype
; }
122 static inline void set_locktype( _Lockit
*lockit
, int type
) { lockit
->locktype
= type
; }
124 static inline int get_locktype( _Lockit
*lockit
) { return 0; }
125 static inline void set_locktype( _Lockit
*lockit
, int type
) { }
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 set_locktype( lockit
, 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
[get_locktype( lockit
)]);
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
357 #define SECSPERDAY 86400
358 /* 1601 to 1970 is 369 years plus 89 leap days */
359 #define SECS_1601_TO_1970 ((369 * 365 + 89) * (ULONGLONG)SECSPERDAY)
360 #define TICKSPERSEC 10000000
361 #define TICKS_1601_TO_1970 (SECS_1601_TO_1970 * TICKSPERSEC)
362 #define NANOSEC_PER_MILLISEC 1000000
363 #define MILLISEC_PER_SEC 1000
365 typedef int MSVCRT_long
;
373 /* _Xtime_get_ticks */
374 LONGLONG __cdecl
_Xtime_get_ticks(void)
380 GetSystemTimeAsFileTime(&ft
);
381 return ((LONGLONG
)ft
.dwHighDateTime
<<32) + ft
.dwLowDateTime
- TICKS_1601_TO_1970
;
385 int __cdecl
xtime_get(xtime
* t
, int unknown
)
394 ticks
= _Xtime_get_ticks();
395 t
->sec
= ticks
/ TICKSPERSEC
;
396 t
->nsec
= ticks
% TICKSPERSEC
* 100;
400 /* _Xtime_diff_to_millis2 */
401 MSVCRT_long __cdecl
_Xtime_diff_to_millis2(xtime
*t1
, xtime
*t2
)
404 MSVCRT_long diff_nsec
, ret
;
406 TRACE("(%p, %p)\n", t1
, t2
);
408 diff_sec
= t1
->sec
- t2
->sec
;
409 diff_nsec
= t1
->nsec
- t2
->nsec
;
410 ret
= diff_sec
* MILLISEC_PER_SEC
+
411 (diff_nsec
+ NANOSEC_PER_MILLISEC
- 1) / NANOSEC_PER_MILLISEC
;
412 return ret
> 0 ? ret
: 0;
415 /* _Xtime_diff_to_millis */
416 MSVCRT_long __cdecl
_Xtime_diff_to_millis(xtime
*t
)
423 return _Xtime_diff_to_millis2(t
, &now
);
428 unsigned int __cdecl
_Random_device(void)
434 /* TODO: throw correct exception in case of failure */
436 throw_exception(EXCEPTION
, "random number generator failed\n");
441 #if _MSVCP_VER >= 110
442 #if defined(__i386__) && !defined(__arm__)
444 extern void *call_thiscall_func
;
445 __ASM_GLOBAL_FUNC(call_thiscall_func
,
452 #define call_func1(func,this) ((void* (WINAPI*)(void*,void*))&call_thiscall_func)(func,this)
456 #define call_func1(func,this) func(this)
458 #endif /* __i386__ */
460 #define MTX_MULTI_LOCK 0x100
470 int __cdecl
_Mtx_init(_Mtx_t
*mtx
, int flags
)
472 if(flags
& ~MTX_MULTI_LOCK
)
473 FIXME("unknown flags ignorred: %x\n", flags
);
475 *mtx
= MSVCRT_operator_new(sizeof(**mtx
));
476 (*mtx
)->flags
= flags
;
477 call_func1(critical_section_ctor
, &(*mtx
)->cs
);
478 (*mtx
)->thread_id
= -1;
483 void __cdecl
_Mtx_destroy(_Mtx_t
*mtx
)
485 call_func1(critical_section_dtor
, &(*mtx
)->cs
);
486 MSVCRT_operator_delete(*mtx
);
489 int __cdecl
_Mtx_lock(_Mtx_t
*mtx
)
491 if((*mtx
)->thread_id
!= GetCurrentThreadId()) {
492 call_func1(critical_section_lock
, &(*mtx
)->cs
);
493 (*mtx
)->thread_id
= GetCurrentThreadId();
494 }else if(!((*mtx
)->flags
& MTX_MULTI_LOCK
)) {
502 int __cdecl
_Mtx_unlock(_Mtx_t
*mtx
)
507 (*mtx
)->thread_id
= -1;
508 call_func1(critical_section_unlock
, &(*mtx
)->cs
);
512 int __cdecl
_Mtx_trylock(_Mtx_t
*mtx
)
514 if((*mtx
)->thread_id
!= GetCurrentThreadId()) {
515 if(!call_func1(critical_section_trylock
, &(*mtx
)->cs
))
517 (*mtx
)->thread_id
= GetCurrentThreadId();
518 }else if(!((*mtx
)->flags
& MTX_MULTI_LOCK
)) {
526 critical_section
* __cdecl
_Mtx_getconcrtcs(_Mtx_t
*mtx
)
532 #if _MSVCP_VER == 100
534 const vtable_ptr
*vtable
;
541 static custom_category iostream_category
;
543 DEFINE_RTTI_DATA0(error_category
, 0, ".?AVerror_category@std@@")
544 DEFINE_RTTI_DATA1(iostream_category
, 0, &error_category_rtti_base_descriptor
, ".?AV_Iostream_error_category@std@@")
546 extern const vtable_ptr MSVCP_iostream_category_vtable
;
548 static void iostream_category_ctor(custom_category
*this)
550 this->base
.vtable
= &MSVCP_iostream_category_vtable
;
551 this->type
= "iostream";
554 DEFINE_THISCALL_WRAPPER(custom_category_vector_dtor
, 8)
555 custom_category
* __thiscall
custom_category_vector_dtor(custom_category
*this, unsigned int flags
)
557 TRACE("(%p %x)\n", this, flags
);
559 /* we have an array, with the number of elements stored before the first object */
560 INT_PTR i
, *ptr
= (INT_PTR
*)this-1;
562 for(i
=*ptr
-1; i
>=0; i
--)
563 MSVCRT_operator_delete(ptr
);
566 MSVCRT_operator_delete(this);
572 DEFINE_THISCALL_WRAPPER(custom_category_name
, 4)
573 const char* __thiscall
custom_category_name(const custom_category
*this)
578 DEFINE_THISCALL_WRAPPER(custom_category_message
, 12)
579 basic_string_char
* __thiscall
custom_category_message(const custom_category
*this,
580 basic_string_char
*ret
, int err
)
582 if(err
== 1) return MSVCP_basic_string_char_ctor_cstr(ret
, "iostream error");
583 return MSVCP_basic_string_char_ctor_cstr(ret
, strerror(err
));
586 DEFINE_THISCALL_WRAPPER(custom_category_default_error_condition
, 12)
587 /*error_condition*/void* __thiscall
custom_category_default_error_condition(
588 custom_category
*this, /*error_condition*/void *ret
, int code
)
590 FIXME("(%p %p %x) stub\n", this, ret
, code
);
594 DEFINE_THISCALL_WRAPPER(custom_category_equivalent
, 12)
595 MSVCP_bool __thiscall
custom_category_equivalent(const custom_category
*this,
596 int code
, const /*error_condition*/void *condition
)
598 FIXME("(%p %x %p) stub\n", this, code
, condition
);
602 DEFINE_THISCALL_WRAPPER(custom_category_equivalent_code
, 12)
603 MSVCP_bool __thiscall
custom_category_equivalent_code(custom_category
*this,
604 const /*error_code*/void *code
, int condition
)
606 FIXME("(%p %p %x) stub\n", this, code
, condition
);
610 /* ?iostream_category@std@@YAABVerror_category@1@XZ */
611 /* ?iostream_category@std@@YAAEBVerror_category@1@XZ */
612 const error_category
* __cdecl
std_iostream_category(void)
615 return &iostream_category
.base
;
619 void __asm_dummy_vtables(void) {
621 __ASM_VTABLE(iostream_category
,
622 VTABLE_ADD_FUNC(custom_category_vector_dtor
)
623 VTABLE_ADD_FUNC(custom_category_name
)
624 VTABLE_ADD_FUNC(custom_category_message
)
625 VTABLE_ADD_FUNC(custom_category_default_error_condition
)
626 VTABLE_ADD_FUNC(custom_category_equivalent
)
627 VTABLE_ADD_FUNC(custom_category_equivalent_code
));
633 #if _MSVCP_VER >= 110
634 static CRITICAL_SECTION call_once_cs
;
635 static CRITICAL_SECTION_DEBUG call_once_cs_debug
=
638 { &call_once_cs_debug
.ProcessLocksList
, &call_once_cs_debug
.ProcessLocksList
},
639 0, 0, { (DWORD_PTR
)(__FILE__
": call_once_cs") }
641 static CRITICAL_SECTION call_once_cs
= { &call_once_cs_debug
, -1, 0, 0, 0, 0 };
643 void __cdecl
_Call_onceEx(int *once
, void (__cdecl
*func
)(void*), void *argv
)
645 TRACE("%p %p %p\n", once
, func
, argv
);
647 EnterCriticalSection(&call_once_cs
);
649 /* FIXME: handle exceptions */
653 LeaveCriticalSection(&call_once_cs
);
656 static void __cdecl
call_once_func_wrapper(void *func
)
658 ((void (__cdecl
*)(void))func
)();
661 void __cdecl
_Call_once(int *once
, void (__cdecl
*func
)(void))
663 TRACE("%p %p\n", once
, func
);
664 _Call_onceEx(once
, call_once_func_wrapper
, func
);
667 void __cdecl
_Do_call(void *this)
669 CALL_VTBL_FUNC(this, 0, void, (void*), (this));
673 void init_misc(void *base
)
676 #if _MSVCP_VER == 100
677 init_error_category_rtti(base
);
678 init_iostream_category_rtti(base
);
682 #if _MSVCP_VER == 100
683 iostream_category_ctor(&iostream_category
);