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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
use {alloc, Buf, MutBuf};
use std::{cmp, fmt, io, ptr};

enum Mark {
    NoMark,
    At { pos: usize, len: usize },
}

/// Buf backed by a continous chunk of memory. Maintains a read cursor and a
/// write cursor. When reads and writes reach the end of the allocated buffer,
/// wraps around to the start.
pub struct RingBuf {
    ptr: alloc::MemRef,  // Pointer to the memory
    cap: usize,          // Capacity of the buffer
    pos: usize,          // Offset of read cursor
    len: usize,          // Number of bytes to read
    mark: Mark,          // Marked read position
}

// TODO: There are most likely many optimizations that can be made
impl RingBuf {
    pub fn new(mut capacity: usize) -> RingBuf {
        // Handle the 0 length buffer case
        if capacity == 0 {
            return RingBuf {
                ptr: alloc::MemRef::none(),
                cap: 0,
                pos: 0,
                len: 0,
                mark: Mark::NoMark,
            }
        }

        // Round to the next power of 2 for better alignment
        capacity = capacity.next_power_of_two();

        let mem = alloc::heap(capacity as usize);

        RingBuf {
            ptr: mem,
            cap: capacity,
            pos: 0,
            len: 0,
            mark: Mark::NoMark,
        }
    }

    pub fn is_full(&self) -> bool {
        self.cap == self.len
    }

    pub fn is_empty(&self) -> bool {
        self.len == 0
    }

    pub fn capacity(&self) -> usize {
        self.cap
    }

    /// Marks the current read location.
    ///
    /// Together with `reset`, this can be used to read from a section of the
    /// buffer multiple times. The mark will be cleared if it is overwritten
    /// during a write.
    pub fn mark(&mut self) {
        self.mark = Mark::At { pos: self.pos, len: self.len };
    }

    /// Resets the read position to the previously marked position.
    ///
    /// Together with `mark`, this can be used to read from a section of the
    /// buffer multiple times.
    ///
    /// # Panics
    ///
    /// This method will panic if no mark has been set,
    pub fn reset(&mut self){
        match self.mark {
            Mark::NoMark => panic!("no mark set"),
            Mark::At {pos, len} => {
                self.pos = pos;
                self.len = len;
                self.mark = Mark::NoMark;
            }
        }
    }

    fn read_remaining(&self) -> usize {
        self.len
    }

    fn write_remaining(&self) -> usize {
        self.cap - self.len
    }

    fn advance_reader(&mut self, mut cnt: usize) {
        if self.cap == 0 {
            return;
        }
        cnt = cmp::min(cnt, self.read_remaining());

        self.pos += cnt;
        self.pos %= self.cap;
        self.len -= cnt;
    }

    fn advance_writer(&mut self, mut cnt: usize) {
        cnt = cmp::min(cnt, self.write_remaining());
        self.len += cnt;

        // Adjust the mark to account for bytes written.
        if let Mark::At { ref mut len, .. } = self.mark {
            *len += cnt;
        }

        // Clear the mark if we've written past it.
        if let Mark::At { len, .. } = self.mark {
            if len > self.cap {
                self.mark = Mark::NoMark;
            }
        }
    }
}

impl Clone for RingBuf {
    fn clone(&self) -> RingBuf {
        use std::cmp;

        let mut ret = RingBuf::new(self.cap);

        ret.pos = self.pos;
        ret.len = self.len;

        unsafe {
            let to = self.pos + self.len;

            if to > self.cap {
                ptr::copy(self.ptr.ptr() as *const u8, ret.ptr.ptr(), to % self.cap);
            }

            ptr::copy(
                self.ptr.ptr().offset(self.pos as isize) as *const u8,
                ret.ptr.ptr().offset(self.pos as isize),
                cmp::min(self.len, self.cap - self.pos));
        }

        ret
    }

    // TODO: an improved version of clone_from is possible that potentially
    // re-uses the buffer
}

impl fmt::Debug for RingBuf {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        write!(fmt, "RingBuf[.. {}]", self.len)
    }
}

impl Buf for RingBuf {

    fn remaining(&self) -> usize {
        self.read_remaining()
    }

    fn bytes(&self) -> &[u8] {
        let mut to = self.pos + self.len;

        if to > self.cap {
            to = self.cap
        }

        &self.ptr.bytes()[self.pos .. to]
    }

    fn advance(&mut self, cnt: usize) {
        self.advance_reader(cnt)
    }
}

impl MutBuf for RingBuf {

    fn remaining(&self) -> usize {
        self.write_remaining()
    }

    fn advance(&mut self, cnt: usize) {
        self.advance_writer(cnt)
    }

    fn mut_bytes(&mut self) -> &mut [u8] {
        if self.cap == 0 {
            return self.ptr.bytes_mut();
        }
        let mut from;
        let mut to;

        from = self.pos + self.len;
        from %= self.cap;

        to = from + <Self as MutBuf>::remaining(&self);

        if to >= self.cap {
            to = self.cap;
        }

        &mut self.ptr.bytes_mut()[from..to]
    }
}

impl io::Read for RingBuf {
    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        if !Buf::has_remaining(self) {
            return Ok(0);
        }

        Ok(self.read_slice(buf))
    }
}

impl io::Write for RingBuf {
    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
        if !MutBuf::has_remaining(self) {
            return Ok(0);
        }

        Ok(self.write_slice(buf))
    }

    fn flush(&mut self) -> io::Result<()> {
        Ok(())
    }
}

unsafe impl Send for RingBuf { }