You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Introduce ET_ALIGN_ALLOC for portable aligned alloc (#10660)
Summary:
|...
Issue with aligned buffers: P1800967583
The alignment requested is 16, and std::max_align_t is also 16. This means we do not need to pad the size to meet any alignment.
However, the buffer we get from malloc is aligned to 8, not 16. When we try to align the buffer, we overflow and error out.
Seems like malloc is not guaranteed to return 8 or 16 byte-aligned buffers, so also a bit hard to test definitively. So far we've only seen this when the buffer size is small (size 2, 4)
```
The malloc(), calloc(), realloc(), and reallocarray() functions
return a pointer to the allocated memory, which is suitably
aligned for any type that fits into the requested size or less.
```
Use std::aligned_alloc (C++17) to ensure buffer is aligned.
For systems that do not have aligned_alloc (or posix_memalign) fallback to malloc.
The malloc implementation is similar to what file_data_loader.cpp does. Except, we do not have a custom free function from FreeableBuffer, so we store an offset just before the aligned ptr to free the actual buffer.
1. Allocate via malloc; buffer = malloc(size + sizeof(uint16_t) + alignment - 1)
- size: the size requested.
- sizeof(uint16_t): a place to store the offset between the aligned buffer and the original ptr.
- alignment-1: extra padding to allow for alignment.
2. Align (buffer + sizeof(uint16_t)) to alignment. This (usually) pushes the buffer forward by `alignment`.
3. Store the difference of (aligned_ptr - buffer).
The memory will look like this:
| buffer start | maybe padding | offset (aligned_buffer - buffer) | aligned_buffer start | maybe padding | buffer end |
We should have between offset_size (2) and 1 aligned block before the actual aligned buffer.
https://embeddedartistry.com/blog/2017/02/22/generating-aligned-memory/
Reviewed By: larryliu0820, mcr229
Differential Revision: D74041198
0 commit comments