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