faces_pfm/frame.rs
1use crate::PageFlags;
2
3/// Metadata associated with a single physical page frame.
4///
5/// This structure stores per‑frame information required by the page frame
6/// manager, such as flags, allocation order, linked list pointers for free
7/// lists, reference count, and a pointer to the kernel virtual address of
8/// the freelist header (if any). It is guaranteed to be 32‑byte aligned.
9#[derive(Debug, Default)]
10#[repr(C, align(32))]
11pub struct PageFrame {
12 /// Page state flags (locked, dirty, reserved, etc.).
13 pub flags: PageFlags,
14 /// Allocation order (2^order pages). 0 means a single 4 KiB page.
15 pub order: u8,
16 /// Index of the next page frame in a linked list (e.g., free list).
17 pub next: u32,
18 /// Index of the previous page frame in a linked list.
19 pub prev: u32,
20 /// Size class index for slab allocators (if used).
21 pub size_class: u8,
22 /// Reference count: number of users of this page frame.
23 pub rc: u16,
24 /// Kernel virtual address of the freelist header (or 0 if none).
25 pub freelist_va: usize,
26 /// Padding to maintain alignment and reserved for future use.
27 pub _pad: u8,
28}
29
30impl PageFrame {
31 /// Creates a new, zeroed (default) page frame metadata structure.
32 ///
33 /// All fields are initialised with their default values. The `flags`
34 /// field is cloned from a zeroed instance of `F`.
35 ///
36 /// # Panics
37 /// This function is `const` and will not panic.
38 pub const fn new() -> Self {
39 Self {
40 flags: (*unsafe { core::mem::transmute::<&usize, &PageFlags>(&0) }).clone(),
41 order: 0,
42 next: 0,
43 prev: 0,
44 size_class: 0,
45 rc: 0,
46 freelist_va: 0,
47 _pad: 0,
48 }
49 }
50
51 /// Resets the page frame metadata to its default state.
52 ///
53 /// This is equivalent to `*self = PageFrame::new()`. After calling this,
54 /// all flags are cleared, and counters are set to zero.
55 pub fn reset(&mut self) {
56 self.flags = (*unsafe { core::mem::transmute::<&usize, &PageFlags>(&0) }).clone();
57 self.order = 0;
58 self.next = 0;
59 self.prev = 0;
60 self.freelist_va = 0;
61 self.rc = 0;
62 self.size_class = 0;
63 }
64}