From 4b021eb03103353c3b0088c57126dc62a2ac4886 Mon Sep 17 00:00:00 2001 From: Taiki Endo Date: Sun, 24 Dec 2023 00:53:34 +0900 Subject: [PATCH] epoch: Remove dependency on memoffset `entry` is the first field of `Local`, so we can remove the needs of `offset_of` by just making `Local` `repr(C)`. --- crossbeam-epoch/Cargo.toml | 1 - crossbeam-epoch/src/internal.rs | 12 +++++------- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/crossbeam-epoch/Cargo.toml b/crossbeam-epoch/Cargo.toml index 8420f9967..7c630f667 100644 --- a/crossbeam-epoch/Cargo.toml +++ b/crossbeam-epoch/Cargo.toml @@ -38,7 +38,6 @@ autocfg = "1" [dependencies] cfg-if = "1" -memoffset = "0.9" # Enable the use of loom for concurrency testing. # diff --git a/crossbeam-epoch/src/internal.rs b/crossbeam-epoch/src/internal.rs index de69a9380..89818fc98 100644 --- a/crossbeam-epoch/src/internal.rs +++ b/crossbeam-epoch/src/internal.rs @@ -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}; @@ -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, @@ -536,18 +536,16 @@ impl Local { impl IsElement 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::(); + let entry_ptr = (local as *const Self).cast::(); &*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::(); + // SAFETY: `Local` is `repr(C)` and `entry` is the first field of it. + let local_ptr = (entry as *const Entry).cast::(); &*local_ptr }