📄 cbbqueue.cpp
字号:
// Ensure m_pMax is the correct value for our size
if (m_pMax != m_pData + m_ulSize)
{
return( FALSE );
}
// Everything looks good!
return( TRUE );
} // IsQueueValid()
/*
** UINT32 CBigByteQueue::Base_DeQueueBytes( pOutBuffer, ulByteCount )
*
* PARAMETERS:
* void *pOutBuffer Pointer to buffer to receive data.
* UINT32 ulAmount Number of bytes desired.
*
* DESCRIPTION:
* Attempts to dequeue nAmount bytes from the Queue and transfers them
* to pOutBuffer.
*
* RETURNS:
* Number of bytes written to pOutBuffer.
*/
UINT32
CBigByteQueue::Base_DeQueueBytes( void *pOutBuffer, UINT32 ulByteCount )
{
UINT32 ulRead;
HX_ASSERT( this );
HX_ASSERT( IsQueueValid() );
HX_ASSERT( pOutBuffer );
// First read the Queue into pOutBuffer
ulRead = Base_PeekBuff( pOutBuffer, ulByteCount );
// Now update m_pHead which is our read pointer
m_pHead = Base_Normalize( m_pHead, ulRead );
HX_ASSERT( IsQueueValid() );
return( ulRead );
} // Base_DeQueueBytes()
void
CBigByteQueue::SetMaxSize(UINT32 ulMax)
{
m_ulMaxSize = ulMax;
}
/*
* Grow the queue to twice its size or at least big enough to hold n more,
* whichever is greater. returns 1 for good, 0 for bad.
*/
int
CBigByteQueue::Grow(UINT32 ulItems)
{
if (m_ulSize == m_ulMaxSize)
{
return 0;
}
/*
* Set our initial guess for the new target size by doubling the
* current size.
*/
UINT32 ulUsedBytes = Base_GetUsedByteCount();
UINT32 ulMinFinalCapacity = ulUsedBytes + ulItems * m_ulElementSize;
UINT32 ulNewSize = m_ulSize * 2;
if (m_ulMaxSize && ulMinFinalCapacity > m_ulMaxSize)
{
return 0;
}
/*
* Keep doubling until we can hold at least ulFinalMinCapacity.
*/
while (ulNewSize < ulMinFinalCapacity)
{
ulNewSize *= 2;
}
if (m_ulMaxSize && ulNewSize > m_ulMaxSize)
{
ulNewSize = m_ulMaxSize;
}
UCHAR* pNewBuf = new UCHAR[ulNewSize];
/*
* Let the queue copy every thing over for us.
* +1 because its best to start out with head pointing at 0,
* and data starting at 1.
*/
Base_DeQueueBytes((void*)(pNewBuf + 1), ulUsedBytes);
/*
* Destroy current structure and re-create with new buffer.
*/
delete[] m_pData;
m_pData = pNewBuf;
m_ulSize = ulNewSize;
//max points one past the end.
m_pMax = m_pData + m_ulSize;
//head points at spot before first queued data
m_pHead = m_pData;
//tail points at last used byte
m_pTail = m_pData + ulUsedBytes;
return 1;
}
/*
** UINT32 CBigByteQueue::Base_EnQueueBytes( pInBuffer, ulByteCount )
*
* PARAMETERS:
* void *pInBuffer Pointer to buffer containing data to EnQueue
* UINT32 ulByteCount Number of bytes items in buffer for EnQueue.
*
* DESCRIPTION:
* Attempts to put nByteCount bytes into queue. If insufficient room, will
* not enqueue anything.
*
* RETURNS:
* Number of bytes written to pInBuffer.
* Should be nByteCount or 0 because we fail if we don't have
* room for ALL data
*/
UINT32
CBigByteQueue::Base_EnQueueBytes( void *pInBuffer, UINT32 ulByteCount )
{
HX_ASSERT( this );
HX_ASSERT( IsQueueValid() );
HX_ASSERT( pInBuffer );
if (!ulByteCount || Base_GetAvailableBytes() < ulByteCount)
{
return( 0 );
}
// Ok, we've guaranteed that we have enough room to enqueue nAmount items
// Now switch on the state of our head & tail pointers
if (m_pTail < m_pHead)
{
// No need to normalize pointers, because we're guaranteed
// that we have room, hence m_pTail + nAmount HAS to be remain < m_pHead
// Remember that m_pTail points at the postion just BEFORE our next
// empty spot in the queue
memcpy( m_pTail + 1, pInBuffer, ulByteCount ); /* Flawfinder: ignore */
m_pTail += ulByteCount;
}
else
{
// m_pTail >= m_pHead
// This may require a copy in two passes if we have to wrap around the buffer
UINT32 ulCopy;
UINT32 ulPrevCopy;
void *pDest;
// Copying from (m_pTail + 1) to the end of the allocated buffer or nAmount
// which ever comes first.
pDest = Base_Normalize( m_pTail, 1);
ulCopy = __min((UINT32)(m_pMax - (UCHAR*)pDest), ulByteCount );
memcpy( pDest, pInBuffer, ulCopy ); /* Flawfinder: ignore */
m_pTail = (UCHAR *)pDest + ulCopy - 1;
// Figure out how much more we have to copy (if any)
ulPrevCopy = ulCopy;
ulCopy = ulByteCount - ulCopy;
if (ulCopy)
{
// Now we're copying into the base of the allocated array
// whatever we didn't copy the first pass around
memcpy( m_pData, (UCHAR *)pInBuffer + ulPrevCopy, ulCopy ); /* Flawfinder: ignore */
m_pTail = m_pData + ulCopy - 1;
}
}
HX_ASSERT( IsQueueValid() );
return( ulByteCount );
} // Base_EnQueueBytes()
UINT32
CBigByteQueue::PeekAt( UINT32 ulIndex, void *pOutBuffer ) const
{
UINT32 ulCopy;
UINT32 ulByteCount;
void *pHead;
void *pTail;
HX_ASSERT( pOutBuffer );
HX_ASSERT( this );
HX_ASSERT( IsQueueValid() );
if (ulIndex >= GetQueuedItemCount())
{
return( 0 );
}
// We don't want to modify m_pTail or m_pHead here, so copy them
// and use our copies to manipulate the buffer.
pTail = m_pTail;
// Advance pHead till it points at the correct position
// relative to the index we want.
ulByteCount = m_ulElementSize;
pHead = Base_Normalize( m_pHead, (ulIndex * ulByteCount + 1) );
if (pHead < pTail)
{
memcpy( pOutBuffer, (UCHAR *)pHead, ulByteCount ); /* Flawfinder: ignore */
return( ulByteCount );
}
else
{
// pHead > pTail
UINT32 ulPrevCopy;
// Copying from (pHead + 1) to the end of the allocated buffer or
// nByteCount which ever comes first.
ulCopy = __min( (UINT32)(m_pMax - (UCHAR *)pHead), ulByteCount );
memcpy( pOutBuffer, pHead, ulCopy ); /* Flawfinder: ignore */
// Figure out how much more we have to copy (if any)
ulPrevCopy = ulCopy;
ulCopy = ulByteCount - ulCopy;
if (ulCopy)
{
// Now we're copying from the base of the allocated array
// whatever we didn't copy the first pass around
memcpy( (UCHAR *)pOutBuffer + ulPrevCopy, m_pData, ulCopy ); /* Flawfinder: ignore */
}
return( ulCopy + ulPrevCopy );
}
}
/*
** UINT32 CBigByteQueue::Base_PeekBuff( pOutBuffer, ulByteCount )
*
* PARAMETERS:
* pOutBuffer Pointer to buffer to receive data in queue.
* ulByteCount Number of bytes to copy out of queue.
*
* DESCRIPTION:
* Private primitive used to copy data out of a queue buffer.
* This is a workhorse function used in DeQueue(), operator=(),
* and our copy constructor.
*
* RETURNS:
* The number of bytes copied out of the buffer.
*/
UINT32
CBigByteQueue::Base_PeekBuff( void *pOutBuffer, UINT32 ulByteCount ) const
{
UINT32 ulCopy;
void *pHead;
void *pTail;
HX_ASSERT( this );
HX_ASSERT( IsQueueValid() );
// if the Queue is empty, then we can't get anything
if (IsEmpty())
{
return( 0 );
}
// We don't want to modify m_pTail or m_pHead here, so copy them
// and use our copies to manipulate the buffer.
pTail = m_pTail;
pHead = m_pHead;
if (pHead < pTail)
{
// We can do the copy in one pass w/o having to Normalize() the pointer
ulCopy = __min( ulByteCount, Base_GetUsedByteCount() );
memcpy( pOutBuffer, (UCHAR *)pHead + 1, ulCopy ); /* Flawfinder: ignore */
return( ulCopy );
}
else
{
// pHead > pTail
UINT32 ulPrevCopy;
UCHAR * pSrc;
// Copying from (pHead + 1) to the end of the allocated buffer or
// nByteCount which ever comes first.
pSrc = Base_Normalize( (UCHAR *)pHead, 1 );
ulCopy = __min( (UINT32)(m_pMax - pSrc), ulByteCount );
memcpy( pOutBuffer, pSrc, ulCopy ); /* Flawfinder: ignore */
// The __min() above ensures we don't need to Normalize the pointer
pHead = pSrc + ulCopy - 1;
// Figure out how much more we have to copy (if any)
ulPrevCopy = ulCopy;
ulCopy = ulByteCount - ulCopy;
if (ulCopy)
{
// Now we're copying from the base of the allocated array
// whatever we didn't copy the first pass around
memcpy( (UCHAR *)pOutBuffer + ulPrevCopy, m_pData, ulCopy ); /* Flawfinder: ignore */
}
return( ulCopy + ulPrevCopy );
}
} // Base_PeekBuff()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -