xMessageBufferReceive follows the same “queue by copy” design. But since there should only be a single reader for buffers, it seems like it would be more efficient to allow reading directly out of the buffer, rather than making a copy of the data.
Here’s the current API:
uint8_t buf; // This unnecessarily copies data into buf size_t len = xMessageBufferReceive(handle, buf, 1000, 0); doStuff(buf, len);
Here’s one possible API extension with
uint8_t *buf; // This just sets a pointer to the data, and does not consider the data "received". size_t len = xMessageBufferPeek(handle, &buf, 1000, 0); doStuff(buf, len); // This marks the data as "received" and frees space in the buffer xMessageBufferAdvance(handle);
There’s an issue issue of reading beyond the circular buffer’s wrap-around boundary though, so this suggestion would probably need an entirely new buffer variant (perhaps
ContiguousBuffer) that only
Sends if there’s enough free contiguous space available and calculates
BufferSpacesAvailable a bit differently.
This proposal would also tackle this question: