I am working on a simple protocol stack for a small embedded system (multidrop, rs485 type stuff). In this stack, losely models after OSI layers:
Each layer has its own header / footer portion that wraps the payload of the layer above it.
I will be using my own buffer pool of statically allocated fixed sized blocks to store the binary packets. (No malloc / free in this app.)
In other API's I have seen that the data is usually passed as a const pointer with an associated length. In this way the data would need a copy operation at each layer as the payload of the layer above is placed in a newly allocated buffer for the current layer.
For a three layer stack this would be 2 copy operations and 3 allocated buffers.
Is there a better way to do this and still maintain clean separation of the protocol layers?
To better anchor the discussion, lets say the packets are typically around 2k and the processor is a small 8 bit micro running at 8Mhz.
You could avoid the copies by having each layer request an empty buffer from the next lower layer, rather than allocating one itself:
buffer + phdr_len
to Datalink Layer.buffer + phdr_len + dhdr_len
to Network Layer.buffer + phdr_len + dhdr_len + nhdr_len
to Application Layer.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With