Skip to content

Commit

Permalink
[eclipse-iceoryx#497] Test increasing chunk size allocation
Browse files Browse the repository at this point in the history
  • Loading branch information
elfenpiff committed Nov 14, 2024
1 parent 743337d commit d35e133
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 7 deletions.
3 changes: 2 additions & 1 deletion iceoryx2-cal/src/resizable_shared_memory/dynamic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,8 @@ where
return Ok(ptr);
}
Err(ShmAllocationError::AllocationError(AllocationError::OutOfMemory))
| Err(ShmAllocationError::ExceedsMaxSupportedAlignment) => {
| Err(ShmAllocationError::ExceedsMaxSupportedAlignment)
| Err(ShmAllocationError::AllocationError(AllocationError::SizeTooLarge)) => {
self.create_resized_segment(&entry.shm, layout)?;
}
Err(e) => return Err(e.into()),
Expand Down
1 change: 1 addition & 0 deletions iceoryx2-cal/src/shm_allocator/bump_allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ impl ShmAllocator for BumpAllocator {
AllocationStrategy::PowerOfTwo => {
(current_payload_size + layout.size()).next_power_of_two()
}
AllocationStrategy::Static => current_payload_size,
};

SharedMemorySetupHint {
Expand Down
15 changes: 10 additions & 5 deletions iceoryx2-cal/src/shm_allocator/pool_allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,22 @@ impl ShmAllocator for PoolAllocator {
AllocationStrategy::PowerOfTwo => {
(self.allocator.number_of_buckets() + 1).next_power_of_two()
}
AllocationStrategy::Static => self.allocator.number_of_buckets(),
}
} else {
self.number_of_buckets()
};

let adjusted_layout = if current_layout != layout {
unsafe {
Layout::from_size_align_unchecked(
layout.size().next_power_of_two(),
layout.align().next_power_of_two(),
)
match strategy {
AllocationStrategy::Static => current_layout,
AllocationStrategy::BestFit => layout,
AllocationStrategy::PowerOfTwo => unsafe {
Layout::from_size_align_unchecked(
layout.size().next_power_of_two(),
layout.align().next_power_of_two(),
)
},
}
} else {
layout
Expand Down
70 changes: 69 additions & 1 deletion iceoryx2-cal/tests/resizable_shared_memory_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ mod resizable_shared_memory {
use iceoryx2_bb_testing::assert_that;
use iceoryx2_cal::named_concept::*;
use iceoryx2_cal::resizable_shared_memory::{self, *};
use iceoryx2_cal::shm_allocator::AllocationStrategy;
use iceoryx2_cal::testing::*;
use iceoryx2_cal::{
shared_memory::SharedMemory,
Expand Down Expand Up @@ -147,8 +148,75 @@ mod resizable_shared_memory {
assert_that!(sut_creator.number_of_active_segments(), eq 1);
}

fn allocate_more_than_hinted_with_increasing_chunk_size_works<
Shm: SharedMemory<DefaultAllocator>,
Sut: ResizableSharedMemory<DefaultAllocator, Shm>,
>(
strategy: AllocationStrategy,
) {
const NUMBER_OF_REALLOCATIONS: usize = 128;
let storage_name = generate_name();
let config = generate_isolated_config::<Sut>();

let sut_creator = Sut::Builder::new(&storage_name)
.config(&config)
.max_chunk_layout_hint(Layout::new::<u8>())
.max_number_of_chunks_hint(1)
.allocation_strategy(strategy)
.create()
.unwrap();

let mut sut_viewer = Sut::Builder::new(&storage_name)
.config(&config)
.open()
.unwrap();

let mut ptrs = vec![];
for i in 0..NUMBER_OF_REALLOCATIONS {
let size = 2 + i;
let layout = unsafe { Layout::from_size_align_unchecked(size, 1) };
let ptr = sut_creator.allocate(layout).unwrap();

for n in 0..size {
unsafe { ptr.data_ptr.add(n).write(i as u8) };
}
ptrs.push(ptr);
}

for i in 0..NUMBER_OF_REALLOCATIONS {
let size = 2 + i;
let ptr_view = sut_viewer
.register_and_translate_offset(ptrs[i].offset)
.unwrap();

for n in 0..size {
assert_that!(unsafe{ *ptr_view.add(n) }, eq i as u8);
}
}
}

#[test]
fn allocate_more_than_hinted_with_increasing_chunk_size_and_best_fit_strategy_works<
Shm: SharedMemory<DefaultAllocator>,
Sut: ResizableSharedMemory<DefaultAllocator, Shm>,
>() {
allocate_more_than_hinted_with_increasing_chunk_size_works::<Shm, Sut>(
AllocationStrategy::BestFit,
);
}

#[test]
fn allocate_more_than_hinted_with_increasing_chunk_size_and_power_of_two_strategy_works<
Shm: SharedMemory<DefaultAllocator>,
Sut: ResizableSharedMemory<DefaultAllocator, Shm>,
>() {
allocate_more_than_hinted_with_increasing_chunk_size_works::<Shm, Sut>(
AllocationStrategy::PowerOfTwo,
);
}

// TODO:
// * increasing chunk size
// * AllocationStrategy::Static is static
// * allocate/deallocate in random order
// * allocate until new segment, old segment id is never used
// * open with no more __0 segment
Expand Down

0 comments on commit d35e133

Please sign in to comment.