blob: e52ac8e628fdff1dc3afc2e4661b48d709236498 [file] [log] [blame]
Alice Wangeacb7382023-06-05 12:53:54 +00001// 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
Pierre-Clément Tosi3d1c2e42024-08-13 22:29:46 +010017use aarch64_paging::paging::MemoryRegion;
Alice Wang4be4dd02023-06-07 07:50:40 +000018use core::ops::Range;
19
Alice Wangeacb7382023-06-05 12:53:54 +000020/// Flatten [[T; N]] into &[T]
21/// TODO: use slice::flatten when it graduates from experimental
22pub fn flatten<T, const N: usize>(original: &[[T; N]]) -> &[T] {
23 // SAFETY: no overflow because original (whose size is len()*N) is already in memory
24 let len = original.len() * N;
25 // SAFETY: [T] has the same layout as [T;N]
26 unsafe { core::slice::from_raw_parts(original.as_ptr().cast(), len) }
27}
28
29/// Computes the largest multiple of the provided alignment smaller or equal to the address.
30///
31/// Note: the result is undefined if alignment isn't a power of two.
32pub const fn unchecked_align_down(addr: usize, alignment: usize) -> usize {
33 addr & !(alignment - 1)
34}
35
36/// Computes the smallest multiple of the provided alignment larger or equal to the address.
37///
38/// Note: the result is undefined if alignment isn't a power of two and may wrap to 0.
39pub const fn unchecked_align_up(addr: usize, alignment: usize) -> usize {
40 unchecked_align_down(addr + alignment - 1, alignment)
41}
42
43/// Safe wrapper around unchecked_align_up() that validates its assumptions and doesn't wrap.
44pub const fn align_up(addr: usize, alignment: usize) -> Option<usize> {
45 if !alignment.is_power_of_two() {
46 None
47 } else if let Some(s) = addr.checked_add(alignment - 1) {
48 Some(unchecked_align_down(s, alignment))
49 } else {
50 None
51 }
52}
53
54/// Aligns the given address to the given alignment, if it is a power of two.
55///
56/// Returns `None` if the alignment isn't a power of two.
57#[allow(dead_code)] // Currently unused but might be needed again.
58const fn align_down(addr: usize, alignment: usize) -> Option<usize> {
59 if !alignment.is_power_of_two() {
60 None
61 } else {
62 Some(unchecked_align_down(addr, alignment))
63 }
64}
65
66/// Performs an integer division rounding up.
67///
68/// Note: Returns None if den isn't a power of two.
69pub const fn ceiling_div(num: usize, den: usize) -> Option<usize> {
70 let Some(r) = align_up(num, den) else {
71 return None;
72 };
73
74 r.checked_div(den)
75}
Alice Wang4be4dd02023-06-07 07:50:40 +000076
77/// Trait to check containment of one range within another.
78pub trait RangeExt {
79 /// Returns true if `self` is contained within the `other` range.
80 fn is_within(&self, other: &Self) -> bool;
81
82 /// Returns true if `self` overlaps with the `other` range.
83 fn overlaps(&self, other: &Self) -> bool;
84}
85
86impl<T: PartialOrd> RangeExt for Range<T> {
87 fn is_within(&self, other: &Self) -> bool {
88 self.start >= other.start && self.end <= other.end
89 }
90
91 fn overlaps(&self, other: &Self) -> bool {
92 self.start < other.end && other.start < self.end
93 }
94}
Pierre-Clément Tosi3d1c2e42024-08-13 22:29:46 +010095
96impl RangeExt for MemoryRegion {
97 fn is_within(&self, other: &Self) -> bool {
98 self.start() >= other.start() && self.end() <= other.end()
99 }
100
101 fn overlaps(&self, other: &Self) -> bool {
102 self.start() < other.end() && other.start() < self.end()
103 }
104}