Skip to content

Commit

Permalink
Use Inko for getting the errno location
Browse files Browse the repository at this point in the history
We can just read this using the C FFI instead of going through an extra
Rust function call.

Changelog: changed
  • Loading branch information
yorickpeterse committed Sep 5, 2024
1 parent e65d1e7 commit e93cb49
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 39 deletions.
32 changes: 0 additions & 32 deletions rt/src/runtime/general.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,6 @@ use crate::mem::{header_of, ClassPointer};
use crate::process::ProcessPointer;
use crate::runtime::process::panic;
use std::alloc::handle_alloc_error;
use std::io::Error;

// Taken from Rust's standard library, with some removals of platforms we don't
// support.
extern "C" {
#[cfg_attr(any(target_os = "linux",), link_name = "__errno_location")]
#[cfg_attr(
any(target_os = "netbsd", target_os = "android",),
link_name = "__errno"
)]
#[cfg_attr(
any(
target_os = "macos",
target_os = "ios",
target_os = "tvos",
target_os = "freebsd",
target_os = "watchos"
),
link_name = "__error"
)]
fn errno_location() -> *mut i32;
}

#[no_mangle]
pub unsafe extern "system" fn inko_reference_count_error(
Expand All @@ -45,13 +23,3 @@ pub unsafe extern "system" fn inko_reference_count_error(
pub unsafe extern "system" fn inko_alloc_error(class: ClassPointer) -> ! {
handle_alloc_error(class.instance_layout());
}

#[no_mangle]
pub unsafe extern "system" fn inko_last_error() -> i64 {
Error::last_os_error().raw_os_error().unwrap_or(-1) as i64
}

#[no_mangle]
pub unsafe extern "system" fn inko_reset_error() {
*errno_location() = 0;
}
10 changes: 3 additions & 7 deletions std/src/std/io.inko
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ let INVALID_DATA = -2
# The error code used when encountering an unexpected end of the input.
let UNEXPECTED_EOF = -3

fn extern inko_last_error -> Int

fn extern inko_reset_error

fn extern inko_process_start_blocking(process: Pointer[UInt8])

fn extern inko_process_stop_blocking(process: Pointer[UInt8])
Expand Down Expand Up @@ -91,7 +87,7 @@ fn pub start_blocking {
# rescheduled the current process may run on a different OS thread and thus read
# a different state.
fn pub stop_blocking -> Int {
let err = inko_last_error
let err = libc.errno_location.0 as Int

# If the operation took too long, this reschedules the current process. This
# means that any global state read after this point may not be from the same
Expand All @@ -101,7 +97,7 @@ fn pub stop_blocking -> Int {
}

fn reset_os_error {
inko_reset_error
libc.errno_location.0 = 0 as Int32
}

# An error type for I/O operations.
Expand Down Expand Up @@ -279,7 +275,7 @@ class pub enum Error {

# Returns the last OS error produced by the current OS thread.
fn pub static last_os_error -> Error {
from_os_error(inko_last_error)
from_os_error(libc.errno_location.0 as Int)
}
}

Expand Down
4 changes: 4 additions & 0 deletions std/src/std/libc.inko
Original file line number Diff line number Diff line change
Expand Up @@ -216,3 +216,7 @@ fn dirent_name(pointer: Pointer[sys.Dirent]) -> Pointer[UInt8] {
fn pipes -> Result[(Int32, Int32), Error] {
sys.pipes
}

fn errno_location -> Pointer[Int32] {
sys.errno_location
}
6 changes: 6 additions & 0 deletions std/src/std/libc/freebsd.inko
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ fn extern copy_file_range(

fn extern pipe2(pipes: Pointer[Pipes], flags: Int32) -> Int32

fn extern __error -> Pointer[Int32]

fn flush(fd: Int32) -> Int32 {
fsync(fd)
}
Expand All @@ -184,3 +186,7 @@ fn pipes -> Result[(Int32, Int32), Error] {

Result.Ok((pipes.reader, pipes.writer))
}

fn errno_location -> Pointer[Int32] {
__error
}
6 changes: 6 additions & 0 deletions std/src/std/libc/linux.inko
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,8 @@ fn extern copy_file_range(

fn extern pipe2(pipes: Pointer[Pipes], flags: Int32) -> Int32

fn extern __errno_location -> Pointer[Int32]

fn flush(fd: Int32) -> Int32 {
fsync(fd)
}
Expand Down Expand Up @@ -272,3 +274,7 @@ fn pipes -> Result[(Int32, Int32), Error] {

Result.Ok((pipes.reader, pipes.writer))
}

fn errno_location -> Pointer[Int32] {
__errno_location
}
6 changes: 6 additions & 0 deletions std/src/std/libc/mac.inko
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ fn extern fcopyfile(

fn extern pipe(pipes: Pointer[Pipes]) -> Int32

fn extern __error -> Pointer[Int32]

fn fstat(fd: Int32, buf: Pointer[StatBuf]) -> Int32 {
sys.fstat(fd, buf)
}
Expand Down Expand Up @@ -223,3 +225,7 @@ fn pipes -> Result[(Int32, Int32), Error] {

Result.Ok((pipes.reader, pipes.writer))
}

fn errno_location -> Pointer[Int32] {
__error
}

0 comments on commit e93cb49

Please sign in to comment.