Skip to content

Commit

Permalink
don't change parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
Holden committed Jan 2, 2024
1 parent bab7692 commit faad97f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 22 deletions.
50 changes: 30 additions & 20 deletions source/FreeRTOS_Stream_Buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,17 @@ void vStreamBufferMoveMid( StreamBuffer_t * const pxBuffer,
const size_t uxCount )
{
/* Increment uxMid, but no further than uxHead */
const size_t uxSize = uxStreamBufferMidSpace( pxBuffer );
const size_t uxMoveCount = ( uxCount > uxSize ) ? uxSize : uxCount;
const size_t uxLength = pxBuffer->LENGTH;
size_t uxMid = pxBuffer->uxMid + uxMoveCount;
const size_t uxSize = uxStreamBufferMidSpace( pxBuffer );
size_t uxMid = pxBuffer->uxMid;
size_t uxMoveCount = uxCount;

if( uxMoveCount > uxSize )
{
uxMoveCount = uxSize;
}

uxMid += uxMoveCount;

if( uxMid >= uxLength )
{
Expand Down Expand Up @@ -240,8 +247,9 @@ size_t uxStreamBufferGetPtr( StreamBuffer_t * const pxBuffer,
size_t uxStreamBufferAdd( StreamBuffer_t * const pxBuffer,
const size_t uxOffset,
const uint8_t * const pucData,
size_t uxByteCount )
const size_t uxByteCount )
{
size_t uxCount;
size_t uxSpace = uxStreamBufferGetSpace( pxBuffer );

/* If uxOffset > 0, items can be placed in front of uxHead */
Expand All @@ -256,9 +264,9 @@ size_t uxStreamBufferAdd( StreamBuffer_t * const pxBuffer,

/* The number of bytes that can be written is the minimum of the number of
* bytes requested and the number available. */
uxByteCount = FreeRTOS_min_size_t( uxSpace, uxByteCount );
uxCount = FreeRTOS_min_size_t( uxSpace, uxByteCount );

if( uxByteCount != 0U )
if( uxCount != 0U )
{
const size_t uxLength = pxBuffer->LENGTH;
size_t uxNextHead = pxBuffer->uxHead;
Expand All @@ -279,18 +287,18 @@ size_t uxStreamBufferAdd( StreamBuffer_t * const pxBuffer,
/* Calculate the number of bytes that can be added in the first
* write - which may be less than the total number of bytes that need
* to be added if the buffer will wrap back to the beginning. */
const size_t uxFirst = FreeRTOS_min_size_t( uxLength - uxNextHead, uxByteCount );
const size_t uxFirst = FreeRTOS_min_size_t( uxLength - uxNextHead, uxCount );

/* Write as many bytes as can be written in the first write. */
( void ) memcpy( &( pxBuffer->ucArray[ uxNextHead ] ), pucData, uxFirst );

/* If the number of bytes written was less than the number that
* could be written in the first write... */
if( uxByteCount > uxFirst )
if( uxCount > uxFirst )
{
/* ...then write the remaining bytes to the start of the
* buffer. */
( void ) memcpy( pxBuffer->ucArray, &( pucData[ uxFirst ] ), uxByteCount - uxFirst );
( void ) memcpy( pxBuffer->ucArray, &( pucData[ uxFirst ] ), uxCount - uxFirst );
}
}

Expand All @@ -301,7 +309,7 @@ size_t uxStreamBufferAdd( StreamBuffer_t * const pxBuffer,
if( uxOffset == 0U )
{
/* ( uxOffset == 0 ) means: write at uxHead position */
uxNextHead += uxByteCount;
uxNextHead += uxCount;

if( uxNextHead >= uxLength )
{
Expand All @@ -320,7 +328,7 @@ size_t uxStreamBufferAdd( StreamBuffer_t * const pxBuffer,
( void ) xTaskResumeAll();
}

return uxByteCount;
return uxCount;
}
/*-----------------------------------------------------------*/

Expand All @@ -330,7 +338,7 @@ size_t uxStreamBufferAdd( StreamBuffer_t * const pxBuffer,
* @param[in] pxBuffer The buffer from which the bytes will be read.
* @param[in] uxOffset can be used to read data located at a certain offset from 'lTail'.
* @param[in,out] pucData If 'pucData' equals NULL, the function is called to advance 'lTail' only.
* @param[in] uxByteCount The number of bytes to read.
* @param[in] uxMaxCount The number of bytes to read.
* @param[in] xPeek if 'xPeek' is pdTRUE, or if 'uxOffset' is non-zero, the 'lTail' pointer will
* not be advanced.
*
Expand All @@ -339,9 +347,11 @@ size_t uxStreamBufferAdd( StreamBuffer_t * const pxBuffer,
size_t uxStreamBufferGet( StreamBuffer_t * const pxBuffer,
const size_t uxOffset,
uint8_t * const pucData,
size_t uxByteCount,
const size_t uxMaxCount,
const BaseType_t xPeek )
{
size_t uxCount;

/* How much data is available? */
size_t uxSize = uxStreamBufferGetSize( pxBuffer );

Expand All @@ -355,9 +365,9 @@ size_t uxStreamBufferGet( StreamBuffer_t * const pxBuffer,
}

/* Use the minimum of the wanted bytes and the available bytes. */
uxByteCount = FreeRTOS_min_size_t( uxSize, uxByteCount );
uxCount = FreeRTOS_min_size_t( uxSize, uxMaxCount );

if( uxByteCount != 0U )
if( uxCount != 0U )
{
const size_t uxLength = pxBuffer->LENGTH;
size_t uxNextTail = pxBuffer->uxTail;
Expand All @@ -377,26 +387,26 @@ size_t uxStreamBufferGet( StreamBuffer_t * const pxBuffer,
/* Calculate the number of bytes that can be read - which may be
* less than the number wanted if the data wraps around to the start of
* the buffer. */
const size_t uxFirst = FreeRTOS_min_size_t( uxLength - uxNextTail, uxByteCount );
const size_t uxFirst = FreeRTOS_min_size_t( uxLength - uxNextTail, uxCount );

/* Obtain the number of bytes it is possible to obtain in the first
* read. */
( void ) memcpy( pucData, &( pxBuffer->ucArray[ uxNextTail ] ), uxFirst );

/* If the total number of wanted bytes is greater than the number
* that could be read in the first read... */
if( uxByteCount > uxFirst )
if( uxCount > uxFirst )
{
/* ...then read the remaining bytes from the start of the buffer. */
( void ) memcpy( &( pucData[ uxFirst ] ), pxBuffer->ucArray, uxByteCount - uxFirst );
( void ) memcpy( &( pucData[ uxFirst ] ), pxBuffer->ucArray, uxCount - uxFirst );
}
}

if( ( xPeek == pdFALSE ) && ( uxOffset == 0U ) )
{
/* Move the tail pointer to effectively remove the data read from
* the buffer. */
uxNextTail += uxByteCount;
uxNextTail += uxCount;

if( uxNextTail >= uxLength )
{
Expand All @@ -407,6 +417,6 @@ size_t uxStreamBufferGet( StreamBuffer_t * const pxBuffer,
}
}

return uxByteCount;
return uxCount;
}
/*-----------------------------------------------------------*/
4 changes: 2 additions & 2 deletions source/include/FreeRTOS_Stream_Buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,12 @@ size_t uxStreamBufferGetPtr( StreamBuffer_t * const pxBuffer,
size_t uxStreamBufferAdd( StreamBuffer_t * const pxBuffer,
const size_t uxOffset,
const uint8_t * const pucData,
size_t uxByteCount );
const size_t uxByteCount );

size_t uxStreamBufferGet( StreamBuffer_t * const pxBuffer,
const size_t uxOffset,
uint8_t * const pucData,
size_t uxByteCount,
const size_t uxMaxCount,
const BaseType_t xPeek );

/* *INDENT-OFF* */
Expand Down

0 comments on commit faad97f

Please sign in to comment.