-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
improve std.heap.c_allocator to use aligned memory functions #3783
Comments
TIL about |
I think that will be a reasonable, safe approach to take. One thing to consider is that in theory we should be able to improve the way interfaces work in zig so that alignment is |
I'm not sure how that would work if we keep the function-pointer style unless function pointers gained known constants for comptime arguments? |
Rich Felker popped by and pointed out that libc specifies Combined with some kind of better interface pattern that allows comptime alignment, this will provide a zero cost wrapper for libc allocation, if the required alignment is within |
Use posix_memalign where available and the _aligned_{malloc,free} API on Windows. Closes ziglang#3783
Use posix_memalign where available and the _aligned_{malloc,free} API on Windows. Closes ziglang#3783
Use posix_memalign where available and the _aligned_{malloc,free} API on Windows. Closes ziglang#3783
|
Currently,
c_allocator
just wrapsrealloc
. This guarantees an alignment of 8 bytes only. However, Zig allocators are supposed to support any alignment. In particular, an async function frame (on some targets, such as x86_64) requires alignment 16. I briefly tried to use one of the libc aligned memory allocation functions in the past, but it wasn't available on all the libcs we wanted to target.However, that was before we had libc versioning support, and upon further reflection, it seems that all the libcs we want to target do support aligned allocation in some form or other.
Here's some brief research:
memalign
,posix_memalign
, andaligned_alloc
. (we provide musl so it will always have all of these)memalign
in all versions;posix_memalign
since 2.1.91,aligned_alloc
since 2.16.posix_memalign
andaligned_alloc
posix_memalign
_aligned_realloc
I believe that covers all the targets, so we can solve this with some additional logic in
std.heap.cRealloc
andstd.heap.cShrink
.The text was updated successfully, but these errors were encountered: