Switch OwnedStore handle count to AtomicU32
This commit is contained in:
parent
cdaa12e3df
commit
3cc601ac7e
@ -3,6 +3,7 @@
|
||||
use super::*;
|
||||
|
||||
use std::marker::PhantomData;
|
||||
use std::sync::atomic::AtomicU32;
|
||||
|
||||
macro_rules! define_handles {
|
||||
(
|
||||
@ -12,8 +13,8 @@ macro_rules! define_handles {
|
||||
#[repr(C)]
|
||||
#[allow(non_snake_case)]
|
||||
pub struct HandleCounters {
|
||||
$($oty: AtomicUsize,)*
|
||||
$($ity: AtomicUsize,)*
|
||||
$($oty: AtomicU32,)*
|
||||
$($ity: AtomicU32,)*
|
||||
}
|
||||
|
||||
impl HandleCounters {
|
||||
@ -21,8 +22,8 @@ impl HandleCounters {
|
||||
// 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
|
||||
}
|
||||
|
@ -4,7 +4,7 @@
|
||||
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 @@
|
||||
/// 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 @@ pub(super) fn new(counter: &'static AtomicUsize) -> Self {
|
||||
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() }
|
||||
}
|
||||
|
||||
|
@ -16,7 +16,6 @@
|
||||
use std::ops::Bound;
|
||||
use std::ops::Range;
|
||||
use std::panic;
|
||||
use std::sync::atomic::AtomicUsize;
|
||||
use std::sync::Once;
|
||||
use std::thread;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user