Skip to content

Commit

Permalink
[eclipse-iceoryx#224] Implement placement new for unions and unnamed …
Browse files Browse the repository at this point in the history
…structs
  • Loading branch information
elfenpiff committed Jun 1, 2024
1 parent 0c2acb0 commit 8f02d2e
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 8 deletions.
26 changes: 24 additions & 2 deletions iceoryx2-bb/derive-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ pub fn placement_default_derive(input: TokenStream) -> TokenStream {
let field_inits = fields_named.named.iter().map(|f| {
let name = &f.ident;
quote! {
PlacementDefault::placement_default(&mut (*ptr).#name);
let field_address = core::ptr::addr_of_mut!((*ptr).#name);
PlacementDefault::placement_default(field_address);
//PlacementDefault::placement_default(&mut (*ptr).#name);
}
});

Expand All @@ -25,7 +27,27 @@ pub fn placement_default_derive(input: TokenStream) -> TokenStream {
}
}
}
_ => unimplemented!(),
Fields::Unnamed(ref fields_unnamed) => {
let field_inits = fields_unnamed.unnamed.iter().map(|f| {
let name = &f.ident;
quote! {
let field_address = core::ptr::addr_of_mut!((*ptr).#name);
PlacementDefault::placement_default(field_address);
}
});

quote! {
unsafe fn placement_default(ptr: *mut #name) {
#(#field_inits)*
}
}
}
Fields::Unit => {
quote! {
unsafe fn placement_default(ptr: *mut #name) {
}
}
}
},
_ => unimplemented!(),
};
Expand Down
51 changes: 45 additions & 6 deletions iceoryx2-bb/trait-tests/tests/placement_new_tests.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,62 @@
#[cfg(test)]
mod placement_new {
use std::alloc::{alloc, dealloc, Layout};
use std::{
alloc::{alloc, dealloc, Layout},
sync::atomic::{AtomicUsize, Ordering},
};

use iceoryx2_bb_derive_macros::PlacementDefault;
use iceoryx2_bb_elementary::placement_new::PlacementDefault;
use iceoryx2_bb_testing::assert_that;

#[derive(Default, PlacementDefault)]
static DEFAULT_CTOR_COUNT: AtomicUsize = AtomicUsize::new(0);

#[derive(Copy, Clone)]
struct UnitStruct;

impl PlacementDefault for UnitStruct {
unsafe fn placement_default(_ptr: *mut Self) {
DEFAULT_CTOR_COUNT.fetch_add(1, Ordering::Relaxed);
}
}

struct Fuu(i32);

impl PlacementDefault for Fuu {
unsafe fn placement_default(ptr: *mut Self) {
DEFAULT_CTOR_COUNT.fetch_add(1, Ordering::Relaxed);
ptr.write(Self(0))
}
}

struct Bar {
value: u64,
}

impl PlacementDefault for Bar {
unsafe fn placement_default(ptr: *mut Self) {
DEFAULT_CTOR_COUNT.fetch_add(1, Ordering::Relaxed);
ptr.write(Self { value: 123 })
}
}

#[derive(PlacementDefault)]
struct TestStruct {
a: i32,
b: u64,
value1: UnitStruct,
value2: Fuu,
value3: Bar,
}

#[test]
fn whatever() {
fn placement_default_works() {
DEFAULT_CTOR_COUNT.store(0, Ordering::Relaxed);

let layout = Layout::new::<TestStruct>();
let memory = unsafe { alloc(layout) } as *mut TestStruct;
let sut = TestStruct::default();
unsafe { TestStruct::placement_default(memory) };

assert_that!(DEFAULT_CTOR_COUNT.load(Ordering::Relaxed), eq 3);

unsafe { dealloc(memory.cast(), layout) };
}
}

0 comments on commit 8f02d2e

Please sign in to comment.