@@ -12,8 +12,7 @@ pub struct MachineAlloc {
12
12
/// the global allocator.
13
13
pages : Vec < * mut u8 > ,
14
14
/// Pointers to multi-page-sized allocations. These must also be page-aligned,
15
- /// with a size of `page_size * count` (where `count` is the second element
16
- /// of the vector).
15
+ /// with their size stored as the second element of the vector.
17
16
huge_allocs : Vec < ( * mut u8 , usize ) > ,
18
17
/// Metadata about which bytes have been allocated on each page. The length
19
18
/// of this vector must be the same as that of `pages`, and the length of the
@@ -45,7 +44,7 @@ impl MachineAlloc {
45
44
huge_allocs : Vec :: new ( ) ,
46
45
allocated : Vec :: new ( ) ,
47
46
page_size : 4096 ,
48
- enabled : false ,
47
+ enabled : true ,
49
48
}
50
49
}
51
50
@@ -79,15 +78,6 @@ impl MachineAlloc {
79
78
( size, align)
80
79
}
81
80
82
- /// If a requested allocation is greater than one page, we simply allocate
83
- /// a fixed number of pages for it.
84
- #[ inline]
85
- fn huge_normalized_layout ( & self , layout : Layout ) -> ( usize , usize ) {
86
- let size = layout. size ( ) . next_multiple_of ( self . page_size ) ;
87
- let align = std:: cmp:: max ( layout. align ( ) , self . page_size ) ;
88
- ( size, align)
89
- }
90
-
91
81
/// Allocates memory as described in `Layout`. If `MachineAlloc::enable()`
92
82
/// has *not* been called yet, this is just a wrapper for `(alloc::alloc(),
93
83
/// true)`. Otherwise, it will allocate from its own memory pool and
@@ -100,7 +90,7 @@ impl MachineAlloc {
100
90
let mut alloc = ALLOCATOR . lock ( ) . unwrap ( ) ;
101
91
unsafe {
102
92
if alloc. enabled {
103
- ( alloc. alloc_inner ( layout) , false )
93
+ ( alloc. alloc_inner ( layout, alloc :: alloc ) , false )
104
94
} else {
105
95
( alloc:: alloc ( layout) , true )
106
96
}
@@ -115,12 +105,7 @@ impl MachineAlloc {
115
105
pub unsafe fn alloc_zeroed ( layout : Layout ) -> ( * mut u8 , bool ) {
116
106
let mut alloc = ALLOCATOR . lock ( ) . unwrap ( ) ;
117
107
if alloc. enabled {
118
- let ptr = unsafe { alloc. alloc_inner ( layout) } ;
119
- if !ptr. is_null ( ) {
120
- unsafe {
121
- ptr. write_bytes ( 0 , layout. size ( ) ) ;
122
- }
123
- }
108
+ let ptr = unsafe { alloc. alloc_inner ( layout, alloc:: alloc_zeroed) } ;
124
109
( ptr, false )
125
110
} else {
126
111
unsafe { ( alloc:: alloc_zeroed ( layout) , true ) }
@@ -129,11 +114,13 @@ impl MachineAlloc {
129
114
130
115
/// SAFETY: The allocator must have been `enable()`d already and
131
116
/// the `layout` must be valid.
132
- unsafe fn alloc_inner ( & mut self , layout : Layout ) -> * mut u8 {
117
+ unsafe fn alloc_inner ( & mut self , layout : Layout , sys_allocator : unsafe fn ( Layout ) -> * mut u8 ) -> * mut u8 {
133
118
let ( size, align) = MachineAlloc :: normalized_layout ( layout) ;
134
119
135
120
if align > self . page_size || size > self . page_size {
136
- unsafe { self . alloc_multi_page ( layout) }
121
+ unsafe {
122
+ self . alloc_multi_page ( layout, sys_allocator)
123
+ }
137
124
} else {
138
125
for ( page, pinfo) in std:: iter:: zip ( & mut self . pages , & mut self . allocated ) {
139
126
for idx in ( 0 ..self . page_size ) . step_by ( align) {
@@ -157,18 +144,15 @@ impl MachineAlloc {
157
144
158
145
// We get here only if there's no space in our existing pages
159
146
self . add_page ( ) ;
160
- unsafe { self . alloc_inner ( layout) }
147
+ unsafe { self . alloc_inner ( layout, sys_allocator ) }
161
148
}
162
149
}
163
150
164
151
/// SAFETY: Same as `alloc_inner()` with the added requirement that `layout`
165
152
/// must ask for a size larger than the host pagesize.
166
- unsafe fn alloc_multi_page ( & mut self , layout : Layout ) -> * mut u8 {
167
- let ( size, align) = self . huge_normalized_layout ( layout) ;
168
-
169
- let layout = unsafe { Layout :: from_size_align_unchecked ( size, align) } ;
170
- let ret = unsafe { alloc:: alloc ( layout) } ;
171
- self . huge_allocs . push ( ( ret, size) ) ;
153
+ unsafe fn alloc_multi_page ( & mut self , layout : Layout , sys_allocator : unsafe fn ( Layout ) -> * mut u8 ) -> * mut u8 {
154
+ let ret = unsafe { sys_allocator ( layout) } ;
155
+ self . huge_allocs . push ( ( ret, layout. size ( ) ) ) ;
172
156
ret
173
157
}
174
158
@@ -236,9 +220,7 @@ impl MachineAlloc {
236
220
. find ( |pg| ptr. addr ( ) == pg. 1 . 0 . addr ( ) )
237
221
. expect ( "Freeing unallocated pages" ) ;
238
222
let ptr = self . huge_allocs . remove ( idx) . 0 ;
239
- let ( size, align) = self . huge_normalized_layout ( layout) ;
240
223
unsafe {
241
- let layout = Layout :: from_size_align_unchecked ( size, align) ;
242
224
alloc:: dealloc ( ptr, layout) ;
243
225
}
244
226
}
0 commit comments