11use super :: * ;
22use core:: alloc:: Layout ;
3+ use core:: ops:: DerefMut ;
34use std:: mem:: { align_of, size_of, MaybeUninit } ;
45use std:: prelude:: v1:: * ;
56
@@ -16,30 +17,69 @@ impl<const N: usize> Chonk<N> {
1617 }
1718}
1819
19- fn new_heap ( ) -> Heap {
20+ pub struct OwnedHeap < F > {
21+ heap : Heap ,
22+ _drop : F ,
23+ }
24+
25+ impl < F > Deref for OwnedHeap < F > {
26+ type Target = Heap ;
27+
28+ fn deref ( & self ) -> & Self :: Target {
29+ & self . heap
30+ }
31+ }
32+
33+ impl < F > DerefMut for OwnedHeap < F > {
34+ fn deref_mut ( & mut self ) -> & mut Self :: Target {
35+ & mut self . heap
36+ }
37+ }
38+
39+ pub fn new_heap ( ) -> OwnedHeap < impl Sized > {
2040 const HEAP_SIZE : usize = 1000 ;
21- let heap_space = Box :: leak ( Box :: new ( Chonk :: < HEAP_SIZE > :: new ( ) ) ) ;
41+ let mut heap_space = Box :: new ( Chonk :: < HEAP_SIZE > :: new ( ) ) ;
2242 let data = & mut heap_space. data ;
2343 let assumed_location = data. as_mut_ptr ( ) . cast ( ) ;
2444
25- let heap = Heap :: from_slice ( data) ;
45+ let heap = unsafe { Heap :: new ( data. as_mut_ptr ( ) . cast ( ) , data . len ( ) ) } ;
2646 assert_eq ! ( heap. bottom( ) , assumed_location) ;
2747 assert_eq ! ( heap. size( ) , align_down_size( HEAP_SIZE , size_of:: <usize >( ) ) ) ;
28- heap
48+
49+ let drop = move || {
50+ let _ = heap_space;
51+ } ;
52+ OwnedHeap { heap, _drop : drop }
2953}
3054
31- fn new_max_heap ( ) -> Heap {
55+ fn new_max_heap ( ) -> OwnedHeap < impl Sized > {
3256 const HEAP_SIZE : usize = 1024 ;
3357 const HEAP_SIZE_MAX : usize = 2048 ;
34- let heap_space = Box :: leak ( Box :: new ( Chonk :: < HEAP_SIZE_MAX > :: new ( ) ) ) ;
58+ let mut heap_space = Box :: new ( Chonk :: < HEAP_SIZE_MAX > :: new ( ) ) ;
3559 let data = & mut heap_space. data ;
3660 let start_ptr = data. as_mut_ptr ( ) . cast ( ) ;
3761
3862 // Unsafe so that we have provenance over the whole allocation.
3963 let heap = unsafe { Heap :: new ( start_ptr, HEAP_SIZE ) } ;
4064 assert_eq ! ( heap. bottom( ) , start_ptr) ;
4165 assert_eq ! ( heap. size( ) , HEAP_SIZE ) ;
42- heap
66+
67+ let drop = move || {
68+ let _ = heap_space;
69+ } ;
70+ OwnedHeap { heap, _drop : drop }
71+ }
72+
73+ fn new_heap_skip ( ct : usize ) -> OwnedHeap < impl Sized > {
74+ const HEAP_SIZE : usize = 1000 ;
75+ let mut heap_space = Box :: new ( Chonk :: < HEAP_SIZE > :: new ( ) ) ;
76+ let data = & mut heap_space. data [ ct..] ;
77+ let heap = unsafe { Heap :: new ( data. as_mut_ptr ( ) . cast ( ) , data. len ( ) ) } ;
78+
79+ let drop = move || {
80+ let _ = heap_space;
81+ } ;
82+ OwnedHeap { heap, _drop : drop }
4383}
4484
4585#[ test]
@@ -51,7 +91,15 @@ fn empty() {
5191
5292#[ test]
5393fn oom ( ) {
54- let mut heap = new_heap ( ) ;
94+ const HEAP_SIZE : usize = 1000 ;
95+ let mut heap_space = Box :: new ( Chonk :: < HEAP_SIZE > :: new ( ) ) ;
96+ let data = & mut heap_space. data ;
97+ let assumed_location = data. as_mut_ptr ( ) . cast ( ) ;
98+
99+ let mut heap = unsafe { Heap :: new ( data. as_mut_ptr ( ) . cast ( ) , data. len ( ) ) } ;
100+ assert_eq ! ( heap. bottom( ) , assumed_location) ;
101+ assert_eq ! ( heap. size( ) , align_down_size( HEAP_SIZE , size_of:: <usize >( ) ) ) ;
102+
55103 let layout = Layout :: from_size_align ( heap. size ( ) + 1 , align_of :: < usize > ( ) ) ;
56104 let addr = heap. allocate_first_fit ( layout. unwrap ( ) ) ;
57105 assert ! ( addr. is_err( ) ) ;
@@ -388,14 +436,6 @@ fn allocate_multiple_unaligned() {
388436 }
389437}
390438
391- fn new_heap_skip ( ct : usize ) -> Heap {
392- const HEAP_SIZE : usize = 1000 ;
393- let heap_space = Box :: leak ( Box :: new ( Chonk :: < HEAP_SIZE > :: new ( ) ) ) ;
394- let data = & mut heap_space. data [ ct..] ;
395- let heap = Heap :: from_slice ( data) ;
396- heap
397- }
398-
399439#[ test]
400440fn allocate_usize ( ) {
401441 let mut heap = new_heap ( ) ;
0 commit comments