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.
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.
11 //! A bare-metal library supplying functions rustc may lower code to
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.
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.
23 #![crate_name = "rlibc"]
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/")]
31 #![feature(import_shadowing, intrinsics, phase)]
34 // This library defines the builtin functions, so it would be a shame for
35 // LLVM to optimize these function calls to themselves!
38 #[phase(plugin, link)] extern crate core;
40 #[cfg(test)] extern crate native;
41 #[cfg(test)] extern crate test;
42 #[cfg(test)] extern crate debug;
44 #[cfg(test)] #[phase(plugin, link)] extern crate std;
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;
54 pub unsafe extern "C" fn memcpy(dest: *mut u8, src: *const u8,
58 *(offset(dest as *const u8, i as int) as *mut u8) =
59 *offset(src, i as int);
66 pub unsafe extern "C" fn memmove(dest: *mut u8, src: *const u8,
68 if src < dest as *const u8 { // copy from end
72 *(offset(dest as *const u8, i as int) as *mut u8) =
73 *offset(src, i as int);
75 } else { // copy from beginning
78 *(offset(dest as *const u8, i as int) as *mut u8) =
79 *offset(src, i as int);
87 pub unsafe extern "C" fn memset(s: *mut u8, c: i32, n: uint) -> *mut u8 {
90 *(offset(s as *const u8, i as int) as *mut u8) = c as u8;
97 pub unsafe extern "C" fn memcmp(s1: *const u8, s2: *const u8, n: uint) -> i32 {
100 let a = *offset(s1, i as int);
101 let b = *offset(s2, i as int);
103 return a as i32 - b as i32
112 use core::collections::Collection;
113 use core::str::StrSlice;
114 use core::slice::{MutableSlice, ImmutableSlice};
116 use super::{memcmp, memset, memcpy, memmove};
119 fn memcmp_single_byte_pointers() {
121 assert_eq!(memcmp(&0xFAu8, &0xFAu8, 1), 0x00);
122 assert!(memcmp(&0xEFu8, &0xFEu8, 1) < 0x00);
127 fn memcmp_strings() {
129 let (x, z) = ("Hello!", "Good Bye.");
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);
138 let (x, z) = ("hey!", "hey.");
141 assert!(memcmp(x.as_ptr(), z.as_ptr(), l) < 0);
147 fn memset_single_byte_pointers() {
148 let mut x: u8 = 0xFF;
150 memset(&mut x, 0xAA, 1);
152 memset(&mut x, 0x00, 1);
155 memset(&mut x, 0x12, 0);
162 let mut buffer = [b'X', .. 100];
164 memset(buffer.as_mut_ptr(), b'#' as i32, buffer.len());
166 for byte in buffer.iter() { assert_eq!(*byte, b'#'); }
170 fn memcpy_and_memcmp_arrays() {
171 let (src, mut dst) = ([b'X', .. 100], [b'Y', .. 100]);
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);
180 fn memmove_overlapping() {
182 let mut buffer = [ b'0', b'1', b'2', b'3', b'4', b'5', b'6', b'7', b'8', b'9' ];
184 memmove(&mut buffer[4], &buffer[0], 6);
186 for byte in b"0123012345".iter() {
187 assert_eq!(buffer[i], *byte);
193 let mut buffer = [ b'0', b'1', b'2', b'3', b'4', b'5', b'6', b'7', b'8', b'9' ];
195 memmove(&mut buffer[0], &buffer[4], 6);
197 for byte in b"4567896789".iter() {
198 assert_eq!(buffer[i], *byte);