From: rtel Date: Thu, 15 Mar 2018 15:51:22 +0000 (+0000) Subject: Introduce sbBYTES_TO_STORE_MESSAGE_LENGTH to allow the number of bytes used to hold... X-Git-Tag: V10.1.0~30 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=d5533b189399e8ce042f4561d7f5d0f50eb48205;p=freertos Introduce sbBYTES_TO_STORE_MESSAGE_LENGTH to allow the number of bytes used to hold a message length in a message buffer to be reduced if 4 bytes is always too many (save a little RAM). git-svn-id: https://svn.code.sf.net/p/freertos/code/trunk@2535 1d2547de-c912-0410-9cb9-b8ca96c0e9e2 --- diff --git a/FreeRTOS/Source/include/FreeRTOS.h b/FreeRTOS/Source/include/FreeRTOS.h index 2b51f2b8f..c6f344513 100644 --- a/FreeRTOS/Source/include/FreeRTOS.h +++ b/FreeRTOS/Source/include/FreeRTOS.h @@ -826,6 +826,13 @@ extern "C" { #define configSTACK_DEPTH_TYPE uint16_t #endif +#ifndef configMESSAGE_BUFFER_LENGTH_TYPE + /* Defaults to size_t for backward compatibility, but can be overridden + in FreeRTOSConfig.h if lengths will always be less than the number of bytes + in a size_t. */ + #define configMESSAGE_BUFFER_LENGTH_TYPE size_t +#endif + /* Sanity check the configuration. */ #if( configUSE_TICKLESS_IDLE != 0 ) #if( INCLUDE_vTaskSuspend != 1 ) diff --git a/FreeRTOS/Source/stream_buffer.c b/FreeRTOS/Source/stream_buffer.c index 9e518907a..ed8baef91 100644 --- a/FreeRTOS/Source/stream_buffer.c +++ b/FreeRTOS/Source/stream_buffer.c @@ -129,7 +129,7 @@ that uses task notifications. */ /*lint -restore (9026) */ /* The number of bytes used to hold the length of a message in the buffer. */ -#define sbBYTES_TO_STORE_MESSAGE_LENGTH ( sizeof( size_t ) ) +#define sbBYTES_TO_STORE_MESSAGE_LENGTH ( sizeof( configMESSAGE_BUFFER_LENGTH_TYPE ) ) /* Bits stored in the ucFlags field of the stream buffer. */ #define sbFLAGS_IS_MESSAGE_BUFFER ( ( uint8_t ) 1 ) /* Set if the stream buffer was created as a message buffer, in which case it holds discrete messages rather than a stream. */ @@ -504,6 +504,9 @@ TimeOut_t xTimeOut; if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0 ) { xRequiredSpace += sbBYTES_TO_STORE_MESSAGE_LENGTH; + + /* Overflow? */ + configASSERT( xRequiredSpace > xDataLengthBytes ); } else { @@ -906,6 +909,9 @@ size_t xOriginalTail, xReceivedLength, xNextMessageLength; returned to its prior state if the length of the message is too large for the provided buffer. */ xOriginalTail = pxStreamBuffer->xTail; + /* Ensure xNextMessageLength is cleared to 0 in case + sizeof( configMESSAGE_BUFFER_LENGTH_TYPE ) != sizeof( size_t ). */ + xNextMessageLength = 0; ( void ) prvReadBytesFromBuffer( pxStreamBuffer, ( uint8_t * ) &xNextMessageLength, xBytesToStoreMessageLength, xBytesAvailable ); /* Reduce the number of bytes available by the number of bytes just