Converted to the new debug interface, using script written by Patrik
[wine] / dlls / ntdll / rtl.c
1 /*
2  * NT basis DLL
3  * 
4  * This file contains the Rtl* API functions. These should be implementable.
5  * 
6  * Copyright 1996-1998 Marcus Meissner
7  *                1999 Alex Korobka
8  */
9
10 #include <stdlib.h>
11 #include <string.h>
12 #include "heap.h"
13 #include "debugtools.h"
14 #include "winuser.h"
15 #include "winerror.h"
16 #include "stackframe.h"
17
18 #include "ntddk.h"
19 #include "winreg.h"
20
21 DEFAULT_DEBUG_CHANNEL(ntdll)
22
23
24 /*
25  *      resource functions
26  */
27
28 /***********************************************************************
29  *           RtlInitializeResource      (NTDLL.409)
30  *
31  * xxxResource() functions implement multiple-reader-single-writer lock.
32  * The code is based on information published in WDJ January 1999 issue.
33  */
34 void WINAPI RtlInitializeResource(LPRTL_RWLOCK rwl)
35 {
36     if( rwl )
37     {
38         rwl->iNumberActive = 0;
39         rwl->uExclusiveWaiters = 0;
40         rwl->uSharedWaiters = 0;
41         rwl->hOwningThreadId = 0;
42         rwl->dwTimeoutBoost = 0; /* no info on this one, default value is 0 */
43         InitializeCriticalSection( &rwl->rtlCS );
44         rwl->hExclusiveReleaseSemaphore = CreateSemaphoreA( NULL, 0, 65535, NULL );
45         rwl->hSharedReleaseSemaphore = CreateSemaphoreA( NULL, 0, 65535, NULL );
46     }
47 }
48
49
50 /***********************************************************************
51  *           RtlDeleteResource          (NTDLL.330)
52  */
53 void WINAPI RtlDeleteResource(LPRTL_RWLOCK rwl)
54 {
55     if( rwl )
56     {
57         EnterCriticalSection( &rwl->rtlCS );
58         if( rwl->iNumberActive || rwl->uExclusiveWaiters || rwl->uSharedWaiters )
59             MESSAGE("Deleting active MRSW lock (%p), expect failure\n", rwl );
60         rwl->hOwningThreadId = 0;
61         rwl->uExclusiveWaiters = rwl->uSharedWaiters = 0;
62         rwl->iNumberActive = 0;
63         CloseHandle( rwl->hExclusiveReleaseSemaphore );
64         CloseHandle( rwl->hSharedReleaseSemaphore );
65         LeaveCriticalSection( &rwl->rtlCS );
66         DeleteCriticalSection( &rwl->rtlCS );
67     }
68 }
69
70
71 /***********************************************************************
72  *          RtlAcquireResourceExclusive (NTDLL.256)
73  */
74 BYTE WINAPI RtlAcquireResourceExclusive(LPRTL_RWLOCK rwl, BYTE fWait)
75 {
76     BYTE retVal = 0;
77     if( !rwl ) return 0;
78
79 start:
80     EnterCriticalSection( &rwl->rtlCS );
81     if( rwl->iNumberActive == 0 ) /* lock is free */
82     {
83         rwl->iNumberActive = -1;
84         retVal = 1;
85     }
86     else if( rwl->iNumberActive < 0 ) /* exclusive lock in progress */
87     {
88          if( rwl->hOwningThreadId == GetCurrentThreadId() )
89          {
90              retVal = 1;
91              rwl->iNumberActive--;
92              goto done;
93          }
94 wait:
95          if( fWait )
96          {
97              rwl->uExclusiveWaiters++;
98
99              LeaveCriticalSection( &rwl->rtlCS );
100              if( WaitForSingleObject( rwl->hExclusiveReleaseSemaphore, INFINITE ) == WAIT_FAILED )
101                  goto done;
102              goto start; /* restart the acquisition to avoid deadlocks */
103          }
104     }
105     else  /* one or more shared locks are in progress */
106          if( fWait )
107              goto wait;
108          
109     if( retVal == 1 )
110         rwl->hOwningThreadId = GetCurrentThreadId();
111 done:
112     LeaveCriticalSection( &rwl->rtlCS );
113     return retVal;
114 }
115
116 /***********************************************************************
117  *          RtlAcquireResourceShared    (NTDLL.257)
118  */
119 BYTE WINAPI RtlAcquireResourceShared(LPRTL_RWLOCK rwl, BYTE fWait)
120 {
121     DWORD dwWait = WAIT_FAILED;
122     BYTE retVal = 0;
123     if( !rwl ) return 0;
124
125 start:
126     EnterCriticalSection( &rwl->rtlCS );
127     if( rwl->iNumberActive < 0 )
128     {
129         if( rwl->hOwningThreadId == GetCurrentThreadId() )
130         {
131             rwl->iNumberActive--;
132             retVal = 1;
133             goto done;
134         }
135         
136         if( fWait )
137         {
138             rwl->uSharedWaiters++;
139             LeaveCriticalSection( &rwl->rtlCS );
140             if( (dwWait = WaitForSingleObject( rwl->hSharedReleaseSemaphore, INFINITE )) == WAIT_FAILED )
141                 goto done;
142             goto start;
143         }
144     }
145     else 
146     {
147         if( dwWait != WAIT_OBJECT_0 ) /* otherwise RtlReleaseResource() has already done it */
148             rwl->iNumberActive++;
149         retVal = 1;
150     }
151 done:
152     LeaveCriticalSection( &rwl->rtlCS );
153     return retVal;
154 }
155
156
157 /***********************************************************************
158  *           RtlReleaseResource         (NTDLL.471)
159  */
160 void WINAPI RtlReleaseResource(LPRTL_RWLOCK rwl)
161 {
162     EnterCriticalSection( &rwl->rtlCS );
163
164     if( rwl->iNumberActive > 0 ) /* have one or more readers */
165     {
166         if( --rwl->iNumberActive == 0 )
167         {
168             if( rwl->uExclusiveWaiters )
169             {
170 wake_exclusive:
171                 rwl->uExclusiveWaiters--;
172                 ReleaseSemaphore( rwl->hExclusiveReleaseSemaphore, 1, NULL );
173             }
174         }
175     }
176     else 
177     if( rwl->iNumberActive < 0 ) /* have a writer, possibly recursive */
178     {
179         if( ++rwl->iNumberActive == 0 )
180         {
181             rwl->hOwningThreadId = 0;
182             if( rwl->uExclusiveWaiters )
183                 goto wake_exclusive;
184             else
185                 if( rwl->uSharedWaiters )
186                 {
187                     UINT n = rwl->uSharedWaiters;
188                     rwl->iNumberActive = rwl->uSharedWaiters; /* prevent new writers from joining until
189                                                                * all queued readers have done their thing */
190                     rwl->uSharedWaiters = 0;
191                     ReleaseSemaphore( rwl->hSharedReleaseSemaphore, n, NULL );
192                 }
193         }
194     }
195     LeaveCriticalSection( &rwl->rtlCS );
196 }
197
198
199 /***********************************************************************
200  *           RtlDumpResource            (NTDLL.340)
201  */
202 void WINAPI RtlDumpResource(LPRTL_RWLOCK rwl)
203 {
204     if( rwl )
205     {
206         MESSAGE("RtlDumpResource(%p):\n\tactive count = %i\n\twaiting readers = %i\n\twaiting writers = %i\n",  
207                 rwl, rwl->iNumberActive, rwl->uSharedWaiters, rwl->uExclusiveWaiters );
208         if( rwl->iNumberActive )
209             MESSAGE("\towner thread = %08x\n", rwl->hOwningThreadId );
210     }
211 }
212
213 /*
214  *      heap functions
215  */
216
217 /******************************************************************************
218  *  RtlCreateHeap               [NTDLL] 
219  */
220 HANDLE WINAPI RtlCreateHeap(
221         ULONG Flags,
222         PVOID BaseAddress,
223         ULONG SizeToReserve,
224         ULONG SizeToCommit,
225         PVOID Unknown,
226         PRTL_HEAP_DEFINITION Definition)
227 {
228         FIXME("(0x%08lx, %p, 0x%08lx, 0x%08lx, %p, %p) semi-stub\n",
229         Flags, BaseAddress, SizeToReserve, SizeToCommit, Unknown, Definition);
230         
231         return HeapCreate ( Flags, SizeToCommit, SizeToReserve);
232
233 }       
234 /******************************************************************************
235  *  RtlAllocateHeap             [NTDLL] 
236  */
237 PVOID WINAPI RtlAllocateHeap(
238         HANDLE Heap,
239         ULONG Flags,
240         ULONG Size)
241 {
242         FIXME("(0x%08x, 0x%08lx, 0x%08lx) semi stub\n",
243         Heap, Flags, Size);
244         return HeapAlloc(Heap, Flags, Size);
245 }
246
247 /******************************************************************************
248  *  RtlFreeHeap         [NTDLL] 
249  */
250 BOOLEAN WINAPI RtlFreeHeap(
251         HANDLE Heap,
252         ULONG Flags,
253         PVOID Address)
254 {
255         FIXME("(0x%08x, 0x%08lx, %p) semi stub\n",
256         Heap, Flags, Address);
257         return HeapFree(Heap, Flags, Address);
258 }
259         
260 /******************************************************************************
261  *  RtlDestroyHeap              [NTDLL] 
262  *
263  * FIXME: prototype guessed
264  */
265 BOOLEAN WINAPI RtlDestroyHeap(
266         HANDLE Heap)
267 {
268         FIXME("(0x%08x) semi stub\n", Heap);
269         return HeapDestroy(Heap);
270 }
271         
272 /*
273  *      misc functions
274  */
275
276 /******************************************************************************
277  *      DbgPrint        [NTDLL] 
278  */
279 void __cdecl DbgPrint(LPCSTR fmt,LPVOID args) {
280         char buf[512];
281
282         wvsprintfA(buf,fmt,&args);
283         MESSAGE("DbgPrint says: %s",buf);
284         /* hmm, raise exception? */
285 }
286
287 /******************************************************************************
288  *  RtlAcquirePebLock           [NTDLL] 
289  */
290 VOID WINAPI RtlAcquirePebLock(void) {
291         FIXME("()\n");
292         /* enter critical section ? */
293 }
294
295 /******************************************************************************
296  *  RtlReleasePebLock           [NTDLL] 
297  */
298 VOID WINAPI RtlReleasePebLock(void) {
299         FIXME("()\n");
300         /* leave critical section ? */
301 }
302
303 /******************************************************************************
304  *  RtlIntegerToChar    [NTDLL] 
305  */
306 DWORD WINAPI RtlIntegerToChar(DWORD x1,DWORD x2,DWORD x3,DWORD x4) {
307         FIXME("(0x%08lx,0x%08lx,0x%08lx,0x%08lx),stub!\n",x1,x2,x3,x4);
308         return 0;
309 }
310 /******************************************************************************
311  *  RtlSetEnvironmentVariable           [NTDLL] 
312  */
313 DWORD WINAPI RtlSetEnvironmentVariable(DWORD x1,PUNICODE_STRING key,PUNICODE_STRING val) {
314         FIXME("(0x%08lx,%s,%s),stub!\n",x1,debugstr_w(key->Buffer),debugstr_w(val->Buffer));
315         return 0;
316 }
317
318 /******************************************************************************
319  *  RtlNewSecurityObject                [NTDLL] 
320  */
321 DWORD WINAPI RtlNewSecurityObject(DWORD x1,DWORD x2,DWORD x3,DWORD x4,DWORD x5,DWORD x6) {
322         FIXME("(0x%08lx,0x%08lx,0x%08lx,0x%08lx,0x%08lx,0x%08lx),stub!\n",x1,x2,x3,x4,x5,x6);
323         return 0;
324 }
325
326 /******************************************************************************
327  *  RtlDeleteSecurityObject             [NTDLL] 
328  */
329 DWORD WINAPI RtlDeleteSecurityObject(DWORD x1) {
330         FIXME("(0x%08lx),stub!\n",x1);
331         return 0;
332 }
333
334 /**************************************************************************
335  *                 RtlNormalizeProcessParams            [NTDLL.441]
336  */
337 LPVOID WINAPI RtlNormalizeProcessParams(LPVOID x)
338 {
339     FIXME("(%p), stub\n",x);
340     return x;
341 }
342
343 /**************************************************************************
344  *                 RtlNtStatusToDosError                        [NTDLL.442]
345  */
346 DWORD WINAPI RtlNtStatusToDosError(DWORD error)
347 {
348         FIXME("(%lx): map STATUS_ to ERROR_\n",error);
349         switch (error)
350         { case STATUS_SUCCESS:                  return ERROR_SUCCESS;
351           case STATUS_INVALID_PARAMETER:        return ERROR_BAD_ARGUMENTS;
352           case STATUS_BUFFER_TOO_SMALL:         return ERROR_INSUFFICIENT_BUFFER;
353 /*        case STATUS_INVALID_SECURITY_DESCR:   return ERROR_INVALID_SECURITY_DESCR;*/
354           case STATUS_NO_MEMORY:                return ERROR_NOT_ENOUGH_MEMORY;
355 /*        case STATUS_UNKNOWN_REVISION:
356           case STATUS_BUFFER_OVERFLOW:*/
357         }
358         FIXME("unknown status (%lx)\n",error);
359         return ERROR_SUCCESS;
360 }
361
362 /**************************************************************************
363  *                 RtlGetNtProductType                  [NTDLL.390]
364  */
365 BOOLEAN WINAPI RtlGetNtProductType(LPDWORD type)
366 {
367     FIXME("(%p): stub\n", type);
368     *type=3; /* dunno. 1 for client, 3 for server? */
369     return 1;
370 }
371
372 /**************************************************************************
373  *                 NTDLL_chkstk                         [NTDLL.862]
374  *                 NTDLL_alloca_probe                           [NTDLL.861]
375  * Glorified "enter xxxx".
376  */
377 void WINAPI REGS_FUNC(NTDLL_chkstk)( CONTEXT *context )
378 {
379     ESP_reg(context) -= EAX_reg(context);
380 }
381 void WINAPI REGS_FUNC(NTDLL_alloca_probe)( CONTEXT *context )
382 {
383     ESP_reg(context) -= EAX_reg(context);
384 }
385
386 /******************************************************************************
387  * RtlExtendedLargeIntegerDivide [NTDLL.359]
388  */
389 INT WINAPI RtlExtendedLargeIntegerDivide(
390         LARGE_INTEGER dividend,
391         DWORD divisor,
392         LPDWORD rest
393 ) {
394 #if SIZEOF_LONG_LONG==8
395         long long x1 = *(long long*)&dividend;
396
397         if (*rest)
398                 *rest = x1 % divisor;
399         return x1/divisor;
400 #else
401         FIXME("((%d<<32)+%d,%d,%p), implement this using normal integer arithmetic!\n",dividend.HighPart,dividend.LowPart,divisor,rest);
402         return 0;
403 #endif
404 }
405
406 /******************************************************************************
407  * RtlExtendedLargeIntegerMultiply [NTDLL.359]
408  * Note: This even works, since gcc returns 64bit values in eax/edx just like
409  * the caller expects. However... The relay code won't grok this I think.
410  */
411 long long WINAPI RtlExtendedIntegerMultiply(
412         LARGE_INTEGER factor1,
413         INT factor2) 
414 {
415 #if SIZEOF_LONG_LONG==8
416         return (*(long long*)&factor1) * factor2;
417 #else
418         FIXME("((%d<<32)+%d,%ld), implement this using normal integer arithmetic!\n",factor1.HighPart,factor1.LowPart,factor2);
419         return 0;
420 #endif
421 }
422
423 /******************************************************************************
424  *  RtlFormatCurrentUserKeyPath         [NTDLL.371] 
425  */
426 DWORD WINAPI RtlFormatCurrentUserKeyPath(DWORD x)
427 {
428     FIXME("(0x%08lx): stub\n",x);
429     return 1;
430 }
431
432 /******************************************************************************
433  *  RtlOpenCurrentUser          [NTDLL] 
434  */
435 DWORD WINAPI RtlOpenCurrentUser(DWORD x1, DWORD *x2)
436 {
437 /* Note: this is not the correct solution, 
438  * But this works pretty good on wine and NT4.0 binaries
439  */
440         if  ( x1 == 0x2000000 )  {
441                 *x2 = HKEY_CURRENT_USER; 
442                 return TRUE;
443         }
444                 
445         return FALSE;
446 }
447 /**************************************************************************
448  *                 RtlDosPathNameToNtPathName_U         [NTDLL.338]
449  *
450  * FIXME: convert to UNC or whatever is expected here
451  */
452 BOOLEAN  WINAPI RtlDosPathNameToNtPathName_U(
453         LPWSTR from,PUNICODE_STRING us,DWORD x2,DWORD x3)
454 {
455         LPSTR   fromA = HEAP_strdupWtoA(GetProcessHeap(),0,from);
456
457         FIXME("(%s,%p,%08lx,%08lx)\n",fromA,us,x2,x3);
458         if (us)
459                 RtlInitUnicodeString(us,HEAP_strdupW(GetProcessHeap(),0,from));
460         return TRUE;
461 }
462
463 /******************************************************************************
464  *  RtlCreateEnvironment                [NTDLL] 
465  */
466 DWORD WINAPI RtlCreateEnvironment(DWORD x1,DWORD x2) {
467         FIXME("(0x%08lx,0x%08lx),stub!\n",x1,x2);
468         return 0;
469 }
470
471
472 /******************************************************************************
473  *  RtlDestroyEnvironment               [NTDLL] 
474  */
475 DWORD WINAPI RtlDestroyEnvironment(DWORD x) {
476         FIXME("(0x%08lx),stub!\n",x);
477         return 0;
478 }
479
480 /******************************************************************************
481  *  RtlQueryEnvironmentVariable_U               [NTDLL] 
482  */
483 DWORD WINAPI RtlQueryEnvironmentVariable_U(DWORD x1,PUNICODE_STRING key,PUNICODE_STRING val) {
484         FIXME("(0x%08lx,%s,%p),stub!\n",x1,debugstr_w(key->Buffer),val);
485         return 0;
486 }