@@ -15,45 +15,59 @@ use core::{alloc::Layout, ptr::NonNull};
1515use iceoryx2_bb_elementary:: { bump_allocator:: * , math:: align} ;
1616use iceoryx2_bb_elementary_traits:: allocator:: BaseAllocator ;
1717use iceoryx2_bb_testing:: assert_that;
18+ use std:: ptr;
1819
1920#[ test]
2021fn start_position_is_correctly_used ( ) {
2122 let mut memory = [ 0u8 ; 8192 ] ;
22- let start_position: * mut u8 = memory. as_mut_ptr ( ) ;
23+ let start_position: Option < core:: ptr:: NonNull < u8 > > =
24+ core:: ptr:: NonNull :: < u8 > :: new ( memory. as_mut_ptr ( ) ) ;
2325 const MEM_SIZE : usize = 91 ;
2426 const MEM_ALIGN : usize = 1 ;
25- let sut = BumpAllocator :: new ( start_position) ;
27+ let sut = BumpAllocator :: new ( start_position. unwrap ( ) , core :: mem :: size_of_val ( & memory ) ) ;
2628
2729 let memory = sut
2830 . allocate ( Layout :: from_size_align ( MEM_SIZE , MEM_ALIGN ) . unwrap ( ) )
2931 . unwrap ( ) ;
3032
31- assert_that ! ( unsafe { memory. as_ref( ) } . as_ptr( ) as usize , eq start_position as usize ) ;
33+ assert_that ! ( unsafe { memory. as_ref( ) } . as_ptr( ) as usize , eq start_position. unwrap ( ) . as_ptr ( ) as usize ) ;
3234 assert_that ! ( unsafe { memory. as_ref( ) } . len( ) , eq MEM_SIZE ) ;
3335}
3436
37+ #[ test]
38+ #[ should_panic]
39+ fn start_position_is_null ( ) {
40+ let null_ptr: * const u8 = ptr:: null ( ) ;
41+ let start_position: core:: ptr:: NonNull < u8 > =
42+ core:: ptr:: NonNull :: < u8 > :: new ( null_ptr. cast_mut ( ) ) . unwrap ( ) ;
43+
44+ let _sut = BumpAllocator :: new ( start_position, 0 ) ;
45+ }
46+
3547#[ test]
3648fn allocated_memory_is_correctly_aligned ( ) {
37- let mut memory = [ 0u8 ; 8192 ] ;
38- let start_position: * mut u8 = unsafe { memory. as_mut_ptr ( ) . add ( 1 ) } ;
49+ let memory = [ 0u8 ; 8192 ] ;
50+ let start_position: core:: ptr:: NonNull < u8 > =
51+ unsafe { core:: ptr:: NonNull :: < u8 > :: new ( memory. as_ptr ( ) . cast_mut ( ) . add ( 1 ) ) . unwrap ( ) } ;
3952 const MEM_SIZE : usize = 128 ;
4053 const MEM_ALIGN : usize = 64 ;
41- let sut = BumpAllocator :: new ( start_position) ;
54+ let sut = BumpAllocator :: new ( start_position, core :: mem :: size_of_val ( & memory ) ) ;
4255
4356 let memory = sut
4457 . allocate ( Layout :: from_size_align ( MEM_SIZE , MEM_ALIGN ) . unwrap ( ) )
4558 . unwrap ( ) ;
4659
47- assert_that ! ( unsafe { memory. as_ref( ) } . as_ptr( ) as usize , eq align( start_position as usize , MEM_ALIGN ) ) ;
60+ assert_that ! ( unsafe { memory. as_ref( ) } . as_ptr( ) as usize , eq align( start_position. as_ptr ( ) as usize , MEM_ALIGN ) ) ;
4861 assert_that ! ( unsafe { memory. as_ref( ) } . len( ) , eq MEM_SIZE ) ;
4962}
5063
5164#[ test]
5265fn allocating_many_aligned_chunks_work ( ) {
53- let mut memory = [ 0u8 ; 8192 ] ;
54- let start_position: * mut u8 = unsafe { memory. as_mut_ptr ( ) . add ( 1 ) } ;
66+ let memory = [ 0u8 ; 8192 ] ;
67+ let start_position: core:: ptr:: NonNull < u8 > =
68+ unsafe { core:: ptr:: NonNull :: < u8 > :: new ( memory. as_ptr ( ) . cast_mut ( ) . add ( 1 ) ) . unwrap ( ) } ;
5569 const ITERATIONS : u32 = 5 ;
56- let sut = BumpAllocator :: new ( start_position) ;
70+ let sut = BumpAllocator :: new ( start_position, core :: mem :: size_of_val ( & memory ) ) ;
5771
5872 let mut last_size = 0 ;
5973 let mut last_position = 0 ;
@@ -76,11 +90,12 @@ fn allocating_many_aligned_chunks_work() {
7690
7791#[ test]
7892fn deallocating_releases_everything ( ) {
79- let mut memory = [ 0u8 ; 8192 ] ;
80- let start_position: * mut u8 = unsafe { memory. as_mut_ptr ( ) . add ( 3 ) } ;
93+ let memory = [ 0u8 ; 8192 ] ;
94+ let start_position: core:: ptr:: NonNull < u8 > =
95+ unsafe { core:: ptr:: NonNull :: < u8 > :: new ( memory. as_ptr ( ) . cast_mut ( ) . add ( 3 ) ) . unwrap ( ) } ;
8196 const MEM_SIZE : usize = 128 ;
8297 const MEM_ALIGN : usize = 1 ;
83- let sut = BumpAllocator :: new ( start_position) ;
98+ let sut = BumpAllocator :: new ( start_position, core :: mem :: size_of_val ( & memory ) ) ;
8499
85100 let layout = Layout :: from_size_align ( MEM_SIZE , MEM_ALIGN ) . unwrap ( ) ;
86101 let mut memory = sut. allocate ( layout) . unwrap ( ) ;
@@ -96,6 +111,6 @@ fn deallocating_releases_everything() {
96111 . allocate ( Layout :: from_size_align ( MEM_SIZE , MEM_ALIGN ) . unwrap ( ) )
97112 . unwrap ( ) ;
98113
99- assert_that ! ( unsafe { memory. as_ref( ) } . as_ptr( ) as usize , eq start_position as usize ) ;
114+ assert_that ! ( unsafe { memory. as_ref( ) } . as_ptr( ) as usize , eq start_position. as_ptr ( ) as usize ) ;
100115 assert_that ! ( unsafe { memory. as_ref( ) } . len( ) , eq MEM_SIZE ) ;
101116}
0 commit comments