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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#![doc = include_str!("../README.md")]

use std::{
    ops::{
        Deref, DerefMut, Index, IndexMut, Range, RangeFrom, RangeFull, RangeTo, RangeToInclusive,
    },
    ptr::{slice_from_raw_parts, slice_from_raw_parts_mut},
};
use thiserror::Error;

#[cfg(target_family = "windows")]
mod windows;

#[cfg(target_family = "windows")]
use windows::*;

#[cfg(target_os = "linux")]
mod linux;

#[cfg(target_os = "linux")]
use linux::*;

#[cfg(any(target_os = "macos", target_os = "ios"))]
mod macos;

#[cfg(any(target_os = "macos", target_os = "ios"))]
use macos::*;

#[derive(Debug, Error)]
pub enum MagicBufferError {
    #[error("out of memory")]
    OOM,
    #[error("invalid buffer len, {msg}")]
    InvalidLen { msg: String },
}

#[derive(Debug)]
pub struct MagicBuffer {
    addr: *mut u8,
    len: usize,
    mask: usize,
}

/// [`MagicBuffer`] provides a ring buffer implementation that
/// can deref into a contiguous slice from any offset wrapping
/// around the buffer.
///
/// This is made possible with virtual address mappings.
/// The underlying buffer is mapped twice into virtual memory where
/// the second mapping is adjacent to the first one. The logic
/// for wrapping around the buffer is pushed down to the hardware.
///
/// # Examples
/// ```
/// # use magic_buffer::*;
/// # fn main() -> Result<(), MagicBufferError> {
/// let len = MagicBuffer::min_len();
/// let buf = MagicBuffer::new(len)?;
/// let slice = &buf[len/2..];
/// assert_eq!(len, slice.len());
/// # Ok(())
/// # }
/// ```
#[allow(clippy::len_without_is_empty)]
impl MagicBuffer {
    /// Allocates a new [`MagicBuffer`] of the specified `len`.
    ///
    /// `len` must be a power of two, and also must be a multiple
    /// of the operating system's allocation granularity. This is
    /// usually the page size - most commonly 4KiB. On Windows
    /// the allocation granularity is 64KiB (see [here](https://devblogs.microsoft.com/oldnewthing/20031008-00/?p=42223)).
    ///
    /// # Errors
    /// Will return an error if the allocation fails.
    ///
    /// # Panics
    /// Will panic if it fails to cleanup in case of an error.
    pub fn new(len: usize) -> Result<Self, MagicBufferError> {
        if len == 0 {
            return Err(MagicBufferError::InvalidLen {
                msg: "len must be greater than 0".to_string(),
            });
        }

        if !len.is_power_of_two() {
            return Err(MagicBufferError::InvalidLen {
                msg: "len must be power of two".to_string(),
            });
        }

        let min_len = Self::min_len();
        if len % min_len != 0 {
            return Err(MagicBufferError::InvalidLen {
                msg: format!("len must be page aligned, {}", min_len),
            });
        }

        Ok(Self {
            addr: unsafe { magic_buf_alloc(len) }?,
            mask: len - 1,
            len,
        })
    }

    /// Returns the minimum buffer len that can be allocated.
    ///
    /// This is usually the page size - most commonly 4KiB. On Windows
    /// the allocation granularity is 64KiB (see [here](https://devblogs.microsoft.com/oldnewthing/20031008-00/?p=42223)).
    pub fn min_len() -> usize {
        unsafe { magic_buf_min_len() }
    }

    /// Returns the length of this [`MagicBuffer`].
    pub fn len(&self) -> usize {
        self.len
    }

    #[inline(always)]
    unsafe fn as_slice(&self, offset: usize, len: usize) -> &[u8] {
        &*(slice_from_raw_parts(self.addr.add(offset), len))
    }

    #[inline(always)]
    unsafe fn as_slice_mut(&mut self, offset: usize, len: usize) -> &mut [u8] {
        &mut *(slice_from_raw_parts_mut(self.addr.add(offset), len))
    }

    #[inline(always)]
    fn fast_mod(&self, v: usize) -> usize {
        v & self.mask
    }
}

impl Drop for MagicBuffer {
    fn drop(&mut self) {
        unsafe { magic_buf_free(self.addr, self.len) }
    }
}

impl Deref for MagicBuffer {
    type Target = [u8];

    fn deref(&self) -> &Self::Target {
        unsafe { self.as_slice(0, self.len) }
    }
}

impl DerefMut for MagicBuffer {
    fn deref_mut(&mut self) -> &mut Self::Target {
        unsafe { self.as_slice_mut(0, self.len) }
    }
}

impl Index<usize> for MagicBuffer {
    type Output = u8;

    fn index(&self, index: usize) -> &Self::Output {
        unsafe { &*self.addr.add(self.fast_mod(index)) }
    }
}

impl IndexMut<usize> for MagicBuffer {
    fn index_mut(&mut self, index: usize) -> &mut Self::Output {
        unsafe { &mut *self.addr.add(self.fast_mod(index)) }
    }
}

impl Index<Range<usize>> for MagicBuffer {
    type Output = [u8];

    fn index(&self, index: Range<usize>) -> &Self::Output {
        if index.start > index.end {
            return &[];
        }

        let len = index.end - index.start;
        if len > self.len {
            panic!("out of bounds")
        }

        unsafe { self.as_slice(self.fast_mod(index.start), len) }
    }
}

impl IndexMut<Range<usize>> for MagicBuffer {
    fn index_mut(&mut self, index: Range<usize>) -> &mut Self::Output {
        if index.start > index.end {
            return &mut [];
        }

        let len = index.end - index.start;
        if len > self.len {
            panic!("out of bounds")
        }

        unsafe { self.as_slice_mut(self.fast_mod(index.start), len) }
    }
}

impl Index<RangeTo<usize>> for MagicBuffer {
    type Output = [u8];

    fn index(&self, index: RangeTo<usize>) -> &Self::Output {
        let start = index.end - self.len;
        unsafe { self.as_slice(self.fast_mod(start), self.len) }
    }
}

impl IndexMut<RangeTo<usize>> for MagicBuffer {
    fn index_mut(&mut self, index: RangeTo<usize>) -> &mut Self::Output {
        let start = index.end - self.len;
        unsafe { self.as_slice_mut(self.fast_mod(start), self.len) }
    }
}

impl Index<RangeFrom<usize>> for MagicBuffer {
    type Output = [u8];

    fn index(&self, index: RangeFrom<usize>) -> &Self::Output {
        unsafe { self.as_slice(self.fast_mod(index.start), self.len) }
    }
}

impl IndexMut<RangeFrom<usize>> for MagicBuffer {
    fn index_mut(&mut self, index: RangeFrom<usize>) -> &mut Self::Output {
        unsafe { self.as_slice_mut(self.fast_mod(index.start), self.len) }
    }
}

impl Index<RangeToInclusive<usize>> for MagicBuffer {
    type Output = [u8];

    fn index(&self, index: RangeToInclusive<usize>) -> &Self::Output {
        let start = index.end - self.len + 1;
        unsafe { self.as_slice(self.fast_mod(start), self.len) }
    }
}

impl IndexMut<RangeToInclusive<usize>> for MagicBuffer {
    fn index_mut(&mut self, index: RangeToInclusive<usize>) -> &mut Self::Output {
        let start = index.end - self.len + 1;
        unsafe { self.as_slice_mut(self.fast_mod(start), self.len) }
    }
}

impl Index<RangeFull> for MagicBuffer {
    type Output = [u8];

    fn index(&self, _: RangeFull) -> &Self::Output {
        unsafe { self.as_slice(0, self.len) }
    }
}

impl IndexMut<RangeFull> for MagicBuffer {
    fn index_mut(&mut self, _: RangeFull) -> &mut Self::Output {
        unsafe { self.as_slice_mut(0, self.len) }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    const VALID_BUF_LEN: usize = 1 << 16;
    const INVALID_BUF_LEN_ALIGN: usize = 1 << 8;
    const INVALID_BUF_LEN_POW2: usize = (1 << 16) + 5;

    #[test]
    fn allocates_buffer() {
        let buf = MagicBuffer::new(VALID_BUF_LEN).expect("should allocate buffer");
        drop(buf);
    }

    #[test]
    fn requires_power_of_two() {
        MagicBuffer::new(INVALID_BUF_LEN_POW2)
            .map_err(|e| {
                println!("{}", e);
                e
            })
            .expect_err("should not allocate buffer");
    }

    #[test]
    fn requires_aligned_len() {
        MagicBuffer::new(INVALID_BUF_LEN_ALIGN)
            .map_err(|e| {
                println!("{}", e);
                e
            })
            .expect_err("should not allocate buffer");
    }

    #[test]
    fn writes_are_visible_wrap_around() {
        let mut buf = MagicBuffer::new(VALID_BUF_LEN).expect("should allocate buffer");
        buf[0] = b'a';
        assert_eq!(buf[0], buf[VALID_BUF_LEN]);
    }

    #[test]
    fn deref_as_slice() {
        let buf = MagicBuffer::new(VALID_BUF_LEN).expect("should allocate buffer");
        let slice: &[u8] = &buf;
        assert_eq!(VALID_BUF_LEN, slice.len());
    }

    #[test]
    fn deref_mut_as_slice() {
        let mut buf = MagicBuffer::new(VALID_BUF_LEN).expect("should allocate buffer");
        let slice: &mut [u8] = &mut buf;
        assert_eq!(VALID_BUF_LEN, slice.len());
    }

    #[test]
    fn closed_range() {
        let buf = MagicBuffer::new(VALID_BUF_LEN).expect("should allocate buffer");
        let slice = &buf[0..VALID_BUF_LEN];
        assert_eq!(VALID_BUF_LEN, slice.len());
    }

    #[test]
    fn closed_range_mut() {
        let mut buf = MagicBuffer::new(VALID_BUF_LEN).expect("should allocate buffer");
        let slice = &mut buf[0..VALID_BUF_LEN];
        assert_eq!(VALID_BUF_LEN, slice.len());
    }

    #[test]
    fn range_to() {
        let buf = MagicBuffer::new(VALID_BUF_LEN).expect("should allocate buffer");
        let slice = &buf[..VALID_BUF_LEN + 1];
        assert_eq!(VALID_BUF_LEN, slice.len());
    }

    #[test]
    fn range_to_mut() {
        let mut buf = MagicBuffer::new(VALID_BUF_LEN).expect("should allocate buffer");
        let slice = &mut buf[..VALID_BUF_LEN + 1];
        assert_eq!(VALID_BUF_LEN, slice.len());
    }

    #[test]
    fn range_from() {
        let buf = MagicBuffer::new(VALID_BUF_LEN).expect("should allocate buffer");
        let slice = &buf[1..];
        assert_eq!(VALID_BUF_LEN, slice.len());
    }

    #[test]
    fn range_from_mut() {
        let mut buf = MagicBuffer::new(VALID_BUF_LEN).expect("should allocate buffer");
        let slice = &mut buf[1..];
        assert_eq!(VALID_BUF_LEN, slice.len());
    }

    #[test]
    fn range_to_inclusive() {
        let buf = MagicBuffer::new(VALID_BUF_LEN).expect("should allocate buffer");
        let slice = &buf[..=VALID_BUF_LEN];
        assert_eq!(VALID_BUF_LEN, slice.len());
    }

    #[test]
    fn range_to_inclusive_mut() {
        let mut buf = MagicBuffer::new(VALID_BUF_LEN).expect("should allocate buffer");
        let slice = &mut buf[..=VALID_BUF_LEN];
        assert_eq!(VALID_BUF_LEN, slice.len());
    }

    #[test]
    fn range_full() {
        let buf = MagicBuffer::new(VALID_BUF_LEN).expect("should allocate buffer");
        let slice = &buf[..];
        assert_eq!(VALID_BUF_LEN, slice.len());
    }

    #[test]
    fn range_full_mut() {
        let mut buf = MagicBuffer::new(VALID_BUF_LEN).expect("should allocate buffer");
        let slice = &mut buf[..];
        assert_eq!(VALID_BUF_LEN, slice.len());
    }
}