prpbuffermanager.cpp

来自「mx27 f14v2 源代码。包括ADS板上诸多驱动的源码。」· C++ 代码 · 共 697 行 · 第 1/2 页

CPP
697
字号
            return NULL;
        }

        retVal = 1;
    } else {
        DEBUGMSG (ZONE_DEVICE, 
            (TEXT("%s: No buffers available?!\r\n"), __WFUNCTION__));
        retVal = -1;
    }

    LeaveCriticalSection(&m_csLockBufferQueues);

    BUFMAN_FUNCTION_EXIT();

    return retVal;
}

//-----------------------------------------------------------------------------
//
// Function: SetFilledBuffer
//
// This function adds a buffer from the busy buffer queue to the message 
// queue of filled buffers.
//
// Parameters:
//      None.
//
// Returns:
//      Returns virtual pointer to buffer created.  
//      Returns NULL if unsuccessful.
//
//-----------------------------------------------------------------------------
BOOL PrpBufferManager::SetFilledBuffer()
{
    BUFMAN_FUNCTION_ENTRY();

    DWORD dwFlags, bytesRead;
    prpBufferData bufData;

    EnterCriticalSection(&m_csLockBufferQueues);

    // Read from Busy queue, and add to the Filled queue
    if (!ReadMsgQueue(m_hReadBusyQueue, &bufData, sizeof(prpBufferData), 
        &bytesRead, 0, &dwFlags)) {
        DEBUGMSG(ZONE_ERROR, 
            (_T("%s: No busy buffers available\r\n"), __WFUNCTION__));
        LeaveCriticalSection(&m_csLockBufferQueues);
        return FALSE;
    }

    if (!WriteMsgQueue(m_hWriteFilledQueue, &bufData, 
        sizeof(prpBufferData), 0, 0)) {
        DEBUGMSG(ZONE_ERROR, 
            (_T("%s: Filled buffer queue is full!  Aborting...\r\n"), 
            __WFUNCTION__));
        LeaveCriticalSection(&m_csLockBufferQueues);
        return FALSE;
    }

    LeaveCriticalSection(&m_csLockBufferQueues);

    BUFMAN_FUNCTION_EXIT();

    return TRUE;
}

//-----------------------------------------------------------------------------
//
// Function: DeleteBuffers
//
// This function deletes all of the buffers in the 
// Idle, Busy, and Filled queues.
//
// Parameters:
//      None.
//
// Returns:
//      TRUE if success, otherwise FALSE.
//
//-----------------------------------------------------------------------------
BOOL PrpBufferManager::DeleteBuffers()
{
    BUFMAN_FUNCTION_ENTRY();

    DWORD dwFlags, bytesRead;
    prpBufferData bufData;

    EnterCriticalSection(&m_csLockBufferQueues);

    // Read and deallocate all buffers from Idle queue.
    while (ReadMsgQueue(m_hReadIdleQueue, &bufData, sizeof(prpBufferData), 
        &bytesRead, 0, &dwFlags)) {
        // Deallocate all buffers
        if (bufData.pPhysAddr != NULL) {
            if (!FreePhysMem(bufData.pVirtAddr)) {
                DEBUGMSG(ZONE_ERROR, 
                    (TEXT("%s: Buffer Deallocation failed in idle queue! \r\n"), 
                    __WFUNCTION__));
                LeaveCriticalSection(&m_csLockBufferQueues);
                return FALSE;
            }
            bufData.pPhysAddr = NULL;
            bufData.pVirtAddr = NULL;
        }
    }

    // Read and deallocate all buffers from Busy queue.
    while (ReadMsgQueue(m_hReadBusyQueue, &bufData, sizeof(prpBufferData), 
        &bytesRead, 0, &dwFlags)) {
        // Deallocate all buffers in busy queue
        if (bufData.pPhysAddr != NULL) {
            if (!FreePhysMem(bufData.pVirtAddr)) {
                DEBUGMSG(ZONE_ERROR, 
                    (TEXT("%s: Buffer Deallocation failed in busy queue! \r\n"), 
                    __WFUNCTION__));
                LeaveCriticalSection(&m_csLockBufferQueues);
                return FALSE;
            }
            bufData.pPhysAddr = NULL;
            bufData.pVirtAddr = NULL;
        }
    }

    // Read and deallocate all buffers from Filled queue.
    while (ReadMsgQueue(m_hReadFilledQueue, &bufData, sizeof(prpBufferData), 
        &bytesRead, 0, &dwFlags)) {
        // Deallocate all buffers in filled queue
        if (bufData.pPhysAddr != NULL) {
            if (!FreePhysMem(bufData.pVirtAddr)) {
                DEBUGMSG(ZONE_ERROR, 
                    (TEXT("%s: Buffer Deallocation failed in filled queue! \r\n"), 
                    __WFUNCTION__));
                LeaveCriticalSection(&m_csLockBufferQueues);
                return FALSE;
            }
            bufData.pPhysAddr = NULL;
            bufData.pVirtAddr = NULL;
        }
    }

    LeaveCriticalSection(&m_csLockBufferQueues);

    BUFMAN_FUNCTION_EXIT();

    return TRUE;
}

//-----------------------------------------------------------------------------
//
// Function: ResetBuffers
//
// This function moves all of the buffers in the Filled and Busy queue to 
// the Idle queue.  This resets the buffers to a ready-to-start state.
//
// Parameters:
//      None.
//
// Returns:
//      None.
//
//-----------------------------------------------------------------------------
VOID PrpBufferManager::ResetBuffers()
{
    BUFMAN_FUNCTION_ENTRY();

    DWORD dwFlags, bytesRead;
    prpBufferData bufData;

    EnterCriticalSection(&m_csLockBufferQueues);

    // Read all buffers from Filled queue and move 
    // them to the Busy queue.

    // Move filled queue buffers
    while (ReadMsgQueue(m_hReadFilledQueue, &bufData, sizeof(prpBufferData), 
        &bytesRead, 0, &dwFlags)) {
        WriteMsgQueue(m_hWriteIdleQueue, &bufData, sizeof(prpBufferData), 0, 0);
    }

    // Move busy queue buffers
    while (ReadMsgQueue(m_hReadBusyQueue, &bufData, sizeof(prpBufferData), 
        &bytesRead, 0, &dwFlags)) {
        WriteMsgQueue(m_hWriteIdleQueue, &bufData, sizeof(prpBufferData), 0, 0);
    }

    LeaveCriticalSection(&m_csLockBufferQueues);

    BUFMAN_FUNCTION_EXIT();
}

//-----------------------------------------------------------------------------
//
// Function:  GetBufFilled
//
// This function reads the queue of filled buffers and returns the buffer 
// at the top of the queue.
//
// Parameters:
//       pBufData
//           [out]  The virtual/physics address of the filled channel buffer.
//
// Returns:
//      TRUE for success, FALSE for failure.
//
//-----------------------------------------------------------------------------
BOOL PrpBufferManager::GetBufferFilled(pPrpBufferData pBufData)
{
    BUFMAN_FUNCTION_ENTRY();

    DWORD dwFlags, bytesRead;

    if (NULL == pBufData)
    {
       return FALSE;
    }

    EnterCriticalSection(&m_csLockBufferQueues);

    // Read from Filled queue, and add to the Idle queue
    if (!ReadMsgQueue(m_hReadFilledQueue, pBufData, sizeof(prpBufferData), 
        &bytesRead, 0, &dwFlags)) {
        DEBUGMSG(ZONE_ERROR, 
            (_T("%s: No filled buffers available!? \r\n"), __WFUNCTION__));
        LeaveCriticalSection(&m_csLockBufferQueues);
        return FALSE;
    }

    LeaveCriticalSection(&m_csLockBufferQueues);

    BUFMAN_FUNCTION_EXIT();

    return TRUE;
}

//-----------------------------------------------------------------------------
//
// Function:  PutBufferIdle
//
// This function put the queue to idle buffers  queue.
//
// Parameters:
//       pBufData
//           [in]  The virtual/physics address of the filled channel buffer.
//
// Returns:
//      TRUE for success ,FALSE for failure.
//
//-----------------------------------------------------------------------------
BOOL PrpBufferManager::PutBufferIdle(pPrpBufferData pBufData)
{
    BUFMAN_FUNCTION_ENTRY();

    if (NULL == pBufData)
    {
       return FALSE;
    }
	
    EnterCriticalSection(&m_csLockBufferQueues);

    if (!WriteMsgQueue(m_hWriteIdleQueue, pBufData, 
        sizeof(prpBufferData), 0, 0)) {
        DEBUGMSG(ZONE_ERROR, 
            (_T("%s: Idle buffer queue is full! Aborting... \r\n"), 
            __WFUNCTION__));
        LeaveCriticalSection(&m_csLockBufferQueues);
        return FALSE;
    }

    LeaveCriticalSection(&m_csLockBufferQueues);

    BUFMAN_FUNCTION_EXIT();

    return TRUE;
}

//-----------------------------------------------------------------------------
//
// Function: PrintBufferInfo
//
// This function iterates through the buffers, returning returns the maximum 
// number of buffers supported by the preprocessor.
//
// Parameters:
//      None.
//
// Returns:
//      None.
//
//-----------------------------------------------------------------------------
VOID PrpBufferManager::PrintBufferInfo(void)
{
    DWORD dwFlags, bytesRead, i, j;
    prpBufferData bufData[100];

    EnterCriticalSection(&m_csLockBufferQueues);

    i = 0;
    // Read from idle queue and print buffer data
    while (ReadMsgQueue(m_hReadIdleQueue, &bufData[i], sizeof(prpBufferData),
        &bytesRead, 0, &dwFlags)) {
        DEBUGMSG(ZONE_DEVICE, 
            (_T("%s: Idle Buffer %d: Phys addr = %x, Virt addr = %x\r\n"), 
            __WFUNCTION__, i, bufData[i].pPhysAddr, bufData[i].pVirtAddr));
        i++;
    }

    // Write buffers back to idle queue
    for (j = 0; j < i; j++) {
        WriteMsgQueue(m_hWriteIdleQueue, &bufData[j], 
            sizeof(prpBufferData), 0, 0);
    }

    i = 0;
    // Read from busy queue and print buffer data
    while (ReadMsgQueue(m_hReadBusyQueue, &bufData[i], sizeof(prpBufferData),
        &bytesRead, 0, &dwFlags)) {
        DEBUGMSG(ZONE_DEVICE, 
            (_T("%s: Busy Buffer %d: Phys addr = %x, Virt addr = %x\r\n"), 
            __WFUNCTION__, i, bufData[i].pPhysAddr, bufData[i].pVirtAddr));
        i++;
    }

    // Write buffers back to busy queue
    for (j = 0; j < i; j++) {
        WriteMsgQueue(m_hWriteBusyQueue, &bufData[j], 
            sizeof(prpBufferData), 0, 0);
    }

    i = 0;
    // Read from filled queue and print buffer data
    while (ReadMsgQueue(m_hReadFilledQueue, &bufData[i], 
        sizeof(prpBufferData), &bytesRead, 0, &dwFlags)) {
        DEBUGMSG(ZONE_DEVICE, 
            (_T("%s: Filled Buffer %d: Phys addr = %x, Virt addr = %x\r\n"), 
            __WFUNCTION__, i, bufData[i].pPhysAddr, bufData[i].pVirtAddr));
        i++;
    }

    // Write buffers back to filled queue
    for (j = 0; j < i; j++) {
        WriteMsgQueue(m_hWriteFilledQueue, &bufData[j], 
            sizeof(prpBufferData), 0, 0);
    }

    LeaveCriticalSection(&m_csLockBufferQueues);
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?