Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do I mark a struct variable volatile? #960

Open
flaviojs opened this issue May 20, 2024 · 1 comment · May be fixed by #962
Open

How do I mark a struct variable volatile? #960

flaviojs opened this issue May 20, 2024 · 1 comment · May be fixed by #962

Comments

@flaviojs
Copy link
Contributor

flaviojs commented May 20, 2024

I am converting code from C to rust.
There is too much code so the transition must be done in steps.
Some struct variables have the volatile modifier, so I have to find a way to generate struct variables with volatile to ensure the C code behaves the same during the transition and everything can be tested.

On the rust side volatile behavior is achieved by using read_volatile/write_volatile manually or with a wrapper.
On the C side the variables must have the volatile modifier or I must cast every singe use of the variable to a volatile type (untested).

I am looking for a solution that avoids having to modify the original C code.
The ideal solution would be cbindgen generating the volatile modifier in specific struct variables, which allows me to change the structure at a later stage while ensuring the remaining C code behaves the same.

So, how do I mark a struct variable volatile?

@flaviojs flaviojs linked a pull request May 22, 2024 that will close this issue
@flaviojs
Copy link
Contributor Author

Temporary workaround for C:

lib.rs

/// cbindgen:no-export
#[repr(transparent)]
#[derive(Debug, Copy, Clone)]
pub struct Volatile<T>(pub T);
impl<T> Volatile<T> {
    pub fn get(&self) -> T {
        unsafe { std::ptr::read_volatile(std::ptr::addr_of!(self.0)) }
    }
    pub fn set(&mut self, x: T) {
        unsafe { std::ptr::write_volatile(std::ptr::addr_of_mut!(self.0), x) }
    }
}

#[repr(C)]
pub struct S {
    pub vi: Volatile<std::ffi::c_int>,
    pub vpi: Volatile<*mut std::ffi::c_int>,
}

#[no_mangle]
pub extern "C" fn _export(_: S) {}

cbindgen.toml

after_includes = """
// volatile types (cbindgen does not emit the C volatile type qualifier)
typedef volatile int Volatile_c_int;
typedef int *volatile Volatile_____c_int;
"""

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant