ReactOS  0.4.13-dev-66-gc714b7f
utility.c
Go to the documentation of this file.
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 
15 
16 /*
17  Allocates memory, zeroes it, and increases the allocation count.
18 */
19 PVOID
21  IN UINT Size)
22 {
23  PVOID Pointer = NULL;
24 
25  if ( ! ProcessHeapHandle )
27 
29 
30  if ( ! Pointer )
31  return NULL;
32 
34 
35  return Pointer;
36 }
37 
38 /*
39  Frees memory and reduces the allocation count.
40 */
41 VOID
43  IN PVOID Pointer)
44 {
46  SND_ASSERT( Pointer );
47 
48  HeapFree(ProcessHeapHandle, 0, Pointer);
49 
51 }
52 
53 /*
54  Returns the current number of memory allocations outstanding. Useful for
55  detecting/tracing memory leaks.
56 */
57 UINT
59 {
60  return CurrentAllocations;
61 }
62 
63 
64 /*
65  Count the number of digits in a UINT
66 */
67 UINT
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 */
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 :
101  return MMSYSERR_NOTSUPPORTED;
102 
104  return MMSYSERR_NOMEM;
105 
106  case ERROR_ACCESS_DENIED :
107  return MMSYSERR_BADDEVICEID;
108 
110  return MMSYSERR_INVALPARAM;
111 
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
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 }
_In_opt_ ULONG _Out_ PULONG Value
Definition: rtlfuncs.h:2327
#define ERROR_INVALID_PARAMETER
Definition: compat.h:91
#define IN
Definition: typedefs.h:38
_In_ NDIS_ERROR_CODE ErrorCode
Definition: ndis.h:4418
#define ERROR_INVALID_FUNCTION
Definition: dderror.h:6
static HANDLE ProcessHeapHandle
Definition: utility.c:13
#define ERROR_NOT_ENOUGH_MEMORY
Definition: dderror.h:7
UINT MMRESULT
Definition: mmsystem.h:962
#define ERROR_IO_PENDING
Definition: dderror.h:15
#define NO_ERROR
Definition: dderror.h:5
#define ERROR_BUSY
Definition: dderror.h:12
UINT GetMemoryAllocationCount()
Definition: utility.c:58
#define MMSYSERR_NOMEM
Definition: mmsystem.h:103
#define ERROR_ACCESS_DENIED
Definition: compat.h:87
smooth NULL
Definition: ftsmooth.c:416
#define SND_ASSERT(condition)
_At_(*)(_In_ PWSK_CLIENT Client, _In_opt_ PUNICODE_STRING NodeName, _In_opt_ PUNICODE_STRING ServiceName, _In_opt_ ULONG NameSpace, _In_opt_ GUID *Provider, _In_opt_ PADDRINFOEXW Hints, _Outptr_ PADDRINFOEXW *Result, _In_opt_ PEPROCESS OwningProcess, _In_opt_ PETHREAD OwningThread, _Inout_ PIRP Irp Result)(Mem)) NTSTATUS(WSKAPI *PFN_WSK_GET_ADDRESS_INFO
Definition: wsk.h:426
MMRESULT TranslateInternalMmResult(IN MMRESULT Result)
Definition: utility.c:132
VOID FreeMemory(IN PVOID Pointer)
Definition: utility.c:42
#define MMSYSERR_NOERROR
Definition: mmsystem.h:96
#define GetProcessHeap()
Definition: compat.h:395
PVOID WINAPI HeapAlloc(HANDLE, DWORD, SIZE_T)
#define MMSYSERR_ERROR
Definition: mmsystem.h:97
#define MMSYSERR_ALLOCATED
Definition: mmsystem.h:100
UINT GetDigitCount(IN UINT Number)
Definition: utility.c:68
#define MMSYSERR_INVALFLAG
Definition: mmsystem.h:106
#define MMSYSERR_NOTSUPPORTED
Definition: mmsystem.h:104
MMRESULT Win32ErrorToMmResult(IN UINT ErrorCode)
Definition: utility.c:87
#define MMSYSERR_BADDEVICEID
Definition: mmsystem.h:98
IN PVOID IN PVOID IN USHORT IN USHORT Size
Definition: pci.h:359
_In_opt_ PENTER_STATE_SYSTEM_HANDLER _In_opt_ PVOID _In_ LONG _In_opt_ LONG volatile * Number
Definition: ntpoapi.h:204
#define MMSYSERR_INVALPARAM
Definition: mmsystem.h:107
unsigned int UINT
Definition: ndis.h:50
#define HEAP_ZERO_MEMORY
Definition: compat.h:123
static UINT CurrentAllocations
Definition: utility.c:14
#define ERROR_NOT_SUPPORTED
Definition: compat.h:90
unsigned int ULONG
Definition: retypes.h:1
PVOID AllocateMemory(IN UINT Size)
Definition: utility.c:20
#define HeapFree(x, y, z)
Definition: compat.h:394
#define ERROR_INSUFFICIENT_BUFFER
Definition: dderror.h:10