From 2eb4f05850e25863f05a56b60931e9eb03944b56 Mon Sep 17 00:00:00 2001 From: Florian Hahn Date: Thu, 12 Dec 2013 22:27:26 +0100 Subject: [PATCH] Replace C types with Rust types in libstd, closes #7313 --- src/libstd/cleanup.rs | 5 +-- src/libstd/local_data.rs | 11 +++-- src/libstd/os.rs | 64 +++++++++++++-------------- src/libstd/rc.rs | 4 +- src/libstd/reflect.rs | 7 ++- src/libstd/repr.rs | 27 ++++++----- src/libstd/rt/global_heap.rs | 28 ++++++------ src/libstd/rt/local_heap.rs | 12 +++-- src/libstd/rt/local_ptr.rs | 12 +++-- src/libstd/rt/thread.rs | 2 +- src/libstd/rt/thread_local_storage.rs | 17 ++++--- src/libstd/rt/unwind.rs | 13 +++--- src/libstd/rt/util.rs | 1 - src/libstd/run.rs | 8 ++-- src/libstd/sync/atomics.rs | 3 +- src/libstd/unstable/dynamic_lib.rs | 39 ++++++++-------- src/libstd/unstable/lang.rs | 13 +++--- src/libstd/unstable/mutex.rs | 56 +++++++++++------------ src/libstd/vec.rs | 9 ++-- 19 files changed, 158 insertions(+), 173 deletions(-) diff --git a/src/libstd/cleanup.rs b/src/libstd/cleanup.rs index ab374ebccfe..40c4ad842ff 100644 --- a/src/libstd/cleanup.rs +++ b/src/libstd/cleanup.rs @@ -10,12 +10,11 @@ #[doc(hidden)]; -use libc::c_void; use ptr; use unstable::intrinsics::TyDesc; use unstable::raw; -type DropGlue<'a> = 'a |**TyDesc, *c_void|; +type DropGlue<'a> = 'a |**TyDesc, *u8|; static RC_IMMORTAL : uint = 0x77777777; @@ -107,7 +106,7 @@ pub unsafe fn annihilate() { stats.n_bytes_freed += (*((*alloc).type_desc)).size + mem::size_of::>(); - local_free(alloc as *i8); + local_free(alloc as *u8); true }); diff --git a/src/libstd/local_data.rs b/src/libstd/local_data.rs index 7ef7a256c16..7406fe8ee40 100644 --- a/src/libstd/local_data.rs +++ b/src/libstd/local_data.rs @@ -41,10 +41,9 @@ // magic. use cast; -use libc; use prelude::*; use rt::task::{Task, LocalStorage}; -use util; +use util::replace; /** * Indexes a task-local data slot. This pointer is used for comparison to @@ -87,7 +86,7 @@ impl LocalData for T {} // n.b. If TLS is used heavily in future, this could be made more efficient with // a proper map. #[doc(hidden)] -pub type Map = ~[Option<(*libc::c_void, TLSValue, LoanState)>]; +pub type Map = ~[Option<(*u8, TLSValue, LoanState)>]; type TLSValue = ~LocalData; // Gets the map from the runtime. Lazily initialises if not done so already. @@ -128,7 +127,7 @@ fn describe(&self) -> &'static str { } } -fn key_to_key_value(key: Key) -> *libc::c_void { +fn key_to_key_value(key: Key) -> *u8 { unsafe { cast::transmute(key) } } @@ -151,7 +150,7 @@ pub fn pop(key: Key) -> Option { // Move the data out of the `entry` slot via util::replace. // This is guaranteed to succeed because we already matched // on `Some` above. - let data = match util::replace(entry, None) { + let data = match replace(entry, None) { Some((_, data, _)) => data, None => abort() }; @@ -302,7 +301,7 @@ pub fn set(key: Key, data: T) { let data = ~data as ~LocalData:; fn insertion_position(map: &mut Map, - key: *libc::c_void) -> Option { + key: *u8) -> Option { // First see if the map contains this key already let curspot = map.iter().position(|entry| { match *entry { diff --git a/src/libstd/os.rs b/src/libstd/os.rs index 36ce3e93127..93762a3cdd5 100644 --- a/src/libstd/os.rs +++ b/src/libstd/os.rs @@ -44,9 +44,9 @@ use sync::atomics::{AtomicInt, INIT_ATOMIC_INT, SeqCst}; /// Delegates to the libc close() function, returning the same return value. -pub fn close(fd: c_int) -> c_int { +pub fn close(fd: int) -> int { unsafe { - libc::close(fd) + libc::close(fd as c_int) as int } } @@ -57,7 +57,7 @@ pub fn close(fd: c_int) -> c_int { pub fn getcwd() -> Path { use c_str::CString; - let mut buf = [0 as libc::c_char, ..BUF_BYTES]; + let mut buf = [0 as c_char, ..BUF_BYTES]; unsafe { if libc::getcwd(buf.as_mut_ptr(), buf.len() as size_t).is_null() { fail!() @@ -164,7 +164,7 @@ unsafe fn get_env_pairs() -> ~[~str] { os::last_os_error()); } let mut result = ~[]; - c_str::from_c_multistring(ch as *libc::c_char, None, |cstr| { + c_str::from_c_multistring(ch as *c_char, None, |cstr| { result.push(cstr.as_str().unwrap().to_owned()); }); FreeEnvironmentStringsA(ch); @@ -173,7 +173,7 @@ unsafe fn get_env_pairs() -> ~[~str] { #[cfg(unix)] unsafe fn get_env_pairs() -> ~[~str] { extern { - fn rust_env_pairs() -> **libc::c_char; + fn rust_env_pairs() -> **c_char; } let environ = rust_env_pairs(); if environ as uint == 0 { @@ -306,9 +306,9 @@ pub struct Pipe { #[cfg(unix)] pub fn pipe() -> Pipe { unsafe { - let mut fds = Pipe {input: 0 as c_int, - out: 0 as c_int }; - assert_eq!(libc::pipe(&mut fds.input), (0 as c_int)); + let mut fds = Pipe {input: 0, + out: 0}; + assert_eq!(libc::pipe(&mut fds.input), 0); return Pipe {input: fds.input, out: fds.out}; } } @@ -321,13 +321,13 @@ pub fn pipe() -> Pipe { // fully understand. Here we explicitly make the pipe non-inheritable, // which means to pass it to a subprocess they need to be duplicated // first, as in std::run. - let mut fds = Pipe {input: 0 as c_int, - out: 0 as c_int }; + let mut fds = Pipe {input: 0, + out: 0}; let res = libc::pipe(&mut fds.input, 1024 as ::libc::c_uint, (libc::O_BINARY | libc::O_NOINHERIT) as c_int); - assert_eq!(res, 0 as c_int); - assert!((fds.input != -1 as c_int && fds.input != 0 as c_int)); - assert!((fds.out != -1 as c_int && fds.input != 0 as c_int)); + assert_eq!(res, 0); + assert!((fds.input != -1 && fds.input != 0 )); + assert!((fds.out != -1 && fds.input != 0)); return Pipe {input: fds.input, out: fds.out}; } } @@ -699,7 +699,7 @@ pub fn get_exit_status() -> int { } #[cfg(target_os = "macos")] -unsafe fn load_argc_and_argv(argc: c_int, argv: **c_char) -> ~[~str] { +unsafe fn load_argc_and_argv(argc: int, argv: **c_char) -> ~[~str] { let mut args = ~[]; for i in range(0u, argc as uint) { args.push(str::raw::from_c_str(*argv.offset(i as int))); @@ -715,7 +715,7 @@ unsafe fn load_argc_and_argv(argc: c_int, argv: **c_char) -> ~[~str] { #[cfg(target_os = "macos")] fn real_args() -> ~[~str] { unsafe { - let (argc, argv) = (*_NSGetArgc() as c_int, + let (argc, argv) = (*_NSGetArgc() as int, *_NSGetArgv() as **c_char); load_argc_and_argv(argc, argv) } @@ -833,7 +833,7 @@ pub struct MemoryMap { /// Pointer to the memory created or modified by this map. data: *mut u8, /// Number of bytes this map applies to - len: size_t, + len: uint, /// Type of mapping kind: MemoryMapKind } @@ -842,7 +842,7 @@ pub struct MemoryMap { pub enum MemoryMapKind { /// Memory-mapped file. On Windows, the inner pointer is a handle to the mapping, and /// corresponds to `CreateFileMapping`. Elsewhere, it is null. - MapFile(*c_void), + MapFile(*u8), /// Virtual memory map. Usually used to change the permissions of a given chunk of memory. /// Corresponds to `VirtualAlloc` on Windows. MapVirtual @@ -857,7 +857,7 @@ pub enum MapOption { /// The memory should be executable MapExecutable, /// Create a map for a specific address range. Corresponds to `MAP_FIXED` on POSIX. - MapAddr(*c_void), + MapAddr(*u8), /// Create a memory mapping for a file with a given fd. MapFd(c_int), /// When using `MapFd`, the start of the map is `uint` bytes from the start of the file. @@ -881,7 +881,7 @@ pub enum MapError { /// using `MapFd`, the target of the fd didn't have enough resources to fulfill the request. ErrNoMem, /// Unrecognized error. The inner value is the unrecognized errno. - ErrUnknown(libc::c_int), + ErrUnknown(int), /// ## The following are win32-specific /// /// Unsupported combination of protection flags (`MapReadable`/`MapWritable`/`MapExecutable`). @@ -926,12 +926,12 @@ impl MemoryMap { pub fn new(min_len: uint, options: &[MapOption]) -> Result { use libc::off_t; - let mut addr: *c_void = ptr::null(); - let mut prot: c_int = 0; - let mut flags: c_int = libc::MAP_PRIVATE; - let mut fd: c_int = -1; - let mut offset: off_t = 0; - let len = round_up(min_len, page_size()) as size_t; + let mut addr: *u8 = ptr::null(); + let mut prot = 0; + let mut flags = libc::MAP_PRIVATE; + let mut fd = -1; + let mut offset = 0; + let len = round_up(min_len, page_size()); for &o in options.iter() { match o { @@ -952,7 +952,7 @@ pub fn new(min_len: uint, options: &[MapOption]) -> Result if fd == -1 { flags |= libc::MAP_ANON; } let r = unsafe { - libc::mmap(addr, len, prot, flags, fd, offset) + libc::mmap(addr as *c_void, len as size_t, prot, flags, fd, offset) }; if r.equiv(&libc::MAP_FAILED) { Err(match errno() as c_int { @@ -961,7 +961,7 @@ pub fn new(min_len: uint, options: &[MapOption]) -> Result libc::EINVAL => ErrUnaligned, libc::ENODEV => ErrNoMapSupport, libc::ENOMEM => ErrNoMem, - code => ErrUnknown(code) + code => ErrUnknown(code as int) }) } else { Ok(MemoryMap { @@ -987,7 +987,7 @@ impl Drop for MemoryMap { /// Unmap the mapping. Fails the task if `munmap` fails. fn drop(&mut self) { unsafe { - match libc::munmap(self.data as *c_void, self.len) { + match libc::munmap(self.data as *c_void, self.len as libc::size_t) { 0 => (), -1 => match errno() as c_int { libc::EINVAL => error!("invalid addr or len"), @@ -1011,7 +1011,7 @@ pub fn new(min_len: uint, options: &[MapOption]) -> Result let mut executable = false; let mut fd: c_int = -1; let mut offset: uint = 0; - let len = round_up(min_len, page_size()) as SIZE_T; + let len = round_up(min_len, page_size()); for &o in options.iter() { match o { @@ -1040,7 +1040,7 @@ pub fn new(min_len: uint, options: &[MapOption]) -> Result } let r = unsafe { libc::VirtualAlloc(lpAddress, - len, + len as SIZE_T, libc::MEM_COMMIT | libc::MEM_RESERVE, flProtect) }; @@ -1085,7 +1085,7 @@ pub fn new(min_len: uint, options: &[MapOption]) -> Result _ => Ok(MemoryMap { data: r as *mut u8, len: len, - kind: MapFile(mapping as *c_void) + kind: MapFile(mapping as *u8) }) } } @@ -1116,7 +1116,7 @@ fn drop(&mut self) { match self.kind { MapVirtual => { if libc::VirtualFree(self.data as *mut c_void, - self.len, + self.len as size_t, libc::MEM_RELEASE) == FALSE { error!("VirtualFree failed: {}", errno()); } diff --git a/src/libstd/rc.rs b/src/libstd/rc.rs index 48e796f0f4a..fe82ac74069 100644 --- a/src/libstd/rc.rs +++ b/src/libstd/rc.rs @@ -78,7 +78,7 @@ fn drop(&mut self) { if (*self.ptr).strong == 0 { read_ptr(self.borrow()); // destroy the contained object if (*self.ptr).weak == 0 { - exchange_free(self.ptr as *mut u8 as *i8) + exchange_free(self.ptr as *u8) } } } @@ -153,7 +153,7 @@ fn drop(&mut self) { if self.ptr != 0 as *mut RcBox { (*self.ptr).weak -= 1; if (*self.ptr).weak == 0 && (*self.ptr).strong == 0 { - exchange_free(self.ptr as *mut u8 as *i8) + exchange_free(self.ptr as *u8) } } } diff --git a/src/libstd/reflect.rs b/src/libstd/reflect.rs index e619e404dac..c0af649f26c 100644 --- a/src/libstd/reflect.rs +++ b/src/libstd/reflect.rs @@ -17,7 +17,6 @@ #[allow(missing_doc)]; use unstable::intrinsics::{Disr, Opaque, TyDesc, TyVisitor}; -use libc::c_void; use mem; use unstable::raw; @@ -28,7 +27,7 @@ * then build a MovePtrAdaptor wrapped around your struct. */ pub trait MovePtr { - fn move_ptr(&mut self, adjustment: |*c_void| -> *c_void); + fn move_ptr(&mut self, adjustment: |*u8| -> *u8); fn push_ptr(&mut self); fn pop_ptr(&mut self); } @@ -50,12 +49,12 @@ pub fn MovePtrAdaptor(v: V) -> MovePtrAdaptor { impl MovePtrAdaptor { #[inline] pub fn bump(&mut self, sz: uint) { - self.inner.move_ptr(|p| ((p as uint) + sz) as *c_void) + self.inner.move_ptr(|p| ((p as uint) + sz) as *u8) } #[inline] pub fn align(&mut self, a: uint) { - self.inner.move_ptr(|p| align(p as uint, a) as *c_void) + self.inner.move_ptr(|p| align(p as uint, a) as *u8) } #[inline] diff --git a/src/libstd/repr.rs b/src/libstd/repr.rs index 8919f9f8903..e3b34147c01 100644 --- a/src/libstd/repr.rs +++ b/src/libstd/repr.rs @@ -21,7 +21,6 @@ use container::Container; use io; use iter::Iterator; -use libc::c_void; use option::{Some, None}; use ptr; use reflect; @@ -98,13 +97,13 @@ enum VariantState { } pub struct ReprVisitor<'a> { - priv ptr: *c_void, - priv ptr_stk: ~[*c_void], + priv ptr: *u8, + priv ptr_stk: ~[*u8], priv var_stk: ~[VariantState], priv writer: &'a mut io::Writer } -pub fn ReprVisitor<'a>(ptr: *c_void, +pub fn ReprVisitor<'a>(ptr: *u8, writer: &'a mut io::Writer) -> ReprVisitor<'a> { ReprVisitor { ptr: ptr, @@ -116,7 +115,7 @@ pub fn ReprVisitor<'a>(ptr: *c_void, impl<'a> MovePtr for ReprVisitor<'a> { #[inline] - fn move_ptr(&mut self, adjustment: |*c_void| -> *c_void) { + fn move_ptr(&mut self, adjustment: |*u8| -> *u8) { self.ptr = adjustment(self.ptr); } fn push_ptr(&mut self) { @@ -133,7 +132,7 @@ impl<'a> ReprVisitor<'a> { #[inline] pub fn get(&mut self, f: |&mut ReprVisitor, &T|) -> bool { unsafe { - f(self, transmute::<*c_void,&T>(self.ptr)); + f(self, transmute::<*u8,&T>(self.ptr)); } true } @@ -144,7 +143,7 @@ pub fn visit_inner(&mut self, inner: *TyDesc) -> bool { } #[inline] - pub fn visit_ptr_inner(&mut self, ptr: *c_void, inner: *TyDesc) -> bool { + pub fn visit_ptr_inner(&mut self, ptr: *u8, inner: *TyDesc) -> bool { unsafe { // This should call the constructor up above, but due to limiting // issues we have to recreate it here. @@ -200,7 +199,7 @@ pub fn write_vec_range(&mut self, ptr: *(), len: uint, inner: *TyDesc) -> bool { } else { self.writer.write(", ".as_bytes()); } - self.visit_ptr_inner(p as *c_void, inner); + self.visit_ptr_inner(p as *u8, inner); p = align(unsafe { ptr::offset(p, sz as int) as uint }, al) as *u8; left -= dec; } @@ -298,20 +297,20 @@ fn visit_box(&mut self, mtbl: uint, inner: *TyDesc) -> bool { self.writer.write(['@' as u8]); self.write_mut_qualifier(mtbl); self.get::<&raw::Box<()>>(|this, b| { - let p = ptr::to_unsafe_ptr(&b.data) as *c_void; + let p = ptr::to_unsafe_ptr(&b.data) as *u8; this.visit_ptr_inner(p, inner); }) } fn visit_uniq(&mut self, _mtbl: uint, inner: *TyDesc) -> bool { self.writer.write(['~' as u8]); - self.get::<*c_void>(|this, b| { + self.get::<*u8>(|this, b| { this.visit_ptr_inner(*b, inner); }) } fn visit_ptr(&mut self, mtbl: uint, _inner: *TyDesc) -> bool { - self.get::<*c_void>(|this, p| { + self.get::<*u8>(|this, p| { write!(this.writer, "({} as *", *p); this.write_mut_qualifier(mtbl); this.writer.write("())".as_bytes()); @@ -321,7 +320,7 @@ fn visit_ptr(&mut self, mtbl: uint, _inner: *TyDesc) -> bool { fn visit_rptr(&mut self, mtbl: uint, inner: *TyDesc) -> bool { self.writer.write(['&' as u8]); self.write_mut_qualifier(mtbl); - self.get::<*c_void>(|this, p| { + self.get::<*u8>(|this, p| { this.visit_ptr_inner(*p, inner); }) } @@ -584,7 +583,7 @@ fn visit_type(&mut self) -> bool { true } fn visit_opaque_box(&mut self) -> bool { self.writer.write(['@' as u8]); self.get::<&raw::Box<()>>(|this, b| { - let p = ptr::to_unsafe_ptr(&b.data) as *c_void; + let p = ptr::to_unsafe_ptr(&b.data) as *u8; this.visit_ptr_inner(p, b.type_desc); }) } @@ -594,7 +593,7 @@ fn visit_closure_ptr(&mut self, _ck: uint) -> bool { true } pub fn write_repr(writer: &mut io::Writer, object: &T) { unsafe { - let ptr = ptr::to_unsafe_ptr(object) as *c_void; + let ptr = ptr::to_unsafe_ptr(object) as *u8; let tydesc = get_tydesc::(); let u = ReprVisitor(ptr, writer); let mut v = reflect::MovePtrAdaptor(u); diff --git a/src/libstd/rt/global_heap.rs b/src/libstd/rt/global_heap.rs index 00195f726cb..54442cedb68 100644 --- a/src/libstd/rt/global_heap.rs +++ b/src/libstd/rt/global_heap.rs @@ -8,7 +8,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use libc::{c_void, c_char, size_t, uintptr_t, free, malloc, realloc}; +use libc::{c_void, size_t, free, malloc, realloc}; use ptr::{RawPtr, mut_null}; use unstable::intrinsics::{TyDesc, abort}; use unstable::raw; @@ -31,7 +31,7 @@ fn align_to(size: uint, align: uint) -> uint { /// A wrapper around libc::malloc, aborting on out-of-memory #[inline] -pub unsafe fn malloc_raw(size: uint) -> *mut c_void { +pub unsafe fn malloc_raw(size: uint) -> *mut u8 { // `malloc(0)` may allocate, but it may also return a null pointer // http://pubs.opengroup.org/onlinepubs/9699919799/functions/malloc.html if size == 0 { @@ -42,25 +42,25 @@ pub unsafe fn malloc_raw(size: uint) -> *mut c_void { // we need a non-allocating way to print an error here abort(); } - p + p as *mut u8 } } /// A wrapper around libc::realloc, aborting on out-of-memory #[inline] -pub unsafe fn realloc_raw(ptr: *mut c_void, size: uint) -> *mut c_void { +pub unsafe fn realloc_raw(ptr: *mut u8, size: uint) -> *mut u8 { // `realloc(ptr, 0)` may allocate, but it may also return a null pointer // http://pubs.opengroup.org/onlinepubs/9699919799/functions/realloc.html if size == 0 { free(ptr as *c_void); mut_null() } else { - let p = realloc(ptr, size as size_t); + let p = realloc(ptr as *mut c_void, size as size_t); if p.is_null() { // we need a non-allocating way to print an error here abort(); } - p + p as *mut u8 } } @@ -68,22 +68,22 @@ pub unsafe fn realloc_raw(ptr: *mut c_void, size: uint) -> *mut c_void { #[cfg(not(test))] #[lang="exchange_malloc"] #[inline] -pub unsafe fn exchange_malloc(size: uintptr_t) -> *c_char { - malloc_raw(size as uint) as *c_char +pub unsafe fn exchange_malloc(size: uint) -> *u8 { + malloc_raw(size) as *u8 } // FIXME: #7496 #[cfg(not(test))] #[lang="closure_exchange_malloc"] #[inline] -pub unsafe fn closure_exchange_malloc_(td: *c_char, size: uintptr_t) -> *c_char { +pub unsafe fn closure_exchange_malloc_(td: *u8, size: uint) -> *u8 { closure_exchange_malloc(td, size) } #[inline] -pub unsafe fn closure_exchange_malloc(td: *c_char, size: uintptr_t) -> *c_char { +pub unsafe fn closure_exchange_malloc(td: *u8, size: uint) -> *u8 { let td = td as *TyDesc; - let size = size as uint; + let size = size; assert!(td.is_not_null()); @@ -93,7 +93,7 @@ pub unsafe fn closure_exchange_malloc(td: *c_char, size: uintptr_t) -> *c_char { let alloc = p as *mut raw::Box<()>; (*alloc).type_desc = td; - alloc as *c_char + alloc as *u8 } // NB: Calls to free CANNOT be allowed to fail, as throwing an exception from @@ -101,12 +101,12 @@ pub unsafe fn closure_exchange_malloc(td: *c_char, size: uintptr_t) -> *c_char { #[cfg(not(test))] #[lang="exchange_free"] #[inline] -pub unsafe fn exchange_free_(ptr: *c_char) { +pub unsafe fn exchange_free_(ptr: *u8) { exchange_free(ptr) } #[inline] -pub unsafe fn exchange_free(ptr: *c_char) { +pub unsafe fn exchange_free(ptr: *u8) { free(ptr as *c_void); } diff --git a/src/libstd/rt/local_heap.rs b/src/libstd/rt/local_heap.rs index 90179612272..36e3bf858e3 100644 --- a/src/libstd/rt/local_heap.rs +++ b/src/libstd/rt/local_heap.rs @@ -12,8 +12,6 @@ use cast; use iter::Iterator; -use libc::{c_void, uintptr_t}; -use libc; use mem; use ops::Drop; use option::{Option, None, Some}; @@ -223,7 +221,7 @@ fn realloc(&mut self, alloc: *mut Box, size: uint) -> *mut Box { let total_size = size + AllocHeader::size(); let alloc: *AllocHeader = unsafe { - global_heap::realloc_raw(orig_alloc as *mut libc::c_void, + global_heap::realloc_raw(orig_alloc as *mut u8, total_size) as *AllocHeader }; @@ -243,7 +241,7 @@ fn free(&mut self, alloc: *mut Box) { self.release(cast::transmute(alloc)); rtassert!(self.live_allocations > 0); self.live_allocations -= 1; - global_heap::exchange_free(alloc as *libc::c_char) + global_heap::exchange_free(alloc as *u8) } } @@ -294,12 +292,12 @@ fn drop(&mut self) { } #[inline] -pub unsafe fn local_malloc(td: *libc::c_char, size: libc::uintptr_t) -> *libc::c_char { +pub unsafe fn local_malloc(td: *u8, size: uint) -> *u8 { // XXX: Unsafe borrow for speed. Lame. let task: Option<*mut Task> = Local::try_unsafe_borrow(); match task { Some(task) => { - (*task).heap.alloc(td as *TyDesc, size as uint) as *libc::c_char + (*task).heap.alloc(td as *TyDesc, size) as *u8 } None => rtabort!("local malloc outside of task") } @@ -307,7 +305,7 @@ pub unsafe fn local_malloc(td: *libc::c_char, size: libc::uintptr_t) -> *libc::c // A little compatibility function #[inline] -pub unsafe fn local_free(ptr: *libc::c_char) { +pub unsafe fn local_free(ptr: *u8) { // XXX: Unsafe borrow for speed. Lame. let task_ptr: Option<*mut Task> = Local::try_unsafe_borrow(); match task_ptr { diff --git a/src/libstd/rt/local_ptr.rs b/src/libstd/rt/local_ptr.rs index b89c06edda3..d4e57ab19b1 100644 --- a/src/libstd/rt/local_ptr.rs +++ b/src/libstd/rt/local_ptr.rs @@ -81,14 +81,13 @@ pub mod compiled { use cast; use option::{Option, Some, None}; use ptr::RawPtr; - #[cfg(not(test))] use libc::c_void; #[cfg(test)] pub use realstd::rt::shouldnt_be_public::RT_TLS_PTR; #[cfg(not(test))] #[thread_local] - pub static mut RT_TLS_PTR: *mut c_void = 0 as *mut c_void; + pub static mut RT_TLS_PTR: *mut u8 = 0 as *mut u8; pub fn init() {} @@ -230,7 +229,6 @@ pub unsafe fn try_unsafe_borrow() -> Option<*mut T> { /// thread-local value. pub mod native { use cast; - use libc::c_void; use option::{Option, Some, None}; use ptr; use ptr::RawPtr; @@ -259,7 +257,7 @@ pub unsafe fn cleanup() { #[inline] pub unsafe fn put(sched: ~T) { let key = tls_key(); - let void_ptr: *mut c_void = cast::transmute(sched); + let void_ptr: *mut u8 = cast::transmute(sched); tls::set(key, void_ptr); } @@ -271,7 +269,7 @@ pub unsafe fn put(sched: ~T) { #[inline] pub unsafe fn take() -> ~T { let key = tls_key(); - let void_ptr: *mut c_void = tls::get(key); + let void_ptr: *mut u8 = tls::get(key); if void_ptr.is_null() { rtabort!("thread-local pointer is null. bogus!"); } @@ -289,7 +287,7 @@ pub unsafe fn take() -> ~T { pub unsafe fn try_take() -> Option<~T> { match maybe_tls_key() { Some(key) => { - let void_ptr: *mut c_void = tls::get(key); + let void_ptr: *mut u8 = tls::get(key); if void_ptr.is_null() { None } else { @@ -311,7 +309,7 @@ pub unsafe fn try_take() -> Option<~T> { #[inline] pub unsafe fn unsafe_take() -> ~T { let key = tls_key(); - let void_ptr: *mut c_void = tls::get(key); + let void_ptr: *mut u8 = tls::get(key); if void_ptr.is_null() { rtabort!("thread-local pointer is null. bogus!"); } diff --git a/src/libstd/rt/thread.rs b/src/libstd/rt/thread.rs index f4f4aaa2765..b5262424c06 100644 --- a/src/libstd/rt/thread.rs +++ b/src/libstd/rt/thread.rs @@ -196,7 +196,7 @@ mod imp { use unstable::intrinsics; pub type rust_thread = libc::pthread_t; - pub type rust_thread_return = *libc::c_void; + pub type rust_thread_return = *u8; pub unsafe fn create(stack: uint, p: ~proc()) -> rust_thread { let mut native: libc::pthread_t = intrinsics::uninit(); diff --git a/src/libstd/rt/thread_local_storage.rs b/src/libstd/rt/thread_local_storage.rs index d5affdd5173..40d9523cf3a 100644 --- a/src/libstd/rt/thread_local_storage.rs +++ b/src/libstd/rt/thread_local_storage.rs @@ -10,7 +10,6 @@ #[allow(dead_code)]; -use libc::c_void; #[cfg(unix)] use libc::c_int; #[cfg(unix)] @@ -27,12 +26,12 @@ pub unsafe fn create(key: &mut Key) { } #[cfg(unix)] -pub unsafe fn set(key: Key, value: *mut c_void) { +pub unsafe fn set(key: Key, value: *mut u8) { assert_eq!(0, pthread_setspecific(key, value)); } #[cfg(unix)] -pub unsafe fn get(key: Key) -> *mut c_void { +pub unsafe fn get(key: Key) -> *mut u8 { pthread_getspecific(key) } @@ -55,8 +54,8 @@ pub unsafe fn destroy(key: Key) { extern { fn pthread_key_create(key: *mut pthread_key_t, dtor: *u8) -> c_int; fn pthread_key_delete(key: pthread_key_t) -> c_int; - fn pthread_getspecific(key: pthread_key_t) -> *mut c_void; - fn pthread_setspecific(key: pthread_key_t, value: *mut c_void) -> c_int; + fn pthread_getspecific(key: pthread_key_t) -> *mut u8; + fn pthread_setspecific(key: pthread_key_t, value: *mut u8) -> c_int; } #[cfg(windows)] @@ -70,13 +69,13 @@ pub unsafe fn create(key: &mut Key) { } #[cfg(windows)] -pub unsafe fn set(key: Key, value: *mut c_void) { - assert!(0 != TlsSetValue(key, value)) +pub unsafe fn set(key: Key, value: *mut u8) { + assert!(0 != TlsSetValue(key, value as *mut ::libc::c_void)) } #[cfg(windows)] -pub unsafe fn get(key: Key) -> *mut c_void { - TlsGetValue(key) +pub unsafe fn get(key: Key) -> *mut u8 { + TlsGetValue(key) as *mut u8 } #[cfg(windows)] diff --git a/src/libstd/rt/unwind.rs b/src/libstd/rt/unwind.rs index ffe254574eb..6f3aa4c4fd0 100644 --- a/src/libstd/rt/unwind.rs +++ b/src/libstd/rt/unwind.rs @@ -59,7 +59,6 @@ use c_str::CString; use cast; use kinds::Send; -use libc::{c_void, c_char, size_t}; use option::{Some, None, Option}; use prelude::drop; use ptr::RawPtr; @@ -79,7 +78,7 @@ mod libunwind { #[allow(non_camel_case_types)]; #[allow(dead_code)] // these are just bindings - use libc::{uintptr_t, uint64_t}; + use libc::{uintptr_t}; #[cfg(not(target_arch = "arm"))] #[repr(C)] @@ -118,7 +117,7 @@ pub enum _Unwind_Reason_Code { _URC_FAILURE = 9, // used only by ARM EABI } - pub type _Unwind_Exception_Class = uint64_t; + pub type _Unwind_Exception_Class = u64; pub type _Unwind_Word = uintptr_t; @@ -164,6 +163,7 @@ pub fn unwinding(&self) -> bool { pub fn try(&mut self, f: ||) { use unstable::raw::Closure; + use libc::{c_void}; unsafe { let closure: Closure = cast::transmute(f); @@ -365,10 +365,11 @@ pub extern "C" fn rust_eh_personality_catch( /// The arguments are normally generated by the compiler, and need to /// have static lifetimes. #[inline(never)] #[cold] // this is the slow path, please never inline this -pub fn begin_unwind_raw(msg: *c_char, file: *c_char, line: size_t) -> ! { +pub fn begin_unwind_raw(msg: *u8, file: *u8, line: uint) -> ! { + use libc::c_char; #[inline] - fn static_char_ptr(p: *c_char) -> &'static str { - let s = unsafe { CString::new(p, false) }; + fn static_char_ptr(p: *u8) -> &'static str { + let s = unsafe { CString::new(p as *c_char, false) }; match s.as_str() { Some(s) => unsafe { cast::transmute::<&str, &'static str>(s) }, None => rtabort!("message wasn't utf8?") diff --git a/src/libstd/rt/util.rs b/src/libstd/rt/util.rs index a5d5a4677f1..b482e2fb67f 100644 --- a/src/libstd/rt/util.rs +++ b/src/libstd/rt/util.rs @@ -70,7 +70,6 @@ pub fn default_sched_threads() -> uint { pub fn dumb_println(args: &fmt::Arguments) { use io; - use libc; struct Stderr; impl io::Writer for Stderr { diff --git a/src/libstd/run.rs b/src/libstd/run.rs index f460d3f4944..f42163791a6 100644 --- a/src/libstd/run.rs +++ b/src/libstd/run.rs @@ -331,7 +331,6 @@ pub fn process_output(prog: &str, args: &[~str]) -> Option { #[cfg(test)] mod tests { use prelude::*; - use libc::c_int; use os; use run; use str; @@ -339,6 +338,7 @@ mod tests { use unstable::running_on_valgrind; use io::pipe::PipeStream; use io::{io_error, FileNotFound}; + use libc::c_int; #[test] #[cfg(not(target_os="android"))] // FIXME(#10380) @@ -410,9 +410,9 @@ fn test_pipes() { err_fd: Some(pipe_err.out) }).expect("failed to exec `cat`"); - os::close(pipe_in.input); - os::close(pipe_out.out); - os::close(pipe_err.out); + os::close(pipe_in.input as int); + os::close(pipe_out.out as int); + os::close(pipe_err.out as int); do spawn { writeclose(pipe_in.out, "test"); diff --git a/src/libstd/sync/atomics.rs b/src/libstd/sync/atomics.rs index bc9d99c0f37..30d9ede8a43 100644 --- a/src/libstd/sync/atomics.rs +++ b/src/libstd/sync/atomics.rs @@ -24,7 +24,6 @@ use unstable::intrinsics; use cast; use option::{Option,Some,None}; -use libc::c_void; use ops::Drop; use util::NonCopyable; @@ -73,7 +72,7 @@ pub struct AtomicPtr { */ #[unsafe_no_drop_flag] pub struct AtomicOption { - priv p: *mut c_void + priv p: *mut u8 } pub enum Ordering { diff --git a/src/libstd/unstable/dynamic_lib.rs b/src/libstd/unstable/dynamic_lib.rs index 24568fe13e5..8529b69c6eb 100644 --- a/src/libstd/unstable/dynamic_lib.rs +++ b/src/libstd/unstable/dynamic_lib.rs @@ -18,12 +18,11 @@ use c_str::ToCStr; use cast; use path; -use libc; use ops::*; use option::*; use result::*; -pub struct DynamicLibrary { priv handle: *libc::c_void } +pub struct DynamicLibrary { priv handle: *u8} impl Drop for DynamicLibrary { fn drop(&mut self) { @@ -142,14 +141,14 @@ pub mod dl { use str; use result::*; - pub unsafe fn open_external(filename: &path::Path) -> *libc::c_void { + pub unsafe fn open_external(filename: &path::Path) -> *u8 { filename.with_c_str(|raw_name| { - dlopen(raw_name, Lazy as libc::c_int) + dlopen(raw_name, Lazy as libc::c_int) as *u8 }) } - pub unsafe fn open_internal() -> *libc::c_void { - dlopen(ptr::null(), Lazy as libc::c_int) + pub unsafe fn open_internal() -> *u8 { + dlopen(ptr::null(), Lazy as libc::c_int) as *u8 } pub fn check_for_errors_in(f: || -> T) -> Result { @@ -174,11 +173,11 @@ pub fn check_for_errors_in(f: || -> T) -> Result { } } - pub unsafe fn symbol(handle: *libc::c_void, symbol: *libc::c_char) -> *libc::c_void { - dlsym(handle, symbol) + pub unsafe fn symbol(handle: *u8, symbol: *libc::c_char) -> *u8 { + dlsym(handle as *libc::c_void, symbol) as *u8 } - pub unsafe fn close(handle: *libc::c_void) { - dlclose(handle); () + pub unsafe fn close(handle: *u8) { + dlclose(handle as *libc::c_void); () } pub enum RTLD { @@ -206,16 +205,16 @@ pub mod dl { use ptr; use result::{Ok, Err, Result}; - pub unsafe fn open_external(filename: &path::Path) -> *libc::c_void { + pub unsafe fn open_external(filename: &path::Path) -> *u8 { os::win32::as_utf16_p(filename.as_str().unwrap(), |raw_name| { - LoadLibraryW(raw_name) + LoadLibraryW(raw_name as *libc::c_void) as *u8 }) } - pub unsafe fn open_internal() -> *libc::c_void { + pub unsafe fn open_internal() -> *u8 { let handle = ptr::null(); GetModuleHandleExW(0 as libc::DWORD, ptr::null(), &handle as **libc::c_void); - handle + handle as *u8 } pub fn check_for_errors_in(f: || -> T) -> Result { @@ -233,17 +232,17 @@ pub fn check_for_errors_in(f: || -> T) -> Result { } } - pub unsafe fn symbol(handle: *libc::c_void, symbol: *libc::c_char) -> *libc::c_void { - GetProcAddress(handle, symbol) + pub unsafe fn symbol(handle: *u8, symbol: *libc::c_char) -> *u8 { + GetProcAddress(handle as *libc::c_void, symbol) as *u8 } - pub unsafe fn close(handle: *libc::c_void) { - FreeLibrary(handle); () + pub unsafe fn close(handle: *u8) { + FreeLibrary(handle as *libc::c_void); () } #[link_name = "kernel32"] extern "system" { - fn SetLastError(error: u32); - fn LoadLibraryW(name: *u16) -> *libc::c_void; + fn SetLastError(error: libc::size_t); + fn LoadLibraryW(name: *libc::c_void) -> *libc::c_void; fn GetModuleHandleExW(dwFlags: libc::DWORD, name: *u16, handle: **libc::c_void) -> *libc::c_void; fn GetProcAddress(handle: *libc::c_void, name: *libc::c_char) -> *libc::c_void; diff --git a/src/libstd/unstable/lang.rs b/src/libstd/unstable/lang.rs index 8460152ff7b..046d3fc820d 100644 --- a/src/libstd/unstable/lang.rs +++ b/src/libstd/unstable/lang.rs @@ -1,4 +1,4 @@ -// Copyright 2012 The Rust Project Developers. See the COPYRIGHT +// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT // file at the top-level directory of this distribution and at // http://rust-lang.org/COPYRIGHT. // @@ -11,25 +11,24 @@ //! Runtime calls emitted by the compiler. use c_str::ToCStr; -use libc::{c_char, size_t, uintptr_t}; #[cold] #[lang="fail_"] -pub fn fail_(expr: *c_char, file: *c_char, line: size_t) -> ! { +pub fn fail_(expr: *u8, file: *u8, line: uint) -> ! { ::rt::begin_unwind_raw(expr, file, line); } #[cold] #[lang="fail_bounds_check"] -pub fn fail_bounds_check(file: *c_char, line: size_t, index: size_t, len: size_t) -> ! { +pub fn fail_bounds_check(file: *u8, line: uint, index: uint, len: uint) -> ! { let msg = format!("index out of bounds: the len is {} but the index is {}", len as uint, index as uint); - msg.with_c_str(|buf| fail_(buf, file, line)) + msg.with_c_str(|buf| fail_(buf as *u8, file, line)) } #[lang="malloc"] #[inline] -pub unsafe fn local_malloc(td: *c_char, size: uintptr_t) -> *c_char { +pub unsafe fn local_malloc(td: *u8, size: uint) -> *u8 { ::rt::local_heap::local_malloc(td, size) } @@ -38,6 +37,6 @@ pub unsafe fn local_malloc(td: *c_char, size: uintptr_t) -> *c_char { // problem occurs, call exit instead. #[lang="free"] #[inline] -pub unsafe fn local_free(ptr: *c_char) { +pub unsafe fn local_free(ptr: *u8) { ::rt::local_heap::local_free(ptr); } diff --git a/src/libstd/unstable/mutex.rs b/src/libstd/unstable/mutex.rs index 81317b7de79..ff38a8c0199 100644 --- a/src/libstd/unstable/mutex.rs +++ b/src/libstd/unstable/mutex.rs @@ -48,7 +48,6 @@ #[allow(non_camel_case_types)]; use int; -use libc::c_void; use sync::atomics; pub struct Mutex { @@ -133,38 +132,37 @@ pub unsafe fn destroy(&mut self) { if cond != 0 { imp::free_cond(cond) } } - unsafe fn getlock(&mut self) -> *c_void { + unsafe fn getlock(&mut self) -> uint{ match self.lock.load(atomics::Relaxed) { 0 => {} - n => return n as *c_void + n => return n } let lock = imp::init_lock(); match self.lock.compare_and_swap(0, lock, atomics::SeqCst) { - 0 => return lock as *c_void, + 0 => return lock, _ => {} } imp::free_lock(lock); - return self.lock.load(atomics::Relaxed) as *c_void; + self.lock.load(atomics::Relaxed) } - unsafe fn getcond(&mut self) -> *c_void { + unsafe fn getcond(&mut self) -> uint { match self.cond.load(atomics::Relaxed) { 0 => {} - n => return n as *c_void + n => return n } let cond = imp::init_cond(); match self.cond.compare_and_swap(0, cond, atomics::SeqCst) { - 0 => return cond as *c_void, + 0 => return cond, _ => {} } imp::free_cond(cond); - return self.cond.load(atomics::Relaxed) as *c_void; + self.cond.load(atomics::Relaxed) } } #[cfg(unix)] mod imp { - use libc::c_void; use libc; use ptr; use rt::global_heap::malloc_raw; @@ -175,49 +173,49 @@ mod imp { type pthread_condattr_t = libc::c_void; pub unsafe fn init_lock() -> uint { - let block = malloc_raw(rust_pthread_mutex_t_size() as uint) as *c_void; + let block = malloc_raw(rust_pthread_mutex_t_size() as uint) as *pthread_mutex_t; let n = pthread_mutex_init(block, ptr::null()); assert_eq!(n, 0); return block as uint; } pub unsafe fn init_cond() -> uint { - let block = malloc_raw(rust_pthread_cond_t_size() as uint) as *c_void; + let block = malloc_raw(rust_pthread_cond_t_size() as uint) as *pthread_cond_t; let n = pthread_cond_init(block, ptr::null()); assert_eq!(n, 0); return block as uint; } pub unsafe fn free_lock(h: uint) { - let block = h as *c_void; + let block = h as *libc::c_void; assert_eq!(pthread_mutex_destroy(block), 0); libc::free(block); } pub unsafe fn free_cond(h: uint) { - let block = h as *c_void; + let block = h as *pthread_cond_t; assert_eq!(pthread_cond_destroy(block), 0); libc::free(block); } - pub unsafe fn lock(l: *pthread_mutex_t) { - assert_eq!(pthread_mutex_lock(l), 0); + pub unsafe fn lock(l: uint) { + assert_eq!(pthread_mutex_lock(l as *pthread_mutex_t), 0); } - pub unsafe fn trylock(l: *c_void) -> bool { - pthread_mutex_trylock(l) == 0 + pub unsafe fn trylock(l: uint) -> bool { + pthread_mutex_trylock(l as *pthread_mutex_t) == 0 } - pub unsafe fn unlock(l: *pthread_mutex_t) { - assert_eq!(pthread_mutex_unlock(l), 0); + pub unsafe fn unlock(l: uint) { + assert_eq!(pthread_mutex_unlock(l as *pthread_mutex_t), 0); } - pub unsafe fn wait(cond: *pthread_cond_t, m: *pthread_mutex_t) { - assert_eq!(pthread_cond_wait(cond, m), 0); + pub unsafe fn wait(cond: uint, m: uint) { + assert_eq!(pthread_cond_wait(cond as *pthread_cond_t, m as *pthread_mutex_t), 0); } - pub unsafe fn signal(cond: *pthread_cond_t) { - assert_eq!(pthread_cond_signal(cond), 0); + pub unsafe fn signal(cond: uint) { + assert_eq!(pthread_cond_signal(cond as *pthread_cond_t), 0); } extern { @@ -273,25 +271,25 @@ pub unsafe fn free_cond(h: uint) { libc::CloseHandle(block); } - pub unsafe fn lock(l: *c_void) { + pub unsafe fn lock(l: uint) { EnterCriticalSection(l as LPCRITICAL_SECTION) } - pub unsafe fn trylock(l: *c_void) -> bool { + pub unsafe fn trylock(l: uint) -> bool { TryEnterCriticalSection(l as LPCRITICAL_SECTION) != 0 } - pub unsafe fn unlock(l: *c_void) { + pub unsafe fn unlock(l: uint) { LeaveCriticalSection(l as LPCRITICAL_SECTION) } - pub unsafe fn wait(cond: *c_void, m: *c_void) { + pub unsafe fn wait(cond: uint, m: uint) { unlock(m); WaitForSingleObject(cond as HANDLE, libc::INFINITE); lock(m); } - pub unsafe fn signal(cond: *c_void) { + pub unsafe fn signal(cond: uint) { assert!(SetEvent(cond as HANDLE) != 0); } diff --git a/src/libstd/vec.rs b/src/libstd/vec.rs index 27e949a5640..885c669ecb4 100644 --- a/src/libstd/vec.rs +++ b/src/libstd/vec.rs @@ -109,7 +109,6 @@ use cmp; use default::Default; use iter::*; -use libc::{c_char, c_void}; use num::{Integer, CheckedAdd, Saturating}; use option::{None, Option, Some}; use ptr::to_unsafe_ptr; @@ -1478,8 +1477,8 @@ fn reserve(&mut self, n: uint) { if alloc / mem::nonzero_size_of::() != n || size < alloc { fail!("vector size is too large: {}", n); } - *ptr = realloc_raw(*ptr as *mut c_void, size) - as *mut Vec<()>; + *ptr = realloc_raw(*ptr as *mut u8, size) + as *mut Vec<()>; (**ptr).alloc = alloc; } } @@ -1513,7 +1512,7 @@ fn shrink_to_fit(&mut self) { let ptr: *mut *mut Vec<()> = cast::transmute(self); let alloc = (**ptr).fill; let size = alloc + mem::size_of::>(); - *ptr = realloc_raw(*ptr as *mut c_void, size) as *mut Vec<()>; + *ptr = realloc_raw(*ptr as *mut u8, size) as *mut Vec<()>; (**ptr).alloc = alloc; } } @@ -2877,7 +2876,7 @@ fn drop(&mut self) { // destroy the remaining elements for _x in *self {} unsafe { - exchange_free(self.allocation as *u8 as *c_char) + exchange_free(self.allocation as *u8) } } } -- GitLab