Implement rand and args, cleanup other modules

This commit is contained in:
Jeremy Soller 2016-10-29 21:46:49 -06:00
parent b1b35dd1f2
commit ea6f5aa1b1
7 changed files with 29 additions and 74 deletions

View file

@ -52,9 +52,10 @@ impl DoubleEndedIterator for Args {
mod imp {
use os::unix::prelude::*;
use mem;
use ffi::{CStr, OsString};
use ffi::OsString;
use marker::PhantomData;
use libc;
use slice;
use str;
use super::Args;
use sys_common::mutex::Mutex;
@ -63,9 +64,12 @@ mod imp {
static LOCK: Mutex = Mutex::new();
pub unsafe fn init(argc: isize, argv: *const *const u8) {
let args = (0..argc).map(|i| {
CStr::from_ptr(*argv.offset(i) as *const libc::c_char).to_bytes().to_vec()
}).collect();
let mut args: Vec<Vec<u8>> = Vec::new();
for i in 0..argc {
let len = *(argv.offset(i * 2)) as usize;
let ptr = *(argv.offset(i * 2 + 1));
args.push(slice::from_raw_parts(ptr, len).to_vec());
}
LOCK.lock();
let ptr = get_global_ptr();

View file

@ -28,7 +28,6 @@ impl Condvar {
#[inline]
pub fn notify_one(&self) {
::sys_common::util::dumb_print(format_args!("condvar notify_one\n"));
unsafe {
let seq = self.seq.get();
@ -40,7 +39,6 @@ impl Condvar {
#[inline]
pub fn notify_all(&self) {
::sys_common::util::dumb_print(format_args!("condvar notify_all\n"));
unsafe {
let lock = self.lock.get();
let seq = self.seq.get();
@ -57,7 +55,6 @@ impl Condvar {
#[inline]
pub fn wait(&self, mutex: &Mutex) {
::sys_common::util::dumb_print(format_args!("condvar wait\n"));
unsafe {
let lock = self.lock.get();
let seq = self.seq.get();

View file

@ -49,32 +49,14 @@ impl FileDesc {
}
pub fn set_cloexec(&self) -> io::Result<()> {
::sys_common::util::dumb_print(format_args!("Set cloexec\n"));
unimplemented!();
/*
unsafe {
let previous = cvt(libc::fcntl(self.fd, libc::F_GETFD, 0))?;
cvt(libc::fcntl(self.fd, libc::F_SETFD, previous | libc::FD_CLOEXEC))?;
Ok(())
}
*/
::sys_common::util::dumb_print(format_args!("{}: set cloexec\n", self.fd));
//unimplemented!();
Ok(())
}
pub fn set_nonblocking(&self, _nonblocking: bool) -> io::Result<()> {
::sys_common::util::dumb_print(format_args!("Set nonblocking\n"));
::sys_common::util::dumb_print(format_args!("{}: set nonblocking\n", self.fd));
unimplemented!();
/*
unsafe {
let previous = cvt(libc::fcntl(self.fd, libc::F_GETFL, 0))?;
let new = if nonblocking {
previous | libc::O_NONBLOCK
} else {
previous & !libc::O_NONBLOCK
};
cvt(libc::fcntl(self.fd, libc::F_SETFL, new))?;
Ok(())
}
*/
}
pub fn duplicate(&self) -> io::Result<FileDesc> {

View file

@ -65,22 +65,18 @@ impl Mutex {
/// Try to lock the mutex
#[inline]
pub unsafe fn try_lock(&self) -> bool {
::sys_common::util::dumb_print(format_args!("mutex try lock\n"));
mutex_try_lock(self.lock.get())
}
/// Lock the mutex
#[inline]
pub unsafe fn lock(&self) {
::sys_common::util::dumb_print(format_args!("mutex lock\n"));
mutex_try_lock(self.lock.get());
//mutex_lock(self.lock.get());
mutex_lock(self.lock.get());
}
/// Unlock the mutex
#[inline]
pub unsafe fn unlock(&self) {
::sys_common::util::dumb_print(format_args!("mutex unlock\n"));
mutex_unlock(self.lock.get());
}
@ -119,7 +115,6 @@ impl ReentrantMutex {
/// Try to lock the mutex
#[inline]
pub unsafe fn try_lock(&self) -> bool {
::sys_common::util::dumb_print(format_args!("remutex try_lock\n"));
let pid = getpid().unwrap();
if *self.own_count.get() > 0 && *self.owner.get() == pid {
*self.own_count.get() += 1;
@ -138,7 +133,6 @@ impl ReentrantMutex {
/// Lock the mutex
#[inline]
pub unsafe fn lock(&self) {
::sys_common::util::dumb_print(format_args!("remutex lock\n"));
let pid = getpid().unwrap();
if *self.own_count.get() > 0 && *self.owner.get() == pid {
*self.own_count.get() += 1;
@ -152,7 +146,6 @@ impl ReentrantMutex {
/// Unlock the mutex
#[inline]
pub unsafe fn unlock(&self) {
::sys_common::util::dumb_print(format_args!("remutex unlock\n"));
let pid = getpid().unwrap();
if *self.own_count.get() > 0 && *self.owner.get() == pid {
*self.own_count.get() -= 1;

View file

@ -15,7 +15,7 @@
use os::unix::prelude::*;
use error::Error as StdError;
use ffi::{CString, CStr, OsString, OsStr};
use ffi::{OsString, OsStr};
use fmt;
use io::{self, Read, Write};
use iter;

View file

@ -8,11 +8,9 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use os::unix::prelude::*;
use collections::hash_map::HashMap;
use env;
use ffi::{OsStr, CString};
use ffi::OsStr;
use fmt;
use io::{self, Error, ErrorKind};
use libc::{self, pid_t, c_int, gid_t, uid_t};
@ -344,13 +342,6 @@ impl Command {
}
}
fn os2c(s: &OsStr, saw_nul: &mut bool) -> CString {
CString::new(s.as_bytes()).unwrap_or_else(|_e| {
*saw_nul = true;
CString::new("<string-with-nul>").unwrap()
})
}
impl Stdio {
fn to_child_stdio(&self, readable: bool)
-> io::Result<(ChildStdio, Option<AnonPipe>)> {
@ -403,18 +394,6 @@ impl ChildStdio {
}
}
fn pair_to_key(key: &OsStr, value: &OsStr, saw_nul: &mut bool) -> CString {
let (key, value) = (key.as_bytes(), value.as_bytes());
let mut v = Vec::with_capacity(key.len() + value.len() + 1);
v.extend(key);
v.push(b'=');
v.extend(value);
CString::new(v).unwrap_or_else(|_e| {
*saw_nul = true;
CString::new("foo=bar").unwrap()
})
}
impl fmt::Debug for Command {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?}", self.program)?;

View file

@ -8,33 +8,33 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use fs::File;
use io;
use libc;
use rand::Rng;
use rand::reader::ReaderRng;
pub struct OsRng {
inner: ReaderRng<File>,
}
pub struct OsRng;
impl OsRng {
/// Create a new `OsRng`.
pub fn new() -> io::Result<OsRng> {
let reader = File::open("rand:")?;
let reader_rng = ReaderRng::new(reader);
Ok(OsRng { inner: reader_rng })
Ok(OsRng)
}
}
impl Rng for OsRng {
fn next_u32(&mut self) -> u32 {
self.inner.next_u32()
self.next_u64() as u32
}
fn next_u64(&mut self) -> u64 {
self.inner.next_u64()
unsafe { libc::random() }
}
fn fill_bytes(&mut self, v: &mut [u8]) {
self.inner.fill_bytes(v)
fn fill_bytes(&mut self, buf: &mut [u8]) {
for chunk in buf.chunks_mut(8) {
let mut rand: u64 = self.next_u64();
for b in chunk.iter_mut() {
*b = rand as u8;
rand = rand >> 8;
}
}
}
}