-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
get the C interop working in general
- Loading branch information
Showing
9 changed files
with
120 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
fn main() { | ||
cc::Build::new() | ||
.file("src/c_code/benchmarker.c") | ||
.compile("c_benchmarker"); | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#!/bin/bash | ||
|
||
# This script generates Rust types for a benchmarker from a C header file using Bindgen. | ||
|
||
if ! command -v bindgen &> /dev/null; then | ||
echo "Error: Bindgen is not found in your PATH." | ||
echo "Please install bindgen by running: \$ cargo install bindgen-cli" | ||
exit 1 | ||
fi | ||
|
||
bindgen src/c_code/benchmarker.h -o src/c_code/benchmarker.rs | ||
|
||
if [ $? -eq 0 ]; then | ||
echo "Bindgen completed successfully. Rust types generated in src/c_code/benchmarker.rs." | ||
else | ||
echo "Error: Bindgen encountered an issue while generating Rust types." | ||
exit 1 | ||
fi |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#include "benchmarker.h" | ||
|
||
struct tuple tuple_add(const struct tuple *a, const struct tuple *b) { | ||
struct tuple result = { a->a + b->a, a->b + b->b }; | ||
return result; | ||
} | ||
|
||
void inline_tuple_add(struct tuple *my, const struct tuple *other) { | ||
my->a += other->a; | ||
my->b += other->b; | ||
} | ||
|
||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#ifndef BLACKHEAP_BENCHMARKER_BENCHMARER_H | ||
#define BLACKHEAP_BENCHMARKER_BENCHMARER_H | ||
|
||
struct tuple { | ||
int a; | ||
int b; | ||
}; | ||
|
||
struct tuple tuple_add(const struct tuple *a, const struct tuple *b); | ||
void inline_tuple_add(struct tuple *my, const struct tuple *other); | ||
|
||
#endif |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* automatically generated by rust-bindgen 0.69.2 */ | ||
|
||
#[repr(C)] | ||
#[derive(Debug, Copy, Clone)] | ||
pub struct tuple { | ||
pub a: ::std::os::raw::c_int, | ||
pub b: ::std::os::raw::c_int, | ||
} | ||
#[test] | ||
fn bindgen_test_layout_tuple() { | ||
const UNINIT: ::std::mem::MaybeUninit<tuple> = ::std::mem::MaybeUninit::uninit(); | ||
let ptr = UNINIT.as_ptr(); | ||
assert_eq!( | ||
::std::mem::size_of::<tuple>(), | ||
8usize, | ||
concat!("Size of: ", stringify!(tuple)) | ||
); | ||
assert_eq!( | ||
::std::mem::align_of::<tuple>(), | ||
4usize, | ||
concat!("Alignment of ", stringify!(tuple)) | ||
); | ||
assert_eq!( | ||
unsafe { ::std::ptr::addr_of!((*ptr).a) as usize - ptr as usize }, | ||
0usize, | ||
concat!("Offset of field: ", stringify!(tuple), "::", stringify!(a)) | ||
); | ||
assert_eq!( | ||
unsafe { ::std::ptr::addr_of!((*ptr).b) as usize - ptr as usize }, | ||
4usize, | ||
concat!("Offset of field: ", stringify!(tuple), "::", stringify!(b)) | ||
); | ||
} | ||
extern "C" { | ||
pub fn tuple_add(a: *const tuple, b: *const tuple) -> tuple; | ||
} | ||
extern "C" { | ||
pub fn inline_tuple_add(my: *mut tuple, other: *const tuple); | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
pub mod benchmarker; | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn c_interop_works_const() { | ||
let tuple1 = benchmarker::tuple {a: 1, b: 2}; | ||
let tuple2 = benchmarker::tuple {a: 3, b: 4}; | ||
let result = unsafe { benchmarker::tuple_add(&tuple1, &tuple2) }; | ||
assert_eq!(result.a, 4); | ||
assert_eq!(result.b, 6); | ||
} | ||
|
||
#[test] | ||
fn c_interop_works_non_const() { | ||
let mut tuple1 = benchmarker::tuple {a: 1, b: 2}; | ||
let tuple2 = benchmarker::tuple {a: 3, b: 4}; | ||
unsafe { benchmarker::inline_tuple_add(&mut tuple1, &tuple2) }; | ||
assert_eq!(tuple1.a, 4); | ||
assert_eq!(tuple1.b, 6); | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
mod c_code; | ||
|
||
pub fn add(left: usize, right: usize) -> usize { | ||
left + right | ||
} | ||
|