rust: make various `alloc` functions `const fn`
Subsystems (2)
Onur Özkan
reviewercover rust: make various `alloc` functions `const fn`
This patch series makes various functions in thealloc crate const fn.
Each patch corresponds to a different module within the same alloc crate.
Onur Özkan (3):
rust: make allocator::aligned_size a const fn
rust: make ArrayLayout::new_unchecked a const fn
rust: make kvec::Vec functions const fn
rust/kernel/alloc/allocator.rs | 2 +- rust/kernel/alloc/kvec.rs | 10 +++++----- rust/kernel/alloc/layout.rs | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-)
--
2.50.0
Onur Özkan
reviewerPATCH 1/3 rust: make `allocator::aligned_size` a `const fn`
Makes allocator::aligned_size a const fn to allow compile-time evaluation.
--- rust/kernel/alloc/allocator.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
rust/kernel/alloc/allocator.rs +1 -1
@@ -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`
Quoted 5 lines · Onur Özkan
Makes
allocator::aligned_sizeaconst fnto allow compile-time evaluation.Signed-off-by Onur Özkan <[email protected]>
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
Onur Özkan
reviewerPATCH 2/3 rust: make `ArrayLayout::new_unchecked` a `const fn`
In reply to Onur ÖzkanMakes ArrayLayout::new_unchecked a const fn to allow compile-time evaluation.
--- rust/kernel/alloc/layout.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
rust/kernel/alloc/layout.rs +1 -1
@@ -80,7 +80,7 @@ pub const fn new(len: usize) -> Result<Self, LayoutError> {/// # Safety /// /// `len` must be a value, for which `len * size_of::<T>() <= isize::MAX` is true. -pub unsafe fn new_unchecked (len:usize ) ->Self { +pub const unsafe fn new_unchecked (len:usize ) ->Self {// INVARIANT: By the safety requirements of this function // `len * size_of::<T>() <= isize::MAX`. Self {
--
2.50.0
Onur Özkan
reviewerPATCH 3/3 rust: make `kvec::Vec` functions `const fn`
In reply to Onur ÖzkanMakes various kvec::Vec functions const fn
to allow compile-time evaluation.
--- rust/kernel/alloc/kvec.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
rust/kernel/alloc/kvec.rs +5 -5
@@ -174,7 +174,7 @@ const fn is_zst() -> bool {/// Returns the number of elements that can be stored within the vector without allocating /// additional memory. -pub fn capacity (&self) ->usize { +pub const fn capacity (&self) ->usize {if const {Self ::is_zst () } { usize::MAX }else {
@@ -184,7 +184,7 @@ pub fn capacity(&self) -> usize {/// Returns the number of elements stored within the vector. #[inline] -pub fn len (&self) ->usize { +pub const fn len (&self) ->usize { self.len }
@@ -195,7 +195,7 @@ pub fn len(&self) -> usize {/// - `additional` must be less than or equal to `self.capacity - self.len`. /// - All elements within the interval [`self.len`,`self.len + additional`) must be initialized. #[inline] -pub unsafe fn inc_len (&mut self, additional:usize ) { +pub const unsafe fn inc_len (&mut self, additional:usize ) {// Guaranteed by the type invariant to never underflow. debug_assert! (additional <= self.capacity () - self.len ());// INVARIANT: By the safety requirements of this method this represents the exact number of
@@ -244,7 +244,7 @@ pub fn as_mut_ptr(&mut self) -> *mut T {/// Returns a raw pointer to the vector's backing buffer, or, if `T` is a ZST, a dangling raw /// pointer. #[inline] -pub fn as_ptr (&self) -> *const T { +pub const fn as_ptr (&self) -> *const T { self.ptr.as_ptr () }
@@ -260,7 +260,7 @@ pub fn as_ptr(&self) -> *const T {/// assert!(!v.is_empty()); /// ``` #[inline] -pub fn is_empty (&self) ->bool { +pub const fn is_empty (&self) ->bool { self.len () ==0 }
--
2.50.0
Quoted 11 lines · Onur Özkan
This patch series makes various functions in the
alloccrate const fn.Each patch corresponds to a different module within the same
alloccrate.Onur Özkan (3):
rust: makeallocator::aligned_sizeaconst fn
rust: makeArrayLayout::new_uncheckedaconst fn
rust: makekvec::Vecfunctionsconst fn
Quoted 4 lines
rust/kernel/alloc/allocator.rs | 2 +- rust/kernel/alloc/kvec.rs | 10 +++++----- rust/kernel/alloc/layout.rs | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-)
This looks sensible, any particular reason for why you need them const?
For the entire series:
---
Cheers,
Benno
Onur Özkan
reviewerQuoted 20 lines · On Sun, 20 Jul 2025 16:43:49 +0200 "Benno Lossin" <[email protected]> wrote:
Quoted 11 lines · Onur Özkan
This patch series makes various functions in the
alloccrate const fn.Each patch corresponds to a different module within the same
alloccrate.Onur Özkan (3):
rust: makeallocator::aligned_sizeaconst fn
rust: makeArrayLayout::new_uncheckedaconst fn
rust: makekvec::Vecfunctionsconst fnQuoted 4 lines
rust/kernel/alloc/allocator.rs | 2 +- rust/kernel/alloc/kvec.rs | 10 +++++----- rust/kernel/alloc/layout.rs | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-)This looks sensible, any particular reason for why you need them const?
Quoted 7 lines
For the entire series:
Reviewed-by Benno Lossin <[email protected]>---
Cheers,
Benno
Personally, I don't have a specific reason. I thought the change is harmless and might extend functionality for other people in the future. It could also (although less likely) help the compiler optimize things further.
Regards,
Onur
Quoted 5 lines · Onur Özkan
Personally, I don't have a specific reason. I thought the change is harmless and might extend functionality for other people in the future. It could also (although less likely) help the compiler optimize things further.
I think it is OK -- even if we promise they are const and we have to remove it in the future, it is fine, since there is no stable kernel API. So that flexibility is another advantage of no promises there.
However, I am curious, in which cases it would help the compiler optimize? The compiler already has the information on whether it could actually be const and whether it can be evaluated at compile-time and so on -- do you mean it has an effect on heuristics like inlining or similar?
Thanks!
Cheers,
Miguel
Onur Özkan
reviewerQuoted 11 lines · On Sun, 20 Jul 2025 17:42:43 +0200 Miguel Ojeda…
Quoted 5 lines · Onur Özkan
Personally, I don't have a specific reason. I thought the change is harmless and might extend functionality for other people in the future. It could also (although less likely) help the compiler optimize things further.
I think it is OK -- even if we promise they are
constand we have to remove it in the future, it is fine, since there is no stable kernel API. So that flexibility is another advantage of no promises there.
Quoted 5 lines
However, I am curious, in which cases it would help the compiler optimize? The compiler already has the information on whether it could actually be
constand whether it can be evaluated at compile-time and so on -- do you mean it has an effect on heuristics like inlining or similar?
I thought it had effects similar to inlining, but after digging into the assembly output of a simple program (with and without const expressions) and reading some related discussions [1], it seems I was wrong about it, sorry.
[1]: https://users.rust-lang.org/t/the-effects-of-const-fn/48303
Regards,
Onur
Quoted 5 lines · Onur Özkan
I thought it had effects similar to inlining, but after digging into the assembly output of a simple program (with and without
constexpressions) and reading some related discussions [1], it seems I was wrong about it, sorry.
No worries at all, and thanks for taking a look into it!
I think Danilo will take this eventually through alloc, but it sounds good to me.
Cheers,
Miguel
Quoted 11 lines · Onur Özkan
This patch series makes various functions in the
alloccrate const fn.Each patch corresponds to a different module within the same
alloccrate.Onur Özkan (3):
rust: makeallocator::aligned_sizeaconst fn
rust: makeArrayLayout::new_uncheckedaconst fn
rust: makekvec::Vecfunctionsconst fn
Quoted 7 lines
rust/kernel/alloc/allocator.rs | 2 +- rust/kernel/alloc/kvec.rs | 10 +++++----- rust/kernel/alloc/layout.rs | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-)--
2.50.0
Quoted 1 line · Onur Özkan
rust: make
allocator::aligned_sizeaconst fn
I dropped this patch, since it would conflict with [1].
Quoted 2 lines
rust: make
ArrayLayout::new_uncheckedaconst fn
rust: makekvec::Vecfunctionsconst fn
Applied to alloc-next, thanks!