]> git.lizzy.rs Git - rust.git/blob - src/librlibc/lib.rs
rollup merge of #17355 : gamazeps/issue17210
[rust.git] / src / librlibc / lib.rs
1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! A bare-metal library supplying functions rustc may lower code to
12 //!
13 //! This library is not intended for general use, and is superseded by a system
14 //! libc if one is available. In a freestanding context, however, common
15 //! functions such as memset, memcpy, etc are not implemented. This library
16 //! provides an implementation of these functions which are either required by
17 //! libcore or called by rustc implicitly.
18 //!
19 //! This library is never included by default, and must be manually included if
20 //! necessary. It is an error to include this library when also linking with
21 //! the system libc library.
22
23 #![crate_name = "rlibc"]
24 #![experimental]
25 #![license = "MIT/ASL2"]
26 #![crate_type = "rlib"]
27 #![doc(html_logo_url = "http://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
28        html_favicon_url = "http://www.rust-lang.org/favicon.ico",
29        html_root_url = "http://doc.rust-lang.org/master/")]
30
31 #![feature(import_shadowing, intrinsics, phase)]
32 #![no_std]
33
34 // This library defines the builtin functions, so it would be a shame for
35 // LLVM to optimize these function calls to themselves!
36 #![no_builtins]
37
38 #[phase(plugin, link)] extern crate core;
39
40 #[cfg(test)] extern crate native;
41 #[cfg(test)] extern crate test;
42 #[cfg(test)] extern crate debug;
43
44 #[cfg(test)] #[phase(plugin, link)] extern crate std;
45
46 // Require the offset intrinsics for LLVM to properly optimize the
47 // implementations below. If pointer arithmetic is done through integers the
48 // optimizations start to break down.
49 extern "rust-intrinsic" {
50     fn offset<T>(dst: *const T, offset: int) -> *const T;
51 }
52
53 #[no_mangle]
54 pub unsafe extern "C" fn memcpy(dest: *mut u8, src: *const u8,
55                                 n: uint) -> *mut u8 {
56     let mut i = 0;
57     while i < n {
58         *(offset(dest as *const u8, i as int) as *mut u8) =
59             *offset(src, i as int);
60         i += 1;
61     }
62     return dest;
63 }
64
65 #[no_mangle]
66 pub unsafe extern "C" fn memmove(dest: *mut u8, src: *const u8,
67                                  n: uint) -> *mut u8 {
68     if src < dest as *const u8 { // copy from end
69         let mut i = n;
70         while i != 0 {
71             i -= 1;
72             *(offset(dest as *const u8, i as int) as *mut u8) =
73                 *offset(src, i as int);
74         }
75     } else { // copy from beginning
76         let mut i = 0;
77         while i < n {
78             *(offset(dest as *const u8, i as int) as *mut u8) =
79                 *offset(src, i as int);
80             i += 1;
81         }
82     }
83     return dest;
84 }
85
86 #[no_mangle]
87 pub unsafe extern "C" fn memset(s: *mut u8, c: i32, n: uint) -> *mut u8 {
88     let mut i = 0;
89     while i < n {
90         *(offset(s as *const u8, i as int) as *mut u8) = c as u8;
91         i += 1;
92     }
93     return s;
94 }
95
96 #[no_mangle]
97 pub unsafe extern "C" fn memcmp(s1: *const u8, s2: *const u8, n: uint) -> i32 {
98     let mut i = 0;
99     while i < n {
100         let a = *offset(s1, i as int);
101         let b = *offset(s2, i as int);
102         if a != b {
103             return a as i32 - b as i32
104         }
105         i += 1;
106     }
107     return 0;
108 }
109
110 #[cfg(test)]
111 mod test {
112     use core::collections::Collection;
113     use core::str::StrSlice;
114     use core::slice::{MutableSlice, ImmutableSlice};
115
116     use super::{memcmp, memset, memcpy, memmove};
117
118     #[test]
119     fn memcmp_single_byte_pointers() {
120         unsafe {
121             assert_eq!(memcmp(&0xFAu8, &0xFAu8, 1), 0x00);
122             assert!(memcmp(&0xEFu8, &0xFEu8, 1) < 0x00);
123         }
124     }
125
126     #[test]
127     fn memcmp_strings() {
128         {
129             let (x, z) = ("Hello!", "Good Bye.");
130             let l = x.len();
131             unsafe {
132                 assert_eq!(memcmp(x.as_ptr(), x.as_ptr(), l), 0);
133                 assert!(memcmp(x.as_ptr(), z.as_ptr(), l) > 0);
134                 assert!(memcmp(z.as_ptr(), x.as_ptr(), l) < 0);
135             }
136         }
137         {
138             let (x, z) = ("hey!", "hey.");
139             let l = x.len();
140             unsafe {
141                 assert!(memcmp(x.as_ptr(), z.as_ptr(), l) < 0);
142             }
143         }
144     }
145
146     #[test]
147     fn memset_single_byte_pointers() {
148         let mut x: u8 = 0xFF;
149         unsafe {
150             memset(&mut x, 0xAA, 1);
151             assert_eq!(x, 0xAA);
152             memset(&mut x, 0x00, 1);
153             assert_eq!(x, 0x00);
154             x = 0x01;
155             memset(&mut x, 0x12, 0);
156             assert_eq!(x, 0x01);
157         }
158     }
159
160     #[test]
161     fn memset_array() {
162         let mut buffer = [b'X', .. 100];
163         unsafe {
164             memset(buffer.as_mut_ptr(), b'#' as i32, buffer.len());
165         }
166         for byte in buffer.iter() { assert_eq!(*byte, b'#'); }
167     }
168
169     #[test]
170     fn memcpy_and_memcmp_arrays() {
171         let (src, mut dst) = ([b'X', .. 100], [b'Y', .. 100]);
172         unsafe {
173             assert!(memcmp(src.as_ptr(), dst.as_ptr(), 100) != 0);
174             let _ = memcpy(dst.as_mut_ptr(), src.as_ptr(), 100);
175             assert_eq!(memcmp(src.as_ptr(), dst.as_ptr(), 100), 0);
176         }
177     }
178
179     #[test]
180     fn memmove_overlapping() {
181         {
182             let mut buffer = [ b'0', b'1', b'2', b'3', b'4', b'5', b'6', b'7', b'8', b'9' ];
183             unsafe {
184                 memmove(&mut buffer[4], &buffer[0], 6);
185                 let mut i = 0;
186                 for byte in b"0123012345".iter() {
187                     assert_eq!(buffer[i], *byte);
188                     i += 1;
189                 }
190             }
191         }
192         {
193             let mut buffer = [ b'0', b'1', b'2', b'3', b'4', b'5', b'6', b'7', b'8', b'9' ];
194             unsafe {
195                 memmove(&mut buffer[0], &buffer[4], 6);
196                 let mut i = 0;
197                 for byte in b"4567896789".iter() {
198                     assert_eq!(buffer[i], *byte);
199                     i += 1;
200                 }
201             }
202         }
203     }
204 }