Fixed build by including the correct header this time!
[reactos.git] / reactos / lib / drivers / sound / mmebuddy / utility.c
1 /*
2 * PROJECT: ReactOS Sound System "MME Buddy" Library
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: lib/sound/mmebuddy/utility.c
5 *
6 * PURPOSE: Provides utility functions used by the library.
7 *
8 * PROGRAMMERS: Andrew Greenwood (silverblade@reactos.org)
9 */
10
11 #include <windows.h>
12 #include <mmsystem.h>
13 #include <mmddk.h>
14
15 #include <mmebuddy.h>
16
17 static HANDLE ProcessHeapHandle = NULL;
18 static UINT CurrentAllocations = 0;
19
20 /*
21 Allocates memory, zeroes it, and increases the allocation count.
22 */
23 PVOID
24 AllocateMemory(
25 IN UINT Size)
26 {
27 PVOID Pointer = NULL;
28
29 if ( ! ProcessHeapHandle )
30 ProcessHeapHandle = GetProcessHeap();
31
32 Pointer = HeapAlloc(ProcessHeapHandle, HEAP_ZERO_MEMORY, Size);
33
34 if ( ! Pointer )
35 return NULL;
36
37 ++ CurrentAllocations;
38
39 return Pointer;
40 }
41
42 /*
43 Frees memory and reduces the allocation count.
44 */
45 VOID
46 FreeMemory(
47 IN PVOID Pointer)
48 {
49 SND_ASSERT( ProcessHeapHandle );
50 SND_ASSERT( Pointer );
51
52 HeapFree(ProcessHeapHandle, 0, Pointer);
53
54 -- CurrentAllocations;
55 }
56
57 /*
58 Returns the current number of memory allocations outstanding. Useful for
59 detecting/tracing memory leaks.
60 */
61 UINT
62 GetMemoryAllocationCount()
63 {
64 return CurrentAllocations;
65 }
66
67
68 /*
69 Count the number of digits in a UINT
70 */
71 UINT
72 GetDigitCount(
73 IN UINT Number)
74 {
75 UINT Value = Number;
76 ULONG Digits = 1;
77
78 while ( Value > 9 )
79 {
80 Value /= 10;
81 ++ Digits;
82 }
83
84 return Digits;
85 }
86
87 /*
88 Translate a Win32 error code into an MMRESULT code.
89 */
90 MMRESULT
91 Win32ErrorToMmResult(
92 IN UINT ErrorCode)
93 {
94 switch ( ErrorCode )
95 {
96 case NO_ERROR :
97 case ERROR_IO_PENDING :
98 return MMSYSERR_NOERROR;
99
100 case ERROR_BUSY :
101 return MMSYSERR_ALLOCATED;
102
103 case ERROR_NOT_SUPPORTED :
104 case ERROR_INVALID_FUNCTION :
105 return MMSYSERR_NOTSUPPORTED;
106
107 case ERROR_NOT_ENOUGH_MEMORY :
108 return MMSYSERR_NOMEM;
109
110 case ERROR_ACCESS_DENIED :
111 return MMSYSERR_BADDEVICEID;
112
113 case ERROR_INSUFFICIENT_BUFFER :
114 return MMSYSERR_INVALPARAM;
115
116 default :
117 return MMSYSERR_ERROR;
118 }
119 }
120
121 /*
122 If a function invokes another function, this aids in translating the
123 result code so that it is applicable in the context of the original caller.
124 For example, specifying that an invalid parameter was passed probably does
125 not make much sense if the parameter wasn't passed by the original caller!
126
127 This could potentially highlight internal logic problems.
128
129 However, things like MMSYSERR_NOMEM make sense to return to the caller.
130 */
131 MMRESULT
132 TranslateInternalMmResult(
133 IN MMRESULT Result)
134 {
135 switch ( Result )
136 {
137 case MMSYSERR_INVALPARAM :
138 case MMSYSERR_INVALFLAG :
139 {
140 return MMSYSERR_ERROR;
141 }
142 }
143
144 return Result;
145 }