ReactOS 0.4.15-dev-7961-gdcf9eb0
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*/
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*/
41VOID
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*/
57UINT
59{
60 return CurrentAllocations;
61}
62
63
64/*
65 Count the number of digits in a UINT
66*/
67UINT
70{
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
102
104 return MMSYSERR_NOMEM;
105
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*/
134{
135 switch ( Result )
136 {
138 case MMSYSERR_INVALFLAG :
139 {
140 return MMSYSERR_ERROR;
141 }
142 }
143
144 return Result;
145}
#define ERROR_NOT_ENOUGH_MEMORY
Definition: dderror.h:7
#define NO_ERROR
Definition: dderror.h:5
#define ERROR_INSUFFICIENT_BUFFER
Definition: dderror.h:10
#define ERROR_BUSY
Definition: dderror.h:12
#define ERROR_IO_PENDING
Definition: dderror.h:15
#define ERROR_INVALID_FUNCTION
Definition: dderror.h:6
#define NULL
Definition: types.h:112
#define GetProcessHeap()
Definition: compat.h:736
#define ERROR_INVALID_PARAMETER
Definition: compat.h:101
#define HeapAlloc
Definition: compat.h:733
#define ERROR_NOT_SUPPORTED
Definition: compat.h:100
#define HeapFree(x, y, z)
Definition: compat.h:735
#define ERROR_ACCESS_DENIED
Definition: compat.h:97
#define HEAP_ZERO_MEMORY
Definition: compat.h:134
#define SND_ASSERT(condition)
#define MMSYSERR_NOMEM
Definition: mmsystem.h:103
UINT MMRESULT
Definition: mmsystem.h:962
#define MMSYSERR_NOTSUPPORTED
Definition: mmsystem.h:104
#define MMSYSERR_INVALPARAM
Definition: mmsystem.h:107
#define MMSYSERR_NOERROR
Definition: mmsystem.h:96
#define MMSYSERR_ALLOCATED
Definition: mmsystem.h:100
#define MMSYSERR_ERROR
Definition: mmsystem.h:97
#define MMSYSERR_INVALFLAG
Definition: mmsystem.h:106
#define MMSYSERR_BADDEVICEID
Definition: mmsystem.h:98
unsigned int UINT
Definition: ndis.h:50
_In_ NDIS_ERROR_CODE ErrorCode
Definition: ndis.h:4436
_In_opt_ PENTER_STATE_SYSTEM_HANDLER _In_opt_ PVOID _In_ LONG _In_opt_ LONG volatile * Number
Definition: ntpoapi.h:207
static HANDLE ProcessHeapHandle
Definition: utility.c:13
UINT GetMemoryAllocationCount()
Definition: utility.c:58
PVOID AllocateMemory(IN UINT Size)
Definition: utility.c:20
MMRESULT TranslateInternalMmResult(IN MMRESULT Result)
Definition: utility.c:132
UINT GetDigitCount(IN UINT Number)
Definition: utility.c:68
VOID FreeMemory(IN PVOID Pointer)
Definition: utility.c:42
static UINT CurrentAllocations
Definition: utility.c:14
MMRESULT Win32ErrorToMmResult(IN UINT ErrorCode)
Definition: utility.c:87
#define IN
Definition: typedefs.h:39
uint32_t ULONG
Definition: typedefs.h:59
_Must_inspect_result_ _In_ WDFDEVICE _In_ PWDF_DEVICE_PROPERTY_DATA _In_ DEVPROPTYPE _In_ ULONG Size
Definition: wdfdevice.h:4533
_Must_inspect_result_ _In_ WDFKEY _In_ PCUNICODE_STRING _Out_opt_ PUSHORT _Inout_opt_ PUNICODE_STRING Value
Definition: wdfregistry.h:413
_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:409