From cfbe27490f30a127a55c4bb165fea53a86338b77 Mon Sep 17 00:00:00 2001 From: Jeremy Soller <jeremy@system76.com> Date: Thu, 8 Mar 2018 15:00:17 -0700 Subject: [PATCH] Remove allocation, fix pipe example --- src/platform/Cargo.toml | 3 --- src/platform/src/lib.rs | 2 -- src/platform/src/redox/mod.rs | 13 +++++-------- tests/pipe.c | 14 ++++++++++++-- 4 files changed, 17 insertions(+), 15 deletions(-) diff --git a/src/platform/Cargo.toml b/src/platform/Cargo.toml index a00eb79b..e25b8251 100644 --- a/src/platform/Cargo.toml +++ b/src/platform/Cargo.toml @@ -8,6 +8,3 @@ sc = "0.2" [target.'cfg(target_os = "redox")'.dependencies] redox_syscall = "0.1" - -[dependencies] -alloc-no-stdlib = "1.2" diff --git a/src/platform/src/lib.rs b/src/platform/src/lib.rs index 6359118c..5f57298d 100644 --- a/src/platform/src/lib.rs +++ b/src/platform/src/lib.rs @@ -3,8 +3,6 @@ #![no_std] #![allow(non_camel_case_types)] //TODO #![feature(thread_local)] -#![feature(alloc)] -extern crate alloc; #[cfg(all(not(feature = "no_std"), target_os = "linux"))] #[macro_use] diff --git a/src/platform/src/redox/mod.rs b/src/platform/src/redox/mod.rs index e9fe6dda..5a18ed23 100644 --- a/src/platform/src/redox/mod.rs +++ b/src/platform/src/redox/mod.rs @@ -1,8 +1,5 @@ -extern crate alloc; - use core::ptr; use core::slice; -use alloc::Vec; use syscall; use c_str; @@ -127,11 +124,11 @@ pub fn open(path: *const c_char, oflag: c_int, mode: mode_t) -> c_int { } pub fn pipe(fds: [c_int; 2]) -> c_int { - let usize_vec = fds.iter().map(|x| *x as usize).collect::<Vec<usize>>(); - let usize_slice = usize_vec.as_slice(); - let mut usize_arr: [usize; 2] = Default::default(); - usize_arr.copy_from_slice(usize_slice); - e(syscall::pipe2(&mut usize_arr, 0)) as c_int + let mut usize_fds: [usize; 2] = [0; 2]; + let res = e(syscall::pipe2(&mut usize_fds)); + fds[0] = usize_fds[0] as c_int; + fds[1] = usize_fds[1] as c_int; + res as c_int } pub fn read(fd: c_int, buf: &mut [u8]) -> ssize_t { diff --git a/tests/pipe.c b/tests/pipe.c index 0ad78bc3..6772cd51 100644 --- a/tests/pipe.c +++ b/tests/pipe.c @@ -1,4 +1,5 @@ //http://www2.cs.uregina.ca/~hamilton/courses/330/notes/unix/pipes/pipes.html +#include <string.h> #include <unistd.h> int main() @@ -6,18 +7,27 @@ int main() int pid, pip[2]; char instring[20]; + char * outstring = "Hello World!"; - pipe(pip); + pipe(pip); pid = fork(); if (pid == 0) /* child : sends message to parent*/ { + /* close read end */ + close(pip[0]); /* send 7 characters in the string, including end-of-string */ - write(pip[1], "Hi Mom!", 7); + write(pip[1], outstring, strlen(outstring)); + /* close write end */ + close(pip[1]); } else /* parent : receives message from child */ { + /* close write end */ + close(pip[1]); /* read from the pipe */ read(pip[0], instring, 7); + /* close read end */ + close(pip[0]); } } -- GitLab