Skip to content

Commit a654a6e

Browse files
Danilo Krummrichojeda
authored andcommitted
rust: alloc: separate aligned_size from krealloc_aligned
Separate `aligned_size` from `krealloc_aligned`. Subsequent patches implement `Allocator` derivates, such as `Kmalloc`, that require `aligned_size` and replace the original `krealloc_aligned`. Reviewed-by: Alice Ryhl <aliceryhl@google.com> Reviewed-by: Benno Lossin <benno.lossin@proton.me> Reviewed-by: Gary Guo <gary@garyguo.net> Signed-off-by: Danilo Krummrich <dakr@kernel.org> Link: https://lore.kernel.org/r/20241004154149.93856-3-dakr@kernel.org Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
1 parent b7a084b commit a654a6e

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

rust/kernel/alloc/allocator.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,25 @@ use core::ptr;
88

99
struct KernelAllocator;
1010

11+
/// Returns a proper size to alloc a new object aligned to `new_layout`'s alignment.
12+
fn aligned_size(new_layout: Layout) -> usize {
13+
// Customized layouts from `Layout::from_size_align()` can have size < align, so pad first.
14+
let layout = new_layout.pad_to_align();
15+
16+
// Note that `layout.size()` (after padding) is guaranteed to be a multiple of `layout.align()`
17+
// which together with the slab guarantees means the `krealloc` will return a properly aligned
18+
// object (see comments in `kmalloc()` for more information).
19+
layout.size()
20+
}
21+
1122
/// Calls `krealloc` with a proper size to alloc a new object aligned to `new_layout`'s alignment.
1223
///
1324
/// # Safety
1425
///
1526
/// - `ptr` can be either null or a pointer which has been allocated by this allocator.
1627
/// - `new_layout` must have a non-zero size.
1728
pub(crate) unsafe fn krealloc_aligned(ptr: *mut u8, new_layout: Layout, flags: Flags) -> *mut u8 {
18-
// Customized layouts from `Layout::from_size_align()` can have size < align, so pad first.
19-
let layout = new_layout.pad_to_align();
20-
21-
// Note that `layout.size()` (after padding) is guaranteed to be a multiple of `layout.align()`
22-
// which together with the slab guarantees means the `krealloc` will return a properly aligned
23-
// object (see comments in `kmalloc()` for more information).
24-
let size = layout.size();
29+
let size = aligned_size(new_layout);
2530

2631
// SAFETY:
2732
// - `ptr` is either null or a pointer returned from a previous `k{re}alloc()` by the

0 commit comments

Comments
 (0)