Explorar el Código

Added memchr and memccpy to string

Tom Almeida hace 7 años
padre
commit
4f601f4896
Se han modificado 4 ficheros con 59 adiciones y 5 borrados
  1. 2 1
      src/string/Cargo.toml
  2. 34 4
      src/string/src/lib.rs
  3. 1 0
      tests/Makefile
  4. 22 0
      tests/mem.c

+ 2 - 1
src/string/Cargo.toml

@@ -10,4 +10,5 @@ cbindgen = { path = "../../cbindgen" }
 [dependencies]
 platform = { path = "../platform" }
 stdlib = { path = "../stdlib" }
-errno = { path = "../errno" }
+errno = { path = "../errno" }
+compiler_builtins = { git = "https://github.com/rust-lang-nursery/compiler-builtins.git", default-features = false, features = ["mem"] }

+ 34 - 4
src/string/src/lib.rs

@@ -1,7 +1,9 @@
 //! 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;
@@ -12,13 +14,41 @@ use core::cmp;
 use core::usize;
 
 #[no_mangle]
-pub extern "C" fn memccpy(s1: *mut c_void, s2: *const c_void, c: c_int, n: usize) -> *mut c_void {
-    unimplemented!();
+pub unsafe extern "C" fn memccpy(
+    dest: *mut c_void,
+    src: *const c_void,
+    c: c_int,
+    n: usize,
+) -> *mut c_void {
+    use compiler_builtins::mem::memcpy;
+    use core::mem;
+    let dest = dest as *mut u8;
+    let to = memchr(src, c, n);
+    if to as usize == 0 {
+        return to;
+    }
+    let src = src as *mut u8;
+    let dist = ((to as usize) - (src as usize)) / mem::size_of::<u8>();
+    if memcpy(dest, src, dist) as usize > 0 {
+        return dest.offset(dist as isize) as *mut c_void;
+    }
+    0usize as *mut c_void
 }
 
 #[no_mangle]
-pub extern "C" fn memchr(s: *const c_void, c: c_int, n: usize) -> *mut c_void {
-    unimplemented!();
+pub unsafe extern "C" fn memchr(s: *const c_void, c: c_int, n: usize) -> *mut c_void {
+    let s = s as *mut u8;
+    let c = c as u8;
+    let mut i = 0;
+    loop {
+        if *s.offset(i as isize) == c {
+            return s.offset(i as isize) as *mut c_void;
+        }
+        i += 1;
+        if i == n {
+            return 0usize as *mut c_void;
+        }
+    }
 }
 
 // #[no_mangle]

+ 1 - 0
tests/Makefile

@@ -16,6 +16,7 @@ BINS=\
 	getid \
 	link \
 	math \
+	mem \
 	pipe \
 	printf \
 	rmdir \

+ 22 - 0
tests/mem.c

@@ -0,0 +1,22 @@
+#include <stdio.h>
+#include <string.h>
+
+int main(int argc, char ** argv) {
+	printf("# mem #\n");
+	char arr[100];
+	memset(arr, 0, 100); // Compiler builtin, should work
+	arr[50] = 1;
+	if ((size_t)memchr((void *)arr, 1, 100) - (size_t)arr != 50) {
+		printf("Incorrect memchr\n");
+		exit(1);
+	}
+	printf("Correct memchr\n");
+	char arr2[51];
+	memset(arr2, 0, 51); // Compiler builtin, should work
+	memccpy((void *)arr2, (void *)arr, 1, 100);
+	if (arr[50] != 1) {
+		printf("Incorrect memccpy\n");
+		exit(1);
+	}
+	printf("Correct memccpy\n");
+}