Allocate a single surrogate. Returns `Exhausted` if the u32 space is full.
(&self)
| 89 | /// Allocate a single surrogate. Returns `Exhausted` if the u32 space |
| 90 | /// is full. |
| 91 | pub fn alloc_one(&self) -> Result<Surrogate, SurrogateAllocError> { |
| 92 | let prev = self.counter.fetch_add(1, Ordering::AcqRel); |
| 93 | if prev > u64::from(u32::MAX) { |
| 94 | // Restore the counter so future callers also see Exhausted |
| 95 | // rather than racing past us into a wrapped value. We don't |
| 96 | // need atomicity with the racing increments — once any caller |
| 97 | // observes `prev > u32::MAX`, the space is effectively dead. |
| 98 | self.counter |
| 99 | .store(u64::from(u32::MAX) + 1, Ordering::Release); |
| 100 | return Err(SurrogateAllocError::Exhausted); |
| 101 | } |
| 102 | self.allocs_since_flush.fetch_add(1, Ordering::AcqRel); |
| 103 | // Safe: prev <= u32::MAX is guaranteed by the check above. |
| 104 | Ok(Surrogate::new(prev as u32)) |
| 105 | } |
| 106 | |
| 107 | /// Allocate `n` contiguous surrogates as an inclusive range. |
| 108 | /// Returns `EmptyBatch` for `n == 0`, `Exhausted` if the batch |