Last active
September 6, 2021 22:52
-
-
Save divi255/dead86a7923465285d4941367733d9a4 to your computer and use it in GitHub Desktop.
C driver vs Rust driver
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* build with | |
gcc -c -Wall -Werror -fpic -O3 driver.c | |
gcc -shared -o libmydriver.so -O3 driver.o | |
*/ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
struct Data { | |
char port[20]; | |
int status; | |
char value[256]; | |
}; | |
struct Data *Result; | |
int current_result = 0; | |
int get_bulk() { | |
int n = 100; | |
Result = malloc(n * sizeof(struct Data)); | |
for (int i=0; i<n; i++) { | |
int i_label = i + 1; | |
sprintf(Result[i].port, "PORT %u", i_label); | |
Result[i].status = i; | |
sprintf(Result[i].value, "VALUE FOR %u", i_label); | |
} | |
return n; | |
} | |
struct Data *get_next_result() { | |
return &Result[current_result++]; | |
} | |
void free_result() { | |
free(Result); | |
current_result = 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* build with | |
cargo build --release | |
*/ | |
type PortName = [i8; 20]; | |
type StateStatus = i32; | |
type StateValue = [i8; 256]; | |
#[derive(Debug)] | |
#[repr(C)] | |
pub struct Data { | |
port: PortName, | |
status: StateStatus, | |
value: StateValue, | |
} | |
impl Data { | |
fn new() -> Self { | |
Self { | |
port: [0; 20], | |
status: -0xff, | |
value: [0; 256], | |
} | |
} | |
} | |
struct DataResult { | |
data: Vec<Data>, | |
current: usize, | |
} | |
impl DataResult { | |
unsafe fn get_next(&mut self) -> &Data { | |
match self.data.get(self.current) { | |
Some(v) => { | |
self.current += 1; | |
v | |
} | |
None => panic!("Result overflow"), | |
} | |
} | |
fn clear(&mut self) { | |
self.data.clear(); | |
self.current = 0; | |
} | |
} | |
fn fill_c_string(s: &str, buf: &mut [i8]) { | |
let b = s.as_bytes(); | |
if b.len() > buf.len() - 1 { | |
panic!("string overflow"); | |
} else { | |
for i in 0..b.len() { | |
buf[i] = b[i] as i8; | |
} | |
buf[b.len()] = 0; | |
} | |
} | |
static mut D: DataResult = DataResult { | |
data: Vec::new(), | |
current: 0, | |
}; | |
#[no_mangle] | |
pub unsafe fn get_bulk() -> i32 { | |
let n = 100; | |
D.clear(); | |
for i in 0..n { | |
let i_label = i + 1; | |
let mut d = Data::new(); | |
d.status = i; | |
fill_c_string(&format!("PORT {}", i_label), &mut d.port); | |
fill_c_string(&format!("VALUE FOR {}", i_label), &mut d.value); | |
D.data.push(d); | |
} | |
n | |
} | |
#[no_mangle] | |
pub unsafe fn get_next_result<'a>() -> &'a Data { | |
D.get_next() | |
} | |
#[no_mangle] | |
pub unsafe fn free_result() { | |
D.clear(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
bingo! thanks man