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)
37 struct __Container_proxy
;
40 struct __Container_proxy
*proxy
;
43 typedef struct __Iterator_base12
{
44 struct __Container_proxy
*proxy
;
45 struct __Iterator_base12
*next
;
48 typedef struct __Container_proxy
{
49 const _Container_base12
*cont
;
50 _Iterator_base12
*head
;
53 /* ??0_Mutex@std@@QAE@XZ */
54 /* ??0_Mutex@std@@QEAA@XZ */
55 DEFINE_THISCALL_WRAPPER(mutex_ctor
, 4)
56 mutex
* __thiscall
mutex_ctor(mutex
*this)
58 CRITICAL_SECTION
*cs
= MSVCRT_operator_new(sizeof(*cs
));
60 ERR("Out of memory\n");
61 throw_exception(EXCEPTION_BAD_ALLOC
, NULL
);
64 InitializeCriticalSection(cs
);
65 cs
->DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": _Mutex critical section");
70 /* ??1_Mutex@std@@QAE@XZ */
71 /* ??1_Mutex@std@@QEAA@XZ */
72 DEFINE_THISCALL_WRAPPER(mutex_dtor
, 4)
73 void __thiscall
mutex_dtor(mutex
*this)
75 ((CRITICAL_SECTION
*)this->mutex
)->DebugInfo
->Spare
[0] = 0;
76 DeleteCriticalSection(this->mutex
);
77 MSVCRT_operator_delete(this->mutex
);
80 /* ?_Lock@_Mutex@std@@QAEXXZ */
81 /* ?_Lock@_Mutex@std@@QEAAXXZ */
82 DEFINE_THISCALL_WRAPPER(mutex_lock
, 4)
83 void __thiscall
mutex_lock(mutex
*this)
85 EnterCriticalSection(this->mutex
);
88 /* ?_Unlock@_Mutex@std@@QAEXXZ */
89 /* ?_Unlock@_Mutex@std@@QEAAXXZ */
90 DEFINE_THISCALL_WRAPPER(mutex_unlock
, 4)
91 void __thiscall
mutex_unlock(mutex
*this)
93 LeaveCriticalSection(this->mutex
);
96 /* ?_Mutex_Lock@_Mutex@std@@CAXPAV12@@Z */
97 /* ?_Mutex_Lock@_Mutex@std@@CAXPEAV12@@Z */
98 void CDECL
mutex_mutex_lock(mutex
*m
)
103 /* ?_Mutex_Unlock@_Mutex@std@@CAXPAV12@@Z */
104 /* ?_Mutex_Unlock@_Mutex@std@@CAXPEAV12@@Z */
105 void CDECL
mutex_mutex_unlock(mutex
*m
)
110 /* ?_Mutex_ctor@_Mutex@std@@CAXPAV12@@Z */
111 /* ?_Mutex_ctor@_Mutex@std@@CAXPEAV12@@Z */
112 void CDECL
mutex_mutex_ctor(mutex
*m
)
117 /* ?_Mutex_dtor@_Mutex@std@@CAXPAV12@@Z */
118 /* ?_Mutex_dtor@_Mutex@std@@CAXPEAV12@@Z */
119 void CDECL
mutex_mutex_dtor(mutex
*m
)
124 static CRITICAL_SECTION lockit_cs
[_MAX_LOCK
];
126 /* ?_Lockit_ctor@_Lockit@std@@SAXH@Z */
127 void __cdecl
_Lockit_init(int locktype
) {
128 InitializeCriticalSection(&lockit_cs
[locktype
]);
129 lockit_cs
[locktype
].DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": _Lockit critical section");
132 /* ?_Lockit_dtor@_Lockit@std@@SAXH@Z */
133 void __cdecl
_Lockit_free(int locktype
)
135 lockit_cs
[locktype
].DebugInfo
->Spare
[0] = 0;
136 DeleteCriticalSection(&lockit_cs
[locktype
]);
139 void init_lockit(void) {
142 for(i
=0; i
<_MAX_LOCK
; i
++)
146 void free_lockit(void) {
149 for(i
=0; i
<_MAX_LOCK
; i
++)
153 /* ?_Lockit_ctor@_Lockit@std@@CAXPAV12@H@Z */
154 /* ?_Lockit_ctor@_Lockit@std@@CAXPEAV12@H@Z */
155 void __cdecl
_Lockit__Lockit_ctor_locktype(_Lockit
*lockit
, int locktype
)
157 lockit
->locktype
= locktype
;
158 EnterCriticalSection(&lockit_cs
[locktype
]);
161 /* ?_Lockit_ctor@_Lockit@std@@CAXPAV12@@Z */
162 /* ?_Lockit_ctor@_Lockit@std@@CAXPEAV12@@Z */
163 void __cdecl
_Lockit__Lockit_ctor(_Lockit
*lockit
)
165 _Lockit__Lockit_ctor_locktype(lockit
, 0);
168 /* ??0_Lockit@std@@QAE@H@Z */
169 /* ??0_Lockit@std@@QEAA@H@Z */
170 DEFINE_THISCALL_WRAPPER(_Lockit_ctor_locktype
, 8)
171 _Lockit
* __thiscall
_Lockit_ctor_locktype(_Lockit
*this, int locktype
)
173 _Lockit__Lockit_ctor_locktype(this, locktype
);
177 /* ??0_Lockit@std@@QAE@XZ */
178 /* ??0_Lockit@std@@QEAA@XZ */
179 DEFINE_THISCALL_WRAPPER(_Lockit_ctor
, 4)
180 _Lockit
* __thiscall
_Lockit_ctor(_Lockit
*this)
182 _Lockit__Lockit_ctor_locktype(this, 0);
186 /* ?_Lockit_dtor@_Lockit@std@@CAXPAV12@@Z */
187 /* ?_Lockit_dtor@_Lockit@std@@CAXPEAV12@@Z */
188 void __cdecl
_Lockit__Lockit_dtor(_Lockit
*lockit
)
190 LeaveCriticalSection(&lockit_cs
[lockit
->locktype
]);
193 /* ??1_Lockit@std@@QAE@XZ */
194 /* ??1_Lockit@std@@QEAA@XZ */
195 DEFINE_THISCALL_WRAPPER(_Lockit_dtor
, 4)
196 void __thiscall
_Lockit_dtor(_Lockit
*this)
198 _Lockit__Lockit_dtor(this);
202 unsigned short __cdecl
wctype(const char *property
)
204 static const struct {
208 { "alnum", _DIGIT
|_ALPHA
},
210 { "cntrl", _CONTROL
},
212 { "graph", _DIGIT
|_PUNCT
|_ALPHA
},
214 { "print", _DIGIT
|_PUNCT
|_BLANK
|_ALPHA
},
222 for(i
=0; i
<sizeof(properties
)/sizeof(properties
[0]); i
++)
223 if(!strcmp(property
, properties
[i
].name
))
224 return properties
[i
].mask
;
229 typedef void (__cdecl
*MSVCP_new_handler_func
)(void);
230 static MSVCP_new_handler_func MSVCP_new_handler
;
231 static int __cdecl
new_handler_wrapper(MSVCP_size_t unused
)
237 /* ?set_new_handler@std@@YAP6AXXZP6AXXZ@Z */
238 MSVCP_new_handler_func __cdecl
set_new_handler(MSVCP_new_handler_func new_handler
)
240 MSVCP_new_handler_func old_handler
= MSVCP_new_handler
;
242 TRACE("%p\n", new_handler
);
244 MSVCP_new_handler
= new_handler
;
245 MSVCRT_set_new_handler(new_handler
? new_handler_wrapper
: NULL
);
249 /* ?set_new_handler@std@@YAP6AXXZH@Z */
250 MSVCP_new_handler_func __cdecl
set_new_handler_reset(int unused
)
252 return set_new_handler(NULL
);
255 /* _Container_base0 is used by apps compiled without iterator checking
256 * (i.e. with _ITERATOR_DEBUG_LEVEL=0 ).
257 * It provides empty versions of methods used by visual c++'s stl's
259 * msvcr100 has to provide them in case apps are compiled with /Od
260 * or the optimizer fails to inline those (empty) calls.
263 /* ?_Orphan_all@_Container_base0@std@@QAEXXZ */
264 /* ?_Orphan_all@_Container_base0@std@@QEAAXXZ */
265 DEFINE_THISCALL_WRAPPER(Container_base0_Orphan_all
, 4)
266 void __thiscall
Container_base0_Orphan_all(void *this)
270 /* ?_Swap_all@_Container_base0@std@@QAEXAAU12@@Z */
271 /* ?_Swap_all@_Container_base0@std@@QEAAXAEAU12@@Z */
272 DEFINE_THISCALL_WRAPPER(Container_base0_Swap_all
, 8)
273 void __thiscall
Container_base0_Swap_all(void *this, void *that
)
277 /* ??4_Container_base0@std@@QAEAAU01@ABU01@@Z */
278 /* ??4_Container_base0@std@@QEAAAEAU01@AEBU01@@Z */
279 DEFINE_THISCALL_WRAPPER(Container_base0_op_assign
, 8)
280 void* __thiscall
Container_base0_op_assign(void *this, const void *that
)
285 /* ??0_Container_base12@std@@QAE@ABU01@@Z */
286 /* ??0_Container_base12@std@@QEAA@AEBU01@@Z */
287 DEFINE_THISCALL_WRAPPER(_Container_base12_copy_ctor
, 8)
288 _Container_base12
* __thiscall
_Container_base12_copy_ctor(
289 _Container_base12
*this, _Container_base12
*that
)
295 /* ??0_Container_base12@std@@QAE@XZ */
296 /* ??0_Container_base12@std@@QEAA@XZ */
297 DEFINE_THISCALL_WRAPPER(_Container_base12_ctor
, 4)
298 _Container_base12
* __thiscall
_Container_base12_ctor(_Container_base12
*this)
304 /* ??1_Container_base12@std@@QAE@XZ */
305 /* ??1_Container_base12@std@@QEAA@XZ */
306 DEFINE_THISCALL_WRAPPER(_Container_base12_dtor
, 4)
307 void __thiscall
_Container_base12_dtor(_Container_base12
*this)
311 /* ??4_Container_base12@std@@QAEAAU01@ABU01@@Z */
312 /* ??4_Container_base12@std@@QEAAAEAU01@AEBU01@@ */
313 DEFINE_THISCALL_WRAPPER(_Container_base12_op_assign
, 8)
314 _Container_base12
* __thiscall
_Container_base12_op_assign(
315 _Container_base12
*this, const _Container_base12
*that
)
320 /* ?_Getpfirst@_Container_base12@std@@QBEPAPAU_Iterator_base12@2@XZ */
321 /* ?_Getpfirst@_Container_base12@std@@QEBAPEAPEAU_Iterator_base12@2@XZ */
322 DEFINE_THISCALL_WRAPPER(_Container_base12__Getpfirst
, 4)
323 _Iterator_base12
** __thiscall
_Container_base12__Getpfirst(_Container_base12
*this)
325 return this->proxy
? &this->proxy
->head
: NULL
;
328 /* ?_Orphan_all@_Container_base12@std@@QAEXXZ */
329 /* ?_Orphan_all@_Container_base12@std@@QEAAXXZ */
330 DEFINE_THISCALL_WRAPPER(_Container_base12__Orphan_all
, 4)
331 void __thiscall
_Container_base12__Orphan_all(_Container_base12
*this)
335 /* ?_Swap_all@_Container_base12@std@@QAEXAAU12@@Z */
336 /* ?_Swap_all@_Container_base12@std@@QEAAXAEAU12@@Z */
337 DEFINE_THISCALL_WRAPPER(_Container_base12__Swap_all
, 8)
338 void __thiscall
_Container_base12__Swap_all(
339 _Container_base12
*this, _Container_base12
*that
)
341 _Container_proxy
*tmp
;
344 this->proxy
= that
->proxy
;
348 this->proxy
->cont
= this;
350 that
->proxy
->cont
= that
;
353 /* _Xtime_get_ticks */
354 LONGLONG __cdecl
_Xtime_get_ticks(void)
360 GetSystemTimeAsFileTime(&ft
);
361 return ((LONGLONG
)ft
.dwHighDateTime
<<32) + ft
.dwLowDateTime
- TICKS_1601_TO_1970
;
365 unsigned int __cdecl
_Random_device(void)
371 /* TODO: throw correct exception in case of failure */
373 throw_exception(EXCEPTION
, "random number generator failed\n");
378 #if _MSVCP_VER >= 110
379 #if defined(__i386__) && !defined(__arm__)
381 #define THISCALL(func) __thiscall_ ## func
382 #define __thiscall __stdcall
383 #define DEFINE_THISCALL_WRAPPER(func,args) \
384 extern void THISCALL(func)(void); \
385 __ASM_GLOBAL_FUNC(__thiscall_ ## func, \
389 "jmp " __ASM_NAME(#func) __ASM_STDCALL(args) )
391 extern void *call_thiscall_func
;
392 __ASM_GLOBAL_FUNC(call_thiscall_func
,
399 #define call_func1(func,this) ((void* (WINAPI*)(void*,void*))&call_thiscall_func)(func,this)
403 #define __thiscall __cdecl
404 #define DEFINE_THISCALL_WRAPPER(func,args) /* nothing */
406 #define call_func1(func,this) func(this)
408 #endif /* __i386__ */
410 #define MTX_MULTI_LOCK 0x100
420 int __cdecl
_Mtx_init(_Mtx_t
*mtx
, int flags
)
422 if(flags
& ~MTX_MULTI_LOCK
)
423 FIXME("unknown flags ignorred: %x\n", flags
);
425 *mtx
= MSVCRT_operator_new(sizeof(**mtx
));
426 (*mtx
)->flags
= flags
;
427 call_func1(critical_section_ctor
, &(*mtx
)->cs
);
428 (*mtx
)->thread_id
= -1;
433 void __cdecl
_Mtx_destroy(_Mtx_t
*mtx
)
435 call_func1(critical_section_dtor
, &(*mtx
)->cs
);
436 MSVCRT_operator_delete(*mtx
);
439 int __cdecl
_Mtx_lock(_Mtx_t
*mtx
)
441 if((*mtx
)->thread_id
!= GetCurrentThreadId()) {
442 call_func1(critical_section_lock
, &(*mtx
)->cs
);
443 (*mtx
)->thread_id
= GetCurrentThreadId();
444 }else if(!((*mtx
)->flags
& MTX_MULTI_LOCK
)) {
452 int __cdecl
_Mtx_unlock(_Mtx_t
*mtx
)
457 (*mtx
)->thread_id
= -1;
458 call_func1(critical_section_unlock
, &(*mtx
)->cs
);
462 int __cdecl
_Mtx_trylock(_Mtx_t
*mtx
)
464 if((*mtx
)->thread_id
!= GetCurrentThreadId()) {
465 if(!call_func1(critical_section_trylock
, &(*mtx
)->cs
))
467 (*mtx
)->thread_id
= GetCurrentThreadId();
468 }else if(!((*mtx
)->flags
& MTX_MULTI_LOCK
)) {
476 critical_section
* __cdecl
_Mtx_getconcrtcs(_Mtx_t
*mtx
)