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
use core::cell::Cell;

use crate::utils;

const NON_MARKED: u32 = 0u32;
const IN_POSSIBLE_CYCLES: u32 = 1u32 << (u32::BITS - 2);
const TRACED: u32 = 2u32 << (u32::BITS - 2);

const COUNTER_MASK: u32 = 0b11111111111111u32; // First 14 bits set to 1
const TRACING_COUNTER_MASK: u32 = COUNTER_MASK << 14; // 14 bits set to 1 followed by 14 bits set to 0
const FINALIZED_MASK: u32 = 1u32 << (u32::BITS - 4);
const DROPPED_MASK: u32 = 1u32 << (u32::BITS - 3);
const BITS_MASK: u32 = !(COUNTER_MASK | TRACING_COUNTER_MASK | FINALIZED_MASK | DROPPED_MASK);
const FIRST_BIT_MASK: u32 = 1u32 << (u32::BITS - 1);

const INITIAL_VALUE: u32 = COUNTER_MASK + 2; // +2 means that tracing counter and counter are both set to 1
const INITIAL_VALUE_FINALIZED: u32 = INITIAL_VALUE | FINALIZED_MASK;

// pub(crate) to make it available in tests
pub(crate) const MAX: u32 = COUNTER_MASK;

/// Internal representation:
/// ```text
/// +-----------+----------+----------+------------+------------+
/// | A: 2 bits | B: 1 bit | C: 1 bit | D: 14 bits | E: 14 bits |  Total: 32 bits
/// +-----------+----------+----------+------------+------------+
/// ```
///
/// * `A` has 3 possible states:
///   * `NON_MARKED`
///   * `IN_POSSIBLE_CYCLES` (this implies `NON_MARKED`)
///   * `TRACED`
/// * `B` is `1` when the element inside `CcBox` has already been dropped, `0` otherwise
/// * `C` is `1` when the element inside `CcBox` has already been finalized, `0` otherwise
/// * `D` is the tracing counter
/// * `E` is the counter (last one for sum/subtraction efficiency)
#[derive(Clone, Debug)]
#[repr(transparent)]
pub(crate) struct CounterMarker {
    counter: Cell<u32>,
}

pub(crate) struct OverflowError;

impl CounterMarker {
    #[inline]
    #[must_use]
    pub(crate) fn new_with_counter_to_one(already_finalized: bool) -> CounterMarker {
        CounterMarker {
            counter: Cell::new(if !already_finalized {
                INITIAL_VALUE
            } else {
                INITIAL_VALUE_FINALIZED
            }),
        }
    }

    #[inline]
    pub(crate) fn increment_counter(&self) -> Result<(), OverflowError> {
        if self.counter() == MAX {
            utils::cold(); // This branch of the if is rarely taken
            Err(OverflowError)
        } else {
            self.counter.set(self.counter.get() + 1);
            Ok(())
        }
    }

    #[inline]
    pub(crate) fn decrement_counter(&self) -> Result<(), OverflowError> {
        if self.counter() == 0 {
            utils::cold(); // This branch of the if is rarely taken
            Err(OverflowError)
        } else {
            self.counter.set(self.counter.get() - 1);
            Ok(())
        }
    }

    #[inline]
    pub(crate) fn increment_tracing_counter(&self) -> Result<(), OverflowError> {
        if self.tracing_counter() == MAX {
            utils::cold(); // This branch of the if is rarely taken
            Err(OverflowError)
        } else {
            // Increment trace_counter and not counter
            self.counter.set(self.counter.get() + (1u32 << 14));
            Ok(())
        }
    }

    #[inline]
    pub(crate) fn _decrement_tracing_counter(&self) -> Result<(), OverflowError> {
        if self.tracing_counter() == 0 {
            utils::cold(); // This branch of the if is rarely taken
            Err(OverflowError)
        } else {
            // Decrement trace_counter and not counter
            self.counter.set(self.counter.get() - (1u32 << 14));
            Ok(())
        }
    }

    #[inline]
    pub(crate) fn counter(&self) -> u32 {
        self.counter.get() & COUNTER_MASK
    }

    #[inline]
    pub(crate) fn tracing_counter(&self) -> u32 {
        (self.counter.get() & TRACING_COUNTER_MASK) >> 14
    }

    #[inline]
    pub(crate) fn reset_tracing_counter(&self) {
        self.counter.set(self.counter.get() & !TRACING_COUNTER_MASK);
    }

    #[inline]
    pub(crate) fn is_in_possible_cycles(&self) -> bool {
        (self.counter.get() & BITS_MASK) == IN_POSSIBLE_CYCLES
    }

    #[cfg(feature = "finalization")]
    #[inline]
    pub(crate) fn needs_finalization(&self) -> bool {
        (self.counter.get() & FINALIZED_MASK) == 0u32
    }

    #[cfg(feature = "finalization")]
    #[inline]
    pub(crate) fn set_finalized(&self, finalized: bool) {
        self.set_bit(finalized, FINALIZED_MASK);
    }

    #[cfg(feature = "weak-ptr")]
    #[inline]
    pub(crate) fn is_dropped(&self) -> bool {
        (self.counter.get() & DROPPED_MASK) == DROPPED_MASK
    }

    #[cfg(feature = "weak-ptr")]
    #[inline]
    pub(crate) fn set_dropped(&self, dropped: bool) {
        self.set_bit(dropped, DROPPED_MASK);
    }

    #[cfg(any(feature = "weak-ptr", feature = "finalization"))]
    #[inline(always)]
    fn set_bit(&self, value: bool, mask: u32) {
        if value {
            self.counter.set(self.counter.get() | mask);
        } else {
            self.counter.set(self.counter.get() & !mask);
        }
    }

    #[inline]
    pub(crate) fn is_not_marked(&self) -> bool {
        // true if (self.counter & BITS_MASK) is equal to 01 or 00,
        // so if the first bit is 0
        (self.counter.get() & FIRST_BIT_MASK) == 0u32
    }

    #[inline]
    pub(crate) fn is_traced(&self) -> bool {
        (self.counter.get() & BITS_MASK) == TRACED
    }

    #[inline]
    pub(crate) fn mark(&self, new_mark: Mark) {
        self.counter.set((self.counter.get() & !BITS_MASK) | (new_mark as u32));
    }
}

#[derive(Copy, Clone, Debug)]
#[repr(u32)]
pub(crate) enum Mark {
    NonMarked = NON_MARKED,
    PossibleCycles = IN_POSSIBLE_CYCLES,
    Traced = TRACED,
}