Fix #4911 : Partially rework some code to remove warnings about potential non-aligned memory accesses #4912
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hi,
As discussed in the issue #4911, here is my approach to contain the memory alignment issue
There is some very easy fixes based on memcpy insted of pointer arithmetic and dereferrence. This is a quick win.
But I had to think of another way to work with the radiobuffer bytebuffer, because of how it was previously handled, the only way was using pointer arithmetic to access payload after the header.
So I just replaced the byte buffer by a struct containing header then payload :
As you can see, this is very simple. And I replaced all the pointer code by struct accesses :
becomes :
radioBuffer.header.from
and
radiobuf + sizeof(PacketHeader)
becomes :
radioBuffer.payload
Its more elegant too :)
The result is, less memory-aligned warnings, and a more safe code to use, and depending of your coding style, more readable I guess.
I quickly tested it on RP2040, HTv3 and HT Tracker 1.1 (to test the gps part) without seeing any issue.
This should Fix #4911 (partially at least) and gives a good start (or just ideas) to a more robust code.
Regards,