Skip to content

Commit

Permalink
auto merge of #6979 : thestinger/rust/libc, r=brson
Browse files Browse the repository at this point in the history
LLVM provides these functions as intrinsics, and will generate calls to
libc when appropriate. They are exposed in the `ptr` module as
`copy_nonoverlapping_memory`, `copy_memory` and `set_memory`.

@graydon: r?
  • Loading branch information
bors committed Jun 7, 2013
2 parents 5d2cadb + 8bcefef commit 74d9de7
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 36 deletions.
17 changes: 4 additions & 13 deletions src/libstd/libc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,8 @@ pub use libc::funcs::c95::stdlib::{free, getenv, labs, malloc, rand};
pub use libc::funcs::c95::stdlib::{realloc, srand, strtod, strtol};
pub use libc::funcs::c95::stdlib::{strtoul, system};

pub use libc::funcs::c95::string::{memchr, memcmp, memcpy, memmove};
pub use libc::funcs::c95::string::{memset, strcat, strchr, strcmp};
pub use libc::funcs::c95::string::{memchr, memcmp};
pub use libc::funcs::c95::string::{strcat, strchr, strcmp};
pub use libc::funcs::c95::string::{strcoll, strcpy, strcspn, strerror};
pub use libc::funcs::c95::string::{strlen, strncat, strncmp, strncpy};
pub use libc::funcs::c95::string::{strpbrk, strrchr, strspn, strstr};
Expand Down Expand Up @@ -1452,26 +1452,17 @@ pub mod funcs {
-> size_t;
unsafe fn wcslen(buf: *wchar_t) -> size_t;

// Omitted: memcpy, memmove, memset (provided by LLVM)

// These are fine to execute on the Rust stack. They must be,
// in fact, because LLVM generates calls to them!
#[rust_stack]
#[inline(always)]
unsafe fn memcpy(s: *c_void, ct: *c_void, n: size_t)
-> *c_void;
#[rust_stack]
#[inline(always)]
unsafe fn memmove(s: *c_void, ct: *c_void, n: size_t)
-> *c_void;
#[rust_stack]
#[inline(always)]
unsafe fn memcmp(cx: *c_void, ct: *c_void, n: size_t)
-> c_int;
#[rust_stack]
#[inline(always)]
unsafe fn memchr(cx: *c_void, c: c_int, n: size_t) -> *c_void;
#[rust_stack]
#[inline(always)]
unsafe fn memset(s: *c_void, c: c_int, n: size_t) -> *c_void;
}
}
}
Expand Down
24 changes: 1 addition & 23 deletions src/libstd/ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,6 @@ use unstable::intrinsics;
#[cfg(not(test))] use cmp::{Eq, Ord};
use uint;

#[cfg(stage0)]
pub mod libc_ {
use libc::c_void;
use libc;

#[nolink]
#[abi = "cdecl"]
pub extern {
#[rust_stack]
unsafe fn memset(dest: *mut c_void,
c: libc::c_int,
len: libc::size_t)
-> *c_void;
}
}

/// Calculate the offset from a pointer
#[inline(always)]
pub fn offset<T>(ptr: *T, count: uint) -> *T {
Expand Down Expand Up @@ -178,13 +162,6 @@ pub unsafe fn copy_nonoverlapping_memory<T>(dst: *mut T, src: *const T, count: u
memcpy64(dst, src as *T, count as u64);
}

#[inline(always)]
#[cfg(stage0)]
pub unsafe fn set_memory<T>(dst: *mut T, c: int, count: uint) {
let n = count * sys::size_of::<T>();
libc_::memset(dst as *mut c_void, c as libc::c_int, n as size_t);
}

/**
* Invokes memset on the specified pointer, setting `count` bytes of memory
* starting at `dst` to `c`.
Expand Down Expand Up @@ -601,6 +578,7 @@ pub mod ptr_tests {
}

#[test]
#[cfg(not(stage0))]
fn test_set_memory() {
let mut xs = [0u8, ..20];
let ptr = vec::raw::to_mut_ptr(xs);
Expand Down

0 comments on commit 74d9de7

Please sign in to comment.