Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the crate no_std + alloc #44

Closed
wants to merge 26 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
64671df
Started replacing `std` for `core` and `alloc`.
Jan 29, 2020
b15845b
Created an `std` feature for the crate, which is on by default.
Jan 29, 2020
73e3da2
Formatted the code with `cargo fmt`.
Jan 29, 2020
e4bda31
Made it so that the main functionality of the crate is not limited to…
Jan 29, 2020
93f6068
Fixed tests.
Jan 29, 2020
6cc5506
Formatted the code.
Jan 29, 2020
9da3663
Use `std::thread::yield_now()` when possible.
Jan 29, 2020
ae15433
Configured imports according to the use of the `std` feature.
Jan 29, 2020
a627cd9
Configured imports according to the use of the `std` feature.
Jan 29, 2020
75cc0d2
Merge branch 'master' of https://github.com/GarkGarcia/flurry
Jan 29, 2020
06f174b
Removed the `cfg_if` business.
Jan 29, 2020
a6c734f
Added comment about the use of `core::sync::atomic::spin_loop_hint()`.
Jan 29, 2020
2da8b06
Worked on fixing issues with `no_std`.
Jan 30, 2020
0928883
Fixed issues regarding the absence of `epoch::pin` in `no_std` enviro…
Jan 30, 2020
366c90c
Removed unnecessary lifetime parameters.
Jan 30, 2020
bb3e0e9
Updated `azure-pipelines.yml`.
Jan 30, 2020
5f94576
Minor changes.
Jan 30, 2020
3b0728e
Integrated `ahash::RandomState` as the dafault value for the `S` type…
Jan 30, 2020
c21ccd4
Added note about criptographic security in the `HashMap` docs.
Jan 30, 2020
57010a0
Fixed `missing_debug_implementations` issue.
Jan 30, 2020
64d2d94
Merge branch 'master' into gg/master
jonhoo Jan 30, 2020
69bb181
cargo fmt
jonhoo Jan 30, 2020
309d746
Add target to use it
jonhoo Jan 30, 2020
7b32a16
num_cpus is a std thing
jonhoo Jan 30, 2020
adb58e0
Make map generic over lock type
jonhoo Jan 30, 2020
f9ad723
Fix tests to match new generic-over-lock API
jonhoo Jan 30, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,17 @@ codecov = { repository = "jonhoo/flurry", branch = "master", service = "github"
maintenance = { status = "experimental" }

[dependencies]
crossbeam-epoch = "0.8"
parking_lot = "0.10"
num_cpus = "1.12.0"

[dependencies.crossbeam-epoch]
version = "0.8"
default-features = false
features = ["alloc"]

[dev-dependencies]
rand = "0.7"

[features]
default = ["std"]
std = ["crossbeam-epoch/std"]
4 changes: 2 additions & 2 deletions src/iter/iter.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::NodeIter;
use core::sync::atomic::Ordering;
use crossbeam_epoch::Guard;
use std::sync::atomic::Ordering;

/// An iterator over a map's entries.
///
Expand Down Expand Up @@ -61,9 +61,9 @@ impl<'g, K, V> Iterator for Values<'g, K, V> {
#[cfg(test)]
mod tests {
use crate::HashMap;
use core::iter::FromIterator;
use crossbeam_epoch as epoch;
use std::collections::HashSet;
use std::iter::FromIterator;

#[test]
fn iter() {
Expand Down
2 changes: 1 addition & 1 deletion src/iter/traverser.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::node::{BinEntry, Node};
use crate::raw::Table;
use core::sync::atomic::Ordering;
use crossbeam_epoch::{Guard, Shared};
use std::sync::atomic::Ordering;

#[derive(Debug)]
pub(crate) struct NodeIter<'g, K, V> {
Expand Down
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,5 +211,7 @@ pub use map::HashMap;

/// Types needed to safely access shared data concurrently.
pub mod epoch {
pub use crossbeam_epoch::{pin, Guard};
#[cfg(feature = "std")]
pub use crossbeam_epoch::pin;
pub use crossbeam_epoch::Guard;
}
30 changes: 17 additions & 13 deletions src/map.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
use crate::iter::*;
use crate::node::*;
use crate::raw::*;
use core::borrow::Borrow;
use core::fmt::{self, Debug, Formatter};
use core::hash::{BuildHasher, Hash, Hasher};
use core::iter::FromIterator;
use core::sync::atomic::{AtomicIsize, AtomicUsize, Ordering};
use crossbeam_epoch::{self as epoch, Atomic, Guard, Owned, Shared};
use std::borrow::Borrow;
#[cfg(feature = "std")]
use std::collections::hash_map::RandomState;
use std::fmt::{self, Debug, Formatter};
use std::hash::{BuildHasher, Hash, Hasher};
use std::iter::FromIterator;
use std::sync::{
atomic::{AtomicIsize, AtomicUsize, Ordering},
Once,
};
use std::sync::Once;

macro_rules! isize_bits {
() => {
std::mem::size_of::<isize>() * 8
core::mem::size_of::<isize>() * 8
};
}
jonhoo marked this conversation as resolved.
Show resolved Hide resolved

Expand Down Expand Up @@ -85,6 +84,7 @@ pub struct HashMap<K, V, S = RandomState> {
build_hasher: S,
}

#[cfg(feature = "std")]
jonhoo marked this conversation as resolved.
Show resolved Hide resolved
impl<K, V> Default for HashMap<K, V, RandomState>
where
K: Sync + Send + Clone + Hash + Eq,
Expand All @@ -95,6 +95,7 @@ where
}
}

#[cfg(feature = "std")]
jonhoo marked this conversation as resolved.
Show resolved Hide resolved
impl<K, V> HashMap<K, V, RandomState>
where
K: Sync + Send + Clone + Hash + Eq,
Expand Down Expand Up @@ -311,7 +312,7 @@ where
let mut sc = self.size_ctl.load(Ordering::SeqCst);
if sc < 0 {
// we lost the initialization race; just spin
std::thread::yield_now();
core::sync::atomic::spin_loop_hint();
jonhoo marked this conversation as resolved.
Show resolved Hide resolved
continue;
}

Expand Down Expand Up @@ -583,7 +584,7 @@ where
fn add_count(&self, n: isize, resize_hint: Option<usize>, guard: &Guard) {
// TODO: implement the Java CounterCell business here

use std::cmp;
use core::cmp;
let mut count = match n.cmp(&0) {
cmp::Ordering::Greater => {
let n = n as usize;
Expand Down Expand Up @@ -672,7 +673,7 @@ where
let ncpu = num_cpus();

let stride = if ncpu > 1 { (n >> 3) / ncpu } else { n };
let stride = std::cmp::max(stride as isize, MIN_TRANSFER_STRIDE);
let stride = core::cmp::max(stride as isize, MIN_TRANSFER_STRIDE);

if next_table.is_null() {
// we are initiating a resize
Expand Down Expand Up @@ -957,7 +958,7 @@ where
// TODO: find out if this is neccessary
let size = size + (size >> 1) + 1;

std::cmp::min(MAXIMUM_CAPACITY, size.next_power_of_two())
core::cmp::min(MAXIMUM_CAPACITY, size.next_power_of_two())
} as isize;

loop {
Expand Down Expand Up @@ -1447,6 +1448,7 @@ where
}
}

#[cfg(feature = "std")]
jonhoo marked this conversation as resolved.
Show resolved Hide resolved
impl<K, V> FromIterator<(K, V)> for HashMap<K, V, RandomState>
where
K: Sync + Send + Clone + Hash + Eq,
Expand All @@ -1472,6 +1474,7 @@ where
}
}

#[cfg(feature = "std")]
impl<'a, K, V> FromIterator<(&'a K, &'a V)> for HashMap<K, V, RandomState>
where
K: Sync + Send + Copy + Hash + Eq,
Expand All @@ -1483,6 +1486,7 @@ where
}
}

#[cfg(feature = "std")]
impl<'a, K, V> FromIterator<&'a (K, V)> for HashMap<K, V, RandomState>
where
K: Sync + Send + Copy + Hash + Eq,
Expand Down
4 changes: 2 additions & 2 deletions src/node.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::raw::Table;
use core::borrow::Borrow;
use core::sync::atomic::Ordering;
use crossbeam_epoch::{Atomic, Guard, Shared};
use parking_lot::Mutex;
use std::borrow::Borrow;
use std::sync::atomic::Ordering;

/// Entry in a bin.
///
Expand Down
9 changes: 6 additions & 3 deletions src/raw/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::node::*;
use core::fmt::Debug;
use core::sync::atomic::Ordering;
use crossbeam_epoch::{Atomic, Guard, Owned, Shared};
use std::fmt::Debug;
use std::sync::atomic::Ordering;

#[derive(Debug)]
pub(crate) struct Table<K, V> {
Expand Down Expand Up @@ -35,7 +35,10 @@ impl<K, V> Table<K, V> {
// anything in the map.
let guard = unsafe { crossbeam_epoch::unprotected() };

for bin in Vec::from(std::mem::replace(&mut self.bins, vec![].into_boxed_slice())) {
for bin in Vec::from(core::mem::replace(
&mut self.bins,
vec![].into_boxed_slice(),
)) {
if bin.load(Ordering::SeqCst, guard).is_null() {
// bin was never used
continue;
Expand Down