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_id = "rlibc#0.11.0"]
24 #![license = "MIT/ASL2"]
25 #![crate_type = "rlib"]
26 #![doc(html_logo_url = "http://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
27 html_favicon_url = "http://www.rust-lang.org/favicon.ico",
28 html_root_url = "http://doc.rust-lang.org/0.11.0/")]
29 #![feature(intrinsics)]
30 #![allow(unknown_features)] // NOTE: remove after stage0 snapshot
35 // This library is definining the builtin functions, so it would be a shame for
36 // LLVM to optimize these function calls to themselves!
39 #[cfg(test)] extern crate std;
40 #[cfg(test)] extern crate native;
42 // Require the offset intrinsics for LLVM to properly optimize the
43 // implementations below. If pointer arithmetic is done through integers the
44 // optimizations start to break down.
45 extern "rust-intrinsic" {
46 fn offset<T>(dst: *T, offset: int) -> *T;
50 pub unsafe extern "C" fn memcpy(dest: *mut u8, src: *u8, n: uint) -> *mut u8 {
53 *(offset(dest as *u8, i as int) as *mut u8) = *offset(src, i as int);
60 pub unsafe extern "C" fn memmove(dest: *mut u8, src: *u8, n: uint) -> *mut u8 {
61 if src < dest as *u8 { // copy from end
65 *(offset(dest as *u8, i as int) as *mut u8) = *offset(src, i as int);
67 } else { // copy from beginning
70 *(offset(dest as *u8, i as int) as *mut u8) = *offset(src, i as int);
78 pub unsafe extern "C" fn memset(s: *mut u8, c: i32, n: uint) -> *mut u8 {
81 *(offset(s as *u8, i as int) as *mut u8) = c as u8;
88 pub unsafe extern "C" fn memcmp(s1: *u8, s2: *u8, n: uint) -> i32 {
91 let a = *offset(s1, i as int);
92 let b = *offset(s2, i as int);
101 #[test] fn work_on_windows() { } // FIXME #10872 needed for a happy windows