]> git.lizzy.rs Git - rust.git/blob - src/librlibc/lib.rs
Update to 0.11.0
[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_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
31
32 #![no_std]
33 #![experimental]
34
35 // This library is definining the builtin functions, so it would be a shame for
36 // LLVM to optimize these function calls to themselves!
37 #![no_builtins]
38
39 #[cfg(test)] extern crate std;
40 #[cfg(test)] extern crate native;
41
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;
47 }
48
49 #[no_mangle]
50 pub unsafe extern "C" fn memcpy(dest: *mut u8, src: *u8, n: uint) -> *mut u8 {
51     let mut i = 0;
52     while i < n {
53         *(offset(dest as *u8, i as int) as *mut u8) = *offset(src, i as int);
54         i += 1;
55     }
56     return dest;
57 }
58
59 #[no_mangle]
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
62         let mut i = n;
63         while i != 0 {
64             i -= 1;
65             *(offset(dest as *u8, i as int) as *mut u8) = *offset(src, i as int);
66         }
67     } else { // copy from beginning
68         let mut i = 0;
69         while i < n {
70             *(offset(dest as *u8, i as int) as *mut u8) = *offset(src, i as int);
71             i += 1;
72         }
73     }
74     return dest;
75 }
76
77 #[no_mangle]
78 pub unsafe extern "C" fn memset(s: *mut u8, c: i32, n: uint) -> *mut u8 {
79     let mut i = 0;
80     while i < n {
81         *(offset(s as *u8, i as int) as *mut u8) = c as u8;
82         i += 1;
83     }
84     return s;
85 }
86
87 #[no_mangle]
88 pub unsafe extern "C" fn memcmp(s1: *u8, s2: *u8, n: uint) -> i32 {
89     let mut i = 0;
90     while i < n {
91         let a = *offset(s1, i as int);
92         let b = *offset(s2, i as int);
93         if a != b {
94             return (a - b) as i32
95         }
96         i += 1;
97     }
98     return 0;
99 }
100
101 #[test] fn work_on_windows() { } // FIXME #10872 needed for a happy windows