1
1
use alloc:: alloc:: { AllocErr , Layout } ;
2
- use core:: mem:: size_of;
2
+ use core:: mem:: { align_of , size_of} ;
3
3
use core:: ptr:: NonNull ;
4
4
5
5
use super :: align_up;
@@ -23,12 +23,15 @@ impl HoleList {
23
23
/// Creates a `HoleList` that contains the given hole. This function is unsafe because it
24
24
/// creates a hole at the given `hole_addr`. This can cause undefined behavior if this address
25
25
/// is invalid or if memory from the `[hole_addr, hole_addr+size) range is used somewhere else.
26
+ ///
27
+ /// The pointer to `hole_addr` is automatically aligned.
26
28
pub unsafe fn new ( hole_addr : usize , hole_size : usize ) -> HoleList {
27
29
assert ! ( size_of:: <Hole >( ) == Self :: min_size( ) ) ;
28
30
29
- let ptr = hole_addr as * mut Hole ;
31
+ let aligned_hole_addr = align_up ( hole_addr, align_of :: < Hole > ( ) ) ;
32
+ let ptr = aligned_hole_addr as * mut Hole ;
30
33
ptr. write ( Hole {
31
- size : hole_size,
34
+ size : hole_size. saturating_sub ( aligned_hole_addr - hole_addr ) ,
32
35
next : None ,
33
36
} ) ;
34
37
@@ -290,6 +293,7 @@ fn deallocate(mut hole: &mut Hole, addr: usize, mut size: usize) {
290
293
next : hole. next . take ( ) , // the reference to the Y block (if it exists)
291
294
} ;
292
295
// write the new hole to the freed memory
296
+ debug_assert_eq ! ( addr % align_of:: <Hole >( ) , 0 ) ;
293
297
let ptr = addr as * mut Hole ;
294
298
unsafe { ptr. write ( new_hole) } ;
295
299
// add the F block as the next block of the X block
0 commit comments