-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Add doc comment for Allocator.alignedAlloc() #25930
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
base: master
Are you sure you want to change the base?
Add doc comment for Allocator.alignedAlloc() #25930
Conversation
lib/std/mem/Allocator.zig
Outdated
| /// Prefer inferred types like `const data = alignedAlloc(...)` as it will use | ||
| /// the correctly aligned type. | ||
| /// | ||
| /// Avoid explicit types like `const data: u8[] = alignedAlloc(...)` as they |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[]u8 would be the correct type here, u8[] is invalid.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, sorry, I didn't actually pull the latest version of the comment into this branch. Please take a look again, it has been rewritten.
lib/std/mem/Allocator.zig
Outdated
| /// Avoid explicit types like `const data: u8[] = alignedAlloc(...)` as they | ||
| /// can change the alignment type information needed when calling `free`. | ||
| /// | ||
| /// If alignment is not comptime known use `rawAlloc` and `rawFree`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If it is runtime known you would use an alignment of 1, not the raw methods.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I removed this part as it's inconsistent with other advice in this file to not use rawAlloc and rawFree althgouh right now the API does not allow specifying alignment at runtime.
There are cases when you want to request your data to be specifically aligned (beyond alignment = 1) but you don't know the exact alignment until runtime. The case I'm hitting against is manipulating bit-matrices and aligning them for cache-lines and simd widths but also not wasting space for small matrices (typically values range between 10 to >1024 bits).
#25908
std.mem.allocator uses type reflection to decide what alignment to free with. Alignment coercion rules allow implicit changing of the alignment which can result in the incorrect free bucket being used in the smp allocator. Document the correct usage.