gemachain_program/
program_memory.rs

1//! @brief Gemachain Rust-based BPF memory operations
2
3/// Memcpy
4///
5/// @param dst - Destination
6/// @param src - Source
7/// @param n - Number of bytes to copy
8#[inline]
9pub fn gema_memcpy(dst: &mut [u8], src: &[u8], n: usize) {
10    #[cfg(target_arch = "bpf")]
11    {
12        extern "C" {
13            fn gema_memcpy_(dst: *mut u8, src: *const u8, n: u64);
14        }
15        unsafe {
16            gema_memcpy_(dst.as_mut_ptr(), src.as_ptr(), n as u64);
17        }
18    }
19
20    #[cfg(not(target_arch = "bpf"))]
21    crate::program_stubs::gema_memcpy(dst.as_mut_ptr(), src.as_ptr(), n);
22}
23
24/// Memmove
25///
26/// @param dst - Destination
27/// @param src - Source
28/// @param n - Number of bytes to copy
29///
30/// # Safety
31#[inline]
32pub unsafe fn gema_memmove(dst: *mut u8, src: *mut u8, n: usize) {
33    #[cfg(target_arch = "bpf")]
34    {
35        extern "C" {
36            fn gema_memmove_(dst: *mut u8, src: *const u8, n: u64);
37        }
38        gema_memmove_(dst, src, n as u64);
39    }
40
41    #[cfg(not(target_arch = "bpf"))]
42    crate::program_stubs::gema_memmove(dst, src, n);
43}
44
45/// Memcmp
46///
47/// @param s1 - Slice to be compared
48/// @param s2 - Slice to be compared
49/// @param n - Number of bytes to compare
50#[inline]
51pub fn gema_memcmp(s1: &[u8], s2: &[u8], n: usize) -> i32 {
52    let mut result = 0;
53
54    #[cfg(target_arch = "bpf")]
55    {
56        extern "C" {
57            fn gema_memcmp_(s1: *const u8, s2: *const u8, n: u64, result: *mut i32);
58        }
59        unsafe {
60            gema_memcmp_(s1.as_ptr(), s2.as_ptr(), n as u64, &mut result as *mut i32);
61        }
62    }
63
64    #[cfg(not(target_arch = "bpf"))]
65    crate::program_stubs::gema_memcmp(s1.as_ptr(), s2.as_ptr(), n, &mut result as *mut i32);
66
67    result
68}
69
70/// Memset
71///
72/// @param s1 - Slice to be compared
73/// @param s2 - Slice to be compared
74/// @param n - Number of bytes to compare
75#[inline]
76pub fn gema_memset(s: &mut [u8], c: u8, n: usize) {
77    #[cfg(target_arch = "bpf")]
78    {
79        extern "C" {
80            fn gema_memset_(s: *mut u8, c: u8, n: u64);
81        }
82        unsafe {
83            gema_memset_(s.as_mut_ptr(), c, n as u64);
84        }
85    }
86
87    #[cfg(not(target_arch = "bpf"))]
88    crate::program_stubs::gema_memset(s.as_mut_ptr(), c, n);
89}