Skip to content

Commit

Permalink
get the C interop working in general
Browse files Browse the repository at this point in the history
  • Loading branch information
lquenti committed Jan 21, 2024
1 parent e56a81c commit 9480197
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ There is a **big recode** right now. But how does one eat an elephant...
- Has to be resumeable
- be able to re-analyze raw data again
- Provide machine generated README in the data
- `blackheap-benchmarker` does not rely on bindgen compile time since this would create LLVM as dep
- [ ] Have a way to be sure that it isnt leaking memory
- [ ] Also provide a standalone binary for the benchmarker?
- [ ] Find a way to have C linting (all warnings, formatter, pedantic C standard)
- [ ] Design a high level architecture based on the requirements
- [ ] Start writing the benchmarker
- [ ] Figure out how C/Rust interop work with a simple test (do some computations in C, test in Rust)
Expand Down
3 changes: 3 additions & 0 deletions blackheap-benchmarker/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]

[build-dependencies]
cc = "1.0"
5 changes: 5 additions & 0 deletions blackheap-benchmarker/build.rs
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");
}
18 changes: 18 additions & 0 deletions blackheap-benchmarker/regenerate_types.sh
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
13 changes: 13 additions & 0 deletions blackheap-benchmarker/src/c_code/benchmarker.c
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;
}


12 changes: 12 additions & 0 deletions blackheap-benchmarker/src/c_code/benchmarker.h
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
39 changes: 39 additions & 0 deletions blackheap-benchmarker/src/c_code/benchmarker.rs
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);
}
24 changes: 24 additions & 0 deletions blackheap-benchmarker/src/c_code/mod.rs
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);
}
}
2 changes: 2 additions & 0 deletions blackheap-benchmarker/src/lib.rs
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
}
Expand Down

0 comments on commit 9480197

Please sign in to comment.