ahrightos-kernel 0.1.0

RISC-V 64 Rust teaching kernel for AhRightOS.
Documentation
//! Memory intrinsics — replaces compiler-builtins-mem feature.
//!
//! These are the minimal set of memory functions required by the Rust
//! compiler when targeting no_std without compiler-builtins-mem.

/// Copy `n` bytes from `src` to `dest`.
///
/// # Safety
///
/// `dest` and `src` must be valid for `n` bytes, properly aligned for byte
/// access, and must not overlap.
#[no_mangle]
pub unsafe extern "C" fn memcpy(dest: *mut u8, src: *const u8, n: usize) -> *mut u8 {
    let mut i = 0;
    while i < n {
        unsafe {
            *dest.add(i) = *src.add(i);
        }
        i += 1;
    }
    dest
}

/// Fill `n` bytes at `dest` with byte value `c`.
///
/// # Safety
///
/// `dest` must be valid for writes of `n` bytes.
#[no_mangle]
pub unsafe extern "C" fn memset(dest: *mut u8, c: i32, n: usize) -> *mut u8 {
    let mut i = 0;
    while i < n {
        unsafe {
            *dest.add(i) = c as u8;
        }
        i += 1;
    }
    dest
}

/// Compare `n` bytes from `s1` and `s2`.
///
/// # Safety
///
/// `s1` and `s2` must each be valid for reads of `n` bytes.
#[no_mangle]
pub unsafe extern "C" fn memcmp(s1: *const u8, s2: *const u8, n: usize) -> i32 {
    let mut i = 0;
    while i < n {
        let a = unsafe { *s1.add(i) };
        let b = unsafe { *s2.add(i) };
        if a != b {
            return a as i32 - b as i32;
        }
        i += 1;
    }
    0
}

/// Move `n` bytes from `src` to `dest`, allowing overlap.
///
/// # Safety
///
/// `dest` and `src` must be valid for `n` bytes.
#[no_mangle]
pub unsafe extern "C" fn memmove(dest: *mut u8, src: *const u8, n: usize) -> *mut u8 {
    if dest as usize > src as usize {
        // Copy backwards to handle overlap safely
        let mut i = n;
        while i > 0 {
            i -= 1;
            unsafe {
                *dest.add(i) = *src.add(i);
            }
        }
    } else {
        let mut i = 0;
        while i < n {
            unsafe {
                *dest.add(i) = *src.add(i);
            }
            i += 1;
        }
    }
    dest
}