Skip to content

Commit

Permalink
epoch: Remove dependency on memoffset
Browse files Browse the repository at this point in the history
`entry` is the first field of `Local`, so we can remove the needs of
`offset_of` by just making `Local` `repr(C)`.
  • Loading branch information
taiki-e committed Dec 23, 2023
1 parent 1827fc4 commit 4b021eb
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 8 deletions.
1 change: 0 additions & 1 deletion crossbeam-epoch/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ autocfg = "1"

[dependencies]
cfg-if = "1"
memoffset = "0.9"

# Enable the use of loom for concurrency testing.
#
Expand Down
12 changes: 5 additions & 7 deletions crossbeam-epoch/src/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ use core::num::Wrapping;
use core::{fmt, ptr};

use crossbeam_utils::CachePadded;
use memoffset::offset_of;

use crate::atomic::{Owned, Shared};
use crate::collector::{Collector, LocalHandle};
Expand Down Expand Up @@ -268,6 +267,7 @@ impl Global {
}

/// Participant for garbage collection.
#[repr(C)] // Note: `entry` must be the first field
pub(crate) struct Local {
/// A node in the intrusive linked list of `Local`s.
entry: Entry,
Expand Down Expand Up @@ -536,18 +536,16 @@ impl Local {

impl IsElement<Self> for Local {
fn entry_of(local: &Self) -> &Entry {
// SAFETY: `Local` is `repr(C)` and `entry` is the first field of it.
unsafe {
let entry_ptr = (local as *const Self as *const u8)
.add(offset_of!(Local, entry))
.cast::<Entry>();
let entry_ptr = (local as *const Self).cast::<Entry>();
&*entry_ptr
}
}

unsafe fn element_of(entry: &Entry) -> &Self {
let local_ptr = (entry as *const Entry as *const u8)
.sub(offset_of!(Local, entry))
.cast::<Self>();
// SAFETY: `Local` is `repr(C)` and `entry` is the first field of it.
let local_ptr = (entry as *const Entry).cast::<Self>();
&*local_ptr
}

Expand Down

0 comments on commit 4b021eb

Please sign in to comment.