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
use std::sync::atomic::{fence, AtomicI32, Ordering};
#[repr(transparent)]
#[derive(Default)]
pub struct RefCount(pub(crate) AtomicI32);
impl RefCount {
pub fn new(count: u32) -> Self {
Self(AtomicI32::new(count as _))
}
pub fn add_ref(&self) -> u32 {
(self.0.fetch_add(1, Ordering::Relaxed) + 1) as u32
}
pub fn release(&self) -> u32 {
let remaining = self.0.fetch_sub(1, Ordering::Release) - 1;
if remaining == 0 {
fence(Ordering::Acquire);
} else if remaining < 0 {
panic!("Object has been over-released.");
}
remaining as u32
}
}