1use std::marker::PhantomData;
2use std::sync::atomic::{AtomicU32, AtomicUsize};
3
4use crate::util::linear_scan::Region;
5use crate::util::{metadata::side_metadata::address_to_meta_address, Address};
6use crate::{
7 policy::immix::{block::Block, line::Line},
8 util::{metadata::side_metadata::SideMetadataSpec, ObjectReference},
9 vm::*,
10};
11use atomic::Ordering;
12
13pub const LOG_REF_COUNT_BITS: usize = 1;
15pub const REF_COUNT_BITS: u8 = 1 << LOG_REF_COUNT_BITS;
17pub const REF_COUNT_MASK: u8 = (((1u16 << REF_COUNT_BITS) - 1) & 0xff) as u8;
19pub const MAX_REF_COUNT: u8 = REF_COUNT_MASK;
22
23pub const LOG_MIN_OBJECT_SIZE: usize = crate::util::constants::LOG_MIN_OBJECT_SIZE as _;
25pub const MIN_OBJECT_SIZE: usize = 1 << LOG_MIN_OBJECT_SIZE;
27
28pub const RC_STRADDLE_LINES: SideMetadataSpec =
31 crate::util::metadata::side_metadata::spec_defs::RC_STRADDLE_LINES;
32
33pub const RC_TABLE: SideMetadataSpec = crate::util::metadata::side_metadata::spec_defs::RC_TABLE;
35
36static INC_BUFFER_SIZE: AtomicUsize = AtomicUsize::new(0);
37
38static TOTAL_INCS_PACKETS: AtomicU32 = AtomicU32::new(0);
39
40static TOTAL_INCS: AtomicU32 = AtomicU32::new(0);
41static ROOT_INCS: AtomicU32 = AtomicU32::new(0);
42static MATURE_INCS: AtomicU32 = AtomicU32::new(0);
43static NURSERY_INCS: AtomicU32 = AtomicU32::new(0);
44static FAST_NURSERY_INCS: AtomicU32 = AtomicU32::new(0);
45static LOS_INCS: AtomicU32 = AtomicU32::new(0);
46
47static PROMOTED_OBJECTS: AtomicU32 = AtomicU32::new(0);
48static PROMOTED_SCALARS: [AtomicU32; 3] = [AtomicU32::new(0), AtomicU32::new(0), AtomicU32::new(0)];
49static PROMOTED_PRIM_ARRAYS: [AtomicU32; 3] =
50 [AtomicU32::new(0), AtomicU32::new(0), AtomicU32::new(0)];
51static PROMOTED_OBJECT_ARRAYS: [AtomicU32; 3] =
52 [AtomicU32::new(0), AtomicU32::new(0), AtomicU32::new(0)];
53
54#[repr(transparent)]
57#[derive(Debug, Copy)]
58pub struct RefCountHelper<VM: VMBinding>(PhantomData<VM>);
59
60impl<VM: VMBinding> RefCountHelper<VM> {
61 pub const NEW: Self = Self(PhantomData);
63 pub const SANITY: bool = cfg!(debug_assertions) || cfg!(feature = "sanity");
65
66 pub fn inc_buffer_size(&self) -> usize {
69 INC_BUFFER_SIZE.load(Ordering::Relaxed)
70 }
71
72 pub fn increase_inc_buffer_size(&self, delta: usize) {
74 let _ = INC_BUFFER_SIZE.fetch_update(Ordering::Relaxed, Ordering::Relaxed, |x| {
75 Some(x.saturating_add(delta))
76 });
77 }
78
79 pub fn reset_inc_buffer_size(&self) {
81 INC_BUFFER_SIZE.store(0, Ordering::Relaxed)
82 }
83
84 pub fn fetch_update(
87 &self,
88 o: ObjectReference,
89 f: impl FnMut(u8) -> Option<u8>,
90 ) -> Result<u8, u8> {
91 RC_TABLE.fetch_update_atomic(o.to_raw_address(), Ordering::Relaxed, Ordering::Relaxed, f)
92 }
93
94 pub fn is_stuck(&self, o: ObjectReference) -> bool {
96 self.count(o) == MAX_REF_COUNT
97 }
98
99 pub fn stick(&self, o: ObjectReference) -> Result<u8, u8> {
102 self.fetch_update(o, |x| {
103 debug_assert!(x <= MAX_REF_COUNT);
104 if x == MAX_REF_COUNT {
105 None
106 } else {
107 Some(MAX_REF_COUNT)
108 }
109 })
110 }
111
112 pub fn inc(&self, o: ObjectReference) -> Result<u8, u8> {
115 #[cfg(feature = "vo_bit")]
116 debug_assert!(
117 crate::util::metadata::vo_bit::is_vo_bit_set(o),
118 "{o}: VO bit not set",
119 );
120
121 self.fetch_update(o, |x| {
122 debug_assert!(x <= MAX_REF_COUNT);
123 if x == MAX_REF_COUNT {
124 None
125 } else {
126 Some(x + 1)
127 }
128 })
129 }
130
131 pub fn dec(&self, o: ObjectReference) -> Result<u8, u8> {
134 #[cfg(feature = "vo_bit")]
135 debug_assert!(
136 crate::util::metadata::vo_bit::is_vo_bit_set(o),
137 "{o}: VO bit not set",
138 );
139
140 self.fetch_update(o, |x| {
141 debug_assert!(x <= MAX_REF_COUNT);
142 if x == 0 || x == MAX_REF_COUNT
143 {
145 None
146 } else {
147 Some(x - 1)
148 }
149 })
150 }
151
152 pub fn set(&self, o: ObjectReference, count: u8) {
154 RC_TABLE.store_atomic(o.to_raw_address(), count, Ordering::Relaxed)
155 }
156
157 pub fn set_relaxed(&self, o: ObjectReference, count: u8) {
160 unsafe { RC_TABLE.store(o.to_raw_address(), count) }
161 }
162
163 pub fn set_line_relaxed(&self, line: Line, count: u8) {
166 unsafe { RC_TABLE.store(line.start(), count) }
167 }
168
169 pub fn count(&self, o: ObjectReference) -> u8 {
171 RC_TABLE.load_atomic(o.to_raw_address(), Ordering::Relaxed)
172 }
173
174 pub fn count_by_address(&self, addr: Address) -> u8 {
178 RC_TABLE.load_atomic(addr, Ordering::Relaxed)
179 }
180
181 pub fn object_or_line_is_dead(&self, o: ObjectReference) -> bool {
184 RC_TABLE.load_byte(o.to_raw_address()) == 0
185 }
186
187 pub fn rc_table_range<UInt: Sized>(&self, b: Block) -> &'static [UInt] {
190 debug_assert!({
191 let log_bits_in_uint: usize =
192 (std::mem::size_of::<UInt>() << 3).trailing_zeros() as usize;
193 Block::LOG_BYTES - super::rc::LOG_MIN_OBJECT_SIZE + super::rc::LOG_REF_COUNT_BITS
194 >= log_bits_in_uint
195 });
196 let start = address_to_meta_address(&super::rc::RC_TABLE, b.start()).to_ptr::<UInt>();
197 let limit = address_to_meta_address(&super::rc::RC_TABLE, b.end()).to_ptr::<UInt>();
198 let rc_table = unsafe { std::slice::from_raw_parts(start, limit.offset_from(start) as _) };
199 rc_table
200 }
201
202 #[allow(unused)]
204 pub fn is_dead(&self, o: ObjectReference) -> bool {
205 let v: u8 = RC_TABLE.load_atomic(o.to_raw_address(), Ordering::Relaxed);
206 v == 0
207 }
208
209 pub fn is_dead_or_stuck(&self, o: ObjectReference) -> bool {
212 let v: u8 = RC_TABLE.load_atomic(o.to_raw_address(), Ordering::Relaxed);
213 v == 0 || v == MAX_REF_COUNT
214 }
215
216 pub fn object_is_in_straddle_line_no_rc_check(&self, o: ObjectReference) -> bool {
218 unsafe { RC_STRADDLE_LINES.load::<u8>(o.to_raw_address()) != 0 }
221 }
222
223 pub fn object_is_in_straddle_line(&self, o: ObjectReference) -> bool {
226 let line = Line::from_unaligned_address(o.to_raw_address());
227 self.count(o) != 0 && unsafe { RC_STRADDLE_LINES.load::<u8>(line.start()) != 0 }
228 }
229
230 fn mark_straddle_object_with_size(&self, o: ObjectReference, size: usize) {
231 debug_assert!(size > Line::BYTES);
232 let start = o.to_object_start::<VM>();
233 let end = start + size;
234 let start_line = Line::from_unaligned_address(o.to_raw_address()).next();
240 let end_line = Line::from_unaligned_address(end);
241 let mut line = start_line;
246 while line < end_line {
247 unsafe { RC_STRADDLE_LINES.store(line.start(), 1u8) };
248 self.set_line_relaxed(line, 1);
249 line = line.next();
250 }
251 }
252
253 pub fn mark_straddle_object(&self, o: ObjectReference) {
256 let size = VM::VMObjectModel::get_current_size(o);
257 self.mark_straddle_object_with_size(o, size)
258 }
259
260 pub fn unmark_straddle_object(&self, o: ObjectReference) {
263 let size = VM::VMObjectModel::get_current_size(o);
265 if size > Line::BYTES {
266 let start = o.to_object_start::<VM>();
267 let end = start + size;
268 let start_line = Line::from_unaligned_address(o.to_raw_address()).next();
270 let end_line = Line::from_unaligned_address(end);
271 let mut line = start_line;
276 while line < end_line {
277 self.set_line_relaxed(line, 0);
278 unsafe { RC_STRADDLE_LINES.store(line.start(), 0u8) };
279 line = line.next();
280 }
281 }
282 }
283
284 pub fn assert_zero_ref_count(&self, o: ObjectReference) {
287 let size = VM::VMObjectModel::get_current_size(o);
288 for i in (0..size).step_by(MIN_OBJECT_SIZE) {
289 let a = o.to_raw_address() + i;
290 assert_eq!(0, self.count_by_address(a));
291 }
292 }
293
294 pub fn promote(&self, o: ObjectReference) {
297 let size = o.get_size::<VM>();
298 if size > Line::BYTES {
299 self.mark_straddle_object_with_size(o, size);
300 }
301 }
302
303 pub fn promote_with_size(&self, o: ObjectReference, size: usize) {
306 if size > Line::BYTES {
307 self.mark_straddle_object_with_size(o, size);
308 }
309 }
310}
311
312impl<VM: VMBinding> Clone for RefCountHelper<VM> {
313 fn clone(&self) -> Self {
314 Self(PhantomData)
315 }
316}