[PATCH v1 1/3] rust: make `allocator::aligned_size` a `const fn`

Onur Özkan posted 3 patches 2 months, 2 weeks ago
[PATCH v1 1/3] rust: make `allocator::aligned_size` a `const fn`
Posted by Onur Özkan 2 months, 2 weeks ago
Makes `allocator::aligned_size` a `const fn` to allow
compile-time evaluation.

Signed-off-by: Onur Özkan <work@onurozkan.dev>
---
 rust/kernel/alloc/allocator.rs | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/rust/kernel/alloc/allocator.rs b/rust/kernel/alloc/allocator.rs
index aa2dfa9dca4c..aa2957fd585b 100644
--- a/rust/kernel/alloc/allocator.rs
+++ b/rust/kernel/alloc/allocator.rs
@@ -44,7 +44,7 @@
 pub struct KVmalloc;

 /// Returns a proper size to alloc a new object aligned to `new_layout`'s alignment.
-fn aligned_size(new_layout: Layout) -> usize {
+const fn aligned_size(new_layout: Layout) -> usize {
     // Customized layouts from `Layout::from_size_align()` can have size < align, so pad first.
     let layout = new_layout.pad_to_align();

--
2.50.0

Re: [PATCH v1 1/3] rust: make `allocator::aligned_size` a `const fn`
Posted by Miguel Ojeda 1 month, 2 weeks ago
On Sun, Jul 20, 2025 at 11:49 AM Onur Özkan <work@onurozkan.dev> wrote:
>
> Makes `allocator::aligned_size` a `const fn` to allow
> compile-time evaluation.
>
> Signed-off-by: Onur Özkan <work@onurozkan.dev>

For future reference: we wouldn't be able to enable it as-is without
the unstable feature anyway (which Danilo noticed when applied his
version of this) -- if it is needed in the future, the patch is at:

    https://lore.kernel.org/all/CANiq72mUXy6AYkwCW_kO3ikjNBc5pLzXw0+fXFGmYum0tGmw1g@mail.gmail.com/

Cheers,
Miguel