Skip to main content

ukernel_sys/
subsystem.rs

1//! Subsystem identity for loadable domain types.
2//!
3//! Each domain type (POSIX, VMM, mainframe, etc.) is identified by a
4//! [`SubsystemId`] — a deterministic FNV-1a hash of the subsystem name.
5//! The kernel has no compile-time knowledge of subsystem types. New
6//! subsystems can be added without modifying the kernel.
7//!
8//! # Example
9//!
10//! ```
11//! use ukernel_sys::SubsystemId;
12//!
13//! const POSIX: SubsystemId = SubsystemId::from_name(b"posix");
14//! const VMM: SubsystemId = SubsystemId::from_name(b"vmm");
15//!
16//! // Deterministic — same name always produces the same ID
17//! assert_eq!(POSIX, SubsystemId::from_name(b"posix"));
18//! assert_ne!(POSIX, VMM);
19//! ```
20
21/// Subsystem identifier derived from name via FNV-1a hash.
22///
23/// Deterministic: `"posix"` always hashes to the same value.
24#[derive(Copy, Clone, Eq, PartialEq, Debug, Hash)]
25pub struct SubsystemId(pub u64);
26
27impl SubsystemId {
28    /// Create from a name using const-evaluable FNV-1a hash.
29    pub const fn from_name(name: &[u8]) -> Self {
30        let mut hash: u64 = 0xcbf29ce484222325; // FNV offset basis
31        let mut i = 0;
32        while i < name.len() {
33            hash ^= name[i] as u64;
34            hash = hash.wrapping_mul(0x100000001b3); // FNV prime
35            i += 1;
36        }
37        Self(hash)
38    }
39
40    /// Raw hash value.
41    #[inline]
42    pub const fn as_u64(self) -> u64 {
43        self.0
44    }
45}