Skip to content

Commit df64d85

Browse files
committed
Use more effective align functions
1 parent e78a580 commit df64d85

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

src/lib.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,20 @@ impl Heap {
7979
}
8080
}
8181

82-
/// Align downwards. Returns the greatest x with alignment `align` so that x<=value.
83-
fn align_down(value: usize, align: usize) -> usize {
84-
value / align * align
82+
/// Align downwards. Returns the greatest x with alignment `align`
83+
/// so that x <= addr. The alignment must be a power of 2.
84+
pub fn align_down(addr: usize, align: usize) -> usize {
85+
if align.is_power_of_two() {
86+
addr & !(align - 1)
87+
} else if align == 0 {
88+
addr
89+
} else {
90+
panic!("`align` must be a power of 2");
91+
}
8592
}
8693

87-
/// Align upwards. Returns the smallest x with alignment `align` so that value<=x.
88-
fn align_up(value: usize, align: usize) -> usize {
89-
align_down(value + align - 1, align)
94+
/// Align upwards. Returns the smallest x with alignment `align`
95+
/// so that x >= addr. The alignment must be a power of 2.
96+
pub fn align_up(addr: usize, align: usize) -> usize {
97+
align_down(addr + align - 1, align)
9098
}

0 commit comments

Comments
 (0)