sel4_externally_shared/ops/
bytewise_ops.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//
// Copyright 2023, Colias Group, LLC
//
// SPDX-License-Identifier: BSD-2-Clause
//

use core::marker::PhantomData;
use core::mem;

use volatile::ops::{BulkOps, Ops, UnitaryOps};
use zerocopy::{FromBytes, Immutable, IntoBytes};

#[derive(Default, Copy, Clone)]
pub struct BytewiseOps<O> {
    _phantom: PhantomData<O>,
}

impl<O: Ops> Ops for BytewiseOps<O> {}

impl<O: BulkOps<u8>, T: FromBytes + IntoBytes + Immutable> UnitaryOps<T> for BytewiseOps<O> {
    unsafe fn read(src: *const T) -> T {
        let mut val = T::new_zeroed();
        let view = val.as_mut_bytes();
        unsafe { O::memcpy(view.as_mut_ptr(), src.cast(), mem::size_of::<T>()) };
        val
    }

    unsafe fn write(dst: *mut T, src: T) {
        let view = src.as_bytes();
        unsafe { O::memcpy(dst.cast(), view.as_ptr(), mem::size_of::<T>()) };
    }
}

impl<O: BulkOps<u8>, T: FromBytes + IntoBytes> BulkOps<T> for BytewiseOps<O> {
    unsafe fn memmove(dst: *mut T, src: *const T, count: usize) {
        unsafe { O::memmove(dst.cast(), src.cast(), count * mem::size_of::<T>()) }
    }

    unsafe fn memcpy(dst: *mut T, src: *const T, count: usize) {
        unsafe { O::memcpy(dst.cast(), src.cast(), count * mem::size_of::<T>()) }
    }

    unsafe fn memset(dst: *mut T, val: u8, count: usize) {
        unsafe { O::memset(dst.cast(), val, count * mem::size_of::<T>()) }
    }
}