Size field of message buffers

nathanolacti wrote on Thursday, March 15, 2018:

Hello everyone,
I have recently upgraded a project I worked on to FreeRTOS 10.
Currently I use queues to send messages between the tasks of my system. All messages are the same structure type containing a 4 byte header and a 4 byte payload.

I would like to use a Message Buffer instead of the queue. As some of my messages only require a single or 2 payload bytes, I could reduce the size of some messages in my system.
I also have the use case now of handling messages with bigger payloads (up to 250 bytes for example but never above 255).

Unfortunately the size field used in the message buffer is 4 bytes long on my ARM Cortex-M0 MCU, so if start using it : a message with 4 bytes of header + 1 byte of payload would actually require 9 bytes of storage in the message buffer where it uses 8 bytes right now ; a 4 byte header + 4 bytes payload would use 12 bytes.

As my use case do not require messages bigger than 255 bytes, would it be possible to allow a configurable type for the size field of the message buffer ?

Like a config in the freertos config file allowing me to use uint8_t instead of the 4 bytes size_t ?

I am sure this feature would be useful for others too.

Nathan O.

rtel wrote on Thursday, March 15, 2018:

There is no facility to do this at the moment, but looking at the code
it seems it would be a simple update to introduce the capability. I
will have a play with it and see.

rtel wrote on Thursday, March 15, 2018:

Try the FreeRTOS.h and message_buffer.c files just checked in. In those
I added the following to FreeRTOS.h to ensure backward compatibility:

     /* 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

…and in stream_buffer.c I changed:

#define sbBYTES_TO_STORE_MESSAGE_LENGTH ( sizeof( size_t ) )


#define sbBYTES_TO_STORE_MESSAGE_LENGTH ( sizeof( 

The size_t is still used to hold the message size in the APIs, only the
number of bytes used to read and write the message size into the buffer
have changed. Therefore sizeof( configMESSAGE_BUFFER_LENGTH_TYPE )
could be less than sizeof( size_t ) - so the size_t variable used to
hold the message size read out of the buffer is cleared to zero before
it is used (in case only one byte is read into the variable the other
bytes must be zero).

Now of course the tests are failing because they specifically expect the
message length field to be 4 bytes - I will update those when I get a

Let me know…

rtel wrote on Thursday, March 15, 2018:

Note having updated the tests I had to fix some regressions in stream_buffer.c, so if you already downloaded the updated version, please do so again.

nathanolacti wrote on Friday, March 16, 2018:

Thank you very much.
This is exactly what I was looking for.

I have downloaded the latest trunk 2536, set the configMESSAGE_BUFFER_LENGTH_TYPE in my FreeRTOSConfig file to uint8_t and it works great.