solana_define_syscall/
lib.rs1pub mod definitions;
2
3#[cfg(target_feature = "static-syscalls")]
4#[macro_export]
5macro_rules! define_syscall {
6 (fn $name:ident($($arg:ident: $typ:ty),*) -> $ret:ty) => {
7 #[inline]
8 pub unsafe fn $name($($arg: $typ),*) -> $ret {
9 #[repr(usize)]
11 enum Syscall {
12 Code = $crate::sys_hash(stringify!($name)),
13 }
14
15 let syscall: extern "C" fn($($arg: $typ),*) -> $ret = core::mem::transmute(Syscall::Code);
16 syscall($($arg),*)
17 }
18
19 };
20 (fn $name:ident($($arg:ident: $typ:ty),*)) => {
21 define_syscall!(fn $name($($arg: $typ),*) -> ());
22 }
23}
24
25#[cfg(not(target_feature = "static-syscalls"))]
26#[macro_export]
27macro_rules! define_syscall {
28 (fn $name:ident($($arg:ident: $typ:ty),*) -> $ret:ty) => {
29 extern "C" {
30 pub fn $name($($arg: $typ),*) -> $ret;
31 }
32 };
33 (fn $name:ident($($arg:ident: $typ:ty),*)) => {
34 define_syscall!(fn $name($($arg: $typ),*) -> ());
35 }
36}
37
38#[cfg(target_feature = "static-syscalls")]
39pub const fn sys_hash(name: &str) -> usize {
40 murmur3_32(name.as_bytes(), 0) as usize
41}
42
43#[cfg(target_feature = "static-syscalls")]
44const fn murmur3_32(buf: &[u8], seed: u32) -> u32 {
45 const fn pre_mix(buf: [u8; 4]) -> u32 {
46 u32::from_le_bytes(buf)
47 .wrapping_mul(0xcc9e2d51)
48 .rotate_left(15)
49 .wrapping_mul(0x1b873593)
50 }
51
52 let mut hash = seed;
53
54 let mut i = 0;
55 while i < buf.len() / 4 {
56 let buf = [buf[i * 4], buf[i * 4 + 1], buf[i * 4 + 2], buf[i * 4 + 3]];
57 hash ^= pre_mix(buf);
58 hash = hash.rotate_left(13);
59 hash = hash.wrapping_mul(5).wrapping_add(0xe6546b64);
60
61 i += 1;
62 }
63
64 match buf.len() % 4 {
65 0 => {}
66 1 => {
67 hash = hash ^ pre_mix([buf[i * 4], 0, 0, 0]);
68 }
69 2 => {
70 hash = hash ^ pre_mix([buf[i * 4], buf[i * 4 + 1], 0, 0]);
71 }
72 3 => {
73 hash = hash ^ pre_mix([buf[i * 4], buf[i * 4 + 1], buf[i * 4 + 2], 0]);
74 }
75 _ => { }
76 }
77
78 hash = hash ^ buf.len() as u32;
79 hash = hash ^ (hash.wrapping_shr(16));
80 hash = hash.wrapping_mul(0x85ebca6b);
81 hash = hash ^ (hash.wrapping_shr(13));
82 hash = hash.wrapping_mul(0xc2b2ae35);
83 hash = hash ^ (hash.wrapping_shr(16));
84
85 hash
86}