aligners/alignment/page.rs
1use super::Alignment;
2
3/// Alignment to page boundary.
4///
5/// Size is the size of a single page in the OS as returned by the
6/// [`page_size`] crate.
7///
8/// # Examples
9/// ```rust
10/// use page_size;
11/// use aligners::alignment::{self, Alignment};
12///
13/// assert_eq!(page_size::get(), alignment::Page::size());
14/// ```
15#[derive(Debug)]
16pub enum Page {}
17
18// SAFETY:
19// We check whether the size is power of two. The [`page_size`] crate caches the result
20// of its call, so it will not change, but I prefer not to rely on an external crate not changing
21// its implementation for safety.
22//
23// No sane platform would have a page size that is not a power of two, but better not to take chances.
24// This assertion will only be called once anyway.
25unsafe impl Alignment for Page {
26 #[inline]
27 fn size() -> usize {
28 use lazy_static::lazy_static;
29
30 lazy_static! {
31 static ref PAGE_SIZE: usize = {
32 let size = page_size::get();
33
34 if size.next_power_of_two() != size {
35 panic!(
36 "detected page size {size} that is not a power of two, this is unsupported"
37 );
38 }
39
40 size
41 };
42 }
43
44 *PAGE_SIZE
45 }
46}