Skip to content

Commit

Permalink
[#210] Use new proc-macro
Browse files Browse the repository at this point in the history
  • Loading branch information
elBoberido committed Jul 14, 2024
1 parent 4d333b5 commit f0c0530
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 208 deletions.
4 changes: 2 additions & 2 deletions iceoryx2-ffi/ffi-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,11 @@ pub fn iceoryx2_ffi(args: TokenStream, input: TokenStream) -> TokenStream {
unsafe { *self.value.as_option_mut() = Some(value) }
}

fn alloc() -> *mut #struct_name {
pub(crate) fn alloc() -> *mut #struct_name {
unsafe { ::std::alloc::alloc(::std::alloc::Layout::new::<#struct_name>()) as _ }
}

fn dealloc(storage: *mut #struct_name) {
pub(crate) fn dealloc(storage: *mut #struct_name) {
unsafe {
::std::alloc::dealloc(storage as _, ::core::alloc::Layout::new::<#struct_name>())
}
Expand Down
100 changes: 25 additions & 75 deletions iceoryx2-ffi/ffi/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ use crate::{
use iceoryx2::node::{NodeListFailure, NodeView};
use iceoryx2::prelude::*;
use iceoryx2_bb_elementary::static_assert::*;
use iceoryx2_ffi_macros::iceoryx2_ffi;

use core::ffi::{c_int, c_void};
use core::mem::{align_of, size_of, ManuallyDrop, MaybeUninit};
use std::alloc::{alloc, dealloc, Layout};
use core::mem::ManuallyDrop;

// BEGIN type definition

Expand Down Expand Up @@ -71,73 +71,29 @@ pub struct iox2_node_storage_t {
internal: [u8; 16], // magic number obtained with size_of::<Option<NodeUnion>>()
}

impl iox2_node_storage_t {
const fn assert_storage_layout() {
static_assert_ge::<
{ align_of::<iox2_node_storage_t>() },
{ align_of::<Option<NodeUnion>>() },
>();
static_assert_ge::<{ size_of::<iox2_node_storage_t>() }, { size_of::<Option<NodeUnion>>() }>(
);
}

fn init(&mut self, node: NodeUnion) {
iox2_node_storage_t::assert_storage_layout();

unsafe { &mut *(self as *mut Self).cast::<MaybeUninit<Option<NodeUnion>>>() }
.write(Some(node));
}

unsafe fn as_option_mut(&mut self) -> &mut Option<NodeUnion> {
&mut *(self as *mut Self).cast::<Option<NodeUnion>>()
}

unsafe fn as_option_ref(&self) -> &Option<NodeUnion> {
&*(self as *const Self).cast::<Option<NodeUnion>>()
}

unsafe fn as_mut(&mut self) -> &mut NodeUnion {
self.as_option_mut().as_mut().unwrap()
}

unsafe fn as_ref(&self) -> &NodeUnion {
self.as_option_ref().as_ref().unwrap()
}
}

#[repr(C)]
#[iceoryx2_ffi(NodeUnion)]
pub struct iox2_node_t {
/// cbindgen:rename=internal1
pub(crate) service_type: iox2_service_type_e,
/// cbindgen:rename=internal2
pub(crate) node: iox2_node_storage_t,
pub(crate) value: iox2_node_storage_t,
pub(crate) deleter: fn(*mut iox2_node_t),
}

impl iox2_node_t {
pub(crate) fn init(
&mut self,
service_type: iox2_service_type_e,
node: NodeUnion,
value: NodeUnion,
deleter: fn(*mut iox2_node_t),
) {
self.service_type = service_type;
self.node.init(node);
self.value.init(value);
self.deleter = deleter;
}

pub(crate) fn cast(node: iox2_node_h) -> *mut Self {
node as *mut _ as *mut Self
}

pub(crate) fn alloc() -> *mut iox2_node_t {
unsafe { alloc(Layout::new::<iox2_node_t>()) as *mut iox2_node_t }
}
pub(crate) fn dealloc(storage: *mut iox2_node_t) {
unsafe {
dealloc(storage as *mut _, Layout::new::<iox2_node_t>());
}
}
}

pub struct iox2_node_h_t;
Expand Down Expand Up @@ -192,13 +148,11 @@ pub type iox2_node_list_callback = extern "C" fn(
pub unsafe extern "C" fn iox2_node_name(node_handle: iox2_node_h) -> iox2_node_name_ptr {
debug_assert!(!node_handle.is_null());

let node_struct = &mut *iox2_node_t::cast(node_handle);
let node = &mut *iox2_node_t::cast(node_handle);

match node_struct.service_type {
iox2_service_type_e::IPC => node_struct.node.as_ref().ipc.name() as *const _ as *const _,
iox2_service_type_e::LOCAL => {
node_struct.node.as_ref().local.name() as *const _ as *const _
}
match node.service_type {
iox2_service_type_e::IPC => node.value.as_ref().ipc.name() as *const _ as *const _,
iox2_service_type_e::LOCAL => node.value.as_ref().local.name() as *const _ as *const _,
}
}

Expand All @@ -211,13 +165,11 @@ pub unsafe extern "C" fn iox2_node_name(node_handle: iox2_node_h) -> iox2_node_n
pub unsafe extern "C" fn iox2_node_config(node_handle: iox2_node_h) -> iox2_config_ptr {
debug_assert!(!node_handle.is_null());

let node_struct = &mut *iox2_node_t::cast(node_handle);
let node = &mut *iox2_node_t::cast(node_handle);

match node_struct.service_type {
iox2_service_type_e::IPC => node_struct.node.as_ref().ipc.config() as *const _ as *const _,
iox2_service_type_e::LOCAL => {
node_struct.node.as_ref().local.config() as *const _ as *const _
}
match node.service_type {
iox2_service_type_e::IPC => node.value.as_ref().ipc.config() as *const _ as *const _,
iox2_service_type_e::LOCAL => node.value.as_ref().local.config() as *const _ as *const _,
}
}

Expand Down Expand Up @@ -371,17 +323,17 @@ pub unsafe extern "C" fn iox2_node_service_builder(
pub unsafe extern "C" fn iox2_node_drop(node_handle: iox2_node_h) {
debug_assert!(!node_handle.is_null());

let node_struct = &mut *iox2_node_t::cast(node_handle);
let node = &mut *iox2_node_t::cast(node_handle);

match node_struct.service_type {
match node.service_type {
iox2_service_type_e::IPC => {
ManuallyDrop::drop(&mut node_struct.node.as_mut().ipc);
ManuallyDrop::drop(&mut node.value.as_mut().ipc);
}
iox2_service_type_e::LOCAL => {
ManuallyDrop::drop(&mut node_struct.node.as_mut().local);
ManuallyDrop::drop(&mut node.value.as_mut().local);
}
}
(node_struct.deleter)(node_struct);
(node.deleter)(node);
}

// END C API
Expand All @@ -391,12 +343,6 @@ mod test {
use crate::*;
use iceoryx2_bb_testing::assert_that;

#[test]
fn assert_storage_sizes() {
// all const functions; if it compiles, the storage size is sufficient
const _STORAGE_LAYOUT_CHECK: () = iox2_node_storage_t::assert_storage_layout();
}

fn create_sut_node() -> iox2_node_h {
unsafe {
let node_builder_handle = iox2_node_builder_new(std::ptr::null_mut());
Expand Down Expand Up @@ -444,7 +390,11 @@ mod test {
fn basic_node_config_test() {
unsafe {
let node_handle = create_sut_node();
let expected_config = (*iox2_node_t::cast(node_handle)).node.as_ref().ipc.config();
let expected_config = (*iox2_node_t::cast(node_handle))
.value
.as_ref()
.ipc
.config();

let config = iox2_node_config(node_handle);

Expand All @@ -458,7 +408,7 @@ mod test {
fn basic_node_name_test() {
unsafe {
let node_handle = create_sut_node();
let expected_node_name = (*iox2_node_t::cast(node_handle)).node.as_ref().ipc.name();
let expected_node_name = (*iox2_node_t::cast(node_handle)).value.as_ref().ipc.name();
assert_that!(expected_node_name.as_str(), eq("hypnotoad"));

let node_name = iox2_node_name(node_handle);
Expand Down
72 changes: 5 additions & 67 deletions iceoryx2-ffi/ffi/src/node_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ use crate::{
use iceoryx2::node::NodeCreationFailure;
use iceoryx2::prelude::*;
use iceoryx2_bb_elementary::static_assert::*;
use iceoryx2_ffi_macros::iceoryx2_ffi;

use core::ffi::c_int;
use core::mem::{align_of, size_of, MaybeUninit};
use std::alloc::{alloc, dealloc, Layout};

// BEGIN types definition

Expand Down Expand Up @@ -51,46 +50,10 @@ pub struct iox2_node_builder_storage_t {
internal: [u8; 18432], // magic number obtained with size_of::<Option<NodeBuilder>>()
}

impl iox2_node_builder_storage_t {
const fn assert_storage_layout() {
static_assert_ge::<
{ align_of::<iox2_node_builder_storage_t>() },
{ align_of::<Option<NodeBuilder>>() },
>();
static_assert_ge::<
{ size_of::<iox2_node_builder_storage_t>() },
{ size_of::<Option<NodeBuilder>>() },
>();
}

fn init(&mut self, node_builder: NodeBuilder) {
iox2_node_builder_storage_t::assert_storage_layout();

unsafe { &mut *(self as *mut Self).cast::<MaybeUninit<Option<NodeBuilder>>>() }
.write(Some(node_builder));
}

unsafe fn as_option_mut(&mut self) -> &mut Option<NodeBuilder> {
&mut *(self as *mut Self).cast::<Option<NodeBuilder>>()
}

unsafe fn _os_option_ref(&self) -> &Option<NodeBuilder> {
&*(self as *const Self).cast::<Option<NodeBuilder>>()
}

unsafe fn _as_mut(&mut self) -> &mut NodeBuilder {
self.as_option_mut().as_mut().unwrap()
}

unsafe fn _as_ref(&self) -> &NodeBuilder {
self._os_option_ref().as_ref().unwrap()
}
}

#[repr(C)]
#[iceoryx2_ffi(NodeBuilder)]
pub struct iox2_node_builder_t {
/// cbindgen:rename=internal
node_builder: iox2_node_builder_storage_t,
value: iox2_node_builder_storage_t,
deleter: fn(*mut iox2_node_builder_t),
}

Expand All @@ -101,23 +64,6 @@ impl iox2_node_builder_t {
pub(crate) fn cast_from_ref(node_builder: iox2_node_builder_ref_h) -> *mut Self {
node_builder as *mut _ as *mut Self
}

pub(crate) fn take(&mut self) -> Option<NodeBuilder> {
unsafe { self.node_builder.as_option_mut().take() }
}

pub(crate) fn set(&mut self, node_builder: NodeBuilder) {
unsafe { *self.node_builder.as_option_mut() = Some(node_builder) }
}

fn alloc() -> *mut iox2_node_builder_t {
unsafe { alloc(Layout::new::<iox2_node_builder_t>()) as *mut iox2_node_builder_t }
}
fn dealloc(storage: *mut iox2_node_builder_t) {
unsafe {
dealloc(storage as *mut _, Layout::new::<iox2_node_builder_t>());
}
}
}

pub struct iox2_node_builder_h_t;
Expand Down Expand Up @@ -159,9 +105,7 @@ pub unsafe extern "C" fn iox2_node_builder_new(
debug_assert!(!node_builder_struct_ptr.is_null());

(*node_builder_struct_ptr).deleter = deleter;
(*node_builder_struct_ptr)
.node_builder
.init(NodeBuilder::new());
(*node_builder_struct_ptr).value.init(NodeBuilder::new());

node_builder_struct_ptr as *mut _ as *mut _
}
Expand Down Expand Up @@ -235,7 +179,7 @@ unsafe fn iox2_node_builder_drop(node_builder_handle: iox2_node_builder_h) {
debug_assert!(!node_builder_handle.is_null());

let node_builder_struct = &mut (*iox2_node_builder_t::cast(node_builder_handle));
std::ptr::drop_in_place(node_builder_struct.node_builder.as_option_mut() as *mut _);
std::ptr::drop_in_place(node_builder_struct.value.as_option_mut() as *mut _);
(node_builder_struct.deleter)(node_builder_struct);
}

Expand Down Expand Up @@ -308,12 +252,6 @@ mod test {
use crate::*;
use iceoryx2_bb_testing::assert_that;

#[test]
fn assert_storage_sizes() {
// all const functions; if it compiles, the storage size is sufficient
const _STORAGE_LAYOUT_CHECK: () = iox2_node_builder_storage_t::assert_storage_layout();
}

#[test]
fn basic_node_builder_api_test() {
unsafe {
Expand Down
Loading

0 comments on commit f0c0530

Please sign in to comment.