@@ -5,8 +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, Heap , MiMalloc } ;
8+ use crate :: { free_sized, realloc_aligned, sys , Heap , MiMalloc , MAX_ALIGN_SIZE } ;
99use core:: alloc:: { AllocError , Allocator , GlobalAlloc , Layout } ;
10+ use core:: mem:: size_of;
1011use core:: ptr:: NonNull ;
1112
1213#[ inline( always) ]
@@ -24,8 +25,8 @@ unsafe fn zero_tail(ptr: *mut u8, old_size: usize, new_size: usize) -> *mut u8 {
2425 ptr
2526}
2627
27- // Reports the requested size to stay as fast as `GlobalAlloc`; use `good_size` upfront
28- // ( or `sys::mi_umalloc`) to get the full usable size (issue #1513).
28+ // Reports the requested size to stay as fast as `GlobalAlloc`; use `good_size` upfront,
29+ // or `MiMallocExcess`, to get the full usable size (issue #1513).
2930unsafe impl Allocator for MiMalloc {
3031 #[ inline]
3132 fn allocate ( & self , layout : Layout ) -> Result < NonNull < [ u8 ] > , AllocError > {
@@ -133,3 +134,115 @@ unsafe impl Allocator for &Heap {
133134 )
134135 }
135136}
137+
138+ /// As [`MiMalloc`] but `allocate` returns the full usable size by requesting `good_size` bytes.
139+ /// This can be useful for vector or buffer allocations to maximise the usable memory and reduce reallocations.
140+ #[ derive( Clone , Copy , Debug , Default ) ]
141+ pub struct MiMallocExcess ;
142+
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`.)
145+ #[ 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 ( ) ) ;
169+ let p = unsafe {
170+ if zero {
171+ sys:: mi_wzalloc_small ( wsize)
172+ } else {
173+ sys:: mi_wmalloc_small ( wsize)
174+ }
175+ } ;
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 ( ) ) }
183+ }
184+
185+ unsafe impl Allocator for MiMallocExcess {
186+ #[ inline]
187+ 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 ( ) )
193+ }
194+
195+ #[ inline]
196+ 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 ( ) )
202+ }
203+
204+ #[ inline]
205+ unsafe fn deallocate ( & self , ptr : NonNull < u8 > , layout : Layout ) {
206+ unsafe { free_sized ( ptr. as_ptr ( ) , layout) }
207+ }
208+
209+ #[ inline]
210+ unsafe fn grow (
211+ & self ,
212+ ptr : NonNull < u8 > ,
213+ _old : Layout ,
214+ new : Layout ,
215+ ) -> 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+ )
221+ }
222+
223+ #[ inline]
224+ unsafe fn grow_zeroed (
225+ & self ,
226+ ptr : NonNull < u8 > ,
227+ old : Layout ,
228+ new : Layout ,
229+ ) -> 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)
233+ }
234+
235+ #[ inline]
236+ unsafe fn shrink (
237+ & self ,
238+ ptr : NonNull < u8 > ,
239+ _old : Layout ,
240+ new : Layout ,
241+ ) -> 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+ )
247+ }
248+ }
0 commit comments