Add doc comment for Allocator.alignedAlloc()#25930
Add doc comment for Allocator.alignedAlloc()#25930michal-dobrogost wants to merge 3 commits intoziglang:masterfrom
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.
[]u8 would be the correct type here, u8[] is invalid.
There was a problem hiding this comment.
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.
If it is runtime known you would use an alignment of 1, not the raw methods.
There was a problem hiding this comment.
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.