-
Notifications
You must be signed in to change notification settings - Fork 13.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #46117 - SimonSapin:min-align, r=alexcrichton
allocators: don’t assume MIN_ALIGN for small sizes See individual commit messages.
- Loading branch information
Showing
4 changed files
with
65 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
use alloc_system::System; | ||
use std::heap::{Heap, Alloc, Layout}; | ||
|
||
/// https://github.com/rust-lang/rust/issues/45955 | ||
/// | ||
/// Note that `#[global_allocator]` is not used, | ||
/// so `liballoc_jemalloc` is linked (on some platforms). | ||
#[test] | ||
fn alloc_system_overaligned_request() { | ||
check_overalign_requests(System) | ||
} | ||
|
||
#[test] | ||
fn std_heap_overaligned_request() { | ||
check_overalign_requests(Heap) | ||
} | ||
|
||
fn check_overalign_requests<T: Alloc>(mut allocator: T) { | ||
let size = 8; | ||
let align = 16; // greater than size | ||
let iterations = 100; | ||
unsafe { | ||
let pointers: Vec<_> = (0..iterations).map(|_| { | ||
allocator.alloc(Layout::from_size_align(size, align).unwrap()).unwrap() | ||
}).collect(); | ||
for &ptr in &pointers { | ||
assert_eq!((ptr as usize) % align, 0, "Got a pointer less aligned than requested") | ||
} | ||
|
||
// Clean up | ||
for &ptr in &pointers { | ||
allocator.dealloc(ptr, Layout::from_size_align(size, align).unwrap()) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters