Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
17 changes: 17 additions & 0 deletions .clippy.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Clippy configuration
# https://doc.rust-lang.org/nightly/clippy/lint_configuration.html

allow-private-module-inception = true
avoid-breaking-exported-api = false
disallowed-names = []
disallowed-macros = [
{ path = "std::dbg", reason = "it is okay to use during development, but please do not include it in master branch" },
]
disallowed-methods = [
# Use helper for safer allocation instead.
{ path = "alloc::alloc::alloc", replacement = "crate::alloc_helper::Global::allocate" },
{ path = "alloc::alloc::alloc_zeroed", replacement = "crate::alloc_helper::Global::allocate_zeroed" },
{ path = "alloc::alloc::dealloc", replacement = "crate::alloc_helper::Global::deallocate" },
]
disallowed-types = [
]
1 change: 1 addition & 0 deletions crossbeam-channel/src/alloc_helper.rs
29 changes: 15 additions & 14 deletions crossbeam-channel/src/flavors/list.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Unbounded channel implemented as a linked list.

use std::alloc::{alloc_zeroed, handle_alloc_error, Layout};
use std::alloc::{handle_alloc_error, Layout};
use std::boxed::Box;
use std::cell::UnsafeCell;
use std::marker::PhantomData;
Expand All @@ -11,6 +11,7 @@ use std::time::Instant;

use crossbeam_utils::{Backoff, CachePadded};

use crate::alloc_helper::Global;
use crate::context::Context;
use crate::err::{RecvTimeoutError, SendTimeoutError, TryRecvError, TrySendError};
use crate::select::{Operation, SelectHandle, Selected, Token};
Expand Down Expand Up @@ -83,20 +84,20 @@ impl<T> Block<T> {

/// Creates an empty block.
fn new() -> Box<Self> {
// SAFETY: layout is not zero-sized
let ptr = unsafe { alloc_zeroed(Self::LAYOUT) };
// Handle allocation failure
if ptr.is_null() {
handle_alloc_error(Self::LAYOUT)
// unsafe { Box::new_zeroed().assume_init() } requires Rust 1.92
match Global.allocate_zeroed(Self::LAYOUT) {
Some(ptr) => {
// SAFETY: This is safe because:
// [1] `Block::next` (AtomicPtr) may be safely zero initialized.
// [2] `Block::slots` (Array) may be safely zero initialized because of [3, 4].
// [3] `Slot::msg` (UnsafeCell) may be safely zero initialized because it
// holds a MaybeUninit.
// [4] `Slot::state` (AtomicUsize) may be safely zero initialized.
unsafe { Box::from_raw(ptr.as_ptr().cast()) }
}
// Handle allocation failure
None => handle_alloc_error(Self::LAYOUT),
}
// SAFETY: This is safe because:
// [1] `Block::next` (AtomicPtr) may be safely zero initialized.
// [2] `Block::slots` (Array) may be safely zero initialized because of [3, 4].
// [3] `Slot::msg` (UnsafeCell) may be safely zero initialized because it
// holds a MaybeUninit.
// [4] `Slot::state` (AtomicUsize) may be safely zero initialized.
// TODO: unsafe { Box::new_zeroed().assume_init() }
unsafe { Box::from_raw(ptr.cast()) }
}

/// Waits until the next pointer is set.
Expand Down
5 changes: 5 additions & 0 deletions crossbeam-channel/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,9 +335,14 @@
))]
#![warn(missing_docs, unsafe_op_in_unsafe_fn)]

#[cfg(feature = "std")]
extern crate alloc;
#[cfg(feature = "std")]
extern crate std;

#[cfg(feature = "std")]
mod alloc_helper;

#[cfg(feature = "std")]
mod channel;
#[cfg(feature = "std")]
Expand Down
1 change: 1 addition & 0 deletions crossbeam-deque/src/alloc_helper.rs
30 changes: 16 additions & 14 deletions crossbeam-deque/src/deque.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::alloc::{alloc_zeroed, handle_alloc_error, Layout};
use std::alloc::{handle_alloc_error, Layout};
use std::boxed::Box;
use std::cell::{Cell, UnsafeCell};
use std::cmp;
Expand All @@ -12,6 +12,8 @@ use std::sync::Arc;
use crossbeam_epoch::{self as epoch, Atomic, Owned};
use crossbeam_utils::{Backoff, CachePadded};

use crate::alloc_helper::Global;

// Minimum buffer capacity.
const MIN_CAP: usize = 64;
// Maximum number of tasks that can be stolen in `steal_batch()` and `steal_batch_and_pop()`.
Expand Down Expand Up @@ -1238,20 +1240,20 @@ impl<T> Block<T> {

/// Creates an empty block.
fn new() -> Box<Self> {
// SAFETY: layout is not zero-sized
let ptr = unsafe { alloc_zeroed(Self::LAYOUT) };
// Handle allocation failure
if ptr.is_null() {
handle_alloc_error(Self::LAYOUT)
// unsafe { Box::new_zeroed().assume_init() } requires Rust 1.92
match Global.allocate_zeroed(Self::LAYOUT) {
Some(ptr) => {
// SAFETY: This is safe because:
// [1] `Block::next` (AtomicPtr) may be safely zero initialized.
// [2] `Block::slots` (Array) may be safely zero initialized because of [3, 4].
// [3] `Slot::task` (UnsafeCell) may be safely zero initialized because it
// holds a MaybeUninit.
// [4] `Slot::state` (AtomicUsize) may be safely zero initialized.
unsafe { Box::from_raw(ptr.as_ptr().cast()) }
}
// Handle allocation failure
None => handle_alloc_error(Self::LAYOUT),
}
// SAFETY: This is safe because:
// [1] `Block::next` (AtomicPtr) may be safely zero initialized.
// [2] `Block::slots` (Array) may be safely zero initialized because of [3, 4].
// [3] `Slot::task` (UnsafeCell) may be safely zero initialized because it
// holds a MaybeUninit.
// [4] `Slot::state` (AtomicUsize) may be safely zero initialized.
// TODO: unsafe { Box::new_zeroed().assume_init() }
unsafe { Box::from_raw(ptr.cast()) }
}

/// Waits until the next pointer is set.
Expand Down
5 changes: 5 additions & 0 deletions crossbeam-deque/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,14 @@
))]
#![warn(missing_docs, unsafe_op_in_unsafe_fn)]

#[cfg(feature = "std")]
extern crate alloc;
#[cfg(feature = "std")]
extern crate std;

#[cfg(feature = "std")]
mod alloc_helper;

#[cfg(feature = "std")]
mod deque;
#[cfg(feature = "std")]
Expand Down
1 change: 1 addition & 0 deletions crossbeam-epoch/src/alloc_helper.rs
20 changes: 11 additions & 9 deletions crossbeam-epoch/src/atomic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ use core::fmt;
use core::marker::PhantomData;
use core::mem::{self, MaybeUninit};
use core::ops::{Deref, DerefMut};
use core::ptr;
use core::ptr::{self, NonNull};

use crate::alloc_helper::Global;
use crate::guard::Guard;
#[cfg(not(miri))]
use crate::primitive::sync::atomic::AtomicUsize;
Expand Down Expand Up @@ -216,15 +217,16 @@ impl<T> Pointable for [MaybeUninit<T>] {

type Init = usize;

#[inline]
unsafe fn init(len: Self::Init) -> *mut () {
let layout = Array::<T>::layout(len);
unsafe {
let ptr = alloc::alloc::alloc(layout).cast::<Array<T>>();
if ptr.is_null() {
alloc::alloc::handle_alloc_error(layout);
}
ptr::addr_of_mut!((*ptr).len).write(len);
ptr.cast::<()>()
match Global.allocate(layout) {
Some(ptr) => unsafe {
let ptr = ptr.as_ptr().cast::<Array<T>>();
ptr::addr_of_mut!((*ptr).len).write(len);
ptr.cast::<()>()
},
None => alloc::alloc::handle_alloc_error(layout),
}
}

Expand All @@ -251,7 +253,7 @@ impl<T> Pointable for [MaybeUninit<T>] {
unsafe {
let len = (*ptr.cast::<Array<T>>()).len;
let layout = Array::<T>::layout(len);
alloc::alloc::dealloc(ptr.cast::<u8>(), layout);
Global.deallocate(NonNull::new_unchecked(ptr.cast::<u8>()), layout);
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions crossbeam-epoch/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ mod primitive {
#[cfg(all(feature = "alloc", target_has_atomic = "ptr"))]
extern crate alloc;

#[cfg(all(feature = "alloc", target_has_atomic = "ptr"))]
mod alloc_helper;
#[cfg(all(feature = "alloc", target_has_atomic = "ptr"))]
mod atomic;
#[cfg(all(feature = "alloc", target_has_atomic = "ptr"))]
Expand Down
1 change: 1 addition & 0 deletions crossbeam-queue/src/alloc_helper.rs
2 changes: 2 additions & 0 deletions crossbeam-queue/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ extern crate alloc;
#[cfg(feature = "std")]
extern crate std;

#[cfg(all(feature = "alloc", target_has_atomic = "ptr"))]
mod alloc_helper;
#[cfg(all(feature = "alloc", target_has_atomic = "ptr"))]
mod array_queue;
#[cfg(all(feature = "alloc", target_has_atomic = "ptr"))]
Expand Down
30 changes: 16 additions & 14 deletions crossbeam-queue/src/seg_queue.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use alloc::alloc::{alloc_zeroed, handle_alloc_error, Layout};
use alloc::alloc::{handle_alloc_error, Layout};
use alloc::boxed::Box;
use core::cell::UnsafeCell;
use core::fmt;
Expand All @@ -10,6 +10,8 @@ use core::sync::atomic::{self, AtomicPtr, AtomicUsize, Ordering};

use crossbeam_utils::{Backoff, CachePadded};

use crate::alloc_helper::Global;

// Bits indicating the state of a slot:
// * If a value has been written into the slot, `WRITE` is set.
// * If a value has been read from the slot, `READ` is set.
Expand Down Expand Up @@ -69,20 +71,20 @@ impl<T> Block<T> {

/// Creates an empty block.
fn new() -> Box<Self> {
// SAFETY: layout is not zero-sized
let ptr = unsafe { alloc_zeroed(Self::LAYOUT) };
// Handle allocation failure
if ptr.is_null() {
handle_alloc_error(Self::LAYOUT)
// unsafe { Box::new_zeroed().assume_init() } requires Rust 1.92
match Global.allocate_zeroed(Self::LAYOUT) {
Some(ptr) => {
// SAFETY: This is safe because:
// [1] `Block::next` (AtomicPtr) may be safely zero initialized.
// [2] `Block::slots` (Array) may be safely zero initialized because of [3, 4].
// [3] `Slot::value` (UnsafeCell) may be safely zero initialized because it
// holds a MaybeUninit.
// [4] `Slot::state` (AtomicUsize) may be safely zero initialized.
unsafe { Box::from_raw(ptr.as_ptr().cast()) }
}
// Handle allocation failure
None => handle_alloc_error(Self::LAYOUT),
}
// SAFETY: This is safe because:
// [1] `Block::next` (AtomicPtr) may be safely zero initialized.
// [2] `Block::slots` (Array) may be safely zero initialized because of [3, 4].
// [3] `Slot::value` (UnsafeCell) may be safely zero initialized because it
// holds a MaybeUninit.
// [4] `Slot::state` (AtomicUsize) may be safely zero initialized.
// TODO: unsafe { Box::new_zeroed().assume_init() }
unsafe { Box::from_raw(ptr.cast()) }
}

/// Waits until the next pointer is set.
Expand Down
1 change: 1 addition & 0 deletions crossbeam-skiplist/src/alloc_helper.rs
13 changes: 7 additions & 6 deletions crossbeam-skiplist/src/base.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! A lock-free skip list. See [`SkipList`].

use super::equivalent::Comparable;
use alloc::alloc::{alloc, dealloc, handle_alloc_error, Layout};
use alloc::alloc::{handle_alloc_error, Layout};
use core::cmp;
use core::fmt;
use core::marker::PhantomData;
Expand All @@ -11,6 +11,7 @@ use core::ptr;
use core::ptr::NonNull;
use core::sync::atomic::{fence, AtomicUsize, Ordering};

use crate::alloc_helper::Global;
use crossbeam_epoch::{self as epoch, Atomic, Collector, Guard, Shared};
use crossbeam_utils::CachePadded;

Expand Down Expand Up @@ -163,10 +164,10 @@ impl<K, V> Node<K, V> {
unsafe fn alloc(height: usize, ref_count: usize) -> *mut Self {
let layout = Self::get_layout(height);
unsafe {
let ptr = alloc(layout).cast::<Self>();
if ptr.is_null() {
handle_alloc_error(layout);
}
let ptr = match Global.allocate(layout) {
Some(ptr) => ptr.as_ptr().cast::<Self>(),
None => handle_alloc_error(layout),
};

ptr::addr_of_mut!((*ptr).refs_and_height)
.write(AtomicUsize::new((height - 1) | (ref_count << HEIGHT_BITS)));
Expand All @@ -184,7 +185,7 @@ impl<K, V> Node<K, V> {
unsafe {
let height = (*ptr).height();
let layout = Self::get_layout(height);
dealloc(ptr.cast::<u8>(), layout);
Global.deallocate(NonNull::new_unchecked(ptr.cast::<u8>()), layout);
}
}

Expand Down
3 changes: 3 additions & 0 deletions crossbeam-skiplist/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,9 @@ extern crate alloc;
#[cfg(feature = "std")]
extern crate std;

#[cfg(all(feature = "alloc", target_has_atomic = "ptr"))]
mod alloc_helper;

#[cfg(all(feature = "alloc", target_has_atomic = "ptr"))]
pub mod base;

Expand Down
Loading