Skip to content

Commit 568df8f

Browse files
committed
Auto merge of #128 - japaric:mem, r=alexcrichton
add implementations of memcpy et al behind the "mem" Cargo feature, which used to be named "weak" fixes #126 Possible solution to #126 r? @alexcrichton
2 parents 70009a3 + a1caa7c commit 568df8f

File tree

4 files changed

+68
-11
lines changed

4 files changed

+68
-11
lines changed

Cargo.toml

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,6 @@ version = "0.1.0"
88
rustc-cfg = "0.2.0"
99
gcc = "0.3.36"
1010

11-
[dependencies]
12-
13-
[dependencies.rlibc]
14-
git = "https://github.com/alexcrichton/rlibc"
15-
optional = true
16-
1711
[dev-dependencies]
1812
quickcheck = "0.3.1"
1913
rand = "0.3.14"
@@ -26,7 +20,8 @@ c = []
2620
# Mark this crate as the #![compiler_builtins] crate
2721
compiler-builtins = []
2822
default = ["compiler-builtins"]
23+
# Include implementations of memory operations like memcpy
24+
mem = []
2925
rustbuild = ["compiler-builtins"]
30-
weak = ["rlibc/weak"]
3126

3227
[workspace]

src/arm.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
use core::intrinsics;
22

3+
#[cfg(feature = "mem")]
4+
use mem::{memcpy, memmove, memset};
5+
36
// NOTE This function and the ones below are implemented using assembly because they using a custom
47
// calling convention which can't be implemented using a normal Rust function
58
#[naked]
@@ -100,6 +103,7 @@ pub extern "C" fn __aeabi_uidiv(a: u32, b: u32) -> u32 {
100103
::int::udiv::__udivsi3(a, b)
101104
}
102105

106+
#[cfg(not(feature = "mem"))]
103107
extern "C" {
104108
fn memcpy(dest: *mut u8, src: *const u8, n: usize) -> *mut u8;
105109
fn memmove(dest: *mut u8, src: *const u8, n: usize) -> *mut u8;

src/lib.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
#![feature(asm)]
1212
#![feature(compiler_builtins)]
1313
#![feature(core_intrinsics)]
14-
#![feature(linkage)]
1514
#![feature(naked_functions)]
1615
#![feature(staged_api)]
1716
#![no_builtins]
@@ -102,16 +101,16 @@ extern crate compiler_rt;
102101
#[cfg(test)]
103102
extern crate rand;
104103

105-
#[cfg(feature = "weak")]
106-
extern crate rlibc;
107-
108104
#[cfg(test)]
109105
#[macro_use]
110106
mod qc;
111107

112108
pub mod int;
113109
pub mod float;
114110

111+
#[cfg(feature = "mem")]
112+
pub mod mem;
113+
115114
#[cfg(target_arch = "arm")]
116115
pub mod arm;
117116

src/mem.rs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#[no_mangle]
2+
pub unsafe extern "C" fn memcpy(dest: *mut u8,
3+
src: *const u8,
4+
n: usize)
5+
-> *mut u8 {
6+
let mut i = 0;
7+
while i < n {
8+
*dest.offset(i as isize) = *src.offset(i as isize);
9+
i += 1;
10+
}
11+
dest
12+
}
13+
14+
#[no_mangle]
15+
pub unsafe extern "C" fn memmove(dest: *mut u8,
16+
src: *const u8,
17+
n: usize)
18+
-> *mut u8 {
19+
if src < dest as *const u8 {
20+
// copy from end
21+
let mut i = n;
22+
while i != 0 {
23+
i -= 1;
24+
*dest.offset(i as isize) = *src.offset(i as isize);
25+
}
26+
} else {
27+
// copy from beginning
28+
let mut i = 0;
29+
while i < n {
30+
*dest.offset(i as isize) = *src.offset(i as isize);
31+
i += 1;
32+
}
33+
}
34+
dest
35+
}
36+
37+
#[no_mangle]
38+
pub unsafe extern "C" fn memset(s: *mut u8, c: i32, n: usize) -> *mut u8 {
39+
let mut i = 0;
40+
while i < n {
41+
*s.offset(i as isize) = c as u8;
42+
i += 1;
43+
}
44+
s
45+
}
46+
47+
#[no_mangle]
48+
pub unsafe extern "C" fn memcmp(s1: *const u8, s2: *const u8, n: usize) -> i32 {
49+
let mut i = 0;
50+
while i < n {
51+
let a = *s1.offset(i as isize);
52+
let b = *s2.offset(i as isize);
53+
if a != b {
54+
return a as i32 - b as i32;
55+
}
56+
i += 1;
57+
}
58+
0
59+
}

0 commit comments

Comments
 (0)