Skip to content

Commit c9c7f45

Browse files
tomakaphil-opp
authored andcommitted
Align up the Hole initialization address (#18)
1 parent 620b8d1 commit c9c7f45

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

src/hole.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use alloc::alloc::{AllocErr, Layout};
2-
use core::mem::size_of;
2+
use core::mem::{align_of, size_of};
33
use core::ptr::NonNull;
44

55
use super::align_up;
@@ -23,12 +23,15 @@ impl HoleList {
2323
/// Creates a `HoleList` that contains the given hole. This function is unsafe because it
2424
/// creates a hole at the given `hole_addr`. This can cause undefined behavior if this address
2525
/// 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.
2628
pub unsafe fn new(hole_addr: usize, hole_size: usize) -> HoleList {
2729
assert!(size_of::<Hole>() == Self::min_size());
2830

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;
3033
ptr.write(Hole {
31-
size: hole_size,
34+
size: hole_size.saturating_sub(aligned_hole_addr - hole_addr),
3235
next: None,
3336
});
3437

@@ -290,6 +293,7 @@ fn deallocate(mut hole: &mut Hole, addr: usize, mut size: usize) {
290293
next: hole.next.take(), // the reference to the Y block (if it exists)
291294
};
292295
// write the new hole to the freed memory
296+
debug_assert_eq!(addr % align_of::<Hole>(), 0);
293297
let ptr = addr as *mut Hole;
294298
unsafe { ptr.write(new_hole) };
295299
// add the F block as the next block of the X block

0 commit comments

Comments
 (0)