1
1
use super :: * ;
2
2
use core:: alloc:: Layout ;
3
+ use core:: ops:: DerefMut ;
3
4
use std:: mem:: { align_of, size_of, MaybeUninit } ;
4
5
use std:: prelude:: v1:: * ;
5
6
@@ -16,30 +17,69 @@ impl<const N: usize> Chonk<N> {
16
17
}
17
18
}
18
19
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 > {
20
40
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 ( ) ) ;
22
42
let data = & mut heap_space. data ;
23
43
let assumed_location = data. as_mut_ptr ( ) . cast ( ) ;
24
44
25
- let heap = Heap :: from_slice ( data) ;
45
+ let heap = unsafe { Heap :: new ( data. as_mut_ptr ( ) . cast ( ) , data . len ( ) ) } ;
26
46
assert_eq ! ( heap. bottom( ) , assumed_location) ;
27
47
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 }
29
53
}
30
54
31
- fn new_max_heap ( ) -> Heap {
55
+ fn new_max_heap ( ) -> OwnedHeap < impl Sized > {
32
56
const HEAP_SIZE : usize = 1024 ;
33
57
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 ( ) ) ;
35
59
let data = & mut heap_space. data ;
36
60
let start_ptr = data. as_mut_ptr ( ) . cast ( ) ;
37
61
38
62
// Unsafe so that we have provenance over the whole allocation.
39
63
let heap = unsafe { Heap :: new ( start_ptr, HEAP_SIZE ) } ;
40
64
assert_eq ! ( heap. bottom( ) , start_ptr) ;
41
65
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 }
43
83
}
44
84
45
85
#[ test]
@@ -51,7 +91,15 @@ fn empty() {
51
91
52
92
#[ test]
53
93
fn 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
+
55
103
let layout = Layout :: from_size_align ( heap. size ( ) + 1 , align_of :: < usize > ( ) ) ;
56
104
let addr = heap. allocate_first_fit ( layout. unwrap ( ) ) ;
57
105
assert ! ( addr. is_err( ) ) ;
@@ -388,14 +436,6 @@ fn allocate_multiple_unaligned() {
388
436
}
389
437
}
390
438
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
-
399
439
#[ test]
400
440
fn allocate_usize ( ) {
401
441
let mut heap = new_heap ( ) ;
0 commit comments