@@ -10,13 +10,13 @@ use std::{
1010
1111#[ repr( align( 128 ) ) ]
1212struct Chonk < const N : usize > {
13- data : [ MaybeUninit < u8 > ; N ] ,
13+ data : MaybeUninit < [ u8 ; N ] > ,
1414}
1515
1616impl < const N : usize > Chonk < N > {
1717 pub fn new ( ) -> Self {
1818 Self {
19- data : [ MaybeUninit :: uninit ( ) ; N ] ,
19+ data : MaybeUninit :: uninit ( ) ,
2020 }
2121 }
2222}
@@ -42,45 +42,56 @@ impl<F> DerefMut for OwnedHeap<F> {
4242
4343pub fn new_heap ( ) -> OwnedHeap < impl Sized > {
4444 const HEAP_SIZE : usize = 1000 ;
45- let mut heap_space = Box :: new ( Chonk :: < HEAP_SIZE > :: new ( ) ) ;
46- let data = & mut Box :: leak ( heap_space) . data ;
47- let assumed_location = data. as_mut_ptr ( ) . cast ( ) ;
45+ let heap_space_ptr: * mut Chonk < HEAP_SIZE > = {
46+ let owned_box = Box :: new ( Chonk :: < HEAP_SIZE > :: new ( ) ) ;
47+ let mutref = Box :: leak ( owned_box) ;
48+ mutref
49+ } ;
50+ let data_ptr: * mut u8 = unsafe { core:: ptr:: addr_of_mut!( ( * heap_space_ptr) . data) . cast ( ) } ;
4851
49- let heap = unsafe { Heap :: new ( data . as_mut_ptr ( ) . cast ( ) , data . len ( ) ) } ;
50- assert_eq ! ( heap. bottom( ) , assumed_location ) ;
52+ let heap = unsafe { Heap :: new ( data_ptr , HEAP_SIZE ) } ;
53+ assert_eq ! ( heap. bottom( ) , data_ptr ) ;
5154 assert_eq ! ( heap. size( ) , align_down_size( HEAP_SIZE , size_of:: <usize >( ) ) ) ;
5255 let drop = move || {
53- // let _ = heap_space ;
56+ let _ = unsafe { Box :: from_raw ( heap_space_ptr ) } ;
5457 } ;
5558 OwnedHeap { heap, _drop : drop }
5659}
5760
5861fn new_max_heap ( ) -> OwnedHeap < impl Sized > {
5962 const HEAP_SIZE : usize = 1024 ;
6063 const HEAP_SIZE_MAX : usize = 2048 ;
61- let mut heap_space = Box :: new ( Chonk :: < HEAP_SIZE_MAX > :: new ( ) ) ;
62- let data = & mut Box :: leak ( heap_space) . data ;
63- let start_ptr = data. as_mut_ptr ( ) . cast ( ) ;
64+ let heap_space_ptr: * mut Chonk < HEAP_SIZE_MAX > = {
65+ let owned_box = Box :: new ( Chonk :: < HEAP_SIZE_MAX > :: new ( ) ) ;
66+ let mutref = Box :: leak ( owned_box) ;
67+ mutref
68+ } ;
69+ let data_ptr: * mut u8 = unsafe { core:: ptr:: addr_of_mut!( ( * heap_space_ptr) . data) . cast ( ) } ;
6470
6571 // Unsafe so that we have provenance over the whole allocation.
66- let heap = unsafe { Heap :: new ( start_ptr , HEAP_SIZE ) } ;
67- assert_eq ! ( heap. bottom( ) , start_ptr ) ;
72+ let heap = unsafe { Heap :: new ( data_ptr , HEAP_SIZE ) } ;
73+ assert_eq ! ( heap. bottom( ) , data_ptr ) ;
6874 assert_eq ! ( heap. size( ) , HEAP_SIZE ) ;
6975
7076 let drop = move || {
71- // let _ = heap_space ;
77+ let _ = unsafe { Box :: from_raw ( heap_space_ptr ) } ;
7278 } ;
7379 OwnedHeap { heap, _drop : drop }
7480}
7581
7682fn new_heap_skip ( ct : usize ) -> OwnedHeap < impl Sized > {
7783 const HEAP_SIZE : usize = 1000 ;
78- let mut heap_space = Box :: new ( Chonk :: < HEAP_SIZE > :: new ( ) ) ;
79- let data = & mut Box :: leak ( heap_space) . data [ ct..] ;
80- let heap = unsafe { Heap :: new ( data. as_mut_ptr ( ) . cast ( ) , data. len ( ) ) } ;
84+ let heap_space_ptr: * mut Chonk < HEAP_SIZE > = {
85+ let owned_box = Box :: new ( Chonk :: < HEAP_SIZE > :: new ( ) ) ;
86+ let mutref = Box :: leak ( owned_box) ;
87+ mutref
88+ } ;
89+ let data_ptr: * mut u8 = unsafe { core:: ptr:: addr_of_mut!( ( * heap_space_ptr) . data) . cast ( ) } ;
90+
91+ let heap = unsafe { Heap :: new ( data_ptr. add ( ct) , HEAP_SIZE - ct) } ;
8192
8293 let drop = move || {
83- // let _ = heap_space ;
94+ let _ = unsafe { Box :: from_raw ( heap_space_ptr ) } ;
8495 } ;
8596 OwnedHeap { heap, _drop : drop }
8697}
@@ -95,17 +106,23 @@ fn empty() {
95106#[ test]
96107fn oom ( ) {
97108 const HEAP_SIZE : usize = 1000 ;
98- let mut heap_space = Box :: new ( Chonk :: < HEAP_SIZE > :: new ( ) ) ;
99- let data = & mut heap_space. data ;
100- let assumed_location = data. as_mut_ptr ( ) . cast ( ) ;
109+ let heap_space_ptr: * mut Chonk < HEAP_SIZE > = {
110+ let owned_box = Box :: new ( Chonk :: < HEAP_SIZE > :: new ( ) ) ;
111+ let mutref = Box :: leak ( owned_box) ;
112+ mutref
113+ } ;
114+ let data_ptr: * mut u8 = unsafe { core:: ptr:: addr_of_mut!( ( * heap_space_ptr) . data) . cast ( ) } ;
101115
102- let mut heap = unsafe { Heap :: new ( data . as_mut_ptr ( ) . cast ( ) , data . len ( ) ) } ;
103- assert_eq ! ( heap. bottom( ) , assumed_location ) ;
116+ let mut heap = unsafe { Heap :: new ( data_ptr , HEAP_SIZE ) } ;
117+ assert_eq ! ( heap. bottom( ) , data_ptr ) ;
104118 assert_eq ! ( heap. size( ) , align_down_size( HEAP_SIZE , size_of:: <usize >( ) ) ) ;
105119
106120 let layout = Layout :: from_size_align ( heap. size ( ) + 1 , align_of :: < usize > ( ) ) ;
107121 let addr = heap. allocate_first_fit ( layout. unwrap ( ) ) ;
108122 assert ! ( addr. is_err( ) ) ;
123+
124+ // Explicitly unleak the heap allocation
125+ let _ = unsafe { Box :: from_raw ( heap_space_ptr) } ;
109126}
110127
111128#[ test]
0 commit comments