Skip to content

Commit

Permalink
Merge pull request #291 from elBoberido/iox2-210-adjust-naming-conven…
Browse files Browse the repository at this point in the history
…tion-for-c-binding

[#210] Adjust naming convention and structuree in the remaining files
  • Loading branch information
elBoberido committed Jul 15, 2024
2 parents 1354b2b + e5a4110 commit 036de1e
Show file tree
Hide file tree
Showing 7 changed files with 335 additions and 248 deletions.
4 changes: 2 additions & 2 deletions examples/c/publish_subscribe/src/publisher.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
#include <stdio.h>

int main(void) {
iox2_node_builder_mut_h node_builder_handle = iox2_node_builder_new(NULL);
iox2_node_mut_h node_handle = NULL;
iox2_node_builder_h node_builder_handle = iox2_node_builder_new(NULL);
iox2_node_h node_handle = NULL;
int ret_val = iox2_node_builder_create(node_builder_handle, NULL, iox2_service_type_e_IPC, &node_handle);
if (ret_val != IOX2_OK) {
printf("Could not create node! Error code: %i", ret_val);
Expand Down
14 changes: 9 additions & 5 deletions iceoryx2-ffi/ffi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

- all constructs start with `iox2_`
- `structs` end with a `_t`
- handles end with a `_h` and are a type definition to a `struct iox2_foo_h_t;` as `pub type iox2_foo_h = *mut iox2_foo_h_t`
- immutable pointer to the Rust type end with a `_ptr` and are a type definition to a `struct iox2_foo_ptr_t;` as `pub type iox2_foo_ptr = *mut iox2_foo_ptr_t`
- owning handles end with a `_h` and are a type definition to a `struct iox2_foo_h_t;` as `pub type iox2_foo_h = *mut iox2_foo_h_t`
- non-owning handles end with a `_ref_h` and are a type definition to a `struct iox2_foo_ref_h_t;` as `pub type iox2_foo_ref_h = *mut iox2_foo_ref_h_t`
- immutable pointer to the Rust type end with a `_ptr` and are a type definition to a `struct iox2_foo_ptr_t;` as `pub type iox2_foo_ptr = *const iox2_foo_ptr_t`
- mutable pointer to the Rust type end with a `_mut_ptr` and are a type definition to a `struct iox2_foo_mut_ptr_t;` as `pub type iox2_foo_mut_ptr = *mut iox2_foo_mut_ptr_t`
- `enums` ends with a `_e`

Expand All @@ -12,7 +13,7 @@
The type erasure is usually done in two stages with `iox2_foo_storage_t` and `iox2_foo_t`.

The `iox2_foo_storage_t` is the storage for the Rust type `Option<Foo>` and must match the size and alignment of `Option<Foo>`.
If the internal storage must hold multiple types, the size and alignment is respectively the max value of the types.
If the internal storage must hold multiple types, a union can be used.
The struct is not supposed to be used standalone but always in combination with an `iox2_foo_t`.
Assuming the size is 160 and the alignment is 8, then the storage is defined as following
```rs
Expand Down Expand Up @@ -40,15 +41,18 @@ corresponding `iox2_foo_drop` shall be used to destruct the underlying Rust type
If the Rust API takes the ownership of `Foo`, the C API will also take the ownership of the handle and `iox2_foo_drop` shall not be
called.

When the handle is passed to a function, the ownership of the underlying data is moved to that specific function and the `*_h` handles
When the owning handle is passed to a function, the ownership of the underlying data is moved to that specific function and the `*_h` handles
as well as all the `*_ptr` related to that handle are invalid. Accessing the handles or pointer afterwards lead to undefined behavior.
The only exception are the `iox2_cast_*` functions which can be used to get `_ptr` and `_mut_ptr` pointer the the Rust type.
The only exception are the `iox2_cast_*` functions which can be used to get `_ptr` and `_mut_ptr` pointer the the Rust type or a non-owning `_ref_h` handle to the C struct.

The corresponding handle and pointer are defined like this
```rs
pub struct iox2_foo_h_t;
pub type iox2_foo_h = *mut iox2_foo_h_t;

pub struct iox2_foo_ref_h_t;
pub type iox2_foo_ref_h = *mut iox2_foo_ref_h_t;

pub struct iox2_foo_ptr_t;
pub type iox2_foo_ptr = *const iox2_foo_ptr_t;

Expand Down
4 changes: 2 additions & 2 deletions iceoryx2-ffi/ffi/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ use core::ffi::c_void;

// BEGIN type definition

pub type iox2_config_h = *const c_void;
pub type iox2_config_ptr = *const c_void;

// END type definition

// BEGIN C API
#[no_mangle]
pub extern "C" fn iox2_config_global_config() -> iox2_config_h {
pub extern "C" fn iox2_config_global_config() -> iox2_config_ptr {
Config::global_config() as *const _ as *const _
}

Expand Down
Loading

0 comments on commit 036de1e

Please sign in to comment.