Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions doc/release-notes/iceoryx2-unreleased.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@
NOTE: Add new entries sorted by issue number to minimize the possibility of
conflicts when merging.
-->

- Use 'NonNull' in 'elementary/BumpAllocator'
[#996](https://github.com/eclipse-iceoryx/iceoryx2/issues/996)

* Remove support for Bazel Workspaces
[#1263](https://github.com/eclipse-iceoryx/iceoryx2/issues/1263)
* Adjust test names to naming convention
Expand Down Expand Up @@ -108,3 +112,20 @@
set_log_level(LogLevel::Info);
info!("some log message")
```

1. Creating a BumpAllocator expects a `NonNull<u8>` instead of a mutable raw pointer.

```rust
// old
let mut memory = [0u8; 8192];
let allocator = BumpAllocator::new(memory.as_mut_ptr());

// new
let mut memory = [0u8; 8192];
let allocator = BumpAllocator::new(
core::ptr::NonNull::<u8>::new(memory.as_mut_ptr().cast()).expect(
"Precondition failed: Memory pointer is null"),
core::mem::size_of_val(&memory)
)
.unwrap();
```
4 changes: 4 additions & 0 deletions iceoryx2-bb/concurrency/src/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ impl<T> RefCell<T> {
pub fn borrow_mut(&self) -> RefMut<'_, T> {
self.0.borrow_mut()
}

pub fn replace(&self, t: T) -> T {
self.0.replace(t)
}
}

impl<T: Default> PlacementDefault for RefCell<T> {
Expand Down
22 changes: 20 additions & 2 deletions iceoryx2-bb/container/src/flatmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,16 @@ impl<K: Eq, V: Clone, const CAPACITY: usize> PlacementDefault for FixedSizeFlatM
unsafe fn placement_default(ptr: *mut Self) {
let map_ptr = core::ptr::addr_of_mut!((*ptr).map);
map_ptr.write(unsafe { RelocatableFlatMap::new_uninit(CAPACITY) });
let allocator = BumpAllocator::new((*ptr)._idx_to_data.as_mut_ptr().cast());

// SAFETY: Creating a pointer to an existing member is always not null
let data_ptr = unsafe {
core::ptr::NonNull::<u8>::new_unchecked((*ptr)._idx_to_data.as_mut_ptr().cast())
};

let allocator = BumpAllocator::new(
data_ptr,
size_of::<Self>() - core::mem::offset_of!(Self, _idx_to_data),
);
(*ptr)
.map
.init(&allocator)
Expand Down Expand Up @@ -715,7 +724,16 @@ impl<K: Eq, V: Clone, const CAPACITY: usize> FixedSizeFlatMap<K, V, CAPACITY> {
_data: MaybeUninit::uninit(),
_data_next_free_index: MaybeUninit::uninit(),
};
let allocator = BumpAllocator::new(new_self._idx_to_data.as_mut_ptr().cast());

// SAFETY: Creating a pointer to an existing member is always not null
let data_ptr = unsafe {
core::ptr::NonNull::<u8>::new_unchecked(new_self._idx_to_data.as_mut_ptr().cast())
};

let allocator = BumpAllocator::new(
data_ptr,
size_of::<Self>() - core::mem::offset_of!(Self, _idx_to_data),
);
unsafe {
new_self
.map
Expand Down
26 changes: 22 additions & 4 deletions iceoryx2-bb/container/src/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,12 @@
//! queue_memory: [const { MaybeUninit::uninit() }; QUEUE_CAPACITY] ,
//! };
//!
//! let allocator = BumpAllocator::new(new_self.queue_memory.as_mut_ptr().cast());
//! let allocator = BumpAllocator::new(
//! core::ptr::NonNull::<u8>::new(new_self.queue_memory.as_mut_ptr().cast())
//! .expect("Precondition failed: Pointer to memory is null"),
//! core::mem::size_of_val(&new_self.queue_memory)
//! );
//!
//! unsafe {
//! new_self.queue.init(&allocator).expect("Enough memory provided.")
//! };
Expand All @@ -103,7 +108,11 @@
//! const MEM_SIZE: usize = RelocatableQueue::<u128>::const_memory_size(QUEUE_CAPACITY);
//! let mut memory = [0u8; MEM_SIZE];
//!
//! let bump_allocator = BumpAllocator::new(memory.as_mut_ptr());
//! let bump_allocator = BumpAllocator::new(
//! core::ptr::NonNull::<u8>::new(memory.as_mut_ptr().cast())
//! .expect("Precondition failed: Pointer to memory is null"),
//! core::mem::size_of_val(&memory)
//! );
//!
//! let mut queue = unsafe { RelocatableQueue::<u128>::new_uninit(QUEUE_CAPACITY) };
//! unsafe { queue.init(&bump_allocator).expect("queue init failed") };
Expand Down Expand Up @@ -472,7 +481,11 @@ impl<T, const CAPACITY: usize> PlacementDefault for FixedSizeQueue<T, CAPACITY>
let state_ptr = core::ptr::addr_of_mut!((*ptr).state);
state_ptr.write(RelocatableQueue::new_uninit(CAPACITY));

let allocator = BumpAllocator::new((*ptr)._data.as_mut_ptr().cast());
// SAFETY: Creating a pointer to an existing member is always not null
let data_ptr =
unsafe { core::ptr::NonNull::<u8>::new_unchecked((*ptr)._data.as_mut_ptr().cast()) };

let allocator = BumpAllocator::new(data_ptr, core::mem::size_of_val((*ptr)._data.as_ref()));
(*ptr)
.state
.init(&allocator)
Expand All @@ -487,7 +500,12 @@ impl<T, const CAPACITY: usize> Default for FixedSizeQueue<T, CAPACITY> {
_data: unsafe { MaybeUninit::uninit().assume_init() },
};

let allocator = BumpAllocator::new(new_self._data.as_mut_ptr().cast());
// SAFETY: Creating a pointer to an existing member is always not null
let data_ptr =
unsafe { core::ptr::NonNull::<u8>::new_unchecked(new_self._data.as_mut_ptr().cast()) };

let allocator =
BumpAllocator::new(data_ptr, core::mem::size_of_val(new_self._data.as_ref()));
unsafe {
new_self
.state
Expand Down
22 changes: 20 additions & 2 deletions iceoryx2-bb/container/src/slotmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,16 @@ impl<T, const CAPACITY: usize> PlacementDefault for FixedSizeSlotMap<T, CAPACITY
unsafe fn placement_default(ptr: *mut Self) {
let state_ptr = core::ptr::addr_of_mut!((*ptr).state);
state_ptr.write(unsafe { RelocatableSlotMap::new_uninit(CAPACITY) });
let allocator = BumpAllocator::new((*ptr)._idx_to_data.as_mut_ptr().cast());

// SAFETY: Creating a pointer to an existing member is always not null
let data_ptr = unsafe {
core::ptr::NonNull::<u8>::new_unchecked((*ptr)._idx_to_data.as_mut_ptr().cast())
};

let allocator = BumpAllocator::new(
data_ptr,
size_of::<Self>() - core::mem::offset_of!(Self, _idx_to_data),
);
(*ptr)
.state
.init(&allocator)
Expand All @@ -621,7 +630,16 @@ impl<T, const CAPACITY: usize> Default for FixedSizeSlotMap<T, CAPACITY> {
state: unsafe { RelocatableSlotMap::new_uninit(CAPACITY) },
};

let allocator = BumpAllocator::new(new_self._idx_to_data.as_mut_ptr().cast());
// SAFETY: Creating a pointer to an existing member is always not null
let data_ptr = unsafe {
core::ptr::NonNull::<u8>::new_unchecked(new_self._idx_to_data.as_mut_ptr().cast())
};

let allocator = BumpAllocator::new(
data_ptr,
size_of::<Self>() - core::mem::offset_of!(Self, _idx_to_data),
);

unsafe {
new_self
.state
Expand Down
13 changes: 11 additions & 2 deletions iceoryx2-bb/container/src/string/relocatable_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@
//! str_memory: [const { MaybeUninit::uninit() }; STRING_CAPACITY + 1] ,
//! };
//!
//! let allocator = BumpAllocator::new(new_self.str_memory.as_mut_ptr().cast());
//! let allocator = BumpAllocator::new(
//! core::ptr::NonNull::<u8>::new(new_self.str_memory.as_mut_ptr().cast())
//! .expect("Precondition failed: Pointer to memory is null"),
//! core::mem::size_of_val(&new_self.str_memory)
//! );
//!
//! unsafe {
//! new_self.my_str.init(&allocator).expect("Enough memory provided.")
//! };
Expand All @@ -60,7 +65,11 @@
//! const MEM_SIZE: usize = RelocatableString::const_memory_size(STRING_CAPACITY);
//! let mut memory = [0u8; MEM_SIZE];
//!
//! let bump_allocator = BumpAllocator::new(memory.as_mut_ptr());
//! let bump_allocator = BumpAllocator::new(
//! core::ptr::NonNull::<u8>::new(memory.as_mut_ptr().cast())
//! .expect("Precondition failed: Pointer to memory is null"),
//! core::mem::size_of_val(&memory)
//! );
//!
//! let mut my_str = unsafe { RelocatableString::new_uninit(STRING_CAPACITY) };
//! unsafe { my_str.init(&bump_allocator).expect("string init failed") };
Expand Down
12 changes: 10 additions & 2 deletions iceoryx2-bb/container/src/vector/relocatable_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@
//! vec_memory: [const { MaybeUninit::uninit() }; VEC_CAPACITY] ,
//! };
//!
//! let allocator = BumpAllocator::new(new_self.vec_memory.as_mut_ptr().cast());
//! let allocator = BumpAllocator::new(
//! core::ptr::NonNull::<u8>::new(new_self.vec_memory.as_mut_ptr().cast())
//! .expect("Precondition failed: Pointer to memory is null"),
//! core::mem::size_of_val(&new_self.vec_memory)
//! );
//! unsafe {
//! new_self.vec.init(&allocator).expect("Enough memory provided.")
//! };
Expand All @@ -57,7 +61,11 @@
//! const MEM_SIZE: usize = RelocatableVec::<u128>::const_memory_size(VEC_CAPACITY);
//! let mut memory = [0u8; MEM_SIZE];
//!
//! let bump_allocator = BumpAllocator::new(memory.as_mut_ptr());
//! let bump_allocator = BumpAllocator::new(
//! core::ptr::NonNull::<u8>::new(memory.as_mut_ptr().cast())
//! .expect("Precondition failed: Pointer to memory is null"),
//! core::mem::size_of_val(&memory)
//! );
//!
//! let mut vec = unsafe { RelocatableVec::<u128>::new_uninit(VEC_CAPACITY) };
//! unsafe { vec.init(&bump_allocator).expect("vec init failed") };
Expand Down
6 changes: 5 additions & 1 deletion iceoryx2-bb/container/tests/flatmap_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,11 @@ mod flat_map {
fn double_init_call_causes_panic() {
const MEM_SIZE: usize = RelocatableFlatMap::<u8, u8>::const_memory_size(CAPACITY);
let mut memory = [0u8; MEM_SIZE];
let bump_allocator = BumpAllocator::new(memory.as_mut_ptr());
let bump_allocator = BumpAllocator::new(
core::ptr::NonNull::<u8>::new(memory.as_mut_ptr().cast())
.expect("Precondition failed: Pointer to memory is null"),
core::mem::size_of_val(&memory),
);

let mut sut = unsafe { RelocatableFlatMap::<u8, u8>::new_uninit(CAPACITY) };
unsafe { sut.init(&bump_allocator).expect("sut init failed") };
Expand Down
4 changes: 3 additions & 1 deletion iceoryx2-bb/container/tests/polymorphic_string_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ impl Test {
unsafe {
if (*self.allocator.get()).is_none() {
*self.allocator.get() = Some(Box::new(BumpAllocator::new(
(*self.raw_memory.get()).as_mut_ptr(),
core::ptr::NonNull::<u8>::new((*self.raw_memory.get()).as_mut_ptr().cast())
.expect("Precondition failed: Pointer to memory is null"),
core::mem::size_of_val((*self.raw_memory.get()).as_ref()),
)))
}
};
Expand Down
4 changes: 3 additions & 1 deletion iceoryx2-bb/container/tests/polymorphic_vec_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ impl Test {
unsafe {
if (*self.allocator.get()).is_none() {
*self.allocator.get() = Some(Box::new(BumpAllocator::new(
(*self.raw_memory.get()).as_mut_ptr(),
core::ptr::NonNull::<u8>::new((*self.raw_memory.get()).as_mut_ptr().cast())
.expect("Precondition failed: Pointer to memory is null"),
core::mem::size_of_val((*self.raw_memory.get()).as_ref()),
)))
}
};
Expand Down
18 changes: 15 additions & 3 deletions iceoryx2-bb/container/tests/queue_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ mod queue {
#[test]
fn relocatable_push_pop_works_with_uninitialized_memory() {
let mut memory = [0u8; 1024];
let allocator = BumpAllocator::new(memory.as_mut_ptr());
let allocator = BumpAllocator::new(
core::ptr::NonNull::<u8>::new(memory.as_mut_ptr().cast())
.expect("Precondition failed: Pointer to memory is null"),
core::mem::size_of_val(&memory),
);

let mut sut = unsafe { RelocatableQueue::<usize>::new_uninit(SUT_CAPACITY) };
unsafe { assert_that!(sut.init(&allocator), is_ok) };
Expand Down Expand Up @@ -53,7 +57,11 @@ mod queue {
#[test]
fn relocatable_clear_empties_queue() {
let mut memory = [0u8; 1024];
let allocator = BumpAllocator::new(memory.as_mut_ptr());
let allocator = BumpAllocator::new(
core::ptr::NonNull::<u8>::new(memory.as_mut_ptr().cast())
.expect("Precondition failed: Pointer to memory is null"),
core::mem::size_of_val(&memory),
);

let mut sut = unsafe { RelocatableQueue::<usize>::new_uninit(SUT_CAPACITY) };
unsafe { assert_that!(sut.init(&allocator), is_ok) };
Expand Down Expand Up @@ -332,7 +340,11 @@ mod queue {
fn double_init_call_causes_panic() {
const MEM_SIZE: usize = RelocatableQueue::<usize>::const_memory_size(SUT_CAPACITY);
let mut memory = [0u8; MEM_SIZE];
let bump_allocator = BumpAllocator::new(memory.as_mut_ptr());
let bump_allocator = BumpAllocator::new(
core::ptr::NonNull::<u8>::new(memory.as_mut_ptr().cast())
.expect("Precondition failed: Pointer to memory is null"),
core::mem::size_of_val(&memory),
);

let mut sut = unsafe { RelocatableQueue::<usize>::new_uninit(SUT_CAPACITY) };
unsafe { sut.init(&bump_allocator).expect("sut init failed") };
Expand Down
42 changes: 35 additions & 7 deletions iceoryx2-bb/container/tests/relocatable_vec_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ fn double_init_call_causes_panic() {
const CAPACITY: usize = 12;
const MEM_SIZE: usize = RelocatableVec::<u128>::const_memory_size(CAPACITY);
let mut memory = [0u8; MEM_SIZE];
let bump_allocator = BumpAllocator::new(memory.as_mut_ptr());
let bump_allocator = BumpAllocator::new(
core::ptr::NonNull::<u8>::new(memory.as_mut_ptr().cast())
.expect("Precondition failed: Pointer to data in memory is null"),
core::mem::size_of_val(&memory),
);
let mut sut = unsafe { RelocatableVec::<u128>::new_uninit(CAPACITY) };
unsafe { sut.init(&bump_allocator).expect("sut init failed") };

Expand All @@ -44,8 +48,16 @@ fn two_vectors_with_same_content_are_equal() {
const MEM_SIZE: usize = RelocatableVec::<usize>::const_memory_size(SUT_CAPACITY);
let mut memory_1 = [0u8; MEM_SIZE];
let mut memory_2 = [0u8; MEM_SIZE];
let bump_allocator_1 = BumpAllocator::new(memory_1.as_mut_ptr());
let bump_allocator_2 = BumpAllocator::new(memory_2.as_mut_ptr());
let bump_allocator_1 = BumpAllocator::new(
core::ptr::NonNull::<u8>::new(memory_1.as_mut_ptr().cast())
.expect("Precondition failed: Pointer to data in memory is null"),
core::mem::size_of_val(&memory_1),
);
let bump_allocator_2 = BumpAllocator::new(
core::ptr::NonNull::<u8>::new(memory_2.as_mut_ptr().cast())
.expect("Precondition failed: Pointer to data in memory is null"),
core::mem::size_of_val(&memory_2),
);
let mut sut_1 = unsafe { RelocatableVec::<usize>::new_uninit(SUT_CAPACITY) };
unsafe { sut_1.init(&bump_allocator_1).unwrap() };
let mut sut_2 = unsafe { RelocatableVec::<usize>::new_uninit(SUT_CAPACITY) };
Expand All @@ -65,8 +77,16 @@ fn two_vectors_with_different_content_are_not_equal() {
const MEM_SIZE: usize = RelocatableVec::<usize>::const_memory_size(SUT_CAPACITY);
let mut memory_1 = [0u8; MEM_SIZE];
let mut memory_2 = [0u8; MEM_SIZE];
let bump_allocator_1 = BumpAllocator::new(memory_1.as_mut_ptr());
let bump_allocator_2 = BumpAllocator::new(memory_2.as_mut_ptr());
let bump_allocator_1 = BumpAllocator::new(
core::ptr::NonNull::<u8>::new(memory_1.as_mut_ptr().cast())
.expect("Precondition failed: Pointer to data in memory is null"),
core::mem::size_of_val(&memory_1),
);
let bump_allocator_2 = BumpAllocator::new(
core::ptr::NonNull::<u8>::new(memory_2.as_mut_ptr().cast())
.expect("Precondition failed: Pointer to data in memory is null"),
core::mem::size_of_val(&memory_2),
);
let mut sut_1 = unsafe { RelocatableVec::<usize>::new_uninit(SUT_CAPACITY) };
unsafe { sut_1.init(&bump_allocator_1).unwrap() };
let mut sut_2 = unsafe { RelocatableVec::<usize>::new_uninit(SUT_CAPACITY) };
Expand All @@ -88,8 +108,16 @@ fn two_vectors_with_different_len_are_not_equal() {
const MEM_SIZE: usize = RelocatableVec::<usize>::const_memory_size(SUT_CAPACITY);
let mut memory_1 = [0u8; MEM_SIZE];
let mut memory_2 = [0u8; MEM_SIZE];
let bump_allocator_1 = BumpAllocator::new(memory_1.as_mut_ptr());
let bump_allocator_2 = BumpAllocator::new(memory_2.as_mut_ptr());
let bump_allocator_1 = BumpAllocator::new(
core::ptr::NonNull::<u8>::new(memory_1.as_mut_ptr().cast())
.expect("Precondition failed: Pointer to data in memory is null"),
core::mem::size_of_val(&memory_1),
);
let bump_allocator_2 = BumpAllocator::new(
core::ptr::NonNull::<u8>::new(memory_2.as_mut_ptr().cast())
.expect("Precondition failed: Pointer to data in memory is null"),
core::mem::size_of_val(&memory_2),
);
let mut sut_1 = unsafe { RelocatableVec::<usize>::new_uninit(SUT_CAPACITY) };
unsafe { sut_1.init(&bump_allocator_1).unwrap() };
let mut sut_2 = unsafe { RelocatableVec::<usize>::new_uninit(SUT_CAPACITY) };
Expand Down
6 changes: 5 additions & 1 deletion iceoryx2-bb/container/tests/slotmap_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,11 @@ mod slot_map {
fn double_init_call_causes_panic() {
const MEM_SIZE: usize = RelocatableSlotMap::<usize>::const_memory_size(SUT_CAPACITY);
let mut memory = [0u8; MEM_SIZE];
let bump_allocator = BumpAllocator::new(memory.as_mut_ptr());
let bump_allocator = BumpAllocator::new(
core::ptr::NonNull::<u8>::new(memory.as_mut_ptr().cast())
.expect("Precondition failed: Pointer to memory is null"),
core::mem::size_of_val(&memory),
);

let mut sut = unsafe { RelocatableSlotMap::<usize>::new_uninit(SUT_CAPACITY) };
unsafe { sut.init(&bump_allocator).expect("sut init failed") };
Expand Down
Loading