📄 csgreq.cpp
字号:
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//
// Use of this sample source code is subject to the terms of the Microsoft
// license agreement under which you licensed this sample source code. If
// you did not accept the terms of the license agreement, you are not
// authorized to use this sample source code. For the terms of the license,
// please see the license agreement between you and Microsoft or, if applicable,
// see the LICENSE.RTF on your install media or the root of your tools installation.
// THE SAMPLE SOURCE CODE IS PROVIDED "AS IS", WITH NO WARRANTIES.
//
#include "csgreq.h"
// ----------------------------------------------------------------------------
// This function returns the length (number of bytes) of a Scatter/Gather
// request by summing the number of bytes in each buffer in the Scatter/
// Gather buffer list. If successful, return the length of the supplied
// Scatter/Gather request. If unsuccessful, return -1.
//
// pSgReq
// [in] Pointer to target Scatter/Gather request.
// ----------------------------------------------------------------------------
DWORD CSgReq::GetSgReqLengthBySgBufList(PSG_REQ pSgReq)
{
DWORD dwSgReqLengthBySgBufList = 0;
if (NULL == pSgReq) {
ASSERT(NULL != pSgReq);
return -1;
}
for (DWORD i = 0; i < pSgReq->sr_num_sg; i += 1) {
dwSgReqLengthBySgBufList += pSgReq->sr_sglist[i].sb_len;
}
return dwSgReqLengthBySgBufList;
}
// ----------------------------------------------------------------------------
// This function initializes a Scatter/Gather buffer list.
//
// rgMappedSgBufList
// [in] Pointer to target Scatter/Gather buffer list.
// dwMappedSgBufListLength
// [in] The number of buffers in the target Scatter/Gather buffer list.
// ----------------------------------------------------------------------------
VOID CSgReq::DoResetMappedSgBufList(PSG_BUF rgMappedSgBufList, DWORD dwMappedSgBufListLength)
{
if (NULL == rgMappedSgBufList) {
ASSERT(NULL != rgMappedSgBufList);
return;
}
if (MAX_SG_BUF < dwMappedSgBufListLength) {
ASSERT(MAX_SG_BUF >= dwMappedSgBufListLength);
return;
}
for (DWORD i = 0; i < dwMappedSgBufListLength; i += 1) {
rgMappedSgBufList[i].sb_len = 0;
rgMappedSgBufList[i].sb_buf = NULL;
}
return;
}
// ----------------------------------------------------------------------------
// This function maps the buffer pointers of a Scatter/Gather buffer list.
// The mapping is redundant as ValidateSg has already performed the mapping.
// So this function just copies values and does a sanity check.
// If successful, return TRUE. If unsuccessful, return FALSE.
//
// rgMappedSgBufList
// [out] Pointer to destination Scatter/Gather buffer list. The mapped
// buffer pointers will reside in this buffer list.
// dwMappedSgBufListLength
// [in] The number of buffers in the destination Scatter/Gather buffer
// list.
// rgSourceSgBufList
// [in] Pointer to source Scatter/Gather buffer list. The buffer pointers
// in this buffer list was previously mapped via CeOpenCallerBuffer. Hence
// in ValidateSg. So we just do a plain copy.
// dwSourceSgBufListLength
// [in] The number of buffers in the source Scatter/Gather buffer list.
// ----------------------------------------------------------------------------
BOOL CSgReq::DoMapSgBufList(PSG_BUF rgMappedSgBufList, DWORD dwMappedSgBufListLength, PSG_BUF rgSourceSgBufList, DWORD dwSourceSgBufListLength)
{
DWORD dwLength = 0;
if (NULL == rgMappedSgBufList) {
ASSERT(NULL != rgMappedSgBufList);
return FALSE;
}
if (MAX_SG_BUF < dwMappedSgBufListLength) {
ASSERT(MAX_SG_BUF >= dwMappedSgBufListLength);
return FALSE;
}
if (NULL == rgSourceSgBufList) {
ASSERT(NULL != rgSourceSgBufList);
return FALSE;
}
if (MAX_SG_BUF < dwSourceSgBufListLength) {
ASSERT(MAX_SG_BUF >= dwSourceSgBufListLength);
return FALSE;
}
if (dwMappedSgBufListLength < dwSourceSgBufListLength) {
ASSERT(dwMappedSgBufListLength >= dwSourceSgBufListLength);
return FALSE;
}
for (DWORD i = 0; i < dwSourceSgBufListLength; i += 1) {
rgMappedSgBufList[i].sb_buf = rgSourceSgBufList[i].sb_buf;
if ((NULL != rgSourceSgBufList[i].sb_buf) && (NULL == rgMappedSgBufList[i].sb_buf)) {
return FALSE;
}
rgMappedSgBufList[i].sb_len = rgSourceSgBufList[i].sb_len;
}
return TRUE;
}
// ----------------------------------------------------------------------------
// This constructor initializes the object's member variables to invalid
// values.
// ----------------------------------------------------------------------------
CSgReq::CSgReq()
{
m_pSgReq = NULL;
m_dwSectorSize = (DWORD)-1;
m_dwCurrentBuffer = (DWORD)-1;
m_dwCurrentBufferPosition = (DWORD)-1;
}
// ----------------------------------------------------------------------------
// This destructor does nothing.
// ----------------------------------------------------------------------------
CSgReq::~CSgReq()
{
}
// ----------------------------------------------------------------------------
// This function returns the starting sector of the Scatter/Gather request.
// If successful, return the starting sector of the Scatter/Gather request.
// If unsuccessful, return -1.
// ----------------------------------------------------------------------------
DWORD CSgReq::GetStartingSector()
{
if (NULL == m_pSgReq) {
ASSERT(NULL != m_pSgReq);
return (DWORD)-1;
}
return m_pSgReq->sr_start;
}
// ----------------------------------------------------------------------------
// This function returns the number of sequential sectors accessed by the
// Scatter/Gather request. If successful, return the starting sector of the
// Scatter/Gather request. If sunsuccessful, return -1.
// ----------------------------------------------------------------------------
DWORD CSgReq::GetNumberOfSectors()
{
if (NULL == m_pSgReq) {
ASSERT(NULL != m_pSgReq);
return (DWORD)-1;
}
return m_pSgReq->sr_num_sec;
}
// ----------------------------------------------------------------------------
// This function returns the number of buffers in the Scatter/Gather buffer
// list. If successful, return the number of buffers in the Scatter/Gather
// buffer list. If unsuccessful, return -1.
// ----------------------------------------------------------------------------
DWORD CSgReq::GetNumberOfBuffers()
{
if (NULL == m_pSgReq) {
ASSERT(NULL != m_pSgReq);
return (DWORD)-1;
}
return m_pSgReq->sr_num_sg;
}
// ----------------------------------------------------------------------------
// This function returns the length (total bytes) of the active buffer in the
// Scatter/Gather buffer list. If successful, return the length of the active
// buffer in the Scatter/Gather buffer list. If unsuccessful, return -1.
// ----------------------------------------------------------------------------
DWORD CSgReq::GetCurrentBufferLength()
{
if (NULL == m_pSgReq) {
ASSERT(NULL != m_pSgReq);
return (DWORD)-1;
}
return m_pSgReq->sr_sglist[m_dwCurrentBuffer].sb_len;
}
// ----------------------------------------------------------------------------
// This function returns the 0-based index number of the active buffer in the
// Scatter/Gather buffer list. If successful, return the 0-based index number
// of the active buffer in the Scatter/Gather buffer list. If unsuccessful,
// return -1.
// ----------------------------------------------------------------------------
DWORD CSgReq::GetCurrentBufferNumber()
{
if (NULL == m_pSgReq) {
ASSERT(NULL != m_pSgReq);
return (DWORD)-1;
}
return m_dwCurrentBuffer;
}
// ----------------------------------------------------------------------------
// This function returns a pointer to the active buffer in the Scatter/Gather
// buffer list. If successful, return a pointer to the active Scatter/Gather
// buffer in the Scatter/Gather buffer list. If unsuccessful, return NULL.
// ----------------------------------------------------------------------------
PBYTE CSgReq::GetCurrentBufferAsPointer()
{
if (NULL == m_pSgReq) {
ASSERT(NULL != m_pSgReq);
return NULL;
}
return m_rgMappedSgBufList[m_dwCurrentBuffer].sb_buf;
}
// ----------------------------------------------------------------------------
// This function returns the current position in the active Scatter/Gather
// buffer in the Scatter/Gather buffer list. If successful, return the 0-based
// index number of the current byte in the active Scatter/Gather buffer. If
// unsuccessful, return -1.
// ----------------------------------------------------------------------------
DWORD CSgReq::GetCurrentBufferPosition()
{
if (NULL == m_pSgReq) {
ASSERT(NULL != m_pSgReq);
return (DWORD)-1;
}
return m_dwCurrentBufferPosition;
}
// ----------------------------------------------------------------------------
// This function returns a pointer to the current byte in the active Scatter/
// Gather buffer in the Scatter/Gather buffer list. If successful, return a
// pointer to the current byte in the active Scatter/Gather buffer in the
// Scatter/Gather buffer list. If unsuccessful, return NULL.
// ----------------------------------------------------------------------------
PBYTE CSgReq::GetCurrentBufferPositionAsPointer()
{
if (NULL == m_pSgReq) {
ASSERT(NULL != m_pSgReq);
return NULL;
}
return &m_rgMappedSgBufList[m_dwCurrentBuffer].sb_buf[m_dwCurrentBufferPosition];
}
// ----------------------------------------------------------------------------
// Since a Scatter/Gather buffer list is a single buffer split over a set of
// sub-buffers, it is possible to calculate the absolute buffer length as the
// sum of the sub-buffer lengths. This function returns the absolute buffer
// length. If successful, return the absolute buffer length. If unsuccessful,
// return -1.
// ----------------------------------------------------------------------------
DWORD CSgReq::GetAbsoluteBufferLength()
{
if (NULL == m_pSgReq) {
ASSERT(NULL != m_pSgReq);
return (DWORD)-1;
}
return GetSgReqLengthBySgBufList(m_pSgReq);
}
// ----------------------------------------------------------------------------
// Since a Scatter/Gather buffer list is a single buffer split over a set of
// sub-buffers, it is possible to calculate the absolute buffer position. This
// function returns the absolute buffer position. If successful, return the
// absolute buffer position. If unsuccessful, return -1.
// ----------------------------------------------------------------------------
DWORD CSgReq::GetAbsoluteBufferPosition()
{
DWORD dwRet = 0;
if (NULL == m_pSgReq) {
ASSERT(NULL != m_pSgReq);
return (DWORD)-1;
}
for (DWORD i = 0; i < m_dwCurrentBuffer; i += 1) {
dwRet += m_pSgReq->sr_sglist[i].sb_len;
}
// m_dwCurrentBufferPosition is 0-based; add 1 for absolute position.
dwRet += (m_dwCurrentBufferPosition + 1);
return dwRet;
}
// ----------------------------------------------------------------------------
// This function sets the active buffer in the Scatter/Gather buffer list.
//
// dwCurrentBuffer
// [in] The 0-based index of the new active buffer in the Scatter/Gather
// buffer list.
// ----------------------------------------------------------------------------
VOID CSgReq::SetCurrentBuffer(DWORD dwCurrentBuffer)
{
if (NULL == m_pSgReq) {
ASSERT(NULL != m_pSgReq);
return;
}
m_dwCurrentBuffer = dwCurrentBuffer;
return;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -