Skip to content

Commit f526131

Browse files
committed
[library stdio]: Stdin Read implementition and a few more Error related changes
1 parent 0eef083 commit f526131

File tree

2 files changed

+35
-18
lines changed

2 files changed

+35
-18
lines changed

library/std/src/sys/pal/safaos/alloc.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ impl Block {
2222
assert!(size <= isize::MAX as usize);
2323

2424
let ptr = get_data_break() as *mut Block;
25-
syscalls::syssbrk(size as isize).ok()?;
25+
syscalls::sbrk(size as isize).ok()?;
2626
unsafe {
2727
*ptr = Self { free: true, data_len: size - size_of::<Block>(), ..Default::default() };
2828
Some(&mut *ptr)
@@ -50,7 +50,8 @@ struct SystemAllocator {
5050
}
5151

5252
fn get_data_break() -> *mut u8 {
53-
syscalls::syssbrk(0).unwrap()
53+
// Should never fail
54+
unsafe { syscalls::sbrk(0).unwrap_unchecked() }
5455
}
5556

5657
impl SystemAllocator {
@@ -71,6 +72,7 @@ impl SystemAllocator {
7172
None
7273
}
7374

75+
#[inline]
7476
pub fn allocate(&mut self, size: usize) -> Option<NonNull<[u8]>> {
7577
if let Some(block) = self.find_block(size) {
7678
unsafe {
@@ -91,6 +93,7 @@ impl SystemAllocator {
9193
}
9294
}
9395

96+
#[inline]
9497
pub fn deallocate(&mut self, block_data: NonNull<u8>) {
9598
unsafe {
9699
let block_ptr = Block::block_from_data_ptr(block_data);
@@ -101,10 +104,11 @@ impl SystemAllocator {
101104

102105
#[unstable(feature = "allocator_api", issue = "32838")]
103106
unsafe impl Allocator for Mutex<SystemAllocator> {
107+
#[inline]
104108
fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, core::alloc::AllocError> {
105109
self.lock().unwrap().allocate(layout.size()).ok_or(core::alloc::AllocError)
106110
}
107-
111+
#[inline]
108112
unsafe fn deallocate(&self, ptr: NonNull<u8>, _: Layout) {
109113
self.lock().unwrap().deallocate(ptr)
110114
}
@@ -114,13 +118,14 @@ static GLOBAL_SYSTEM_ALLOCATOR: Mutex<SystemAllocator> = Mutex::new(SystemAlloca
114118

115119
#[stable(feature = "alloc_system_type", since = "1.28.0")]
116120
unsafe impl GlobalAlloc for System {
121+
#[inline]
117122
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
118123
match GLOBAL_SYSTEM_ALLOCATOR.allocate(layout) {
119124
Ok(data) => data.as_ptr() as *mut u8,
120125
Err(core::alloc::AllocError) => ptr::null_mut(),
121126
}
122127
}
123-
128+
#[inline]
124129
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
125130
unsafe { GLOBAL_SYSTEM_ALLOCATOR.deallocate(NonNull::new_unchecked(ptr), layout) }
126131
}

library/std/src/sys/pal/safaos/stdio.rs

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,72 @@
11
use super::syscalls;
22
use crate::io;
3+
4+
#[stable(feature = "stdio", since = "1.0.0")]
5+
impl From<syscalls::ErrorStatus> for io::Error {
6+
fn from(err: syscalls::ErrorStatus) -> io::Error {
7+
let kind = err.into_io_error_kind();
8+
let error = err.as_str();
9+
10+
io::Error::new(kind, error)
11+
}
12+
}
13+
314
pub struct Stdin;
415
pub struct Stdout;
516
pub struct Stderr;
617

718
impl Stdin {
19+
pub const FD: usize = 0;
820
pub const fn new() -> Stdin {
921
Stdin
1022
}
1123
}
1224

1325
impl io::Read for Stdin {
14-
fn read(&mut self, _buf: &mut [u8]) -> io::Result<usize> {
15-
Ok(0)
26+
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
27+
Ok(syscalls::read(Self::FD, -1, buf)?)
1628
}
1729
}
1830

1931
impl Stdout {
32+
const FD: usize = 1;
2033
pub const fn new() -> Stdout {
2134
Stdout
2235
}
2336
}
2437

2538
impl io::Write for Stdout {
2639
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
27-
let mut dest_wrote = 0;
28-
syscalls::syswrite(1, -1, buf.as_ptr(), buf.len(), &mut dest_wrote);
29-
Ok(dest_wrote)
40+
Ok(syscalls::write(Self::FD, -1, buf)?)
3041
}
3142

3243
fn flush(&mut self) -> io::Result<()> {
33-
syscalls::syssync(1);
34-
Ok(())
44+
Ok(syscalls::sync(1)?)
3545
}
3646
}
3747

3848
impl Stderr {
49+
// TODO: please add a seprate stderr fd
50+
const FD: usize = Stdout::FD;
3951
pub const fn new() -> Stderr {
4052
Stderr
4153
}
4254
}
4355

4456
impl io::Write for Stderr {
57+
#[inline]
4558
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
46-
let mut dest_wrote = 0;
47-
syscalls::syswrite(2, -1, buf.as_ptr(), buf.len(), &mut dest_wrote);
48-
Ok(dest_wrote)
59+
let wrote = syscalls::write(Self::FD, -1, buf)?;
60+
self.flush()?;
61+
Ok(wrote)
4962
}
50-
63+
#[inline]
5164
fn flush(&mut self) -> io::Result<()> {
52-
syscalls::syssync(2);
53-
Ok(())
65+
Ok(syscalls::sync(Self::FD)?)
5466
}
5567
}
5668

57-
pub const STDIN_BUF_SIZE: usize = 0;
69+
pub const STDIN_BUF_SIZE: usize = 1024;
5870

5971
pub fn is_ebadf(_err: &io::Error) -> bool {
6072
true

0 commit comments

Comments
 (0)