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

Switch OwnedStore handle count to AtomicU32 #120527

Merged
merged 1 commit into from
Feb 7, 2024
Merged
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
9 changes: 5 additions & 4 deletions library/proc_macro/src/bridge/client.rs
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@
use super::*;

use std::marker::PhantomData;
use std::sync::atomic::AtomicU32;

macro_rules! define_handles {
(
@@ -12,17 +13,17 @@ macro_rules! define_handles {
#[repr(C)]
#[allow(non_snake_case)]
pub struct HandleCounters {
$($oty: AtomicUsize,)*
$($ity: AtomicUsize,)*
$($oty: AtomicU32,)*
$($ity: AtomicU32,)*
}

impl HandleCounters {
// FIXME(eddyb) use a reference to the `static COUNTERS`, instead of
// a wrapper `fn` pointer, once `const fn` can reference `static`s.
extern "C" fn get() -> &'static Self {
static COUNTERS: HandleCounters = HandleCounters {
$($oty: AtomicUsize::new(1),)*
$($ity: AtomicUsize::new(1),)*
$($oty: AtomicU32::new(1),)*
$($ity: AtomicU32::new(1),)*
};
&COUNTERS
}
10 changes: 5 additions & 5 deletions library/proc_macro/src/bridge/handle.rs
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@ use std::collections::BTreeMap;
use std::hash::Hash;
use std::num::NonZeroU32;
use std::ops::{Index, IndexMut};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::atomic::{AtomicU32, Ordering};

use super::fxhash::FxHashMap;

@@ -13,12 +13,12 @@ pub(super) type Handle = NonZeroU32;
/// A store that associates values of type `T` with numeric handles. A value can
/// be looked up using its handle.
pub(super) struct OwnedStore<T: 'static> {
counter: &'static AtomicUsize,
counter: &'static AtomicU32,
data: BTreeMap<Handle, T>,
}

impl<T> OwnedStore<T> {
pub(super) fn new(counter: &'static AtomicUsize) -> Self {
pub(super) fn new(counter: &'static AtomicU32) -> Self {
// Ensure the handle counter isn't 0, which would panic later,
// when `NonZeroU32::new` (aka `Handle::new`) is called in `alloc`.
assert_ne!(counter.load(Ordering::SeqCst), 0);
@@ -30,7 +30,7 @@ impl<T> OwnedStore<T> {
impl<T> OwnedStore<T> {
pub(super) fn alloc(&mut self, x: T) -> Handle {
let counter = self.counter.fetch_add(1, Ordering::SeqCst);
let handle = Handle::new(counter as u32).expect("`proc_macro` handle counter overflowed");
let handle = Handle::new(counter).expect("`proc_macro` handle counter overflowed");
assert!(self.data.insert(handle, x).is_none());
handle
}
@@ -60,7 +60,7 @@ pub(super) struct InternedStore<T: 'static> {
}

impl<T: Copy + Eq + Hash> InternedStore<T> {
pub(super) fn new(counter: &'static AtomicUsize) -> Self {
pub(super) fn new(counter: &'static AtomicU32) -> Self {
InternedStore { owned: OwnedStore::new(counter), interner: FxHashMap::default() }
}

1 change: 0 additions & 1 deletion library/proc_macro/src/bridge/mod.rs
Original file line number Diff line number Diff line change
@@ -16,7 +16,6 @@ use std::mem;
use std::ops::Bound;
use std::ops::Range;
use std::panic;
use std::sync::atomic::AtomicUsize;
use std::sync::Once;
use std::thread;