--- /dev/null
+// 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)
+}
+
+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())
+ }
+ }
+}
#![deny(warnings)]
+#![feature(allocator_api)]
+#![feature(alloc_system)]
#![feature(attr_literals)]
#![feature(box_syntax)]
#![feature(inclusive_range_syntax)]
#![feature(unboxed_closures)]
#![feature(unicode)]
+extern crate alloc_system;
extern crate std_unicode;
extern crate rand;
mod btree;
mod cow_str;
mod fmt;
+mod heap;
mod linked_list;
mod slice;
mod str;
unsafe impl<'a> Alloc for &'a System {
#[inline]
unsafe fn alloc(&mut self, layout: Layout) -> Result<*mut u8, AllocErr> {
- let ptr = if layout.align() <= MIN_ALIGN {
+ let ptr = if layout.align() <= MIN_ALIGN && layout.align() <= layout.size() {
libc::malloc(layout.size()) as *mut u8
} else {
aligned_malloc(&layout)
unsafe fn alloc_zeroed(&mut self, layout: Layout)
-> Result<*mut u8, AllocErr>
{
- if layout.align() <= MIN_ALIGN {
+ if layout.align() <= MIN_ALIGN && layout.align() <= layout.size() {
let ptr = libc::calloc(layout.size(), 1) as *mut u8;
if !ptr.is_null() {
Ok(ptr)
})
}
- if new_layout.align() <= MIN_ALIGN {
+ if new_layout.align() <= MIN_ALIGN && new_layout.align() <= new_layout.size(){
let ptr = libc::realloc(ptr as *mut libc::c_void, new_layout.size());
if !ptr.is_null() {
Ok(ptr as *mut u8)