📄 util.cpp
字号:
{
*pnAllocated -= nGrowSize;
}
return fSuccess;
}
//
// Buffer ctor
//
CBuffer::CBuffer()
: m_szData(NULL),
m_cbLength(0),
m_cbAlloc(0)
{
// FUNCTION_TRACE(CBuffer::CBuffer);
}
//
// Buffer dtor
//
CBuffer::~CBuffer()
{
// FUNCTION_TRACE(CBuffer::~CBuffer);
delete[] m_szData;
}
__inline BOOL safeIntUAddThree(unsigned int addend1, unsigned int addend2, unsigned int addend3, unsigned int *pSum)
{
BOOL ReturnValue = FALSE;
if ( pSum )
{
if ( TRUE == safeIntUAdd(addend1, addend2, pSum))
{
if ( TRUE == safeIntUAdd( *pSum, addend3, pSum ) )
{
ReturnValue = TRUE;
}
}
}
return ReturnValue;
}
//
// Append data to the response
//
BOOL CBuffer::Append(const LPCSTR szString, const UINT cbString)
{
// FUNCTION_TRACE(CBuffer::Append);
LPSTR szTemp = NULL;
BOOL fRet = FALSE;
UINT SafeAdd = 0;
if (!m_szData)
{
m_cbLength = 0;
m_cbAlloc = ALLOC_SIZE;
m_szData = new char[m_cbAlloc];
if (!m_szData)
{
goto Error;
}
m_szData[ALLOC_SIZE-1]=0;
}
if ( FALSE == safeIntUAddThree(m_cbLength, cbString, 1, &SafeAdd))
{
goto Error;
}
if (SafeAdd > m_cbAlloc)
{
do
{
if ( FALSE == safeIntUAdd(m_cbAlloc, ALLOC_SIZE, &m_cbAlloc))
{
goto Error;
}
} while (SafeAdd > m_cbAlloc);
szTemp = new char[m_cbAlloc];
if (!szTemp)
{
goto Error;
}
szTemp[m_cbAlloc-1] = 0;
memcpy(szTemp, m_szData, m_cbLength);
delete[] m_szData;
m_szData = szTemp;
}
memcpy(m_szData + m_cbLength, szString, cbString);
m_cbLength += cbString;
fRet = TRUE;
Error:
return fRet;
}
//
// Get the data out of the buffer
// (the caller takes ownership of the m_szData storage)
//
LPSTR CBuffer::GiveUpData()
{
// FUNCTION_TRACE(CBuffer::GiveUpData);
LPSTR szTemp = m_szData;
m_szData = NULL;
m_cbLength = 0;
m_cbAlloc = 0;
return szTemp;
}
//
//
//
void CBuffer::InheritData(CBuffer* pSrcBuffer)
{
// FUNCTION_TRACE(CBuffer::InheritData);
delete[] m_szData;
m_szData = NULL;
m_cbLength = 0;
m_cbAlloc = 0;
m_cbAlloc = pSrcBuffer->m_cbAlloc;
m_cbLength = pSrcBuffer->m_cbLength;
m_szData = pSrcBuffer->GiveUpData();
}
//
// Shared resource ctor
//
CSharedResource::CSharedResource()
: m_fInited(FALSE),
m_dwSharedUsers(0),
m_hSharedUseSph(NULL),
m_hExclusiveUseSph(NULL),
m_hCancelEvent(NULL)
{
FUNCTION_TRACE(CSharedResource::CSharedResource);
InitializeCriticalSection(&m_cs);
}
//
// Shared resource dtor
//
CSharedResource::~CSharedResource()
{
FUNCTION_TRACE(CSharedResource::~CSharedResource);
if (m_hSharedUseSph)
{
(void)CloseHandle(m_hSharedUseSph);
m_hSharedUseSph = NULL;
}
if (m_hExclusiveUseSph)
{
(void)CloseHandle(m_hExclusiveUseSph);
m_hExclusiveUseSph = NULL;
}
DeleteCriticalSection(&m_cs);
m_fInited = FALSE;
}
//
// Shared resource intialization
//
BOOL CSharedResource::Init(const HANDLE hCancelEvent)
{
FUNCTION_TRACE(CSharedResource::Init);
if (m_fInited)
{
goto Error;
}
m_hSharedUseSph = CreateSemaphore(NULL, 1, 1, NULL);
if (!m_hSharedUseSph)
{
goto Error;
}
m_hExclusiveUseSph = CreateSemaphore(NULL, 1, 1, NULL);
if (!m_hExclusiveUseSph)
{
goto Error;
}
m_hCancelEvent = hCancelEvent;
m_fInited = TRUE;
Error:
return m_fInited;
}
//
// Called to acquire shared use permission
//
BOOL CSharedResource::EnterSharedUse()
{
FUNCTION_TRACE(CSharedResource::EnterSharedUse);
HANDLE rghObjects[2] = { m_hSharedUseSph, m_hCancelEvent};
DWORD dwWait;
BOOL fRet = FALSE;
if (!m_fInited)
{
goto Error;
}
// Wait until shared use is granted
dwWait = WaitForMultipleObjects(2, rghObjects, FALSE, INFINITE);
if (WAIT_OBJECT_0 + 1 == dwWait)
{
// We hit the terminate event -- quit
goto Error;
}
else if (WAIT_OBJECT_0 != dwWait)
{
DEBUGMSG(ZONE_ERROR, (TEXT("RILDrv : E : WaitForMultipleObjects : Failed, RetVal=0x%x, GetLastError=0x%x\r\n"),dwWait,GetLastError()));
DEBUGCHK(WAIT_FAILED == dwWait);
goto Error;
}
{
SYNCBLOCK(m_cs);
// Grant shared use for anyone who's waiting
if (!ReleaseSemaphore(m_hSharedUseSph, 1, NULL))
{
goto Error;
}
// Increment the number of shared users
m_dwSharedUsers++;
// If this is the first shared user, we need to make sure there are no exclusive users at the moment
if (1 == m_dwSharedUsers)
{
// Wait until the exclusive user is done
rghObjects[0] = m_hExclusiveUseSph;
dwWait = WaitForMultipleObjects(2, rghObjects, FALSE, INFINITE);
if (WAIT_OBJECT_0 + 1 == dwWait)
{
// We hit the terminate event -- quit
goto Error;
}
else if (WAIT_OBJECT_0 != dwWait)
{
DEBUGCHK(FALSE);
goto Error;
}
}
}
fRet = TRUE;
Error:
return fRet;
}
//
// Called to relinquish shared use permission
//
BOOL CSharedResource::ExitSharedUse()
{
FUNCTION_TRACE(CSharedResource::ExitSharedUse);
SYNCBLOCK(m_cs);
BOOL fRet = FALSE;
if (!m_fInited)
{
goto Error;
}
// Decrement the number of shared users
m_dwSharedUsers--;
// If this is the last shared user, allow exclusive use
if (!m_dwSharedUsers)
{
if (!ReleaseSemaphore(m_hExclusiveUseSph, 1, NULL))
{
goto Error;
}
}
fRet = TRUE;
Error:
return fRet;
}
//
// Called to acquire exclusive use permission
//
BOOL CSharedResource::EnterExclusiveUse(PFN_SHRDUSE_ACTION pfnAction, const DWORD dwParam) const
{
FUNCTION_TRACE(CSharedResource::EnterExclusiveUse);
HANDLE rghObjects[2] = { m_hSharedUseSph, m_hCancelEvent};
DWORD dwWait;
BOOL fRet = FALSE;
int RetryCount = 10;
if (!m_fInited)
{
goto Error;
}
// Lock shared users out until this exclusive user can finish its operation
dwWait = WaitForMultipleObjects(2, rghObjects, FALSE, INFINITE);
if (WAIT_OBJECT_0 + 1 == dwWait)
{
// We hit the terminate event -- quit
goto Error;
}
else if (WAIT_OBJECT_0 != dwWait)
{
DEBUGCHK(FALSE);
goto Error;
}
for (;;)
{
// Call the custom action routine, passing it the specified parameter
pfnAction(dwParam);
// Wait until there are no other exclusive users
rghObjects[0] = m_hExclusiveUseSph;
dwWait = WaitForMultipleObjects(2, rghObjects, FALSE, 500);
if (WAIT_OBJECT_0 == dwWait)
{
// We own it now.
break;
}
if (WAIT_OBJECT_0 + 1 == dwWait)
{
// We hit the terminate event -- quit
goto Error;
}
else if (WAIT_TIMEOUT == dwWait)
{
// We timed out.
// Have we exceeded our retry count?
if (--RetryCount<=0)
{
goto Error;
}
// Try to signal and wait again.
}
else
{
// Some other error
DEBUGCHK(FALSE);
goto Error;
}
}
// Allow shared users to proceed (they will be locked out until there're no more exclusive users)
if (!ReleaseSemaphore(m_hSharedUseSph, 1, NULL))
{
goto Error;
}
fRet = TRUE;
Error:
return fRet;
}
//
// Called to relinquish exclusive use permission
//
BOOL CSharedResource::ExitExclusiveUse() const
{
FUNCTION_TRACE(CSharedResource::ExitExclusiveUse);
// Allow other exclusive users and shared users to proceed
return ReleaseSemaphore(m_hExclusiveUseSph, 1, NULL);
}
extern BOOL g_bRadioOff;
void IndicateCallActivityToAudioSubsystem (BOOL fActive)
{
#ifdef OEM1_DRIVER_WAVEXT
DWORD dwRet;
DWORD dwModule;
if ( TRUE == fActive )
{
//signal audio driver that at least one call is active
if( (FALSE == g_bRadioOff) && (g_hWavDev != NULL) && (g_hWavDev != INVALID_HANDLE_VALUE) )
{
if (DeviceIoControl (g_hWavDev, IOCTL_DRIVER_AUDIO_CALL_ACTIVE, 0, 0, NULL, 0, &dwRet, NULL))
{
dwModule = AUDIO_ACTIVE_ROUTER_OMAP;
if(DeviceIoControl (g_hWavDev, IOCTL_DRIVER_ROUTER_CALL_AUDIO, (LPVOID)&dwModule, sizeof(dwModule), NULL, 0, &dwRet, NULL))
{
RETAILMSG(1, (TEXT("[TI]IndicateCallActivityToAudioSubsystem IOCTL_DRIVER_ROUTER_CALL_AUDIO \r\n")));
}
}
}
}
else // If fChecklist is TRUE, an INACTIVE is implied
{
if( (g_hWavDev != NULL) && (g_hWavDev != INVALID_HANDLE_VALUE))
{
DWORD dwRet;
DeviceIoControl (g_hWavDev, IOCTL_DRIVER_AUDIO_CALL_INACTIVE, 0, 0, NULL, 0, &dwRet, NULL);
RETAILMSG(1, (TEXT("[TI]IndicateCallActivityToAudioSubsystem IOCTL_DRIVER_AUDIO_CALL_INACTIVE \r\n")));
}
}
#endif
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -