1 // Copyright 2023, The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 //! Utility functions.
16
17 use aarch64_paging::paging::MemoryRegion;
18 use core::ops::Range;
19
20 /// Computes the largest multiple of the provided alignment smaller or equal to the address.
21 ///
22 /// Note: the result is undefined if alignment isn't a power of two.
unchecked_align_down(addr: usize, alignment: usize) -> usize23 pub const fn unchecked_align_down(addr: usize, alignment: usize) -> usize {
24 addr & !(alignment - 1)
25 }
26
27 /// Computes the smallest multiple of the provided alignment larger or equal to the address.
28 ///
29 /// Note: the result is undefined if alignment isn't a power of two and may wrap to 0.
unchecked_align_up(addr: usize, alignment: usize) -> usize30 pub const fn unchecked_align_up(addr: usize, alignment: usize) -> usize {
31 unchecked_align_down(addr + alignment - 1, alignment)
32 }
33
34 /// Safe wrapper around unchecked_align_up() that validates its assumptions and doesn't wrap.
align_up(addr: usize, alignment: usize) -> Option<usize>35 pub const fn align_up(addr: usize, alignment: usize) -> Option<usize> {
36 if !alignment.is_power_of_two() {
37 None
38 } else if let Some(s) = addr.checked_add(alignment - 1) {
39 Some(unchecked_align_down(s, alignment))
40 } else {
41 None
42 }
43 }
44
45 /// Aligns the given address to the given alignment, if it is a power of two.
46 ///
47 /// Returns `None` if the alignment isn't a power of two.
48 #[allow(dead_code)] // Currently unused but might be needed again.
align_down(addr: usize, alignment: usize) -> Option<usize>49 const fn align_down(addr: usize, alignment: usize) -> Option<usize> {
50 if !alignment.is_power_of_two() {
51 None
52 } else {
53 Some(unchecked_align_down(addr, alignment))
54 }
55 }
56
57 /// Performs an integer division rounding up.
58 ///
59 /// Note: Returns None if den isn't a power of two.
ceiling_div(num: usize, den: usize) -> Option<usize>60 pub const fn ceiling_div(num: usize, den: usize) -> Option<usize> {
61 let Some(r) = align_up(num, den) else {
62 return None;
63 };
64
65 r.checked_div(den)
66 }
67
68 /// Trait to check containment of one range within another.
69 pub trait RangeExt {
70 /// Returns true if `self` is contained within the `other` range.
is_within(&self, other: &Self) -> bool71 fn is_within(&self, other: &Self) -> bool;
72
73 /// Returns true if `self` overlaps with the `other` range.
overlaps(&self, other: &Self) -> bool74 fn overlaps(&self, other: &Self) -> bool;
75 }
76
77 impl<T: PartialOrd> RangeExt for Range<T> {
is_within(&self, other: &Self) -> bool78 fn is_within(&self, other: &Self) -> bool {
79 self.start >= other.start && self.end <= other.end
80 }
81
overlaps(&self, other: &Self) -> bool82 fn overlaps(&self, other: &Self) -> bool {
83 self.start < other.end && other.start < self.end
84 }
85 }
86
87 impl RangeExt for MemoryRegion {
is_within(&self, other: &Self) -> bool88 fn is_within(&self, other: &Self) -> bool {
89 self.start() >= other.start() && self.end() <= other.end()
90 }
91
overlaps(&self, other: &Self) -> bool92 fn overlaps(&self, other: &Self) -> bool {
93 self.start() < other.end() && other.start() < self.end()
94 }
95 }
96