atl80: Added AtlComModuleRegisterServer implementation (based on AtlModuleRegisterSer...
[wine] / dlls / msvcp100 / misc.c
1 /*
2  * Copyright 2010 Piotr Caban for CodeWeavers
3  *
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.
8  *
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.
13  *
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
17  */
18
19 #include "config.h"
20
21 #include <stdarg.h>
22 #include <limits.h>
23
24 #include "msvcp.h"
25
26 #include "windef.h"
27 #include "winbase.h"
28 #include "wine/debug.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(msvcp);
30
31 struct __Container_proxy;
32
33 typedef struct {
34     struct __Container_proxy *proxy;
35 } _Container_base12;
36
37 typedef struct __Iterator_base12 {
38     struct __Container_proxy *proxy;
39     struct __Iterator_base12 *next;
40 } _Iterator_base12;
41
42 typedef struct __Container_proxy {
43     const _Container_base12 *cont;
44     _Iterator_base12 *head;
45 } _Container_proxy;
46
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)
51 {
52     CRITICAL_SECTION *cs = MSVCRT_operator_new(sizeof(*cs));
53     if(!cs) {
54         ERR("Out of memory\n");
55         throw_exception(EXCEPTION_BAD_ALLOC, NULL);
56     }
57
58     InitializeCriticalSection(cs);
59     cs->DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": _Mutex critical section");
60     this->mutex = cs;
61     return this;
62 }
63
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)
68 {
69     ((CRITICAL_SECTION*)this->mutex)->DebugInfo->Spare[0] = 0;
70     DeleteCriticalSection(this->mutex);
71     MSVCRT_operator_delete(this->mutex);
72 }
73
74 /* ?_Lock@_Mutex@std@@QAEXXZ */
75 /* ?_Lock@_Mutex@std@@QEAAXXZ */
76 DEFINE_THISCALL_WRAPPER(mutex_lock, 4)
77 void __thiscall mutex_lock(mutex *this)
78 {
79     EnterCriticalSection(this->mutex);
80 }
81
82 /* ?_Unlock@_Mutex@std@@QAEXXZ */
83 /* ?_Unlock@_Mutex@std@@QEAAXXZ */
84 DEFINE_THISCALL_WRAPPER(mutex_unlock, 4)
85 void __thiscall mutex_unlock(mutex *this)
86 {
87     LeaveCriticalSection(this->mutex);
88 }
89
90 /* ?_Mutex_Lock@_Mutex@std@@CAXPAV12@@Z */
91 /* ?_Mutex_Lock@_Mutex@std@@CAXPEAV12@@Z */
92 void CDECL mutex_mutex_lock(mutex *m)
93 {
94     mutex_lock(m);
95 }
96
97 /* ?_Mutex_Unlock@_Mutex@std@@CAXPAV12@@Z */
98 /* ?_Mutex_Unlock@_Mutex@std@@CAXPEAV12@@Z */
99 void CDECL mutex_mutex_unlock(mutex *m)
100 {
101     mutex_unlock(m);
102 }
103
104 /* ?_Mutex_ctor@_Mutex@std@@CAXPAV12@@Z */
105 /* ?_Mutex_ctor@_Mutex@std@@CAXPEAV12@@Z */
106 void CDECL mutex_mutex_ctor(mutex *m)
107 {
108     mutex_ctor(m);
109 }
110
111 /* ?_Mutex_dtor@_Mutex@std@@CAXPAV12@@Z */
112 /* ?_Mutex_dtor@_Mutex@std@@CAXPEAV12@@Z */
113 void CDECL mutex_mutex_dtor(mutex *m)
114 {
115     mutex_dtor(m);
116 }
117
118 static CRITICAL_SECTION lockit_cs[_MAX_LOCK];
119
120 /* ?_Lockit_ctor@_Lockit@std@@SAXH@Z */
121 void __cdecl _Lockit_init(int locktype) {
122     InitializeCriticalSection(&lockit_cs[locktype]);
123     lockit_cs[locktype].DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": _Lockit critical section");
124 }
125
126 /* ?_Lockit_dtor@_Lockit@std@@SAXH@Z */
127 void __cdecl _Lockit_free(int locktype)
128 {
129     lockit_cs[locktype].DebugInfo->Spare[0] = 0;
130     DeleteCriticalSection(&lockit_cs[locktype]);
131 }
132
133 void init_lockit(void) {
134     int i;
135
136     for(i=0; i<_MAX_LOCK; i++)
137         _Lockit_init(i);
138 }
139
140 void free_lockit(void) {
141     int i;
142
143     for(i=0; i<_MAX_LOCK; i++)
144         _Lockit_free(i);
145 }
146
147 /* ?_Lockit_ctor@_Lockit@std@@CAXPAV12@H@Z */
148 /* ?_Lockit_ctor@_Lockit@std@@CAXPEAV12@H@Z */
149 void __cdecl _Lockit__Lockit_ctor_locktype(_Lockit *lockit, int locktype)
150 {
151     lockit->locktype = locktype;
152     EnterCriticalSection(&lockit_cs[locktype]);
153 }
154
155 /* ?_Lockit_ctor@_Lockit@std@@CAXPAV12@@Z */
156 /* ?_Lockit_ctor@_Lockit@std@@CAXPEAV12@@Z */
157 void __cdecl _Lockit__Lockit_ctor(_Lockit *lockit)
158 {
159     _Lockit__Lockit_ctor_locktype(lockit, 0);
160 }
161
162 /* ??0_Lockit@std@@QAE@H@Z */
163 /* ??0_Lockit@std@@QEAA@H@Z */
164 DEFINE_THISCALL_WRAPPER(_Lockit_ctor_locktype, 8)
165 _Lockit* __thiscall _Lockit_ctor_locktype(_Lockit *this, int locktype)
166 {
167     _Lockit__Lockit_ctor_locktype(this, locktype);
168     return this;
169 }
170
171 /* ??0_Lockit@std@@QAE@XZ */
172 /* ??0_Lockit@std@@QEAA@XZ */
173 DEFINE_THISCALL_WRAPPER(_Lockit_ctor, 4)
174 _Lockit* __thiscall _Lockit_ctor(_Lockit *this)
175 {
176     _Lockit__Lockit_ctor_locktype(this, 0);
177     return this;
178 }
179
180 /* ?_Lockit_dtor@_Lockit@std@@CAXPAV12@@Z */
181 /* ?_Lockit_dtor@_Lockit@std@@CAXPEAV12@@Z */
182 void __cdecl _Lockit__Lockit_dtor(_Lockit *lockit)
183 {
184     LeaveCriticalSection(&lockit_cs[lockit->locktype]);
185 }
186
187 /* ??1_Lockit@std@@QAE@XZ */
188 /* ??1_Lockit@std@@QEAA@XZ */
189 DEFINE_THISCALL_WRAPPER(_Lockit_dtor, 4)
190 void __thiscall _Lockit_dtor(_Lockit *this)
191 {
192     _Lockit__Lockit_dtor(this);
193 }
194
195 /* wctype */
196 unsigned short __cdecl wctype(const char *property)
197 {
198     static const struct {
199         const char *name;
200         unsigned short mask;
201     } properties[] = {
202         { "alnum", _DIGIT|_ALPHA },
203         { "alpha", _ALPHA },
204         { "cntrl", _CONTROL },
205         { "digit", _DIGIT },
206         { "graph", _DIGIT|_PUNCT|_ALPHA },
207         { "lower", _LOWER },
208         { "print", _DIGIT|_PUNCT|_BLANK|_ALPHA },
209         { "punct", _PUNCT },
210         { "space", _SPACE },
211         { "upper", _UPPER },
212         { "xdigit", _HEX }
213     };
214     int i;
215
216     for(i=0; i<sizeof(properties)/sizeof(properties[0]); i++)
217         if(!strcmp(property, properties[i].name))
218             return properties[i].mask;
219
220     return 0;
221 }
222
223 typedef void (__cdecl *MSVCP_new_handler_func)(void);
224 static MSVCP_new_handler_func MSVCP_new_handler;
225 static int __cdecl new_handler_wrapper(MSVCP_size_t unused)
226 {
227     MSVCP_new_handler();
228     return 1;
229 }
230
231 /* ?set_new_handler@std@@YAP6AXXZP6AXXZ@Z */
232 MSVCP_new_handler_func __cdecl set_new_handler(MSVCP_new_handler_func new_handler)
233 {
234     MSVCP_new_handler_func old_handler = MSVCP_new_handler;
235
236     TRACE("%p\n", new_handler);
237
238     MSVCP_new_handler = new_handler;
239     MSVCRT_set_new_handler(new_handler ? new_handler_wrapper : NULL);
240     return old_handler;
241 }
242
243 /* ?set_new_handler@std@@YAP6AXXZH@Z */
244 MSVCP_new_handler_func __cdecl set_new_handler_reset(int unused)
245 {
246     return set_new_handler(NULL);
247 }
248
249 /* _Container_base0 is used by apps compiled without iterator checking
250  * (i.e. with _ITERATOR_DEBUG_LEVEL=0 ).
251  * It provides empty versions of methods used by visual c++'s stl's
252  * iterator checking.
253  * msvcr100 has to provide them in case apps are compiled with /Od
254  * or the optimizer fails to inline those (empty) calls.
255  */
256
257 /* ?_Orphan_all@_Container_base0@std@@QAEXXZ */
258 /* ?_Orphan_all@_Container_base0@std@@QEAAXXZ */
259 DEFINE_THISCALL_WRAPPER(Container_base0_Orphan_all, 4)
260 void __thiscall Container_base0_Orphan_all(void *this)
261 {
262 }
263
264 /* ?_Swap_all@_Container_base0@std@@QAEXAAU12@@Z */
265 /* ?_Swap_all@_Container_base0@std@@QEAAXAEAU12@@Z */
266 DEFINE_THISCALL_WRAPPER(Container_base0_Swap_all, 8)
267 void __thiscall Container_base0_Swap_all(void *this, void *that)
268 {
269 }
270
271 /* ??4_Container_base0@std@@QAEAAU01@ABU01@@Z */
272 /* ??4_Container_base0@std@@QEAAAEAU01@AEBU01@@Z */
273 DEFINE_THISCALL_WRAPPER(Container_base0_op_assign, 8)
274 void* __thiscall Container_base0_op_assign(void *this, const void *that)
275 {
276     return this;
277 }
278
279 /* ??0_Container_base12@std@@QAE@ABU01@@Z */
280 /* ??0_Container_base12@std@@QEAA@AEBU01@@Z */
281 DEFINE_THISCALL_WRAPPER(_Container_base12_copy_ctor, 8)
282 _Container_base12* __thiscall _Container_base12_copy_ctor(
283         _Container_base12 *this, _Container_base12 *that)
284 {
285     this->proxy = NULL;
286     return this;
287 }
288
289 /* ??0_Container_base12@std@@QAE@XZ */
290 /* ??0_Container_base12@std@@QEAA@XZ */
291 DEFINE_THISCALL_WRAPPER(_Container_base12_ctor, 4)
292 _Container_base12* __thiscall _Container_base12_ctor(_Container_base12 *this)
293 {
294     this->proxy = NULL;
295     return this;
296 }
297
298 /* ??1_Container_base12@std@@QAE@XZ */
299 /* ??1_Container_base12@std@@QEAA@XZ */
300 DEFINE_THISCALL_WRAPPER(_Container_base12_dtor, 4)
301 void __thiscall _Container_base12_dtor(_Container_base12 *this)
302 {
303 }
304
305 /* ??4_Container_base12@std@@QAEAAU01@ABU01@@Z */
306 /* ??4_Container_base12@std@@QEAAAEAU01@AEBU01@@ */
307 DEFINE_THISCALL_WRAPPER(_Container_base12_op_assign, 8)
308 _Container_base12* __thiscall _Container_base12_op_assign(
309         _Container_base12 *this, const _Container_base12 *that)
310 {
311     return this;
312 }
313
314 /* ?_Getpfirst@_Container_base12@std@@QBEPAPAU_Iterator_base12@2@XZ */
315 /* ?_Getpfirst@_Container_base12@std@@QEBAPEAPEAU_Iterator_base12@2@XZ */
316 DEFINE_THISCALL_WRAPPER(_Container_base12__Getpfirst, 4)
317 _Iterator_base12** __thiscall _Container_base12__Getpfirst(_Container_base12 *this)
318 {
319     return this->proxy ? &this->proxy->head : NULL;
320 }
321
322 /* ?_Orphan_all@_Container_base12@std@@QAEXXZ */
323 /* ?_Orphan_all@_Container_base12@std@@QEAAXXZ */
324 DEFINE_THISCALL_WRAPPER(_Container_base12__Orphan_all, 4)
325 void __thiscall _Container_base12__Orphan_all(_Container_base12 *this)
326 {
327 }
328
329 /* ?_Swap_all@_Container_base12@std@@QAEXAAU12@@Z */
330 /* ?_Swap_all@_Container_base12@std@@QEAAXAEAU12@@Z */
331 DEFINE_THISCALL_WRAPPER(_Container_base12__Swap_all, 8)
332 void __thiscall _Container_base12__Swap_all(
333         _Container_base12 *this, _Container_base12 *that)
334 {
335     _Container_proxy *tmp;
336
337     tmp = this->proxy;
338     this->proxy = that->proxy;
339     that->proxy = tmp;
340
341     if(this->proxy)
342         this->proxy->cont = this;
343     if(that->proxy)
344         that->proxy->cont = that;
345 }