Skip to content

Conversation

@michal-dobrogost
Copy link

#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.

/// 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
Copy link
Contributor

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.

Copy link
Author

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.

/// 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`.
Copy link
Contributor

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.

Copy link
Author

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants