Nothing to see here, move along meow
1use crate::object_tag::ObjectTag;
2
3pub const NONE_SENTINEL: u32 = u32::MAX;
4
5#[derive(Debug, Clone, Copy)]
6#[repr(C)]
7pub struct KernelObjectHeader {
8 pub tag: u8,
9 pub _pad: [u8; 3],
10 pub generation: u32,
11 pub ref_count: u32,
12 pub parent_untyped: u32,
13 pub next_child: u32,
14 pub object_size: u16,
15 pub _reserved: [u8; 2],
16}
17
18const _: () = assert!(core::mem::size_of::<KernelObjectHeader>() == 24);
19
20impl KernelObjectHeader {
21 pub const fn new(tag: ObjectTag, generation: u32, object_size: u16) -> Self {
22 Self {
23 tag: tag as u8,
24 _pad: [0; 3],
25 generation,
26 ref_count: 1,
27 parent_untyped: NONE_SENTINEL,
28 next_child: NONE_SENTINEL,
29 object_size,
30 _reserved: [0; 2],
31 }
32 }
33
34 pub const fn tag_byte(&self) -> u8 {
35 self.tag
36 }
37
38 pub const fn generation(&self) -> u32 {
39 self.generation
40 }
41
42 pub const fn ref_count(&self) -> u32 {
43 self.ref_count
44 }
45
46 pub const fn parent_untyped(&self) -> u32 {
47 self.parent_untyped
48 }
49
50 pub const fn next_child(&self) -> u32 {
51 self.next_child
52 }
53
54 pub const fn object_size(&self) -> u16 {
55 self.object_size
56 }
57
58 pub fn inc_ref(&mut self) -> Option<u32> {
59 match self.ref_count.checked_add(1) {
60 Some(new) => {
61 self.ref_count = new;
62 Some(new)
63 }
64 None => None,
65 }
66 }
67
68 pub fn dec_ref(&mut self) -> u32 {
69 self.ref_count = self.ref_count.saturating_sub(1);
70 self.ref_count
71 }
72
73 pub const fn is_stale(&self, expected_gen: u32) -> bool {
74 self.generation != expected_gen
75 }
76
77 pub fn set_parent_untyped(&mut self, idx: u32) {
78 self.parent_untyped = idx;
79 }
80
81 pub fn set_next_child(&mut self, idx: u32) {
82 self.next_child = idx;
83 }
84
85 pub fn bump_generation(&mut self) {
86 self.generation = self.generation.wrapping_add(1);
87 }
88
89 pub fn invalidate(&mut self) {
90 self.bump_generation();
91 self.ref_count = 0;
92 }
93}
94
95pub const fn phys_to_obj_link(phys: u64) -> u32 {
96 debug_assert!(phys.is_multiple_of(64), "phys_to_obj_link: unaligned");
97 (phys >> 6) as u32
98}
99
100pub const fn obj_link_to_phys(link: u32) -> u64 {
101 (link as u64) << 6
102}