File tree Expand file tree Collapse file tree 1 file changed +12
-7
lines changed Expand file tree Collapse file tree 1 file changed +12
-7
lines changed Original file line number Diff line number Diff line change @@ -8,20 +8,25 @@ use core::ptr;
8
8
9
9
struct KernelAllocator ;
10
10
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
+
11
22
/// Calls `krealloc` with a proper size to alloc a new object aligned to `new_layout`'s alignment.
12
23
///
13
24
/// # Safety
14
25
///
15
26
/// - `ptr` can be either null or a pointer which has been allocated by this allocator.
16
27
/// - `new_layout` must have a non-zero size.
17
28
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) ;
25
30
26
31
// SAFETY:
27
32
// - `ptr` is either null or a pointer returned from a previous `k{re}alloc()` by the
You can’t perform that action at this time.
0 commit comments