2015-03-10 23:58:16 -05:00
|
|
|
// Copyright 2015 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.
|
|
|
|
|
2016-01-30 18:19:37 -06:00
|
|
|
#![deny(warnings)]
|
|
|
|
|
alloc_system: don’t assume MIN_ALIGN for small sizes, fix #45955
The GNU C library (glibc) is documented to always allocate with an alignment
of at least 8 or 16 bytes, on 32-bit or 64-bit platforms:
https://www.gnu.org/software/libc/manual/html_node/Aligned-Memory-Blocks.html
This matches our use of `MIN_ALIGN` before this commit.
However, even when libc is glibc, the program might be linked
with another allocator that redefines the `malloc` symbol and friends.
(The `alloc_jemalloc` crate does, in some cases.)
So `alloc_system` doesn’t know which allocator it calls,
and needs to be conservative in assumptions it makes.
The C standard says:
https://port70.net/%7Ensz/c/c11/n1570.html#7.22.3
> The pointer returned if the allocation succeeds is suitably aligned
> so that it may be assigned to a pointer to any type of object
> with a fundamental alignment requirement
https://port70.net/~nsz/c/c11/n1570.html#6.2.8p2
> A fundamental alignment is represented by an alignment less than
> or equal to the greatest alignment supported by the implementation
> in all contexts, which is equal to `_Alignof (max_align_t)`.
`_Alignof (max_align_t)` depends on the ABI and doesn’t seem to have
a clear definition, but it seems to match our `MIN_ALIGN` in practice.
However, the size of objects is rounded up to the next multiple
of their alignment (since that size is also the stride used in arrays).
Conversely, the alignment of a non-zero-size object is at most its size.
So for example it seems ot be legal for `malloc(8)` to return a pointer
that’s only 8-bytes-aligned, even if `_Alignof (max_align_t)` is 16.
2017-11-20 08:30:04 -06:00
|
|
|
#![feature(allocator_api)]
|
|
|
|
#![feature(alloc_system)]
|
2017-05-31 22:24:19 -05:00
|
|
|
#![feature(attr_literals)]
|
2015-03-10 23:58:16 -05:00
|
|
|
#![feature(box_syntax)]
|
2017-02-18 12:57:18 -06:00
|
|
|
#![feature(inclusive_range_syntax)]
|
2016-12-22 11:15:13 -06:00
|
|
|
#![feature(collection_placement)]
|
2015-05-29 08:42:32 -05:00
|
|
|
#![feature(const_fn)]
|
2017-07-14 20:54:17 -05:00
|
|
|
#![feature(drain_filter)]
|
2016-11-22 16:31:31 -06:00
|
|
|
#![feature(exact_size_is_empty)]
|
2017-05-30 10:40:50 -05:00
|
|
|
#![feature(iterator_step_by)]
|
2015-06-10 15:33:52 -05:00
|
|
|
#![feature(pattern)]
|
2016-12-22 11:15:13 -06:00
|
|
|
#![feature(placement_in_syntax)]
|
2015-03-10 23:58:16 -05:00
|
|
|
#![feature(rand)]
|
2017-04-08 15:55:53 -05:00
|
|
|
#![feature(splice)]
|
2015-06-10 15:33:52 -05:00
|
|
|
#![feature(str_escape)]
|
2017-07-26 17:09:32 -05:00
|
|
|
#![feature(string_retain)]
|
2015-03-10 23:58:16 -05:00
|
|
|
#![feature(unboxed_closures)]
|
|
|
|
#![feature(unicode)]
|
2018-01-11 04:13:45 -06:00
|
|
|
#![feature(exact_chunks)]
|
2015-07-11 06:34:57 -05:00
|
|
|
|
alloc_system: don’t assume MIN_ALIGN for small sizes, fix #45955
The GNU C library (glibc) is documented to always allocate with an alignment
of at least 8 or 16 bytes, on 32-bit or 64-bit platforms:
https://www.gnu.org/software/libc/manual/html_node/Aligned-Memory-Blocks.html
This matches our use of `MIN_ALIGN` before this commit.
However, even when libc is glibc, the program might be linked
with another allocator that redefines the `malloc` symbol and friends.
(The `alloc_jemalloc` crate does, in some cases.)
So `alloc_system` doesn’t know which allocator it calls,
and needs to be conservative in assumptions it makes.
The C standard says:
https://port70.net/%7Ensz/c/c11/n1570.html#7.22.3
> The pointer returned if the allocation succeeds is suitably aligned
> so that it may be assigned to a pointer to any type of object
> with a fundamental alignment requirement
https://port70.net/~nsz/c/c11/n1570.html#6.2.8p2
> A fundamental alignment is represented by an alignment less than
> or equal to the greatest alignment supported by the implementation
> in all contexts, which is equal to `_Alignof (max_align_t)`.
`_Alignof (max_align_t)` depends on the ABI and doesn’t seem to have
a clear definition, but it seems to match our `MIN_ALIGN` in practice.
However, the size of objects is rounded up to the next multiple
of their alignment (since that size is also the stride used in arrays).
Conversely, the alignment of a non-zero-size object is at most its size.
So for example it seems ot be legal for `malloc(8)` to return a pointer
that’s only 8-bytes-aligned, even if `_Alignof (max_align_t)` is 16.
2017-11-20 08:30:04 -06:00
|
|
|
extern crate alloc_system;
|
2016-11-29 13:38:08 -06:00
|
|
|
extern crate std_unicode;
|
2017-11-01 14:32:13 -05:00
|
|
|
extern crate rand;
|
2015-03-10 23:58:16 -05:00
|
|
|
|
2016-09-28 19:23:36 -05:00
|
|
|
use std::hash::{Hash, Hasher};
|
|
|
|
use std::collections::hash_map::DefaultHasher;
|
2015-08-11 19:27:05 -05:00
|
|
|
|
2015-03-10 23:58:16 -05:00
|
|
|
mod binary_heap;
|
|
|
|
mod btree;
|
2016-11-03 20:07:00 -05:00
|
|
|
mod cow_str;
|
2015-03-10 23:58:16 -05:00
|
|
|
mod fmt;
|
alloc_system: don’t assume MIN_ALIGN for small sizes, fix #45955
The GNU C library (glibc) is documented to always allocate with an alignment
of at least 8 or 16 bytes, on 32-bit or 64-bit platforms:
https://www.gnu.org/software/libc/manual/html_node/Aligned-Memory-Blocks.html
This matches our use of `MIN_ALIGN` before this commit.
However, even when libc is glibc, the program might be linked
with another allocator that redefines the `malloc` symbol and friends.
(The `alloc_jemalloc` crate does, in some cases.)
So `alloc_system` doesn’t know which allocator it calls,
and needs to be conservative in assumptions it makes.
The C standard says:
https://port70.net/%7Ensz/c/c11/n1570.html#7.22.3
> The pointer returned if the allocation succeeds is suitably aligned
> so that it may be assigned to a pointer to any type of object
> with a fundamental alignment requirement
https://port70.net/~nsz/c/c11/n1570.html#6.2.8p2
> A fundamental alignment is represented by an alignment less than
> or equal to the greatest alignment supported by the implementation
> in all contexts, which is equal to `_Alignof (max_align_t)`.
`_Alignof (max_align_t)` depends on the ABI and doesn’t seem to have
a clear definition, but it seems to match our `MIN_ALIGN` in practice.
However, the size of objects is rounded up to the next multiple
of their alignment (since that size is also the stride used in arrays).
Conversely, the alignment of a non-zero-size object is at most its size.
So for example it seems ot be legal for `malloc(8)` to return a pointer
that’s only 8-bytes-aligned, even if `_Alignof (max_align_t)` is 16.
2017-11-20 08:30:04 -06:00
|
|
|
mod heap;
|
2015-03-10 23:58:16 -05:00
|
|
|
mod linked_list;
|
|
|
|
mod slice;
|
|
|
|
mod str;
|
|
|
|
mod string;
|
|
|
|
mod vec_deque;
|
|
|
|
mod vec;
|
2015-08-11 19:27:05 -05:00
|
|
|
|
|
|
|
fn hash<T: Hash>(t: &T) -> u64 {
|
2016-09-28 19:23:36 -05:00
|
|
|
let mut s = DefaultHasher::new();
|
2015-08-11 19:27:05 -05:00
|
|
|
t.hash(&mut s);
|
|
|
|
s.finish()
|
|
|
|
}
|
2017-08-21 09:15:02 -05:00
|
|
|
|
2017-09-10 11:13:19 -05:00
|
|
|
// FIXME: Instantiated functions with i128 in the signature is not supported in Emscripten.
|
|
|
|
// See https://github.com/kripken/emscripten-fastcomp/issues/169
|
|
|
|
#[cfg(not(target_os = "emscripten"))]
|
2017-08-21 09:15:02 -05:00
|
|
|
#[test]
|
|
|
|
fn test_boxed_hasher() {
|
|
|
|
let ordinary_hash = hash(&5u32);
|
|
|
|
|
|
|
|
let mut hasher_1 = Box::new(DefaultHasher::new());
|
|
|
|
5u32.hash(&mut hasher_1);
|
|
|
|
assert_eq!(ordinary_hash, hasher_1.finish());
|
|
|
|
|
|
|
|
let mut hasher_2 = Box::new(DefaultHasher::new()) as Box<Hasher>;
|
|
|
|
5u32.hash(&mut hasher_2);
|
|
|
|
assert_eq!(ordinary_hash, hasher_2.finish());
|
|
|
|
}
|