about summary refs log tree commit diff
path: root/src/liballoc/tests
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2017-11-25 06:16:19 +0000
committerbors <bors@rust-lang.org>2017-11-25 06:16:19 +0000
commit59bf09d4d473c803609d3ad925a0ebf13bdbb0ab (patch)
tree8357c4f6e256ae8b198de5f1bd729118d6dd1f74 /src/liballoc/tests
parentca8ef2629363ead527452d15ab628633c8fd2d52 (diff)
parent43e32b53462e139c560672102724e8a8c859dbf7 (diff)
downloadrust-59bf09d4d473c803609d3ad925a0ebf13bdbb0ab.tar.gz
rust-59bf09d4d473c803609d3ad925a0ebf13bdbb0ab.zip
Auto merge of #46117 - SimonSapin:min-align, r=alexcrichton
allocators: don’t assume MIN_ALIGN for small sizes

See individual commit messages.
Diffstat (limited to 'src/liballoc/tests')
-rw-r--r--src/liballoc/tests/heap.rs45
-rw-r--r--src/liballoc/tests/lib.rs4
2 files changed, 49 insertions, 0 deletions
diff --git a/src/liballoc/tests/heap.rs b/src/liballoc/tests/heap.rs
new file mode 100644
index 00000000000..d3ce12056bb
--- /dev/null
+++ b/src/liballoc/tests/heap.rs
@@ -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())
+        }
+    }
+}
diff --git a/src/liballoc/tests/lib.rs b/src/liballoc/tests/lib.rs
index 00ebd88d464..f1e95883b38 100644
--- a/src/liballoc/tests/lib.rs
+++ b/src/liballoc/tests/lib.rs
@@ -10,6 +10,8 @@
 
 #![deny(warnings)]
 
+#![feature(allocator_api)]
+#![feature(alloc_system)]
 #![feature(attr_literals)]
 #![feature(box_syntax)]
 #![feature(inclusive_range_syntax)]
@@ -29,6 +31,7 @@
 #![feature(unboxed_closures)]
 #![feature(unicode)]
 
+extern crate alloc_system;
 extern crate std_unicode;
 extern crate rand;
 
@@ -39,6 +42,7 @@ mod binary_heap;
 mod btree;
 mod cow_str;
 mod fmt;
+mod heap;
 mod linked_list;
 mod slice;
 mod str;