Skip to content
This repository was archived by the owner on Feb 10, 2024. It is now read-only.

Commit 85fddaa

Browse files
bors[bot]glandiumAmanieuPhlosioneerfrewsxcv
committed
Merge #23
23: Merge upstream changes and bump versions r=cuviper a=cuviper Co-authored-by: Mike Hommey <mh@glandium.org> Co-authored-by: Amanieu d'Antras <amanieu@gmail.com> Co-authored-by: Phlosioneer <mattmdrr2@gmail.com> Co-authored-by: Corey Farwell <coreyf@rwell.org> Co-authored-by: Guillaume Gomez <guillaume1.gomez@gmail.com> Co-authored-by: Kornel <kornel@geekhood.net> Co-authored-by: Simon Sapin <simon.sapin@exyr.org> Co-authored-by: bors <bors@rust-lang.org> Co-authored-by: Josh Stone <cuviper@gmail.com>
2 parents 823da8b + 5261ef4 commit 85fddaa

File tree

11 files changed

+369
-248
lines changed

11 files changed

+369
-248
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ sudo: false
44
matrix:
55
fast_finish: true
66
include:
7-
- rust: 1.26.0
7+
- rust: 1.28.0
88
- rust: stable
99
- rust: beta
1010
- rust: nightly

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
authors = ["Josh Stone <cuviper@gmail.com>"]
33
name = "rayon-hash"
4-
version = "0.3.0"
4+
version = "0.4.0"
55
repository = "https://github.com/rayon-rs/rayon-hash"
66
documentation = "https://docs.rs/rayon-hash/"
77
keywords = ["parallel", "iterator", "hash", "map", "set"]
@@ -14,4 +14,4 @@ readme = "README.md"
1414
rayon = "1.0"
1515

1616
[dev-dependencies]
17-
rand = "0.4"
17+
rand = "0.5"

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ test std_set_sum_parallel ... bench: 8,519,683 ns/iter (+/- 219,785)
1919
test std_set_sum_serial ... bench: 6,295,263 ns/iter (+/- 98,600)
2020
```
2121

22-
This crate currently requires `rustc 1.26.0` or greater.
22+
This crate currently requires `rustc 1.28.0` or greater.
2323

2424
## Known limitations
2525

benches/set_sum.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ extern crate rayon;
66
extern crate rayon_hash;
77

88
use rand::{Rng, SeedableRng, XorShiftRng};
9+
use rand::distributions::Standard;
910
use std::collections::HashSet as StdHashSet;
1011
use rayon_hash::HashSet as RayonHashSet;
1112
use std::iter::FromIterator;
@@ -14,8 +15,9 @@ use test::Bencher;
1415

1516

1617
fn default_set<C: FromIterator<u32>>(n: usize) -> C {
17-
let mut rng = XorShiftRng::from_seed([0, 1, 2, 3]);
18-
(0..n).map(|_| rng.next_u32()).collect()
18+
let mut seed = <XorShiftRng as SeedableRng>::Seed::default();
19+
(0..).zip(seed.as_mut()).for_each(|(i, x)| *x = i);
20+
XorShiftRng::from_seed(seed).sample_iter(&Standard).take(n).collect()
1921
}
2022

2123
macro_rules! bench_set_sum {

src/alloc.rs

Lines changed: 219 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,216 @@
1-
#[cfg(rayon_hash_unstable)] pub use std::alloc::oom;
2-
#[cfg(not(rayon_hash_unstable))] pub use std::process::abort as oom;
1+
// Copyright 2015 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+
//! Memory allocation APIs (forked from `libcore/alloc.rs`)
12+
13+
// #![stable(feature = "alloc_module", since = "1.28.0")]
14+
15+
use std::cmp;
16+
use std::mem;
17+
use std::usize;
18+
use std::alloc::{self, LayoutErr};
19+
20+
#[inline]
21+
fn layout_err() -> LayoutErr {
22+
alloc::Layout::from_size_align(usize::MAX, usize::MAX).unwrap_err()
23+
}
24+
25+
/// Layout of a block of memory.
26+
///
27+
/// An instance of `Layout` describes a particular layout of memory.
28+
/// You build a `Layout` up as an input to give to an allocator.
29+
///
30+
/// All layouts have an associated non-negative size and a
31+
/// power-of-two alignment.
32+
///
33+
/// (Note however that layouts are *not* required to have positive
34+
/// size, even though many allocators require that all memory
35+
/// requests have positive size. A caller to the `Alloc::alloc`
36+
/// method must either ensure that conditions like this are met, or
37+
/// use specific allocators with looser requirements.)
38+
// #[stable(feature = "alloc_layout", since = "1.28.0")]
39+
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
40+
pub(crate) struct Layout {
41+
inner: alloc::Layout,
42+
}
43+
44+
impl Layout {
45+
/// Constructs a `Layout` from a given `size` and `align`,
46+
/// or returns `LayoutErr` if either of the following conditions
47+
/// are not met:
48+
///
49+
/// * `align` must be a power of two,
50+
///
51+
/// * `size`, when rounded up to the nearest multiple of `align`,
52+
/// must not overflow (i.e. the rounded value must be less than
53+
/// `usize::MAX`).
54+
// #[stable(feature = "alloc_layout", since = "1.28.0")]
55+
#[inline]
56+
pub(crate) fn from_size_align(size: usize, align: usize) -> Result<Self, LayoutErr> {
57+
alloc::Layout::from_size_align(size, align).map(Layout::from)
58+
}
59+
60+
/// Creates a layout, bypassing all checks.
61+
///
62+
/// # Safety
63+
///
64+
/// This function is unsafe as it does not verify the preconditions from
65+
/// [`Layout::from_size_align`](#method.from_size_align).
66+
// #[stable(feature = "alloc_layout", since = "1.28.0")]
67+
#[inline]
68+
pub(crate) unsafe fn from_size_align_unchecked(size: usize, align: usize) -> Self {
69+
Layout::from(alloc::Layout::from_size_align_unchecked(size, align))
70+
}
71+
72+
/// The minimum size in bytes for a memory block of this layout.
73+
// #[stable(feature = "alloc_layout", since = "1.28.0")]
74+
#[inline]
75+
pub(crate) fn size(&self) -> usize { self.inner.size() }
76+
77+
/// The minimum byte alignment for a memory block of this layout.
78+
// #[stable(feature = "alloc_layout", since = "1.28.0")]
79+
#[inline]
80+
pub(crate) fn align(&self) -> usize { self.inner.align() }
81+
82+
/// Constructs a `Layout` suitable for holding a value of type `T`.
83+
// #[stable(feature = "alloc_layout", since = "1.28.0")]
84+
#[inline]
85+
pub(crate) fn new<T>() -> Self {
86+
Layout::from(alloc::Layout::new::<T>())
87+
}
88+
89+
/// Returns the amount of padding we must insert after `self`
90+
/// to ensure that the following address will satisfy `align`
91+
/// (measured in bytes).
92+
///
93+
/// E.g. if `self.size()` is 9, then `self.padding_needed_for(4)`
94+
/// returns 3, because that is the minimum number of bytes of
95+
/// padding required to get a 4-aligned address (assuming that the
96+
/// corresponding memory block starts at a 4-aligned address).
97+
///
98+
/// The return value of this function has no meaning if `align` is
99+
/// not a power-of-two.
100+
///
101+
/// Note that the utility of the returned value requires `align`
102+
/// to be less than or equal to the alignment of the starting
103+
/// address for the whole allocated block of memory. One way to
104+
/// satisfy this constraint is to ensure `align <= self.align()`.
105+
// #[unstable(feature = "allocator_api", issue = "32838")]
106+
#[inline]
107+
pub(crate) fn padding_needed_for(&self, align: usize) -> usize {
108+
let len = self.size();
109+
110+
// Rounded up value is:
111+
// len_rounded_up = (len + align - 1) & !(align - 1);
112+
// and then we return the padding difference: `len_rounded_up - len`.
113+
//
114+
// We use modular arithmetic throughout:
115+
//
116+
// 1. align is guaranteed to be > 0, so align - 1 is always
117+
// valid.
118+
//
119+
// 2. `len + align - 1` can overflow by at most `align - 1`,
120+
// so the &-mask wth `!(align - 1)` will ensure that in the
121+
// case of overflow, `len_rounded_up` will itself be 0.
122+
// Thus the returned padding, when added to `len`, yields 0,
123+
// which trivially satisfies the alignment `align`.
124+
//
125+
// (Of course, attempts to allocate blocks of memory whose
126+
// size and padding overflow in the above manner should cause
127+
// the allocator to yield an error anyway.)
128+
129+
let len_rounded_up = len.wrapping_add(align).wrapping_sub(1)
130+
& !align.wrapping_sub(1);
131+
return len_rounded_up.wrapping_sub(len);
132+
}
133+
134+
/// Creates a layout describing the record for `n` instances of
135+
/// `self`, with a suitable amount of padding between each to
136+
/// ensure that each instance is given its requested size and
137+
/// alignment. On success, returns `(k, offs)` where `k` is the
138+
/// layout of the array and `offs` is the distance between the start
139+
/// of each element in the array.
140+
///
141+
/// On arithmetic overflow, returns `LayoutErr`.
142+
// #[unstable(feature = "allocator_api", issue = "32838")]
143+
#[inline]
144+
pub(crate) fn repeat(&self, n: usize) -> Result<(Self, usize), LayoutErr> {
145+
let padded_size = self.size().checked_add(self.padding_needed_for(self.align()))
146+
.ok_or(layout_err())?;
147+
let alloc_size = padded_size.checked_mul(n)
148+
.ok_or(layout_err())?;
149+
150+
unsafe {
151+
// self.align is already known to be valid and alloc_size has been
152+
// padded already.
153+
Ok((Layout::from_size_align_unchecked(alloc_size, self.align()), padded_size))
154+
}
155+
}
156+
157+
/// Creates a layout describing the record for `self` followed by
158+
/// `next`, including any necessary padding to ensure that `next`
159+
/// will be properly aligned. Note that the result layout will
160+
/// satisfy the alignment properties of both `self` and `next`.
161+
///
162+
/// Returns `Some((k, offset))`, where `k` is layout of the concatenated
163+
/// record and `offset` is the relative location, in bytes, of the
164+
/// start of the `next` embedded within the concatenated record
165+
/// (assuming that the record itself starts at offset 0).
166+
///
167+
/// On arithmetic overflow, returns `LayoutErr`.
168+
// #[unstable(feature = "allocator_api", issue = "32838")]
169+
#[inline]
170+
pub(crate) fn extend(&self, next: Self) -> Result<(Self, usize), LayoutErr> {
171+
let new_align = cmp::max(self.align(), next.align());
172+
let pad = self.padding_needed_for(next.align());
173+
174+
let offset = self.size().checked_add(pad)
175+
.ok_or(layout_err())?;
176+
let new_size = offset.checked_add(next.size())
177+
.ok_or(layout_err())?;
178+
179+
let layout = Layout::from_size_align(new_size, new_align)?;
180+
Ok((layout, offset))
181+
}
182+
183+
/// Creates a layout describing the record for a `[T; n]`.
184+
///
185+
/// On arithmetic overflow, returns `LayoutErr`.
186+
// #[unstable(feature = "allocator_api", issue = "32838")]
187+
#[inline]
188+
pub(crate) fn array<T>(n: usize) -> Result<Self, LayoutErr> {
189+
Layout::new::<T>()
190+
.repeat(n)
191+
.map(|(k, offs)| {
192+
debug_assert!(offs == mem::size_of::<T>());
193+
k
194+
})
195+
}
196+
}
197+
198+
impl From<alloc::Layout> for Layout {
199+
#[inline]
200+
fn from(inner: alloc::Layout) -> Layout {
201+
Layout { inner }
202+
}
203+
}
204+
205+
impl From<Layout> for alloc::Layout {
206+
#[inline]
207+
fn from(layout: Layout) -> alloc::Layout {
208+
layout.inner
209+
}
210+
}
3211

4212
/// Augments `AllocErr` with a CapacityOverflow variant.
213+
// FIXME: should this be in libcore or liballoc?
5214
#[derive(Clone, PartialEq, Eq, Debug)]
6215
// #[unstable(feature = "try_reserve", reason = "new API", issue="48043")]
7216
pub enum CollectionAllocErr {
@@ -11,3 +220,11 @@ pub enum CollectionAllocErr {
11220
/// Error due to the allocator (see the `AllocErr` type's docs).
12221
AllocErr,
13222
}
223+
224+
// #[unstable(feature = "try_reserve", reason = "new API", issue="48043")]
225+
impl From<LayoutErr> for CollectionAllocErr {
226+
#[inline]
227+
fn from(_: LayoutErr) -> Self {
228+
CollectionAllocErr::CapacityOverflow
229+
}
230+
}

src/heap.rs

Lines changed: 0 additions & 71 deletions
This file was deleted.

src/lib.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
1-
#![doc(html_root_url = "https://docs.rs/rayon-hash/0.3")]
2-
3-
#![cfg_attr(rayon_hash_unstable, feature(allocator_api))]
1+
#![doc(html_root_url = "https://docs.rs/rayon-hash/0.4")]
42

53
extern crate rayon;
64

75
#[cfg(test)] extern crate rand;
86

97
mod alloc;
10-
mod heap;
11-
128
mod ptr;
139

1410
// #[stable(feature = "rust1", since = "1.0.0")]

src/ptr.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use std::ptr::NonNull;
3232
///
3333
/// Unlike `*mut T`, `Unique<T>` is covariant over `T`. This should always be correct
3434
/// for any type which upholds Unique's aliasing requirements.
35-
pub struct Unique<T: ?Sized> {
35+
pub(crate) struct Unique<T: ?Sized> {
3636
pointer: NonNull<T>,
3737
// NOTE: this marker has no consequences for variance, but is necessary
3838
// for dropck to understand that we logically own a `T`.
@@ -66,12 +66,12 @@ impl<T: ?Sized> Unique<T> {
6666
/// # Safety
6767
///
6868
/// `ptr` must be non-null.
69-
pub unsafe fn new_unchecked(ptr: *mut T) -> Self {
69+
pub(crate) unsafe fn new_unchecked(ptr: *mut T) -> Self {
7070
Unique { pointer: NonNull::new_unchecked(ptr), _marker: PhantomData }
7171
}
7272

7373
/// Acquires the underlying `*mut` pointer.
74-
pub fn as_ptr(self) -> *mut T {
74+
pub(crate) fn as_ptr(self) -> *mut T {
7575
self.pointer.as_ptr()
7676
}
7777
}

0 commit comments

Comments
 (0)