Skip to content

Commit

Permalink
some change
Browse files Browse the repository at this point in the history
  • Loading branch information
coolyjg committed Jun 13, 2023
1 parent 1727c69 commit f4b789f
Show file tree
Hide file tree
Showing 16 changed files with 165 additions and 170 deletions.
4 changes: 2 additions & 2 deletions ulib/c_libax/include/dirent.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include <sys/types.h>

struct dirstream {
struct __dirstream {
long long tell;
int fd;
int buf_pos;
Expand All @@ -12,7 +12,7 @@ struct dirstream {
char buf[2048];
};

typedef struct dirstream DIR;
typedef struct __dirstream DIR;

struct dirent {
ino_t d_ino;
Expand Down
2 changes: 1 addition & 1 deletion ulib/c_libax/include/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ size_t strcspn(const char *s1, const char *s2);
size_t strspn(const char *s, const char *c);
char *strpbrk(const char *, const char *);

char *__strchrnul(const char *, int);
char *strchrnul(const char *, int);

char *strrchr(const char *str, int c);
char *strchr(const char *str, int c);
Expand Down
6 changes: 5 additions & 1 deletion ulib/c_libax/include/sys/epoll.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ typedef union epoll_data {
struct epoll_event {
uint32_t events;
epoll_data_t data;
};
}
#ifdef __x86_64__
__attribute__((__packed__))
#endif
;

int epoll_create(int __size);
int epoll_ctl(int, int, int, struct epoll_event *);
Expand Down
2 changes: 0 additions & 2 deletions ulib/c_libax/include/unistd.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,4 @@ long int sysconf(int name);
#define _SC_THREAD_ROBUST_PRIO_INHERIT 247
#define _SC_THREAD_ROBUST_PRIO_PROTECT 248

extern char **__environ;

#endif
14 changes: 14 additions & 0 deletions ulib/c_libax/src/env.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <string.h>
#include <unistd.h>

char **environ = 0;

char *getenv(const char *name)
{
size_t l = strchrnul(name, '=') - name;
if (l && !name[l] && environ)
for (char **e = environ; *e; e++)
if (!strncmp(name, *e, l) && l[*e] == '=')
return *e + l + 1;
return 0;
}
6 changes: 0 additions & 6 deletions ulib/c_libax/src/math.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,4 @@ double floor(double x)
return x + y;
}

// TODO
long double __extenddftf2(double a)
{
return 0;
}

#endif
10 changes: 0 additions & 10 deletions ulib/c_libax/src/stdlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,6 @@ _Noreturn void abort(void)
__builtin_unreachable();
}

char *getenv(const char *name)
{
size_t l = __strchrnul(name, '=') - name;
if (l && !name[l] && __environ)
for (char **e = __environ; *e; e++)
if (!strncmp(name, *e, l) && l[*e] == '=')
return *e + l + 1;
return 0;
}

// TODO:
int __clzdi2(int a)
{
Expand Down
2 changes: 1 addition & 1 deletion ulib/c_libax/src/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ char *strpbrk(const char *s, const char *b)
return *s ? (char *)s : 0;
}

char *__strchrnul(const char *s, int c)
char *strchrnul(const char *s, int c)
{
c = (unsigned char)c;
if (!c)
Expand Down
2 changes: 0 additions & 2 deletions ulib/c_libax/src/unistd.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
#include <time.h>
#include <unistd.h>

char **__environ = 0;

// TODO:
uid_t geteuid(void)
{
Expand Down
3 changes: 0 additions & 3 deletions ulib/libax/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,6 @@ net = ["alloc", "axruntime/net", "dep:axdriver", "dep:axnet"]
# Pipe
pipe = ["alloc"]

# Epoll
epoll = ["alloc"]

# Display
display = ["axruntime/display", "dep:axdriver", "dep:axdisplay"]

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use super::ctypes;
use super::fd_ops::FileLike;
use crate::cbindings::fd_ops::{add_file_like, get_file_like};
use crate::cbindings::{
ctypes,
fd_ops::{add_file_like, get_file_like, FileLike},
};
use crate::debug;
use alloc::collections::{BTreeMap, BTreeSet};
use alloc::sync::Arc;
use axerrno::{LinuxError, LinuxResult};
use axsync::Mutex;

use alloc::collections::{BTreeMap, BTreeSet};
use alloc::sync::Arc;
use core::ffi::c_int;

pub struct EPollCtlOp;
Expand Down
2 changes: 2 additions & 0 deletions ulib/libax/src/cbindings/io_mpx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
//!
//! * [`select`](select::ax_select)
mod epoll;
mod select;

pub use self::epoll::{ax_epoll_create, ax_epoll_ctl, ax_epoll_wait};
pub use self::select::ax_select;
6 changes: 2 additions & 4 deletions ulib/libax/src/cbindings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ mod ctypes;
#[macro_use]
mod utils;

#[cfg(feature = "epoll")]
mod epoll;
#[cfg(feature = "alloc")]
mod fd_ops;
#[cfg(feature = "fs")]
Expand All @@ -26,7 +24,7 @@ mod pipe;
#[cfg(feature = "net")]
mod socket;
#[cfg(feature = "fp_simd")]
mod str;
mod strtod;
#[cfg(feature = "multitask")]
mod thread;

Expand Down Expand Up @@ -85,7 +83,7 @@ pub use self::pipe::ax_pipe;
pub use self::io_mpx::ax_select;

#[cfg(feature = "fp_simd")]
pub use self::str::{ax_strtod, ax_strtof};
pub use self::strtod::{ax_strtod, ax_strtof};

#[cfg(feature = "epoll")]
pub use self::epoll::{ax_epoll_create, ax_epoll_ctl, ax_epoll_wait};
Expand Down
24 changes: 0 additions & 24 deletions ulib/libax/src/cbindings/str.rs

This file was deleted.

131 changes: 131 additions & 0 deletions ulib/libax/src/cbindings/strtod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
use core::ffi::{c_char, c_double, c_float, c_int};

macro_rules! strto_float_impl {
($type:ident, $s:expr, $endptr:expr) => {{
let mut s = $s;
let endptr = $endptr;

// TODO: Handle named floats: NaN, Inf...

while isspace(*s as c_int) {
s = s.offset(1);
}

let mut result: $type = 0.0;
let mut radix = 10;

let result_sign = match *s as u8 {
b'-' => {
s = s.offset(1);
-1.0
}
b'+' => {
s = s.offset(1);
1.0
}
_ => 1.0,
};

if *s as u8 == b'0' && *s.offset(1) as u8 == b'x' {
s = s.offset(2);
radix = 16;
}

while let Some(digit) = (*s as u8 as char).to_digit(radix) {
result *= radix as $type;
result += digit as $type;
s = s.offset(1);
}

if *s as u8 == b'.' {
s = s.offset(1);

let mut i = 1.0;
while let Some(digit) = (*s as u8 as char).to_digit(radix) {
i *= radix as $type;
result += digit as $type / i;
s = s.offset(1);
}
}

let s_before_exponent = s;

let exponent = match (*s as u8, radix) {
(b'e' | b'E', 10) | (b'p' | b'P', 16) => {
s = s.offset(1);

let is_exponent_positive = match *s as u8 {
b'-' => {
s = s.offset(1);
false
}
b'+' => {
s = s.offset(1);
true
}
_ => true,
};

// Exponent digits are always in base 10.
if (*s as u8 as char).is_digit(10) {
let mut exponent_value = 0;

while let Some(digit) = (*s as u8 as char).to_digit(10) {
exponent_value *= 10;
exponent_value += digit;
s = s.offset(1);
}

let exponent_base = match radix {
10 => 10u128,
16 => 2u128,
_ => unreachable!(),
};

if is_exponent_positive {
Some(exponent_base.pow(exponent_value) as $type)
} else {
Some(1.0 / (exponent_base.pow(exponent_value) as $type))
}
} else {
// Exponent had no valid digits after 'e'/'p' and '+'/'-', rollback
s = s_before_exponent;
None
}
}
_ => None,
};

// Return pointer should be *mut
if !endptr.is_null() {
*endptr = s as *mut _;
}

if let Some(exponent) = exponent {
result_sign * result * exponent
} else {
result_sign * result
}
}};
}

fn isspace(c: c_int) -> bool {
c == c_int::from(b' ')
|| c == c_int::from(b'\t')
|| c == c_int::from(b'\n')
|| c == c_int::from(b'\r')
|| c == 0x0b
|| c == 0x0c
}

/// `strtod` implementation
#[no_mangle]
pub unsafe extern "C" fn ax_strtod(s: *const c_char, endptr: *mut *mut c_char) -> c_double {
strto_float_impl!(c_double, s, endptr)
}

/// `strtof`implementation
#[no_mangle]
pub unsafe extern "C" fn ax_strtof(s: *const c_char, endptr: *mut *mut c_char) -> c_float {
strto_float_impl!(c_float, s, endptr)
}
Loading

0 comments on commit f4b789f

Please sign in to comment.