@@ -5,10 +5,9 @@ terms of the MIT license. A copy of the license can be found in the file
55"LICENSE" at the root of this distribution.
66-----------------------------------------------------------------------------*/
77use crate :: heap:: realloc_in;
8- use crate :: { free_sized, realloc_aligned, sys, Heap , MiMalloc , MAX_ALIGN_SIZE } ;
8+ use crate :: { free_sized, malloc_aligned_size , realloc_aligned, sys, Heap , MiMalloc } ;
99use core:: alloc:: { AllocError , Allocator , GlobalAlloc , Layout } ;
10- use core:: mem:: size_of;
11- use core:: ptr:: NonNull ;
10+ use core:: ptr:: { self , NonNull } ;
1211
1312#[ inline( always) ]
1413fn block ( ptr : * mut u8 , size : usize ) -> Result < NonNull < [ u8 ] > , AllocError > {
@@ -135,70 +134,56 @@ unsafe impl Allocator for &Heap {
135134 }
136135}
137136
138- /// As [`MiMalloc`] but `allocate` returns the full usable size by requesting `good_size` bytes .
137+ /// As [`MiMalloc`] but returns the full usable size of the allocated block (using the `mi_u*` functions) .
139138/// This can be useful for vector or buffer allocations to maximise the usable memory and reduce reallocations.
140139#[ derive( Clone , Copy , Debug , Default ) ]
141140pub struct MiMallocExcess ;
142141
143- // As `mi_good_size` for sizes up to `MI_SMALL_SIZE_MAX` (in words, see `mi_bin`); a mismatch only wastes space as we allocate the returned size.
144- // (If the size classes ever change, we catch this in the tests where we verify `small_good_wsize` against `mi_good_size`.)
145142#[ inline( always) ]
146- pub ( crate ) const fn small_good_wsize ( size : usize ) -> usize {
147- const ALIGN_WSIZE : usize = MAX_ALIGN_SIZE / size_of :: < usize > ( ) ;
148- let wsize = crate :: wsize_from_size ( size) ;
149- if wsize <= 1 {
150- 1
151- } else if size <= 16 {
152- ( wsize + 1 ) & !1
153- } else if size <= 64 {
154- ( wsize + ALIGN_WSIZE - 1 ) & !( ALIGN_WSIZE - 1 )
155- } else {
156- // 4 size classes per power of two
157- let w = wsize - 1 ;
158- let shift = ( usize:: BITS - 1 - w. leading_zeros ( ) ) - 2 ;
159- ( ( w >> shift) + 1 ) << shift
160- }
161- }
162-
163- #[ inline( always) ]
164- fn small_alloc ( layout : Layout , zero : bool ) -> Option < Result < NonNull < [ u8 ] > , AllocError > > {
165- if layout. align ( ) > MAX_ALIGN_SIZE || layout. size ( ) > sys:: MI_SMALL_SIZE_MAX {
166- return None ;
167- }
168- let wsize = small_good_wsize ( layout. size ( ) ) ;
143+ unsafe fn urealloc ( ptr : * mut u8 , new : Layout ) -> ( * mut u8 , usize ) {
144+ let mut usable = 0 ;
169145 let p = unsafe {
170- if zero {
171- sys:: mi_wzalloc_small ( wsize)
172- } else {
173- sys:: mi_wmalloc_small ( wsize)
146+ match malloc_aligned_size ( new. size ( ) , new. align ( ) ) {
147+ Some ( size) => sys:: mi_urealloc ( ptr. cast ( ) , size, ptr:: null_mut ( ) , & mut usable) ,
148+ None => sys:: mi_urealloc_aligned (
149+ ptr. cast ( ) ,
150+ new. size ( ) ,
151+ new. align ( ) ,
152+ ptr:: null_mut ( ) ,
153+ & mut usable,
154+ ) ,
174155 }
175156 } ;
176- Some ( block ( p. cast ( ) , wsize * size_of :: < usize > ( ) ) )
177- }
178-
179- #[ inline( always) ]
180- fn good_layout ( layout : Layout ) -> Layout {
181- let size = crate :: good_size ( layout. size ( ) ) ;
182- unsafe { Layout :: from_size_align_unchecked ( size, layout. align ( ) ) }
157+ ( p. cast ( ) , usable)
183158}
184159
185160unsafe impl Allocator for MiMallocExcess {
186161 #[ inline]
187162 fn allocate ( & self , layout : Layout ) -> Result < NonNull < [ u8 ] > , AllocError > {
188- if let Some ( r) = small_alloc ( layout, false ) {
189- return r;
190- }
191- let layout = good_layout ( layout) ;
192- block ( unsafe { MiMalloc . alloc ( layout) } , layout. size ( ) )
163+ let ( size, align) = ( layout. size ( ) , layout. align ( ) ) ;
164+ let mut usable = 0 ;
165+ let p = unsafe {
166+ if let Some ( size) = malloc_aligned_size ( size, align) {
167+ sys:: mi_umalloc ( size, & mut usable)
168+ } else {
169+ sys:: mi_umalloc_aligned ( size, align, & mut usable)
170+ }
171+ } ;
172+ block ( p. cast ( ) , usable)
193173 }
194174
195175 #[ inline]
196176 fn allocate_zeroed ( & self , layout : Layout ) -> Result < NonNull < [ u8 ] > , AllocError > {
197- if let Some ( r) = small_alloc ( layout, true ) {
198- return r;
199- }
200- let layout = good_layout ( layout) ;
201- block ( unsafe { MiMalloc . alloc_zeroed ( layout) } , layout. size ( ) )
177+ let ( size, align) = ( layout. size ( ) , layout. align ( ) ) ;
178+ let mut usable = 0 ;
179+ let p = unsafe {
180+ if let Some ( size) = malloc_aligned_size ( size, align) {
181+ sys:: mi_uzalloc ( size, & mut usable)
182+ } else {
183+ sys:: mi_uzalloc_aligned ( size, align, & mut usable)
184+ }
185+ } ;
186+ block ( p. cast ( ) , usable)
202187 }
203188
204189 #[ inline]
@@ -213,11 +198,8 @@ unsafe impl Allocator for MiMallocExcess {
213198 _old : Layout ,
214199 new : Layout ,
215200 ) -> Result < NonNull < [ u8 ] > , AllocError > {
216- let size = crate :: good_size ( new. size ( ) ) ;
217- block (
218- unsafe { realloc_aligned ( ptr. as_ptr ( ) , new. align ( ) , size) } ,
219- size,
220- )
201+ let ( p, usable) = unsafe { urealloc ( ptr. as_ptr ( ) , new) } ;
202+ block ( p, usable)
221203 }
222204
223205 #[ inline]
@@ -227,9 +209,8 @@ unsafe impl Allocator for MiMallocExcess {
227209 old : Layout ,
228210 new : Layout ,
229211 ) -> Result < NonNull < [ u8 ] > , AllocError > {
230- let size = crate :: good_size ( new. size ( ) ) ;
231- let p = unsafe { realloc_aligned ( ptr. as_ptr ( ) , new. align ( ) , size) } ;
232- block ( unsafe { zero_tail ( p, old. size ( ) , size) } , size)
212+ let ( p, usable) = unsafe { urealloc ( ptr. as_ptr ( ) , new) } ;
213+ block ( unsafe { zero_tail ( p, old. size ( ) , usable) } , usable)
233214 }
234215
235216 #[ inline]
@@ -239,10 +220,7 @@ unsafe impl Allocator for MiMallocExcess {
239220 _old : Layout ,
240221 new : Layout ,
241222 ) -> Result < NonNull < [ u8 ] > , AllocError > {
242- let size = crate :: good_size ( new. size ( ) ) ;
243- block (
244- unsafe { realloc_aligned ( ptr. as_ptr ( ) , new. align ( ) , size) } ,
245- size,
246- )
223+ let ( p, usable) = unsafe { urealloc ( ptr. as_ptr ( ) , new) } ;
224+ block ( p, usable)
247225 }
248226}
0 commit comments