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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
use std::io::{Error, ErrorKind};
use std::ops::{Index, IndexMut};
use std::slice::SliceIndex;
use std::sync::atomic::{AtomicI32, Ordering};
use std::{cmp, process, ptr, slice};

/// A unique identifier used to create shared memory mapped files.
static BUFFER_ID: AtomicI32 = AtomicI32::new(0);

/// A raw circular buffer of bytes. The buffer holds exactly `size` many
/// bytes but it is presented as a `size + wrap` length slice where the last
/// `wrap` many bytes overlap with the first `wrap` many bytes of the slice.
/// This magic trick is performed with virtual memory, the same physical pages
/// are mapped both at the start and at the end of the buffer.
pub struct Buffer {
    ptr: *const u8,
    len: usize,
    size: usize,
}

fn os_error(message: &'static str) -> Error {
    let kind = Error::last_os_error().kind();
    Error::new(kind, message)
}

#[cfg(unix)]
unsafe fn os_page_size() -> Result<usize, Error> {
    extern crate libc;

    let page = libc::sysconf(libc::_SC_PAGESIZE);
    if page <= 0 {
        Err(os_error("page_size failed"))
    } else {
        Ok(page as usize)
    }
}

#[cfg(unix)]
unsafe fn os_create(name: &str, size: usize, wrap: usize) -> Result<Buffer, Error> {
    extern crate libc;
    use std::ffi::CString;

    // convert name to c-string
    let name = CString::new(name)?;
    let mut err: Option<Error> = None;

    // create temporary shared memory file
    let file_desc = libc::shm_open(
        name.as_ptr(),
        libc::O_RDWR | libc::O_CREAT | libc::O_EXCL,
        0o600,
    );
    if file_desc < 0 {
        err = Some(os_error("shm_open failed"));
    }

    // truncate the file to size + wrap
    if err.is_none() {
        let ret = libc::ftruncate(file_desc, (size + wrap) as libc::off_t);
        if ret != 0 {
            err = Some(os_error("first ftruncate failed"));
        }
    }

    // map it fully
    let mut first_copy = libc::MAP_FAILED;
    if err.is_none() {
        first_copy = libc::mmap(
            ptr::null_mut(),
            size + wrap,
            libc::PROT_READ | libc::PROT_WRITE,
            libc::MAP_SHARED,
            file_desc,
            0,
        );
        if first_copy == libc::MAP_FAILED {
            err = Some(os_error("first mmap failed"));
        }
    }

    // unmap the wrap part
    if err.is_none() {
        let ret = libc::munmap(first_copy.add(size), wrap);
        if ret != 0 {
            err = Some(os_error("munmap failed"));
        }
    }

    // memory map the wrap part again
    if err.is_none() {
        let second_copy = libc::mmap(
            first_copy.add(size),
            wrap,
            libc::PROT_READ | libc::PROT_WRITE,
            libc::MAP_SHARED,
            file_desc,
            0,
        );
        if second_copy == libc::MAP_FAILED {
            err = Some(os_error("second mmap failed"));
        } else if second_copy != first_copy.add(size) {
            err = Some(Error::new(ErrorKind::Other, "bad second address"));
        }
    }

    // unmap memory if error
    if err.is_some() && first_copy != libc::MAP_FAILED {
        libc::munmap(first_copy, size + wrap);
    }

    if file_desc >= 0 {
        // close the file descriptor
        let ret = libc::close(file_desc);
        if ret != 0 && err.is_none() {
            err = Some(os_error("close failed"));
        }

        // unlink the shared memory
        let ret = libc::shm_unlink(name.as_ptr());
        if ret != 0 && err.is_none() {
            err = Some(os_error("shm_unlink failed"));
        }
    }

    match err {
        Some(err) => Err(err),
        None => Ok(Buffer {
            ptr: first_copy as *const u8,
            len: size + wrap,
            size,
        }),
    }
}

#[cfg(unix)]
impl Drop for Buffer {
    fn drop(&mut self) {
        extern crate libc;

        let ptr = self.ptr as *mut libc::c_void;
        let ret = unsafe { libc::munmap(ptr, self.len) };
        debug_assert_eq!(ret, 0);
    }
}

#[cfg(windows)]
unsafe fn os_page_size() -> Result<usize, Error> {
    extern crate winapi;
    use std::mem;
    use winapi::um::sysinfoapi::GetSystemInfo;

    let mut info = mem::zeroed();
    GetSystemInfo(&mut info);
    let page = info.dwAllocationGranularity as usize;
    if page <= 0 {
        Err(Error::new(ErrorKind::Other, "invalid page size"))
    } else {
        Ok(page)
    }
}

#[cfg(windows)]
unsafe fn os_create(name: &str, size: usize, wrap: usize) -> Result<Buffer, Error> {
    extern crate winapi;
    use std::ffi::OsStr;
    use std::iter;
    use std::os::windows::ffi::OsStrExt;
    use winapi::shared::basetsd::SIZE_T;
    use winapi::shared::minwindef::DWORD;
    use winapi::um::handleapi::{CloseHandle, INVALID_HANDLE_VALUE};
    use winapi::um::memoryapi::{
        CreateFileMappingW, MapViewOfFileEx, UnmapViewOfFile, VirtualAlloc, VirtualFree,
        FILE_MAP_WRITE,
    };
    use winapi::um::winnt::{MEM_RELEASE, MEM_RESERVE, PAGE_NOACCESS, PAGE_READWRITE};

    // encode name as WSTR
    let name: Vec<u16> = OsStr::new(name)
        .encode_wide()
        .chain(iter::once(0))
        .collect();
    let mut err: Option<Error> = None;

    // create a paging file
    let mut handle = CreateFileMappingW(
        INVALID_HANDLE_VALUE,
        ptr::null_mut(),
        PAGE_READWRITE,
        (size >> 32) as DWORD,
        size as DWORD,
        name.as_ptr(),
    );
    if handle == ptr::null_mut() || handle == INVALID_HANDLE_VALUE {
        err = Some(os_error("CreateFileMappingA failed"));
        handle = ptr::null_mut();
    }

    // allocate virtual memory
    let mut first_copy = ptr::null_mut();
    if err.is_none() {
        first_copy = VirtualAlloc(
            ptr::null_mut(),
            (size + wrap) as SIZE_T,
            MEM_RESERVE,
            PAGE_NOACCESS,
        );
        if first_copy == ptr::null_mut() {
            err = Some(os_error("VirtualAlloc failed"));
        }
    }

    // and free it, we need the address only
    if err.is_none() {
        let ret = VirtualFree(first_copy, 0 as SIZE_T, MEM_RELEASE);
        if ret == 0 {
            err = Some(os_error("VirtualFree failed"));
        }
    }

    // map first copy
    if err.is_none() {
        let first_temp = MapViewOfFileEx(handle, FILE_MAP_WRITE, 0, 0, size as SIZE_T, first_copy);
        if first_temp == ptr::null_mut() {
            err = Some(os_error("first MapViewOfFileEx failed"));
        } else if first_temp != first_copy {
            err = Some(os_error("invalid first address"));
        }
    }

    // map second copy
    if err.is_none() {
        let second_copy = MapViewOfFileEx(
            handle,
            FILE_MAP_WRITE,
            0,
            0,
            wrap as SIZE_T,
            first_copy.add(size),
        );
        if second_copy == ptr::null_mut() {
            err = Some(os_error("second MapViewOfFileEx failed"));
        } else if second_copy != first_copy.add(size) {
            err = Some(os_error("invalid second address"));
        }
    }

    // unmap memory on error
    if err.is_some() && first_copy != ptr::null_mut() {
        UnmapViewOfFile(first_copy);
        UnmapViewOfFile(first_copy.add(size));
    }

    // close handle
    if handle != ptr::null_mut() {
        let ret = CloseHandle(handle);
        if ret == 0 && err.is_none() {
            err = Some(os_error("CloseHandle failed"));
        }
    }

    match err {
        Some(err) => Err(err),
        None => Ok(Buffer {
            ptr: first_copy as *const u8,
            len: size + wrap,
            size,
        }),
    }
}

#[cfg(windows)]
impl Drop for Buffer {
    fn drop(&mut self) {
        extern crate winapi;
        use winapi::ctypes::c_void;
        use winapi::um::memoryapi::UnmapViewOfFile;

        let ptr = self.ptr as *mut c_void;
        let ret = unsafe { UnmapViewOfFile(ptr) };
        debug_assert_ne!(ret, 0);
        let ret = unsafe { UnmapViewOfFile(ptr.add(self.size)) };
        debug_assert_ne!(ret, 0);
    }
}

impl Buffer {
    /// Returns the page size of the underlying operating system.
    #[inline]
    pub fn page_size() -> Result<usize, Error> {
        unsafe { os_page_size() }
    }

    /// Creates a new circular buffer with the given `size` and `wrap`. The
    /// returned `size` and `wrap` will be rounded up to an integer multiple
    /// of the page size. The `wrap` value cannot be larger than `size`, and
    /// both can be zero to get exactly the page size.
    pub fn new(mut size: usize, mut wrap: usize) -> Result<Buffer, Error> {
        let page = Buffer::page_size()?;

        // round up to a multiple of the page size, be safe
        size = cmp::max(size, page);
        size = ((size + page - 1) / page) * page;
        wrap = cmp::max(wrap, page);
        wrap = ((wrap + page - 1) / page) * page;
        if wrap > size || size + wrap > i32::max_value() as usize {
            return Err(Error::new(ErrorKind::Other, "invalid sizes"));
        }

        // create temporary file name
        let name = format!(
            "/rust-vmcircbuf-{}-{}",
            process::id(),
            BUFFER_ID.fetch_add(1, Ordering::Relaxed)
        );

        unsafe { os_create(&name, size, wrap) }
    }

    /// Returns the size of the circular buffer.
    #[inline]
    pub fn size(&self) -> usize {
        self.size
    }

    /// Returns the wrap of the circular buffer.
    #[inline]
    pub fn wrap(&self) -> usize {
        self.len - self.size
    }

    /// Returns an immutable slice of the circular buffer. The last `wrap`
    /// many bytes are mapped to the first `wrap` many bytes, so you can
    /// read the same content at both places.
    #[inline]
    pub fn as_slice(&self) -> &[u8] {
        unsafe { slice::from_raw_parts(self.ptr, self.len) }
    }

    /// Returns a mutable slice of the circular buffer. The last `wrap`
    /// many bytes are mapped to the first `wrap` many bytes, so you can
    /// read and write the same content at both places.
    #[inline]
    pub fn as_mut_slice(&mut self) -> &mut [u8] {
        unsafe { slice::from_raw_parts_mut(self.ptr as *mut u8, self.len) }
    }
}

unsafe impl Send for Buffer {}
unsafe impl Sync for Buffer {}

impl<I: SliceIndex<[u8]>> Index<I> for Buffer {
    type Output = I::Output;

    #[inline]
    fn index(&self, index: I) -> &Self::Output {
        self.as_slice().index(index)
    }
}

impl<I: SliceIndex<[u8]>> IndexMut<I> for Buffer {
    #[inline]
    fn index_mut(&mut self, index: I) -> &mut Self::Output {
        self.as_mut_slice().index_mut(index)
    }
}

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

    #[test]
    fn wrap() {
        let page = Buffer::page_size().unwrap();
        println!("page size: {}", page);
        let mut buffer = Buffer::new(2 * page, page).unwrap();
        println!("buffer size: {}, wrap: {}", buffer.size(), buffer.wrap());

        for (i, a) in buffer.as_mut_slice().iter_mut().enumerate() {
            let b = i % 101;
            *a = b as u8;
        }

        for (i, a) in buffer.as_slice().iter().take(buffer.wrap()).enumerate() {
            let b = (i + buffer.size()) % 101;
            assert_eq!(*a, b as u8);
        }
    }

    #[test]
    fn simple() {
        let mut buffer = Buffer::new(0, 0).unwrap();
        let size = buffer.size();
        let wrap = buffer.wrap();
        let slice: &mut [u8] = buffer.as_mut_slice();
        assert_eq!(slice.len(), size + wrap);

        for a in slice.iter_mut() {
            *a = 0;
        }

        slice[0] = 123;
        assert_eq!(slice[size], 123);
    }
}