make errno a static instead of TLS

This commit is contained in:
nora 2023-10-03 08:52:40 +02:00
parent 00f78e9749
commit 7a16d84b25
15 changed files with 57 additions and 55 deletions

View file

@ -1,18 +1,20 @@
use core::{cell::UnsafeCell, ptr::addr_of}; use core::{cell::UnsafeCell, ptr::addr_of};
#[thread_local] use crate::utils::SyncUnsafeCell;
static ERRNO: UnsafeCell<i32> = UnsafeCell::new(0);
// Todo: This should be a thread local once we have threads.
static ERRNO: SyncUnsafeCell<i32> = SyncUnsafeCell(UnsafeCell::new(0));
pub fn errno_location() -> *const i32 { pub fn errno_location() -> *const i32 {
addr_of!(ERRNO).cast() addr_of!(ERRNO).cast()
} }
pub fn errno() -> i32 { pub fn errno() -> i32 {
unsafe { *ERRNO.get() } unsafe { *ERRNO.0.get() }
} }
pub fn set_errno(errno: i32) { pub fn set_errno(errno: i32) {
unsafe { ERRNO.get().write(errno) } unsafe { ERRNO.0.get().write(errno) }
} }
pub const EPERM: i32 = 1; /* Operation not permitted */ pub const EPERM: i32 = 1; /* Operation not permitted */

View file

@ -79,7 +79,7 @@ mod tests {
fn memcmp_eq_one() { fn memcmp_eq_one() {
let a = [1]; let a = [1];
let b = [1]; let b = [1];
let result = unsafe { super::memcmp(a.as_ptr(), b.as_ptr(), 1)}; let result = unsafe { super::memcmp(a.as_ptr(), b.as_ptr(), 1) };
assert_eq!(result, 0); assert_eq!(result, 0);
} }
@ -103,7 +103,7 @@ mod tests {
fn memcmp_lt_one() { fn memcmp_lt_one() {
let a = [0]; let a = [0];
let b = [1]; let b = [1];
let result = unsafe { super::memcmp(a.as_ptr(), b.as_ptr(), 1)}; let result = unsafe { super::memcmp(a.as_ptr(), b.as_ptr(), 1) };
assert_eq!(result, -1); assert_eq!(result, -1);
} }
@ -127,7 +127,7 @@ mod tests {
fn memcmp_gt_one() { fn memcmp_gt_one() {
let a = [255]; let a = [255];
let b = [1]; let b = [1];
let result = unsafe { super::memcmp(a.as_ptr(), b.as_ptr(), 1)}; let result = unsafe { super::memcmp(a.as_ptr(), b.as_ptr(), 1) };
assert_eq!(result, 1); assert_eq!(result, 1);
} }

View file

@ -0,0 +1 @@

View file

@ -13,7 +13,6 @@ pub unsafe extern "C" fn memcmp(s1: *const u8, s2: *const u8, size: usize) -> i3
libuwuc::mem::memcmp(s1, s2, size) libuwuc::mem::memcmp(s1, s2, size)
} }
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn bcmp(s1: *const u8, s2: *const u8, size: usize) -> i32 { pub unsafe extern "C" fn bcmp(s1: *const u8, s2: *const u8, size: usize) -> i32 {
libuwuc::mem::memcmp(s1, s2, size) libuwuc::mem::memcmp(s1, s2, size)

View file

@ -1,6 +1,6 @@
#include<stdio.h> #include <stdio.h>
int main(int argc, char* argv[]) { int main(int argc, char *argv[]) {
char *self = argv[0]; char *self = argv[0];
char first = self[0]; char first = self[0];
if (first != '/') { if (first != '/') {

View file

@ -1,5 +1,4 @@
//@ignore doens't initialize fs yet #include <errno.h>
#include<errno.h>
int main(void) { int main(void) {
int err = errno; int err = errno;

View file

@ -6,7 +6,8 @@ int main(int argc, char *argv[]) {
return 1; return 1;
} }
char *env2 = getenv("__some absolutely NONSENSE that no one would ever define please.."); char *env2 = getenv(
"__some absolutely NONSENSE that no one would ever define please..");
if (env2) { if (env2) {
return 1; return 1;
} }

View file

@ -1,7 +1,7 @@
#include<stdlib.h> #include <stdlib.h>
int main(void) { int main(void) {
char *alloc = (char*) malloc(10); char *alloc = (char *)malloc(10);
*alloc = 1; *alloc = 1;
*(alloc + 9) = 2; *(alloc + 9) = 2;
free(alloc); free(alloc);

View file

@ -1,4 +1,4 @@
#include<string.h> #include <string.h>
int main(void) { int main(void) {
char buf[10]; char buf[10];

View file

@ -1,4 +1,4 @@
#include<stdio.h> #include <stdio.h>
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
int result = printf("Hello, world!\n"); int result = printf("Hello, world!\n");

View file

@ -1,4 +1,4 @@
#include<stdlib.h> #include <stdlib.h>
int main(void) { int main(void) {
char *str = "12"; char *str = "12";