builder: use new abstraction to guarantee alignment #156
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.
Before this PR, both builder structs (
HeaderBuilder
,BootInformationBuilder
) build their corresponding structures in aVec<u8>
. However, there is no guarantee of an 8-byte alignment in that case. As long as theallocator_api
is not stable, I chose the following workaround:Use a regular
Vec<u8>
with a capacity ofexpected_len + 7
. This way, I can add zeroes at the beginning to the vec for alignment adjustments. A wrapper type returns a slice to the actual and aligned bytes of the corresponding structure, i.e, the wrapper type knows an offset between 0 and 7 where the structure actually starts.The only problem with this type: I can't create a reliable unit test for the alignment offsetting magic. I created a local test run using the allocator_api and a "weird" (unaligned) pointer from the allocator and everything worked as expected. I tested the new code with miri and miri reported no allocation or alignment errors 馃コ