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 "precomp.h" 12 13 static HANDLE ProcessHeapHandle = NULL; 14 static UINT CurrentAllocations = 0; 15 16 /* 17 Allocates memory, zeroes it, and increases the allocation count. 18 */ 19 PVOID 20 AllocateMemory( 21 IN UINT Size) 22 { 23 PVOID Pointer = NULL; 24 25 if ( ! ProcessHeapHandle ) 26 ProcessHeapHandle = GetProcessHeap(); 27 28 Pointer = HeapAlloc(ProcessHeapHandle, HEAP_ZERO_MEMORY, Size); 29 30 if ( ! Pointer ) 31 return NULL; 32 33 ++ CurrentAllocations; 34 35 return Pointer; 36 } 37 38 /* 39 Frees memory and reduces the allocation count. 40 */ 41 VOID 42 FreeMemory( 43 IN PVOID Pointer) 44 { 45 SND_ASSERT( ProcessHeapHandle ); 46 SND_ASSERT( Pointer ); 47 48 HeapFree(ProcessHeapHandle, 0, Pointer); 49 50 -- CurrentAllocations; 51 } 52 53 /* 54 Returns the current number of memory allocations outstanding. Useful for 55 detecting/tracing memory leaks. 56 */ 57 UINT 58 GetMemoryAllocationCount() 59 { 60 return CurrentAllocations; 61 } 62 63 64 /* 65 Count the number of digits in a UINT 66 */ 67 UINT 68 GetDigitCount( 69 IN UINT Number) 70 { 71 UINT Value = Number; 72 ULONG Digits = 1; 73 74 while ( Value > 9 ) 75 { 76 Value /= 10; 77 ++ Digits; 78 } 79 80 return Digits; 81 } 82 83 /* 84 Translate a Win32 error code into an MMRESULT code. 85 */ 86 MMRESULT 87 Win32ErrorToMmResult( 88 IN UINT ErrorCode) 89 { 90 switch ( ErrorCode ) 91 { 92 case NO_ERROR : 93 case ERROR_IO_PENDING : 94 return MMSYSERR_NOERROR; 95 96 case ERROR_BUSY : 97 return MMSYSERR_ALLOCATED; 98 99 case ERROR_NOT_SUPPORTED : 100 case ERROR_INVALID_FUNCTION : 101 return MMSYSERR_NOTSUPPORTED; 102 103 case ERROR_NOT_ENOUGH_MEMORY : 104 return MMSYSERR_NOMEM; 105 106 case ERROR_ACCESS_DENIED : 107 return MMSYSERR_BADDEVICEID; 108 109 case ERROR_INSUFFICIENT_BUFFER : 110 return MMSYSERR_INVALPARAM; 111 112 case ERROR_INVALID_PARAMETER : 113 return MMSYSERR_INVALPARAM; 114 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 } 146