Skip to content

Commit

Permalink
Auto merge of #304 - kamalmarhubi:uio-libc, r=posborne
Browse files Browse the repository at this point in the history
uio: Use bindings from libc instead of our own

Refs #264
  • Loading branch information
homu committed Mar 10, 2016
2 parents f848e2c + 5089e0f commit 987dcf4
Showing 1 changed file with 14 additions and 58 deletions.
72 changes: 14 additions & 58 deletions src/sys/uio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,56 +2,18 @@
#![allow(improper_ctypes)]

use {Errno, Result};
use libc::{c_int, c_void, size_t, off_t};
use libc::{self, c_int, c_void, size_t, off_t};
use std::marker::PhantomData;
use std::os::unix::io::RawFd;

mod ffi {
use super::IoVec;
use libc::{ssize_t, c_int, size_t, off_t, c_void};
use std::os::unix::io::RawFd;

extern {
// vectorized version of write
// doc: http://man7.org/linux/man-pages/man2/writev.2.html
pub fn writev(fd: RawFd, iov: *const IoVec<&[u8]>, iovcnt: c_int) -> ssize_t;

// vectorized version of read
// doc: http://man7.org/linux/man-pages/man2/readv.2.html
pub fn readv(fd: RawFd, iov: *const IoVec<&mut [u8]>, iovcnt: c_int) -> ssize_t;

// vectorized write at a specified offset
// doc: http://man7.org/linux/man-pages/man2/pwritev.2.html
#[cfg(feature = "preadv_pwritev")]
pub fn pwritev(fd: RawFd, iov: *const IoVec<&[u8]>, iovcnt: c_int,
offset: off_t) -> ssize_t;

// vectorized read at a specified offset
// doc: http://man7.org/linux/man-pages/man2/preadv.2.html
#[cfg(feature = "preadv_pwritev")]
pub fn preadv(fd: RawFd, iov: *const IoVec<&mut [u8]>, iovcnt: c_int,
offset: off_t) -> ssize_t;

// write to a file at a specified offset
// doc: http://man7.org/linux/man-pages/man2/pwrite.2.html
pub fn pwrite(fd: RawFd, buf: *const c_void, nbyte: size_t,
offset: off_t) -> ssize_t;

// read from a file at a specified offset
// doc: http://man7.org/linux/man-pages/man2/pread.2.html
pub fn pread(fd: RawFd, buf: *mut c_void, nbyte: size_t, offset: off_t)
-> ssize_t;
}
}

pub fn writev(fd: RawFd, iov: &[IoVec<&[u8]>]) -> Result<usize> {
let res = unsafe { ffi::writev(fd, iov.as_ptr(), iov.len() as c_int) };
let res = unsafe { libc::writev(fd, iov.as_ptr() as *const libc::iovec, iov.len() as c_int) };

Errno::result(res).map(|r| r as usize)
}

pub fn readv(fd: RawFd, iov: &mut [IoVec<&mut [u8]>]) -> Result<usize> {
let res = unsafe { ffi::readv(fd, iov.as_ptr(), iov.len() as c_int) };
let res = unsafe { libc::readv(fd, iov.as_ptr() as *const libc::iovec, iov.len() as c_int) };

Errno::result(res).map(|r| r as usize)
}
Expand All @@ -60,7 +22,7 @@ pub fn readv(fd: RawFd, iov: &mut [IoVec<&mut [u8]>]) -> Result<usize> {
pub fn pwritev(fd: RawFd, iov: &[IoVec<&[u8]>],
offset: off_t) -> Result<usize> {
let res = unsafe {
ffi::pwritev(fd, iov.as_ptr(), iov.len() as c_int, offset)
libc::pwritev(fd, iov.as_ptr() as *const libc::iovec, iov.len() as c_int, offset)
};

Errno::result(res).map(|r| r as usize)
Expand All @@ -70,15 +32,15 @@ pub fn pwritev(fd: RawFd, iov: &[IoVec<&[u8]>],
pub fn preadv(fd: RawFd, iov: &mut [IoVec<&mut [u8]>],
offset: off_t) -> Result<usize> {
let res = unsafe {
ffi::preadv(fd, iov.as_ptr(), iov.len() as c_int, offset)
libc::preadv(fd, iov.as_ptr() as *const libc::iovec, iov.len() as c_int, offset)
};

Errno::result(res).map(|r| r as usize)
}

pub fn pwrite(fd: RawFd, buf: &[u8], offset: off_t) -> Result<usize> {
let res = unsafe {
ffi::pwrite(fd, buf.as_ptr() as *const c_void, buf.len() as size_t,
libc::pwrite(fd, buf.as_ptr() as *const c_void, buf.len() as size_t,
offset)
};

Expand All @@ -87,19 +49,15 @@ pub fn pwrite(fd: RawFd, buf: &[u8], offset: off_t) -> Result<usize> {

pub fn pread(fd: RawFd, buf: &mut [u8], offset: off_t) -> Result<usize>{
let res = unsafe {
ffi::pread(fd, buf.as_mut_ptr() as *mut c_void, buf.len() as size_t,
libc::pread(fd, buf.as_mut_ptr() as *mut c_void, buf.len() as size_t,
offset)
};

Errno::result(res).map(|r| r as usize)
}

#[repr(C)]
pub struct IoVec<T> {
iov_base: *mut c_void,
iov_len: size_t,
phantom: PhantomData<T>
}
pub struct IoVec<T>(libc::iovec, PhantomData<T>);

impl<T> IoVec<T> {
#[inline]
Expand All @@ -108,29 +66,27 @@ impl<T> IoVec<T> {

unsafe {
slice::from_raw_parts(
self.iov_base as *const u8,
self.iov_len as usize)
self.0.iov_base as *const u8,
self.0.iov_len as usize)
}
}
}

impl<'a> IoVec<&'a [u8]> {
pub fn from_slice(buf: &'a [u8]) -> IoVec<&'a [u8]> {
IoVec {
IoVec(libc::iovec {
iov_base: buf.as_ptr() as *mut c_void,
iov_len: buf.len() as size_t,
phantom: PhantomData
}
}, PhantomData)
}
}

impl<'a> IoVec<&'a mut [u8]> {
pub fn from_mut_slice(buf: &'a mut [u8]) -> IoVec<&'a mut [u8]> {
IoVec {
IoVec(libc::iovec {
iov_base: buf.as_ptr() as *mut c_void,
iov_len: buf.len() as size_t,
phantom: PhantomData
}
}, PhantomData)
}
}

Expand Down

0 comments on commit 987dcf4

Please sign in to comment.