File size: 6,766 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
use std::{
    any::type_name,
    marker::PhantomData,
    num::NonZeroU64,
    sync::atomic::{AtomicU64, Ordering},
};

use concurrent_queue::ConcurrentQueue;

/// A helper for constructing id types like [`FunctionId`][crate::FunctionId].
///
/// For ids that may be re-used, see [`IdFactoryWithReuse`].
pub struct IdFactory<T> {
    /// A value starting at 0 and incremented each time a new id is allocated. Regardless of the
    /// underlying type, a u64 is used to cheaply detect overflows.
    counter: AtomicU64,
    /// We've overflowed if the `counter > max_count`.
    max_count: u64,
    id_offset: u64, // added to the value received from `counter`
    _phantom_data: PhantomData<T>,
}

impl<T> IdFactory<T> {
    /// Create a factory for ids in the range `start..=max`.
    pub fn new(start: T, max: T) -> Self
    where
        T: Into<NonZeroU64> + Ord,
    {
        Self::new_const(start.into(), max.into())
    }

    /// Create a factory for ids in the range `start..=max`.
    ///
    /// Provides a less convenient API than [`IdFactory::new`], but skips a type conversion that
    /// would make the function non-const.
    pub const fn new_const(start: NonZeroU64, max: NonZeroU64) -> Self {
        assert!(start.get() < max.get());
        Self {
            // Always start `counter` at 0, don't use the value of `start` because `start` could be
            // close to `u64::MAX`.
            counter: AtomicU64::new(0),
            max_count: max.get() - start.get(),
            id_offset: start.get(),
            _phantom_data: PhantomData,
        }
    }
}

impl<T> IdFactory<T>
where
    T: TryFrom<NonZeroU64>,
{
    /// Return a unique new id.
    ///
    /// Panics if the id type overflows.
    pub fn get(&self) -> T {
        let count = self.counter.fetch_add(1, Ordering::Relaxed);

        #[cfg(debug_assertions)]
        {
            if count == u64::MAX {
                // u64 counter is about to overflow -- this should never happen! A `u64` counter
                // starting at 0 should take decades to overflow on a single machine.
                //
                // This is unrecoverable because other threads may have already read the overflowed
                // value, so abort the entire process.
                std::process::abort()
            }
        }

        // `max_count` might be something like `u32::MAX`. The extra bits of `u64` are useful to
        // detect overflows in that case. We assume the u64 counter is large enough to never
        // overflow.
        if count > self.max_count {
            panic!(
                "Max id limit (overflow) hit while attempting to generate a unique {}",
                type_name::<T>(),
            )
        }

        let new_id_u64 = count + self.id_offset;
        // Safety:
        // - `count` is assumed not to overflow.
        // - `id_offset` is a non-zero value.
        // - `id_offset + count < u64::MAX`.
        let new_id = unsafe { NonZeroU64::new_unchecked(new_id_u64) };

        match new_id.try_into() {
            Ok(id) => id,
            // With any sane implementation of `TryFrom`, this shouldn't happen, as we've already
            // checked the `max_count` bound. (Could happen with the `new_const` constructor)
            Err(_) => panic!(
                "Failed to convert NonZeroU64 value of {} into {}",
                new_id,
                type_name::<T>()
            ),
        }
    }

    /// Returns an id, potentially allowing an overflow. This may cause ids to be silently re-used.
    /// Used for [`crate::id::ExecutionId`].
    ///
    /// If id re-use is desired only for "freed" ids, use [`IdFactoryWithReuse`] instead.
    pub fn wrapping_get(&self) -> T {
        let count = self.counter.fetch_add(1, Ordering::Relaxed);

        let new_id_u64 = (count % self.max_count) + self.id_offset;
        // Safety:
        // - `id_offset` is a non-zero value.
        // - `id_offset + max_count < u64::MAX`.
        let new_id = unsafe { NonZeroU64::new_unchecked(new_id_u64) };

        match new_id.try_into() {
            Ok(id) => id,
            Err(_) => panic!(
                "Failed to convert NonZeroU64 value of {} into {}",
                new_id,
                type_name::<T>()
            ),
        }
    }
}

/// An [`IdFactory`], but extended with a free list to allow for id reuse for ids such as
/// [`BackendJobId`][crate::backend::BackendJobId].
///
/// If silent untracked re-use of ids is okay, consider using the cheaper
/// [`IdFactory::wrapping_get`] method.
pub struct IdFactoryWithReuse<T> {
    factory: IdFactory<T>,
    free_ids: ConcurrentQueue<T>,
}

impl<T> IdFactoryWithReuse<T>
where
    T: Into<NonZeroU64> + Ord,
{
    /// Create a factory for ids in the range `start..=max`.
    pub fn new(start: T, max: T) -> Self {
        Self {
            factory: IdFactory::new(start, max),
            free_ids: ConcurrentQueue::unbounded(),
        }
    }

    /// Create a factory for ids in the range `start..=max`. Provides a less convenient API than
    /// [`IdFactoryWithReuse::new`], but skips a type conversion that would make the function
    /// non-const.
    pub const fn new_const(start: NonZeroU64, max: NonZeroU64) -> Self {
        Self {
            factory: IdFactory::new_const(start, max),
            free_ids: ConcurrentQueue::unbounded(),
        }
    }
}

impl<T> IdFactoryWithReuse<T>
where
    T: TryFrom<NonZeroU64>,
{
    /// Return a new or potentially reused id.
    ///
    /// Panics if the id type overflows.
    pub fn get(&self) -> T {
        self.free_ids.pop().unwrap_or_else(|_| self.factory.get())
    }

    /// Add an id to the free list, allowing it to be re-used on a subsequent call to
    /// [`IdFactoryWithReuse::get`].
    ///
    /// # Safety
    ///
    /// The id must no longer be used. Must be a valid id that was previously returned by
    /// [`IdFactoryWithReuse::get`].
    pub unsafe fn reuse(&self, id: T) {
        let _ = self.free_ids.push(id);
    }
}

#[cfg(test)]
mod tests {
    use std::num::NonZeroU8;

    use super::*;

    #[test]
    #[should_panic(expected = "Max id limit (overflow)")]
    fn test_overflow_detection() {
        let factory = IdFactory::new(NonZeroU8::MIN, NonZeroU8::MAX);
        assert_eq!(factory.get(), NonZeroU8::new(1).unwrap());
        assert_eq!(factory.get(), NonZeroU8::new(2).unwrap());
        for _ in 2..256 {
            factory.get();
        }
    }

    #[test]
    #[should_panic(expected = "Max id limit (overflow)")]
    fn test_overflow_detection_near_u64_max() {
        let factory = IdFactory::new(NonZeroU64::try_from(u64::MAX - 5).unwrap(), NonZeroU64::MAX);
        for _ in 0..=6 {
            factory.get();
        }
    }
}