Nothing to see here, move along
1use crate::object_tag::ObjectTag;
2
3pub const NONE_SENTINEL: u16 = 0xFFFF;
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: u16,
13 pub next_child: u16,
14 pub object_size: u16,
15 pub _reserved: [u8; 14],
16}
17
18const _: () = assert!(core::mem::size_of::<KernelObjectHeader>() == 32);
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; 14],
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) -> u16 {
47 self.parent_untyped
48 }
49
50 pub const fn next_child(&self) -> u16 {
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: u16) {
78 self.parent_untyped = idx;
79 }
80
81 pub fn set_next_child(&mut self, idx: u16) {
82 self.next_child = idx;
83 }
84}