From ffda43c082b79f7698152287b8236b6c0c9918ec Mon Sep 17 00:00:00 2001 From: Justus Winter Date: Tue, 8 Dec 2020 18:12:44 +0100 Subject: buffered-reader: Don't explicitly storing the mapping. - This makes file_unix::File Send and Sync. --- buffered-reader/src/file_unix.rs | 20 +++++++++----------- buffered-reader/src/memory.rs | 5 +++++ 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/buffered-reader/src/file_unix.rs b/buffered-reader/src/file_unix.rs index 0b645f3c..8adca1e6 100644 --- a/buffered-reader/src/file_unix.rs +++ b/buffered-reader/src/file_unix.rs @@ -4,7 +4,7 @@ //! performance of the statistics example by ~10% over the //! Generic. -use libc::{c_void, size_t, mmap, munmap, PROT_READ, MAP_PRIVATE}; +use libc::{mmap, munmap, PROT_READ, MAP_PRIVATE}; use std::fmt; use std::fs; use std::io; @@ -46,8 +46,6 @@ impl<'a, C: fmt::Debug> fmt::Debug for File<'a, C> { enum Imp<'a, C: fmt::Debug> { Generic(Generic), MMAP { - addr: *mut c_void, - length: size_t, reader: Memory<'a, C>, } } @@ -56,10 +54,12 @@ impl<'a, C: fmt::Debug> Drop for Imp<'a, C> { fn drop(&mut self) { match self { Imp::Generic(_) => (), - Imp::MMAP { addr, length, .. } => + Imp::MMAP { reader, } => { + let buf = reader.source_buffer(); unsafe { - munmap(*addr, *length); - }, + munmap(buf.as_ptr() as *mut _, buf.len()); + } + }, } } } @@ -82,10 +82,10 @@ impl<'a, C: fmt::Debug> fmt::Debug for Imp<'a, C> { f.debug_tuple("Generic") .field(&g) .finish(), - Imp::MMAP { ref addr, ref length, ref reader } => + Imp::MMAP { reader, } => f.debug_struct("MMAP") - .field("addr", addr) - .field("length", length) + .field("addr", &reader.source_buffer().as_ptr()) + .field("length", &reader.source_buffer().len()) .field("reader", reader) .finish(), } @@ -150,8 +150,6 @@ impl<'a, C: fmt::Debug> File<'a, C> { Ok(File( Imp::MMAP { - addr, - length, reader: Memory::with_cookie(slice, cookie), }, path.into(), diff --git a/buffered-reader/src/memory.rs b/buffered-reader/src/memory.rs index b09a3478..de2f61bb 100644 --- a/buffered-reader/src/memory.rs +++ b/buffered-reader/src/memory.rs @@ -55,6 +55,11 @@ impl<'a, C: fmt::Debug> Memory<'a, C> { pub fn total_out(&self) -> usize { return self.cursor; } + + #[allow(dead_code)] + pub(crate) fn source_buffer(&self) -> &[u8] { + self.buffer + } } impl<'a, C: fmt::Debug> io::Read for Memory<'a, C> { -- cgit v1.2.3