@@ -10,13 +10,13 @@ use std::{
10
10
11
11
#[ repr( align( 128 ) ) ]
12
12
struct Chonk < const N : usize > {
13
- data : [ MaybeUninit < u8 > ; N ] ,
13
+ data : MaybeUninit < [ u8 ; N ] > ,
14
14
}
15
15
16
16
impl < const N : usize > Chonk < N > {
17
17
pub fn new ( ) -> Self {
18
18
Self {
19
- data : [ MaybeUninit :: uninit ( ) ; N ] ,
19
+ data : MaybeUninit :: uninit ( ) ,
20
20
}
21
21
}
22
22
}
@@ -42,45 +42,56 @@ impl<F> DerefMut for OwnedHeap<F> {
42
42
43
43
pub fn new_heap ( ) -> OwnedHeap < impl Sized > {
44
44
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 ( ) } ;
48
51
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 ) ;
51
54
assert_eq ! ( heap. size( ) , align_down_size( HEAP_SIZE , size_of:: <usize >( ) ) ) ;
52
55
let drop = move || {
53
- // let _ = heap_space ;
56
+ let _ = unsafe { Box :: from_raw ( heap_space_ptr ) } ;
54
57
} ;
55
58
OwnedHeap { heap, _drop : drop }
56
59
}
57
60
58
61
fn new_max_heap ( ) -> OwnedHeap < impl Sized > {
59
62
const HEAP_SIZE : usize = 1024 ;
60
63
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 ( ) } ;
64
70
65
71
// 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 ) ;
68
74
assert_eq ! ( heap. size( ) , HEAP_SIZE ) ;
69
75
70
76
let drop = move || {
71
- // let _ = heap_space ;
77
+ let _ = unsafe { Box :: from_raw ( heap_space_ptr ) } ;
72
78
} ;
73
79
OwnedHeap { heap, _drop : drop }
74
80
}
75
81
76
82
fn new_heap_skip ( ct : usize ) -> OwnedHeap < impl Sized > {
77
83
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) } ;
81
92
82
93
let drop = move || {
83
- // let _ = heap_space ;
94
+ let _ = unsafe { Box :: from_raw ( heap_space_ptr ) } ;
84
95
} ;
85
96
OwnedHeap { heap, _drop : drop }
86
97
}
@@ -95,17 +106,23 @@ fn empty() {
95
106
#[ test]
96
107
fn oom ( ) {
97
108
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 ( ) } ;
101
115
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 ) ;
104
118
assert_eq ! ( heap. size( ) , align_down_size( HEAP_SIZE , size_of:: <usize >( ) ) ) ;
105
119
106
120
let layout = Layout :: from_size_align ( heap. size ( ) + 1 , align_of :: < usize > ( ) ) ;
107
121
let addr = heap. allocate_first_fit ( layout. unwrap ( ) ) ;
108
122
assert ! ( addr. is_err( ) ) ;
123
+
124
+ // Explicitly unleak the heap allocation
125
+ let _ = unsafe { Box :: from_raw ( heap_space_ptr) } ;
109
126
}
110
127
111
128
#[ test]
0 commit comments