aboutsummaryrefslogtreecommitdiff
path: root/src/rust/c_string
diff options
context:
space:
mode:
Diffstat (limited to 'src/rust/c_string')
-rw-r--r--src/rust/c_string/Cargo.toml13
-rw-r--r--src/rust/c_string/ffi.rs19
-rw-r--r--src/rust/c_string/include.am12
3 files changed, 44 insertions, 0 deletions
diff --git a/src/rust/c_string/Cargo.toml b/src/rust/c_string/Cargo.toml
new file mode 100644
index 0000000000..dc7504856b
--- /dev/null
+++ b/src/rust/c_string/Cargo.toml
@@ -0,0 +1,13 @@
+[package]
+authors = ["The Tor Project"]
+version = "0.0.1"
+name = "c_string"
+
+[dependencies]
+libc = "0.2.22"
+
+[lib]
+name = "c_string"
+path = "ffi.rs"
+crate_type = ["rlib", "staticlib"]
+
diff --git a/src/rust/c_string/ffi.rs b/src/rust/c_string/ffi.rs
new file mode 100644
index 0000000000..edce250829
--- /dev/null
+++ b/src/rust/c_string/ffi.rs
@@ -0,0 +1,19 @@
+//! FFI functions, only to be called from C.
+//!
+//! This module provides the ability for C to free strings that have been
+//! allocated in Rust.
+
+extern crate libc;
+
+use libc::c_char;
+use std::ffi::CString;
+
+/// This allows strings allocated in Rust to be freed in Rust. Every string
+/// sent across the Rust/C FFI boundary should utilize this function for
+/// freeing strings allocated in Rust.
+#[no_mangle]
+pub extern "C" fn free_rust_str(ptr: *mut c_char) {
+ if !ptr.is_null() {
+ unsafe { CString::from_raw(ptr) };
+ }
+}
diff --git a/src/rust/c_string/include.am b/src/rust/c_string/include.am
new file mode 100644
index 0000000000..8e9229ae6a
--- /dev/null
+++ b/src/rust/c_string/include.am
@@ -0,0 +1,12 @@
+EXTRA_DIST +=\
+ src/rust/c_string/Cargo.toml \
+ src/rust/c_string/ffi.rs
+
+src/rust/target/release/@TOR_RUST_C_STRING_STATIC_NAME@: FORCE
+ ( cd "$(abs_top_srcdir)/src/rust/c_string" ; \
+ CARGO_TARGET_DIR="$(abs_top_builddir)/src/rust/target" \
+ CARGO_HOME="$(abs_top_builddir)/src/rust" \
+ $(CARGO) build --release --quiet $(CARGO_ONLINE) )
+
+FORCE:
+