I'm writing an embedded control system in C that consists of multiple tasks that send messages to each other (a fairly common idiom, I believe!), but I'm having a hard time designing a mechanism which:
Conceptually, I'd like to represent each message type as a separate struct definition, and I'd like a system with the following functions (simplified):
void sendMsg(queue_t *pQueue, void *pMsg, size_t size);
void *dequeueMsg(queue_t *pQueue);
where a queue_t
comprises a linked list of nodes, each with a char buf[MAX_SIZE]
field. The system I'm on doesn't have a malloc()
implementation, so there'll need to be a global pool of free nodes, and then one of the following (perceived issues in bold):
sendMsg()
does a memcpy
of the incoming message into the buffer of a free node.dequeueMsg()
does a further memcpy
on the return value.void *getFreeBuffer()
function which returns the buf[]
of the next free node, which the caller (the sender) will cast to the appropriate pointer-to-type.memcpy
after dequeueMsg()
to avoid alignment issues on the way out.queue_t
nodes as (e.g.) uint32_t buf[MAX_SIZE]
.The only other option I can see is to create a union of all the message types along with char buf[MAX_SIZE]
, but I don't count this as "neat"!
So my question is, how does one do this properly?
The way we deal with this is to have our free list consist entirely of aligned nodes. In fact we have multiple free lists for different sizes of node, so we have lists that are aligned on 2 byte, 4 byte, and 16 byte boundaries (our platform doesn't care about alignment larger than one SIMD vector) . Any allocation gets rounded up to one of those values and put in a properly aligned node. So, sendMsg always copies its data into an aligned node. Since you're writing the free list yourself, you can easily enforce alignment.
We would also use a #pragma or declspec to force that char buf[MAX_SIZE] array to be aligned to at least a word boundary inside the queue_t node struct.
This assumes of course that the input data is aligned, but if for some reason you're passing in a message that expects to be (let's say) 3 bytes off from alignment, you can always detect that with a modulus and return an offset into the free node.
With that underlying design we have interfaces that support both option 1 and 2 above. Again, we precondition that input data is always natively aligned, so our dequeue of course returns an aligned pointer; but if you need oddly aligned data in, again, just offset into the free node and return the offset pointer.
This keeps you dealing in void *s thus avoiding your strict aliasing problems. (In general though I think you may need to relax your strict aliasing requirements when writing your own memory allocators, since by nature they blur types internally.)