Skip to content

Commit

Permalink
Changed: Made the C API public for Rust
Browse files Browse the repository at this point in the history
  • Loading branch information
Sewer56 committed Sep 6, 2023
1 parent 012c61a commit e49ef5e
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src-rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src-rust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
32 changes: 15 additions & 17 deletions src-rust/src/c/buffers_c_buffers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 {
Expand Down Expand Up @@ -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);
Expand All @@ -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);
}
}
2 changes: 1 addition & 1 deletion src-rust/src/internal/memory_mapped_file_unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
9 changes: 6 additions & 3 deletions src-rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

0 comments on commit e49ef5e

Please sign in to comment.