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
use std::sync::atomic::AtomicUsize;
use std::sync::atomic::Ordering;

pub trait FlagsT {
    const DEFAULT: bool;
    ///  constructor creates memory to sufficiently
    /// keep track of garbage collection information for size entries.
    ///
    /// # Arguments
    /// * `size` the number of elements to allocate space for
    ///
    /// # Postconditions
    /// * bit_set, bit_unset, and bit_is_set function properly forall x. x <
    /// size.
    /// * All calls to bit_is_set (without subsequent bit_unset) will return
    /// `Self::DEFAULT`.
    fn new(size: usize) -> Self;

    /// setup marks all entries and ensures that bit_packed_atomic_flags can store
    /// at least size entries
    ///
    /// # Arguments
    /// * `b` the number of elements to allocate space for
    /// # Postconditions
    /// * bit_set, bit_unset, and bit_is_set function properly forall x. x <
    /// b
    /// * All calls to bit_is_set (without subsequent bit_unset) will return
    /// `Self::DEFAULT`.
    fn setup(&mut self, b: usize);

    /// bit_set sets an entry as discardable.
    ///
    /// # Arguments
    /// * `s` the index of the entry to bit_set
    /// # Postconditions
    /// * immediately subsequent call (assuming proper external memory
    /// ordering) to bit_is_set(s) == true.
    fn bit_set(&mut self, s: usize);

    ///  bit_unset marks an entry as something that should not be overwritten
    ///
    /// # Arguments
    /// * `s` the index of the entry to bit_unset
    /// # Postconditions
    /// * immediately subsequent call (assuming proper external memory
    /// ordering) to bit_is_set(s) == false.
    fn bit_unset(&mut self, s: usize);

    /// bit_set_to sets an entry as specified.
    /// # Arguments
    /// * `s` the index of the entry to modify
    /// * `discardable` true does unset, false does set
    /// # Postconditions
    /// * immediately subsequent call (assuming proper external memory
    /// ordering) to bit_is_set(s) == true.
    fn bit_set_to(&mut self, s: usize, discardable: bool);

    fn bit_is_set(&self, s: usize) -> bool;
}

pub trait AtomicFlagsT: FlagsT {
    /// bit_set sets an entry as discardable.
    ///
    /// # Arguments
    /// * `s` the index of the entry to bit_set
    /// # Postconditions
    /// * immediately subsequent call (assuming proper external memory
    /// ordering) to bit_is_set(s) == true.
    fn bit_set(&self, s: usize);

    ///  bit_unset marks an entry as something that should not be overwritten
    ///
    /// # Arguments
    /// * `s` the index of the entry to bit_unset
    /// # Postconditions
    /// * immediately subsequent call (assuming proper external memory
    /// ordering) to bit_is_set(s) == false.
    fn bit_unset(&self, s: usize);
}

pub mod non_atomic {
    use super::*;
    type FlagType = u64;
    const WIDTH: usize = 8 * std::mem::size_of::<FlagType>();
    const NBITS: usize = (WIDTH).trailing_zeros() as usize;
    const MASK: usize = WIDTH - 1;
    pub struct Flags {
        mem: Vec<FlagType>,
    }
    impl FlagsT for Flags {
        const DEFAULT: bool = false;
        fn new(size: usize) -> Self {
            // pad out the size if needed
            let size = (size + MASK) / WIDTH;
            let mut s = Flags {
                mem: Vec::with_capacity(size),
            };
            s.mem.resize(size, 0);
            s
        }

        fn setup(&mut self, b: usize) {
            std::mem::swap(&mut self.mem, &mut Flags::new(b).mem);
        }

        fn bit_set(&mut self, s: usize) {
            self.mem[s >> NBITS] |= 1 << (s & MASK);
        }

        fn bit_unset(&mut self, s: usize) {
            self.mem[s >> NBITS] &= !(1 << (s & MASK));
        }

        fn bit_set_to(&mut self, s: usize, discardable: bool) {
            // clear the bit
            self.mem[s >> NBITS] &= !(1 << (s & MASK));
            // then set iff discardable
            self.mem[s >> NBITS] |= (1 << (s & MASK)) * (discardable as u64);
        }
        fn bit_is_set(&self, s: usize) -> bool {
            return ((1 << (s & MASK)) & self.mem[s >> NBITS]) != 0;
        }
    }
}

pub mod atomic {
    use super::*;
    type FlagType = AtomicUsize;
    const WIDTH: usize = 8 * std::mem::size_of::<FlagType>();
    const NBITS: usize = (WIDTH).trailing_zeros() as usize;
    const MASK: usize = WIDTH - 1;
    pub struct Flags {
        mem: Vec<FlagType>,
    }
    impl FlagsT for Flags {
        const DEFAULT: bool = true;
        fn new(size: usize) -> Self {
            // pad out the size if needed
            let size = (size + MASK) / WIDTH;
            let mut s = Flags {
                mem: Vec::with_capacity(size),
            };
            for _ in 0..size {
                s.mem.push(std::usize::MAX.into());
            }
            s
        }

        fn setup(&mut self, b: usize) {
            std::mem::swap(&mut self.mem, &mut Flags::new(b).mem);
        }

        fn bit_set(&mut self, s: usize) {
            self.mem[s >> NBITS].fetch_or(1 << (s & MASK), Ordering::Relaxed);
        }
        fn bit_unset(&mut self, s: usize) {
            self.mem[s >> NBITS].fetch_and(!(1 << (s & MASK)), Ordering::Relaxed);
        }
        fn bit_set_to(&mut self, s: usize, discardable: bool) {
            // clear the bit
            self.mem[s >> NBITS].fetch_and(!(1 << (s & MASK)), Ordering::Relaxed);
            // then set iff discardable
            self.mem[s >> NBITS].fetch_or(
                (1 << (s & MASK)) * (discardable as usize),
                Ordering::Relaxed,
            );
        }
        fn bit_is_set(&self, s: usize) -> bool {
            return ((1 << (s & MASK)) & self.mem[s >> NBITS].load(Ordering::Relaxed)) != 0;
        }
    }
    impl AtomicFlagsT for Flags {
        /// bit_set sets an entry as discardable.
        ///
        /// # Arguments
        /// * `s` the index of the entry to bit_set
        /// @post immediately subsequent call (assuming proper external memory
        /// ordering) to bit_is_set(s) == true.
        fn bit_set(&self, s: usize) {
            self.mem[s >> NBITS].fetch_or(1 << (s & MASK), Ordering::Relaxed);
        }

        ///  bit_unset marks an entry as something that should not be overwritten
        ///
        /// # Arguments
        /// * `s` the index of the entry to bit_unset
        /// @post immediately subsequent call (assuming proper external memory
        /// ordering) to bit_is_set(s) == false.
        fn bit_unset(&self, s: usize) {
            self.mem[s >> NBITS].fetch_and(!(1 << (s & MASK)), Ordering::Relaxed);
        }
    }
}