Skip to content

Commit

Permalink
Format hashglobe #21373
Browse files Browse the repository at this point in the history
  • Loading branch information
kingdido999 committed Sep 9, 2018
1 parent 2c9e32a commit 951bda3
Show file tree
Hide file tree
Showing 7 changed files with 552 additions and 434 deletions.
46 changes: 20 additions & 26 deletions components/hashglobe/src/alloc.rs
@@ -1,25 +1,26 @@
// FORK NOTE: Copied from liballoc_system, removed unnecessary APIs,
// APIs take size/align directly instead of Layout




// The minimum alignment guaranteed by the architecture. This value is used to
// add fast paths for low alignment values. In practice, the alignment is a
// constant at the call site and the branch will be optimized out.
#[cfg(all(any(target_arch = "x86",
target_arch = "arm",
target_arch = "mips",
target_arch = "powerpc",
target_arch = "powerpc64",
target_arch = "asmjs",
target_arch = "wasm32")))]
#[cfg(all(any(
target_arch = "x86",
target_arch = "arm",
target_arch = "mips",
target_arch = "powerpc",
target_arch = "powerpc64",
target_arch = "asmjs",
target_arch = "wasm32"
)))]
const MIN_ALIGN: usize = 8;
#[cfg(all(any(target_arch = "x86_64",
target_arch = "aarch64",
target_arch = "mips64",
target_arch = "s390x",
target_arch = "sparc64")))]
#[cfg(all(any(
target_arch = "x86_64",
target_arch = "aarch64",
target_arch = "mips64",
target_arch = "s390x",
target_arch = "sparc64"
)))]
const MIN_ALIGN: usize = 16;

pub use self::platform::{alloc, dealloc, realloc};
Expand Down Expand Up @@ -100,7 +101,6 @@ mod platform {
type DWORD = u32;
type BOOL = i32;


extern "system" {
fn GetProcessHeap() -> HANDLE;
fn HeapAlloc(hHeap: HANDLE, dwFlags: DWORD, dwBytes: SIZE_T) -> LPVOID;
Expand All @@ -123,8 +123,7 @@ mod platform {
}

#[inline]
unsafe fn allocate_with_flags(size: usize, align: usize, flags: DWORD) -> *mut u8
{
unsafe fn allocate_with_flags(size: usize, align: usize, flags: DWORD) -> *mut u8 {
if align <= MIN_ALIGN {
HeapAlloc(GetProcessHeap(), flags, size)
} else {
Expand All @@ -147,21 +146,16 @@ mod platform {
pub unsafe fn dealloc(ptr: *mut u8, align: usize) {
if align <= MIN_ALIGN {
let err = HeapFree(GetProcessHeap(), 0, ptr as LPVOID);
debug_assert!(err != 0, "Failed to free heap memory: {}",
GetLastError());
debug_assert!(err != 0, "Failed to free heap memory: {}", GetLastError());
} else {
let header = get_header(ptr);
let err = HeapFree(GetProcessHeap(), 0, header.0 as LPVOID);
debug_assert!(err != 0, "Failed to free heap memory: {}",
GetLastError());
debug_assert!(err != 0, "Failed to free heap memory: {}", GetLastError());
}
}

#[inline]
pub unsafe fn realloc(ptr: *mut u8, new_size: usize) -> *mut u8 {
HeapReAlloc(GetProcessHeap(),
0,
ptr as LPVOID,
new_size) as *mut u8
HeapReAlloc(GetProcessHeap(), 0, ptr as LPVOID, new_size) as *mut u8
}
}
87 changes: 48 additions & 39 deletions components/hashglobe/src/fake.rs
Expand Up @@ -26,7 +26,6 @@ pub use std::collections::hash_set::{Iter as SetIter, IntoIter as SetIntoIter};
#[derive(Clone)]
pub struct HashMap<K, V, S = RandomState>(StdMap<K, V, S>);


use FailedAllocationError;

impl<K, V, S> Deref for HashMap<K, V, S> {
Expand All @@ -43,26 +42,30 @@ impl<K, V, S> DerefMut for HashMap<K, V, S> {
}

impl<K, V, S> HashMap<K, V, S>
where K: Eq + Hash,
S: BuildHasher
where
K: Eq + Hash,
S: BuildHasher,
{
#[inline]
pub fn try_with_hasher(hash_builder: S) -> Result<HashMap<K, V, S>, FailedAllocationError> {
Ok(HashMap(StdMap::with_hasher(hash_builder)))
}

#[inline]
pub fn try_with_capacity_and_hasher(capacity: usize,
hash_builder: S)
-> Result<HashMap<K, V, S>, FailedAllocationError> {
Ok(HashMap(StdMap::with_capacity_and_hasher(capacity, hash_builder)))
pub fn try_with_capacity_and_hasher(
capacity: usize,
hash_builder: S,
) -> Result<HashMap<K, V, S>, FailedAllocationError> {
Ok(HashMap(StdMap::with_capacity_and_hasher(
capacity,
hash_builder,
)))
}

pub fn with_capacity_and_hasher(capacity: usize, hash_builder: S) -> HashMap<K, V, S> {
HashMap(StdMap::with_capacity_and_hasher(capacity, hash_builder))
}


#[inline]
pub fn try_reserve(&mut self, additional: usize) -> Result<(), FailedAllocationError> {
Ok(self.reserve(additional))
Expand All @@ -85,7 +88,6 @@ impl<K, V, S> HashMap<K, V, S>
#[derive(Clone)]
pub struct HashSet<T, S = RandomState>(StdSet<T, S>);


impl<T, S> Deref for HashSet<T, S> {
type Target = StdSet<T, S>;
fn deref(&self) -> &Self::Target {
Expand All @@ -111,17 +113,16 @@ impl<T: Hash + Eq> HashSet<T, RandomState> {
}
}


impl<T, S> HashSet<T, S>
where T: Eq + Hash,
S: BuildHasher
where
T: Eq + Hash,
S: BuildHasher,
{
#[inline]
pub fn with_hasher(hasher: S) -> HashSet<T, S> {
HashSet(StdSet::with_hasher(hasher))
}


#[inline]
pub fn with_capacity_and_hasher(capacity: usize, hasher: S) -> HashSet<T, S> {
HashSet(StdSet::with_capacity_and_hasher(capacity, hasher))
Expand Down Expand Up @@ -153,34 +154,39 @@ impl<K: Hash + Eq, V, S: BuildHasher + Default> Default for HashMap<K, V, S> {
}

impl<K, V, S> fmt::Debug for HashMap<K, V, S>
where K: Eq + Hash + fmt::Debug,
V: fmt::Debug,
S: BuildHasher {
where
K: Eq + Hash + fmt::Debug,
V: fmt::Debug,
S: BuildHasher,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt(f)
}
}

impl<K, V, S> PartialEq for HashMap<K, V, S>
where K: Eq + Hash,
V: PartialEq,
S: BuildHasher
where
K: Eq + Hash,
V: PartialEq,
S: BuildHasher,
{
fn eq(&self, other: &HashMap<K, V, S>) -> bool {
self.0.eq(&other.0)
}
}

impl<K, V, S> Eq for HashMap<K, V, S>
where K: Eq + Hash,
V: Eq,
S: BuildHasher
where
K: Eq + Hash,
V: Eq,
S: BuildHasher,
{
}

impl<'a, K, V, S> IntoIterator for &'a HashMap<K, V, S>
where K: Eq + Hash,
S: BuildHasher
where
K: Eq + Hash,
S: BuildHasher,
{
type Item = (&'a K, &'a V);
type IntoIter = MapIter<'a, K, V>;
Expand All @@ -191,8 +197,9 @@ impl<'a, K, V, S> IntoIterator for &'a HashMap<K, V, S>
}

impl<'a, K, V, S> IntoIterator for &'a mut HashMap<K, V, S>
where K: Eq + Hash,
S: BuildHasher
where
K: Eq + Hash,
S: BuildHasher,
{
type Item = (&'a K, &'a mut V);
type IntoIter = MapIterMut<'a, K, V>;
Expand All @@ -209,32 +216,36 @@ impl<T: Eq + Hash, S: BuildHasher + Default> Default for HashSet<T, S> {
}

impl<T, S> fmt::Debug for HashSet<T, S>
where T: Eq + Hash + fmt::Debug,
S: BuildHasher
where
T: Eq + Hash + fmt::Debug,
S: BuildHasher,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt(f)
}
}

impl<T, S> PartialEq for HashSet<T, S>
where T: Eq + Hash,
S: BuildHasher
where
T: Eq + Hash,
S: BuildHasher,
{
fn eq(&self, other: &HashSet<T, S>) -> bool {
self.0.eq(&other.0)
}
}

impl<T, S> Eq for HashSet<T, S>
where T: Eq + Hash,
S: BuildHasher
where
T: Eq + Hash,
S: BuildHasher,
{
}

impl<'a, T, S> IntoIterator for &'a HashSet<T, S>
where T: Eq + Hash,
S: BuildHasher
where
T: Eq + Hash,
S: BuildHasher,
{
type Item = &'a T;
type IntoIter = SetIter<'a, T>;
Expand All @@ -245,16 +256,14 @@ impl<'a, T, S> IntoIterator for &'a HashSet<T, S>
}

impl<T, S> IntoIterator for HashSet<T, S>
where T: Eq + Hash,
S: BuildHasher
where
T: Eq + Hash,
S: BuildHasher,
{
type Item = T;
type IntoIter = SetIntoIter<T>;


fn into_iter(self) -> SetIntoIter<T> {
self.0.into_iter()
}
}


0 comments on commit 951bda3

Please sign in to comment.