advapi32: Implement RegSetKeySecurity on top of NtSetSecurityObject.
[wine.git] / dlls / msvcp90 / misc.c
blobabd53d79c86d98522b0d38c8d5aefcb89e3ec50a
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 "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;
39 typedef struct {
40 struct __Container_proxy *proxy;
41 } _Container_base12;
43 typedef struct __Iterator_base12 {
44 struct __Container_proxy *proxy;
45 struct __Iterator_base12 *next;
46 } _Iterator_base12;
48 typedef struct __Container_proxy {
49 const _Container_base12 *cont;
50 _Iterator_base12 *head;
51 } _Container_proxy;
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));
59 if(!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");
66 this->mutex = cs;
67 return this;
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)
100 mutex_lock(m);
103 /* ?_Mutex_Unlock@_Mutex@std@@CAXPAV12@@Z */
104 /* ?_Mutex_Unlock@_Mutex@std@@CAXPEAV12@@Z */
105 void CDECL mutex_mutex_unlock(mutex *m)
107 mutex_unlock(m);
110 /* ?_Mutex_ctor@_Mutex@std@@CAXPAV12@@Z */
111 /* ?_Mutex_ctor@_Mutex@std@@CAXPEAV12@@Z */
112 void CDECL mutex_mutex_ctor(mutex *m)
114 mutex_ctor(m);
117 /* ?_Mutex_dtor@_Mutex@std@@CAXPAV12@@Z */
118 /* ?_Mutex_dtor@_Mutex@std@@CAXPEAV12@@Z */
119 void CDECL mutex_mutex_dtor(mutex *m)
121 mutex_dtor(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) {
140 int i;
142 for(i=0; i<_MAX_LOCK; i++)
143 _Lockit_init(i);
146 void free_lockit(void) {
147 int i;
149 for(i=0; i<_MAX_LOCK; i++)
150 _Lockit_free(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);
174 return this;
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);
183 return this;
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);
201 /* wctype */
202 unsigned short __cdecl wctype(const char *property)
204 static const struct {
205 const char *name;
206 unsigned short mask;
207 } properties[] = {
208 { "alnum", _DIGIT|_ALPHA },
209 { "alpha", _ALPHA },
210 { "cntrl", _CONTROL },
211 { "digit", _DIGIT },
212 { "graph", _DIGIT|_PUNCT|_ALPHA },
213 { "lower", _LOWER },
214 { "print", _DIGIT|_PUNCT|_BLANK|_ALPHA },
215 { "punct", _PUNCT },
216 { "space", _SPACE },
217 { "upper", _UPPER },
218 { "xdigit", _HEX }
220 unsigned int i;
222 for(i=0; i<sizeof(properties)/sizeof(properties[0]); i++)
223 if(!strcmp(property, properties[i].name))
224 return properties[i].mask;
226 return 0;
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)
233 MSVCP_new_handler();
234 return 1;
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);
246 return old_handler;
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
258 * iterator checking.
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)
282 return this;
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)
291 this->proxy = NULL;
292 return this;
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)
300 this->proxy = NULL;
301 return 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)
317 return this;
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;
343 tmp = this->proxy;
344 this->proxy = that->proxy;
345 that->proxy = tmp;
347 if(this->proxy)
348 this->proxy->cont = this;
349 if(that->proxy)
350 that->proxy->cont = that;
353 /* _Xtime_get_ticks */
354 LONGLONG __cdecl _Xtime_get_ticks(void)
356 FILETIME ft;
358 TRACE("\n");
360 GetSystemTimeAsFileTime(&ft);
361 return ((LONGLONG)ft.dwHighDateTime<<32) + ft.dwLowDateTime - TICKS_1601_TO_1970;
364 #if _MSVCP_VER >= 90
365 unsigned int __cdecl _Random_device(void)
367 unsigned int ret;
369 TRACE("\n");
371 /* TODO: throw correct exception in case of failure */
372 if(rand_s(&ret))
373 throw_exception(EXCEPTION, "random number generator failed\n");
374 return ret;
376 #endif
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, \
386 "popl %eax\n\t" \
387 "pushl %ecx\n\t" \
388 "pushl %eax\n\t" \
389 "jmp " __ASM_NAME(#func) __ASM_STDCALL(args) )
391 extern void *call_thiscall_func;
392 __ASM_GLOBAL_FUNC(call_thiscall_func,
393 "popl %eax\n\t"
394 "popl %edx\n\t"
395 "popl %ecx\n\t"
396 "pushl %eax\n\t"
397 "jmp *%edx\n\t")
399 #define call_func1(func,this) ((void* (WINAPI*)(void*,void*))&call_thiscall_func)(func,this)
401 #else /* __i386__ */
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
411 #define MTX_LOCKED 3
412 typedef struct
414 DWORD flags;
415 critical_section cs;
416 DWORD thread_id;
417 DWORD count;
418 } *_Mtx_t;
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;
429 (*mtx)->count = 0;
430 return 0;
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)) {
445 return MTX_LOCKED;
448 (*mtx)->count++;
449 return 0;
452 int __cdecl _Mtx_unlock(_Mtx_t *mtx)
454 if(--(*mtx)->count)
455 return 0;
457 (*mtx)->thread_id = -1;
458 call_func1(critical_section_unlock, &(*mtx)->cs);
459 return 0;
462 int __cdecl _Mtx_trylock(_Mtx_t *mtx)
464 if((*mtx)->thread_id != GetCurrentThreadId()) {
465 if(!call_func1(critical_section_trylock, &(*mtx)->cs))
466 return MTX_LOCKED;
467 (*mtx)->thread_id = GetCurrentThreadId();
468 }else if(!((*mtx)->flags & MTX_MULTI_LOCK)) {
469 return MTX_LOCKED;
472 (*mtx)->count++;
473 return 0;
476 critical_section* __cdecl _Mtx_getconcrtcs(_Mtx_t *mtx)
478 return &(*mtx)->cs;
480 #endif