Queuing Question - Different Integral Types

westmorelandeng wrote on Friday, October 12, 2012:

Hello Richard and Fellow FreeRTOS’ers,

I have a question about Queuing - can I use one queue that is large - large being 128 bytes in an embedded system - for different integral types or is that just a no-no?

For instance - I would like to use one General Q for a UART, let’s say - but, I need to pass different types of packets.  I am finding the overhead of doing something like vPrintString() (have written my own which is similar) - could be a little excessive in some cases - but will the Q ‘behave’ properly if I were to pass something like:

1)  a structure pointer to a communications packet that is, let’s say, 48 bytes; and then
2)  a structure pointer for a 3 byte ‘ACK’ packet
3)  just a byte of some sort,

Does the Q only support one integral type?  For instance; if I have sized a Q for chars but one of my structure’s has unsigned shorts, for instance - will the Q operate properly?  I can unwrap this on the other end char-by-char - but I would like to be able to use the Qing facilities and not have to declare a specific Q for each specific type of packet.

Thanks In Advance,
John W.

richard_damon wrote on Saturday, October 13, 2012:

A queue only supports items of one type (or more precisely one size). This item could be a union of the various types of object you want to queue (or a struct with a flag to indicate the type and then a union).

It could also be a void* pointer (or a structure with a flag and a void* pointer), with the pointer pointing to the data. In this case the queue will NOT save the data pointed by, so the program needs to manage the life of these buffers and keep track of when they are free to reuse or still busy. One way to do this is to heap allocate them and free them when done. Many design rules don’t like using the heap in embedded environments, due to the fact that you need to make sure you can handle a failed allocation gracefully. 

Another option is to enqueue a packet in smaller pieces, where some packet use fewer pieces than others to reduce queue space used for the smaller packets (if that is important). One thing to watch out for is that if multiple tasks might be adding or removing packets from the queue, that side will need a mutex or equivalent to avoid interleaving or splitting of packets.

The third option is to just queue up a maximal size packet. This wastes queue space for small packets, but simplifies the code significantly which may be a very positive result.

Which one to use depends a lot on the application and the sizes and likelihood of different types of packets. In general large packets are normally best to send via pointers, to avoid all the data copying that otherwise happens. 48 byte packets, particularly if a lot of the packets are of that size, may qualify as large here. It may not be large enough that variable size buffers are important and perhaps ALL packets are make 48 bytes, and take the loss of space efficiency to greatly limit program complexity. Perhaps if most packets are 48 or 3, it might make sense to have two buffer sizes, especially of the ratio of the number of needed packets of each size is known.