|
| 1 | +// Copyright 2017 The Rust Project Developers. See the COPYRIGHT |
| 2 | +// file at the top-level directory of this distribution and at |
| 3 | +// http://rust-lang.org/COPYRIGHT. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 7 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 8 | +// option. This file may not be copied, modified, or distributed |
| 9 | +// except according to those terms. |
| 10 | + |
| 11 | +use alloc_system::System; |
| 12 | +use std::heap::{Heap, Alloc, Layout}; |
| 13 | + |
| 14 | +/// https://github.com/rust-lang/rust/issues/45955 |
| 15 | +/// |
| 16 | +/// Note that `#[global_allocator]` is not used, |
| 17 | +/// so `liballoc_jemalloc` is linked (on some platforms). |
| 18 | +#[test] |
| 19 | +fn alloc_system_overaligned_request() { |
| 20 | + check_overalign_requests(System) |
| 21 | +} |
| 22 | + |
| 23 | +fn check_overalign_requests<T: Alloc>(mut allocator: T) { |
| 24 | + let size = 8; |
| 25 | + let align = 16; // greater than size |
| 26 | + let iterations = 100; |
| 27 | + unsafe { |
| 28 | + let pointers: Vec<_> = (0..iterations).map(|_| { |
| 29 | + allocator.alloc(Layout::from_size_align(size, align).unwrap()).unwrap() |
| 30 | + }).collect(); |
| 31 | + for &ptr in &pointers { |
| 32 | + assert_eq!((ptr as usize) % align, 0, "Got a pointer less aligned than requested") |
| 33 | + } |
| 34 | + |
| 35 | + // Clean up |
| 36 | + for &ptr in &pointers { |
| 37 | + allocator.dealloc(ptr, Layout::from_size_align(size, align).unwrap()) |
| 38 | + } |
| 39 | + } |
| 40 | +} |
0 commit comments