From dabd8dc6a2348052c3dd4f82e0862a3bef93ca6f Mon Sep 17 00:00:00 2001 From: Jeremy Soller <jackpot51@gmail.com> Date: Tue, 27 Mar 2018 21:28:48 -0600 Subject: [PATCH] Move memory handling into string, do not use compiler_builtins for memory handling --- Cargo.lock | 2 - Cargo.toml | 2 +- include/bits/string.h | 9 ---- src/stdio/Cargo.toml | 1 - src/stdio/src/lib.rs | 2 - src/string/Cargo.toml | 1 - src/string/cbindgen.toml | 1 - src/string/src/lib.rs | 111 ++++++++++++++++++++++++--------------- 8 files changed, 71 insertions(+), 58 deletions(-) delete mode 100644 include/bits/string.h diff --git a/Cargo.lock b/Cargo.lock index 06c311a61..bb3c3a928 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -379,7 +379,6 @@ name = "stdio" version = "0.1.0" dependencies = [ "cbindgen 0.5.2", - "compiler_builtins 0.1.0 (git+https://github.com/rust-lang-nursery/compiler-builtins.git)", "errno 0.1.0", "fcntl 0.1.0", "platform 0.1.0", @@ -405,7 +404,6 @@ name = "string" version = "0.1.0" dependencies = [ "cbindgen 0.5.2", - "compiler_builtins 0.1.0 (git+https://github.com/rust-lang-nursery/compiler-builtins.git)", "errno 0.1.0", "platform 0.1.0", "stdlib 0.1.0", diff --git a/Cargo.toml b/Cargo.toml index 516799ba8..109e7e2cc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,7 +14,7 @@ members = ["src/crt0"] cc = "1.0" [dependencies] -compiler_builtins = { git = "https://github.com/rust-lang-nursery/compiler-builtins.git", default-features = false, features = ["mem"] } +compiler_builtins = { git = "https://github.com/rust-lang-nursery/compiler-builtins.git", default-features = false } ctype = { path = "src/ctype" } errno = { path = "src/errno" } fcntl = { path = "src/fcntl" } diff --git a/include/bits/string.h b/include/bits/string.h deleted file mode 100644 index c62f315fa..000000000 --- a/include/bits/string.h +++ /dev/null @@ -1,9 +0,0 @@ -#ifndef _BITS_STRING_H -#define _BITS_STRING_H - -int memcmp(const void *s1, const void *s2, size_t n); -void *memcpy(void *dest, const void *src, size_t n); -void *memmove(void *dest, const void *src, size_t n); -void *memset(void *s, int c, size_t n); - -#endif /* _BITS_STRING_H */ diff --git a/src/stdio/Cargo.toml b/src/stdio/Cargo.toml index 1a920812a..aa82586fe 100644 --- a/src/stdio/Cargo.toml +++ b/src/stdio/Cargo.toml @@ -8,7 +8,6 @@ build = "build.rs" cbindgen = { path = "../../cbindgen" } [dependencies] -compiler_builtins = { git = "https://github.com/rust-lang-nursery/compiler-builtins.git", default-features = false, features = ["mem"] } platform = { path = "../platform" } va_list = { path = "../../va_list", features = ["no_std"] } fcntl = { path = "../fcntl" } diff --git a/src/stdio/src/lib.rs b/src/stdio/src/lib.rs index 88be52ae1..189365c9d 100644 --- a/src/stdio/src/lib.rs +++ b/src/stdio/src/lib.rs @@ -1,9 +1,7 @@ //! stdio implementation for Redox, following http://pubs.opengroup.org/onlinepubs/7908799/xsh/stdio.h.html -#![feature(compiler_builtins_lib)] #![no_std] -extern crate compiler_builtins; extern crate errno; extern crate fcntl; extern crate platform; diff --git a/src/string/Cargo.toml b/src/string/Cargo.toml index 771d72c5c..9bf1d9dc9 100644 --- a/src/string/Cargo.toml +++ b/src/string/Cargo.toml @@ -11,4 +11,3 @@ cbindgen = { path = "../../cbindgen" } platform = { path = "../platform" } stdlib = { path = "../stdlib" } errno = { path = "../errno" } -compiler_builtins = { git = "https://github.com/rust-lang-nursery/compiler-builtins.git", default-features = false, features = ["mem"] } diff --git a/src/string/cbindgen.toml b/src/string/cbindgen.toml index dd39d24e2..33100dc27 100644 --- a/src/string/cbindgen.toml +++ b/src/string/cbindgen.toml @@ -1,6 +1,5 @@ sys_includes = ["stddef.h", "stdint.h"] include_guard = "_STRING_H" -trailer = "#include <bits/string.h>" language = "C" [enum] diff --git a/src/string/src/lib.rs b/src/string/src/lib.rs index c97302b92..80696ac89 100644 --- a/src/string/src/lib.rs +++ b/src/string/src/lib.rs @@ -1,9 +1,7 @@ //! string implementation for Redox, following http://pubs.opengroup.org/onlinepubs/7908799/xsh/string.h.html -#![feature(compiler_builtins_lib)] #![no_std] -extern crate compiler_builtins; extern crate errno; extern crate platform; extern crate stdlib; @@ -21,18 +19,15 @@ pub unsafe extern "C" fn memccpy( c: c_int, n: usize, ) -> *mut c_void { - use compiler_builtins::mem::memcpy; - let dest = dest as *mut u8; let to = memchr(src, c, n); if to.is_null() { return to; } - let src = src as *mut u8; let dist = (to as usize) - (src as usize); if memcpy(dest, src, dist).is_null() { return ptr::null_mut(); } - dest.offset(dist as isize + 1) as *mut c_void + (dest as *mut u8).offset(dist as isize + 1) as *mut c_void } #[no_mangle] @@ -47,41 +42,75 @@ pub unsafe extern "C" fn memchr(s: *const c_void, c: c_int, n: usize) -> *mut c_ ptr::null_mut() } -// #[no_mangle] -// pub extern "C" fn memcmp( -// s1: *const c_void, -// s2: *const c_void, -// n: usize, -// ) -> c_int { -// unimplemented!(); -// } - -// #[no_mangle] -// pub extern "C" fn memcpy( -// s1: *mut c_void, -// s2: *const c_void, -// n: usize, -// ) -> *mut c_void { -// unimplemented!(); -// } - -// #[no_mangle] -// pub extern "C" fn memmove( -// s1: *mut c_void, -// s2: *const c_void, -// n: usize, -// ) -> *mut c_void { -// unimplemented!(); -// } - -// #[no_mangle] -// pub extern "C" fn memset( -// s: *mut c_void, -// c: c_int, -// n: usize, -// ) -> *mut c_void { -// unimplemented!(); -// } +#[no_mangle] +pub unsafe extern "C" fn memcmp( + s1: *const c_void, + s2: *const c_void, + n: usize, +) -> c_int { + let mut i = 0; + while i < n { + let a = *(s1 as *const u8).offset(i as isize); + let b = *(s2 as *const u8).offset(i as isize); + if a != b { + return a as i32 - b as i32; + } + i += 1; + } + 0 +} + +#[no_mangle] +pub unsafe extern "C" fn memcpy( + s1: *mut c_void, + s2: *const c_void, + n: usize, +) -> *mut c_void { + let mut i = 0; + while i < n { + *(s1 as *mut u8).offset(i as isize) = *(s2 as *const u8).offset(i as isize); + i += 1; + } + s1 +} + +#[no_mangle] +pub unsafe extern "C" fn memmove( + s1: *mut c_void, + s2: *const c_void, + n: usize, +) -> *mut c_void { + if s2 < s1 as *const c_void { + // copy from end + let mut i = n; + while i != 0 { + i -= 1; + *(s1 as *mut u8).offset(i as isize) = *(s2 as *const u8).offset(i as isize); + } + } else { + // copy from beginning + let mut i = 0; + while i < n { + *(s1 as *mut u8).offset(i as isize) = *(s2 as *const u8).offset(i as isize); + i += 1; + } + } + s1 +} + +#[no_mangle] +pub unsafe extern "C" fn memset( + s: *mut c_void, + c: c_int, + n: usize, +) -> *mut c_void { + let mut i = 0; + while i < n { + *(s as *mut u8).offset(i as isize) = c as u8; + i += 1; + } + s +} #[no_mangle] pub unsafe extern "C" fn strcat(s1: *mut c_char, s2: *const c_char) -> *mut c_char { -- GitLab