Skip to content

Commit 2dd268b

Browse files
committed
alloc_jemalloc: don’t assume MIN_ALIGN for small sizes
See previous commit’s message for what is expected of allocators in general, and jemalloc/jemalloc#1072 for discussion of what jemalloc does specifically.
1 parent 21d8992 commit 2dd268b

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

src/liballoc/tests/heap.rs

+5
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ fn alloc_system_overaligned_request() {
2020
check_overalign_requests(System)
2121
}
2222

23+
#[test]
24+
fn std_heap_overaligned_request() {
25+
check_overalign_requests(Heap)
26+
}
27+
2328
fn check_overalign_requests<T: Alloc>(mut allocator: T) {
2429
let size = 8;
2530
let align = 16; // greater than size

src/liballoc_jemalloc/lib.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ mod contents {
9292
a.trailing_zeros() as c_int
9393
}
9494

95-
fn align_to_flags(align: usize) -> c_int {
96-
if align <= MIN_ALIGN {
95+
fn align_to_flags(align: usize, size: usize) -> c_int {
96+
if align <= MIN_ALIGN && align <= size {
9797
0
9898
} else {
9999
mallocx_align(align)
@@ -111,7 +111,7 @@ mod contents {
111111
pub unsafe extern fn __rde_alloc(size: usize,
112112
align: usize,
113113
err: *mut u8) -> *mut u8 {
114-
let flags = align_to_flags(align);
114+
let flags = align_to_flags(align, size);
115115
let ptr = mallocx(size as size_t, flags) as *mut u8;
116116
if ptr.is_null() {
117117
let layout = Layout::from_size_align_unchecked(size, align);
@@ -132,7 +132,7 @@ mod contents {
132132
pub unsafe extern fn __rde_dealloc(ptr: *mut u8,
133133
size: usize,
134134
align: usize) {
135-
let flags = align_to_flags(align);
135+
let flags = align_to_flags(align, size);
136136
sdallocx(ptr as *mut c_void, size, flags);
137137
}
138138

@@ -142,7 +142,7 @@ mod contents {
142142
min: *mut usize,
143143
max: *mut usize) {
144144
let layout = &*(layout as *const Layout);
145-
let flags = align_to_flags(layout.align());
145+
let flags = align_to_flags(layout.align(), layout.size());
146146
let size = nallocx(layout.size(), flags) as usize;
147147
*min = layout.size();
148148
if size > 0 {
@@ -166,7 +166,7 @@ mod contents {
166166
return 0 as *mut u8
167167
}
168168

169-
let flags = align_to_flags(new_align);
169+
let flags = align_to_flags(new_align, new_size);
170170
let ptr = rallocx(ptr as *mut c_void, new_size, flags) as *mut u8;
171171
if ptr.is_null() {
172172
let layout = Layout::from_size_align_unchecked(new_size, new_align);
@@ -181,10 +181,10 @@ mod contents {
181181
pub unsafe extern fn __rde_alloc_zeroed(size: usize,
182182
align: usize,
183183
err: *mut u8) -> *mut u8 {
184-
let ptr = if align <= MIN_ALIGN {
184+
let ptr = if align <= MIN_ALIGN && align <= size {
185185
calloc(size as size_t, 1) as *mut u8
186186
} else {
187-
let flags = align_to_flags(align) | MALLOCX_ZERO;
187+
let flags = align_to_flags(align, size) | MALLOCX_ZERO;
188188
mallocx(size as size_t, flags) as *mut u8
189189
};
190190
if ptr.is_null() {
@@ -203,7 +203,7 @@ mod contents {
203203
err: *mut u8) -> *mut u8 {
204204
let p = __rde_alloc(size, align, err);
205205
if !p.is_null() {
206-
let flags = align_to_flags(align);
206+
let flags = align_to_flags(align, size);
207207
*excess = nallocx(size, flags) as usize;
208208
}
209209
return p
@@ -220,7 +220,7 @@ mod contents {
220220
err: *mut u8) -> *mut u8 {
221221
let p = __rde_realloc(ptr, old_size, old_align, new_size, new_align, err);
222222
if !p.is_null() {
223-
let flags = align_to_flags(new_align);
223+
let flags = align_to_flags(new_align, new_size);
224224
*excess = nallocx(new_size, flags) as usize;
225225
}
226226
p
@@ -244,7 +244,7 @@ mod contents {
244244
new_size: usize,
245245
new_align: usize) -> u8 {
246246
if old_align == new_align {
247-
let flags = align_to_flags(new_align);
247+
let flags = align_to_flags(new_align, new_size);
248248
(xallocx(ptr as *mut c_void, new_size, 0, flags) == new_size) as u8
249249
} else {
250250
0

0 commit comments

Comments
 (0)