From e49ef5e3855a150656e02c6c56011c8d392dada0 Mon Sep 17 00:00:00 2001 From: sewer56 Date: Wed, 6 Sep 2023 10:18:04 +0100 Subject: [PATCH] Changed: Made the C API public for Rust --- src-rust/Cargo.lock | 2 +- src-rust/Cargo.toml | 2 +- src-rust/src/c/buffers_c_buffers.rs | 32 +++++++++---------- .../src/internal/memory_mapped_file_unix.rs | 2 +- src-rust/src/lib.rs | 9 ++++-- 5 files changed, 24 insertions(+), 23 deletions(-) diff --git a/src-rust/Cargo.lock b/src-rust/Cargo.lock index 88e7259..82ce433 100644 --- a/src-rust/Cargo.lock +++ b/src-rust/Cargo.lock @@ -906,7 +906,7 @@ checksum = "4bf2521270932c3c7bed1a59151222bd7643c79310f2916f01925e1e16255698" [[package]] name = "reloaded-memory-buffers" -version = "3.0.5" +version = "3.1.0" dependencies = [ "criterion", "dirs", diff --git a/src-rust/Cargo.toml b/src-rust/Cargo.toml index fb41d62..5e51e10 100644 --- a/src-rust/Cargo.toml +++ b/src-rust/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "reloaded-memory-buffers" -version = "3.0.5" +version = "3.1.0" edition = "2021" authors = [ "sewer56" ] description = "Shared, Concurrent, Permanent Memory Allocator tied to Process Lifetime" diff --git a/src-rust/src/c/buffers_c_buffers.rs b/src-rust/src/c/buffers_c_buffers.rs index 3fbd507..563952e 100644 --- a/src-rust/src/c/buffers_c_buffers.rs +++ b/src-rust/src/c/buffers_c_buffers.rs @@ -187,8 +187,10 @@ pub extern "C" fn free_string(s: *mut c_char) { /// Frees an error string returned from the library. #[no_mangle] -pub unsafe extern "C" fn free_locator_item(item: *mut LocatorItem) { - (*item).unlock(); +pub extern "C" fn free_locator_item(item: *mut LocatorItem) { + unsafe { + (*item).unlock(); + } } /// Frees a private allocation returned from the library. @@ -209,7 +211,7 @@ pub extern "C" fn free_allocation_result(item: AllocationResult) { /// Frees a get buffer result returned from the 'buffers' operation. #[no_mangle] -pub unsafe extern "C" fn free_get_buffer_result(item: GetBufferResult) { +pub extern "C" fn free_get_buffer_result(item: GetBufferResult) { if item.is_ok { free_locator_item(item.ok); } else { @@ -367,14 +369,12 @@ mod tests { if result.is_ok { // Check that the address is aligned as expected. - unsafe { - assert_eq!( - crate::c::buffers_c_locatoritem::locatoritem_min_address(result.ok) - % alignment as usize, - 0 - ); - free_get_buffer_result(result); - } + assert_eq!( + crate::c::buffers_c_locatoritem::locatoritem_min_address(result.ok) + % alignment as usize, + 0 + ); + free_get_buffer_result(result); } else { // Handle the error (just print here). println!("Error getting buffer: {:?}", result.err); @@ -400,12 +400,10 @@ mod tests { assert!(result.is_ok); - unsafe { - assert!(locatoritem_bytes_left(result.ok) >= SIZE as u32); + assert!(locatoritem_bytes_left(result.ok) >= SIZE as u32); - let offset = (locatoritem_min_address(result.ok) as i64 - base_address as i64).abs(); - assert!(offset < (i32::MAX as i64)); - free_get_buffer_result(result); - } + let offset = (locatoritem_min_address(result.ok) as i64 - base_address as i64).abs(); + assert!(offset < (i32::MAX as i64)); + free_get_buffer_result(result); } } diff --git a/src-rust/src/internal/memory_mapped_file_unix.rs b/src-rust/src/internal/memory_mapped_file_unix.rs index deabd5d..45f1f9e 100644 --- a/src-rust/src/internal/memory_mapped_file_unix.rs +++ b/src-rust/src/internal/memory_mapped_file_unix.rs @@ -25,7 +25,7 @@ pub struct UnixMemoryMappedFile { impl UnixMemoryMappedFile { pub fn new(name: &str, length: usize) -> UnixMemoryMappedFile { let mut new_name = String::with_capacity(BASE_DIR.len() + name.len()); - new_name.push_str(&BASE_DIR); + new_name.push_str(BASE_DIR); new_name.push_str(name); let file_name = CString::new(new_name.to_string()).expect("CString::new failed"); diff --git a/src-rust/src/lib.rs b/src-rust/src/lib.rs index 160592b..2f80ac9 100644 --- a/src-rust/src/lib.rs +++ b/src-rust/src/lib.rs @@ -121,10 +121,13 @@ pub(crate) mod utilities { pub mod linux_map_parser; } +/// Provides a C interface to the library. pub mod c { - mod buffers_c_buffers; - mod buffers_c_locatoritem; - mod buffers_fnptr; + #[allow(clippy::not_unsafe_ptr_arg_deref)] + pub mod buffers_c_buffers; + #[allow(clippy::not_unsafe_ptr_arg_deref)] + pub mod buffers_c_locatoritem; + pub mod buffers_fnptr; } pub mod buffers;