@@ -47,8 +47,8 @@ fn mallocx_align(a: usize) -> c_int {
47
47
a. trailing_zeros ( ) as c_int
48
48
}
49
49
50
- fn align_to_flags ( align : usize ) -> c_int {
51
- if align <= MIN_ALIGN {
50
+ fn align_to_flags ( align : usize , size : usize ) -> c_int {
51
+ if align <= MIN_ALIGN && align <= size {
52
52
0
53
53
} else {
54
54
mallocx_align ( align)
@@ -131,7 +131,7 @@ unsafe impl Alloc for Jemalloc {
131
131
unsafe impl < ' a > Alloc for & ' a Jemalloc {
132
132
#[ inline]
133
133
unsafe fn alloc ( & mut self , layout : Layout ) -> Result < * mut u8 , AllocErr > {
134
- let flags = align_to_flags ( layout. align ( ) ) ;
134
+ let flags = align_to_flags ( layout. align ( ) , layout . size ( ) ) ;
135
135
let ptr = ffi:: mallocx ( layout. size ( ) , flags) ;
136
136
if ptr. is_null ( ) {
137
137
Err ( AllocErr :: Exhausted { request : layout } )
@@ -144,10 +144,10 @@ unsafe impl<'a> Alloc for &'a Jemalloc {
144
144
unsafe fn alloc_zeroed ( & mut self , layout : Layout )
145
145
-> Result < * mut u8 , AllocErr >
146
146
{
147
- let ptr = if layout. align ( ) <= MIN_ALIGN {
147
+ let ptr = if layout. align ( ) <= MIN_ALIGN && layout . align ( ) <= layout . size ( ) {
148
148
ffi:: calloc ( 1 , layout. size ( ) )
149
149
} else {
150
- let flags = align_to_flags ( layout. align ( ) ) | ffi:: MALLOCX_ZERO ;
150
+ let flags = align_to_flags ( layout. align ( ) , layout . size ( ) ) | ffi:: MALLOCX_ZERO ;
151
151
ffi:: mallocx ( layout. size ( ) , flags)
152
152
} ;
153
153
if ptr. is_null ( ) {
@@ -159,7 +159,7 @@ unsafe impl<'a> Alloc for &'a Jemalloc {
159
159
160
160
#[ inline]
161
161
unsafe fn dealloc ( & mut self , ptr : * mut u8 , layout : Layout ) {
162
- let flags = align_to_flags ( layout. align ( ) ) ;
162
+ let flags = align_to_flags ( layout. align ( ) , layout . size ( ) ) ;
163
163
ffi:: sdallocx ( ptr as * mut c_void , layout. size ( ) , flags)
164
164
}
165
165
@@ -171,7 +171,7 @@ unsafe impl<'a> Alloc for &'a Jemalloc {
171
171
if old_layout. align ( ) != new_layout. align ( ) {
172
172
return Err ( AllocErr :: Unsupported { details : "cannot change align" } )
173
173
}
174
- let flags = align_to_flags ( new_layout. align ( ) ) ;
174
+ let flags = align_to_flags ( new_layout. align ( ) , new_layout . size ( ) ) ;
175
175
let ptr = ffi:: rallocx ( ptr as * mut c_void , new_layout. size ( ) , flags) ;
176
176
if ptr. is_null ( ) {
177
177
Err ( AllocErr :: Exhausted { request : new_layout } )
@@ -186,7 +186,7 @@ unsafe impl<'a> Alloc for &'a Jemalloc {
186
186
187
187
#[ inline]
188
188
fn usable_size ( & self , layout : & Layout ) -> ( usize , usize ) {
189
- let flags = align_to_flags ( layout. align ( ) ) ;
189
+ let flags = align_to_flags ( layout. align ( ) , layout . size ( ) ) ;
190
190
unsafe {
191
191
let max = ffi:: nallocx ( layout. size ( ) , flags) ;
192
192
( layout. size ( ) , max)
@@ -209,7 +209,7 @@ unsafe impl<'a> Alloc for &'a Jemalloc {
209
209
if old_layout. align ( ) != new_layout. align ( ) {
210
210
return Err ( CannotReallocInPlace )
211
211
}
212
- let flags = align_to_flags ( new_layout. align ( ) ) ;
212
+ let flags = align_to_flags ( new_layout. align ( ) , new_layout . size ( ) ) ;
213
213
let size = ffi:: xallocx ( ptr as * mut c_void , new_layout. size ( ) , 0 , flags) ;
214
214
if size >= new_layout. size ( ) {
215
215
Err ( CannotReallocInPlace )
0 commit comments