auto merge of #12445 : huonw/rust/less-unsafe, r=alexcrichton
Commits for details. Highlights: - `flate` returns `CVec<u8>` to save reallocating a whole new `&[u8]` - a lot of `transmute`s removed outright or replaced with `as` (etc.)
This commit is contained in:
commit
b48bc9ec93
@ -65,7 +65,7 @@ DEPS_rustc := syntax native:rustllvm flate arena serialize sync getopts \
|
||||
collections time extra
|
||||
DEPS_rustdoc := rustc native:sundown serialize sync getopts collections \
|
||||
test time
|
||||
DEPS_flate := std native:miniz
|
||||
DEPS_flate := std extra native:miniz
|
||||
DEPS_arena := std collections
|
||||
DEPS_glob := std
|
||||
DEPS_serialize := std
|
||||
|
@ -167,13 +167,12 @@ unsafe fn destroy_chunk(chunk: &Chunk) {
|
||||
// is necessary in order to properly do cleanup if a failure occurs
|
||||
// during an initializer.
|
||||
#[inline]
|
||||
unsafe fn bitpack_tydesc_ptr(p: *TyDesc, is_done: bool) -> uint {
|
||||
let p_bits: uint = transmute(p);
|
||||
p_bits | (is_done as uint)
|
||||
fn bitpack_tydesc_ptr(p: *TyDesc, is_done: bool) -> uint {
|
||||
p as uint | (is_done as uint)
|
||||
}
|
||||
#[inline]
|
||||
unsafe fn un_bitpack_tydesc_ptr(p: uint) -> (*TyDesc, bool) {
|
||||
(transmute(p & !1), p & 1 == 1)
|
||||
fn un_bitpack_tydesc_ptr(p: uint) -> (*TyDesc, bool) {
|
||||
((p & !1) as *TyDesc, p & 1 == 1)
|
||||
}
|
||||
|
||||
impl Arena {
|
||||
|
@ -35,7 +35,9 @@
|
||||
* if necessary.
|
||||
*/
|
||||
|
||||
use std::cast;
|
||||
use std::ptr;
|
||||
use std::raw;
|
||||
|
||||
/**
|
||||
* The type representing a foreign chunk of memory
|
||||
@ -111,6 +113,20 @@ impl <T> CVec<T> {
|
||||
}
|
||||
}
|
||||
|
||||
/// View the stored data as a slice.
|
||||
pub fn as_slice<'a>(&'a self) -> &'a [T] {
|
||||
unsafe {
|
||||
cast::transmute(raw::Slice { data: self.base as *T, len: self.len })
|
||||
}
|
||||
}
|
||||
|
||||
/// View the stored data as a mutable slice.
|
||||
pub fn as_mut_slice<'a>(&'a mut self) -> &'a mut [T] {
|
||||
unsafe {
|
||||
cast::transmute(raw::Slice { data: self.base as *T, len: self.len })
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves an element at a given index
|
||||
*
|
||||
|
@ -20,9 +20,10 @@ Simple compression
|
||||
#[license = "MIT/ASL2"];
|
||||
#[allow(missing_doc)];
|
||||
|
||||
extern crate extra;
|
||||
use std::libc::{c_void, size_t, c_int};
|
||||
use std::libc;
|
||||
use std::vec;
|
||||
use extra::c_vec::CVec;
|
||||
|
||||
pub mod rustrt {
|
||||
use std::libc::{c_int, c_void, size_t};
|
||||
@ -33,13 +34,13 @@ pub mod rustrt {
|
||||
src_buf_len: size_t,
|
||||
pout_len: *mut size_t,
|
||||
flags: c_int)
|
||||
-> *c_void;
|
||||
-> *mut c_void;
|
||||
|
||||
pub fn tinfl_decompress_mem_to_heap(psrc_buf: *c_void,
|
||||
src_buf_len: size_t,
|
||||
pout_len: *mut size_t,
|
||||
flags: c_int)
|
||||
-> *c_void;
|
||||
-> *mut c_void;
|
||||
}
|
||||
}
|
||||
|
||||
@ -47,49 +48,43 @@ static LZ_NORM : c_int = 0x80; // LZ with 128 probes, "normal"
|
||||
static TINFL_FLAG_PARSE_ZLIB_HEADER : c_int = 0x1; // parse zlib header and adler32 checksum
|
||||
static TDEFL_WRITE_ZLIB_HEADER : c_int = 0x01000; // write zlib header and adler32 checksum
|
||||
|
||||
fn deflate_bytes_internal(bytes: &[u8], flags: c_int) -> ~[u8] {
|
||||
fn deflate_bytes_internal(bytes: &[u8], flags: c_int) -> CVec<u8> {
|
||||
unsafe {
|
||||
let mut outsz : size_t = 0;
|
||||
let res = rustrt::tdefl_compress_mem_to_heap(bytes.as_ptr() as *c_void,
|
||||
bytes.len() as size_t,
|
||||
&mut outsz,
|
||||
flags);
|
||||
assert!(res as int != 0);
|
||||
let out = vec::raw::from_buf_raw(res as *u8,
|
||||
outsz as uint);
|
||||
libc::free(res as *mut c_void);
|
||||
out
|
||||
assert!(!res.is_null());
|
||||
CVec::new_with_dtor(res as *mut u8, outsz as uint, proc() libc::free(res))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn deflate_bytes(bytes: &[u8]) -> ~[u8] {
|
||||
pub fn deflate_bytes(bytes: &[u8]) -> CVec<u8> {
|
||||
deflate_bytes_internal(bytes, LZ_NORM)
|
||||
}
|
||||
|
||||
pub fn deflate_bytes_zlib(bytes: &[u8]) -> ~[u8] {
|
||||
pub fn deflate_bytes_zlib(bytes: &[u8]) -> CVec<u8> {
|
||||
deflate_bytes_internal(bytes, LZ_NORM | TDEFL_WRITE_ZLIB_HEADER)
|
||||
}
|
||||
|
||||
fn inflate_bytes_internal(bytes: &[u8], flags: c_int) -> ~[u8] {
|
||||
fn inflate_bytes_internal(bytes: &[u8], flags: c_int) -> CVec<u8> {
|
||||
unsafe {
|
||||
let mut outsz : size_t = 0;
|
||||
let res = rustrt::tinfl_decompress_mem_to_heap(bytes.as_ptr() as *c_void,
|
||||
bytes.len() as size_t,
|
||||
&mut outsz,
|
||||
flags);
|
||||
assert!(res as int != 0);
|
||||
let out = vec::raw::from_buf_raw(res as *u8,
|
||||
outsz as uint);
|
||||
libc::free(res as *mut c_void);
|
||||
out
|
||||
assert!(!res.is_null());
|
||||
CVec::new_with_dtor(res as *mut u8, outsz as uint, proc() libc::free(res))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn inflate_bytes(bytes: &[u8]) -> ~[u8] {
|
||||
pub fn inflate_bytes(bytes: &[u8]) -> CVec<u8> {
|
||||
inflate_bytes_internal(bytes, 0)
|
||||
}
|
||||
|
||||
pub fn inflate_bytes_zlib(bytes: &[u8]) -> ~[u8] {
|
||||
pub fn inflate_bytes_zlib(bytes: &[u8]) -> CVec<u8> {
|
||||
inflate_bytes_internal(bytes, TINFL_FLAG_PARSE_ZLIB_HEADER)
|
||||
}
|
||||
|
||||
@ -115,11 +110,11 @@ mod tests {
|
||||
debug!("de/inflate of {} bytes of random word-sequences",
|
||||
input.len());
|
||||
let cmp = deflate_bytes(input);
|
||||
let out = inflate_bytes(cmp);
|
||||
let out = inflate_bytes(cmp.as_slice());
|
||||
debug!("{} bytes deflated to {} ({:.1f}% size)",
|
||||
input.len(), cmp.len(),
|
||||
100.0 * ((cmp.len() as f64) / (input.len() as f64)));
|
||||
assert_eq!(input, out);
|
||||
assert_eq!(input.as_slice(), out.as_slice());
|
||||
}
|
||||
}
|
||||
|
||||
@ -127,7 +122,7 @@ mod tests {
|
||||
fn test_zlib_flate() {
|
||||
let bytes = ~[1, 2, 3, 4, 5];
|
||||
let deflated = deflate_bytes(bytes);
|
||||
let inflated = inflate_bytes(deflated);
|
||||
assert_eq!(inflated, bytes);
|
||||
let inflated = inflate_bytes(deflated.as_slice());
|
||||
assert_eq!(inflated.as_slice(), bytes.as_slice());
|
||||
}
|
||||
}
|
||||
|
@ -9,8 +9,7 @@
|
||||
// except according to those terms.
|
||||
|
||||
use std::uint;
|
||||
use std::cast::{transmute, transmute_mut_unsafe,
|
||||
transmute_region, transmute_mut_region};
|
||||
use std::cast::{transmute, transmute_mut_unsafe};
|
||||
use stack::Stack;
|
||||
use std::rt::stack;
|
||||
use std::raw;
|
||||
@ -55,10 +54,6 @@ impl Context {
|
||||
// Save and then immediately load the current context,
|
||||
// which we will then modify to call the given function when restored
|
||||
let mut regs = new_regs();
|
||||
unsafe {
|
||||
rust_swap_registers(transmute_mut_region(&mut *regs),
|
||||
transmute_region(&*regs));
|
||||
};
|
||||
|
||||
initialize_call_frame(&mut *regs,
|
||||
init,
|
||||
@ -294,11 +289,8 @@ fn initialize_call_frame(regs: &mut Registers, fptr: InitFn, arg: uint,
|
||||
}
|
||||
|
||||
fn align_down(sp: *mut uint) -> *mut uint {
|
||||
unsafe {
|
||||
let sp: uint = transmute(sp);
|
||||
let sp = sp & !(16 - 1);
|
||||
transmute::<uint, *mut uint>(sp)
|
||||
}
|
||||
let sp = (sp as uint) & !(16 - 1);
|
||||
sp as *mut uint
|
||||
}
|
||||
|
||||
// ptr::mut_offset is positive ints only
|
||||
|
@ -612,9 +612,7 @@ impl Scheduler {
|
||||
f: |&mut Scheduler, ~GreenTask|) -> ~GreenTask {
|
||||
let f_opaque = ClosureConverter::from_fn(f);
|
||||
|
||||
let current_task_dupe = unsafe {
|
||||
*cast::transmute::<&~GreenTask, &uint>(¤t_task)
|
||||
};
|
||||
let current_task_dupe = &*current_task as *GreenTask;
|
||||
|
||||
// The current task is placed inside an enum with the cleanup
|
||||
// function. This enum is then placed inside the scheduler.
|
||||
@ -633,13 +631,8 @@ impl Scheduler {
|
||||
cast::transmute_mut_region(*next_task.sched.get_mut_ref());
|
||||
|
||||
let current_task: &mut GreenTask = match sched.cleanup_job {
|
||||
Some(CleanupJob { task: ref task, .. }) => {
|
||||
let task_ptr: *~GreenTask = task;
|
||||
cast::transmute_mut_region(*cast::transmute_mut_unsafe(task_ptr))
|
||||
}
|
||||
None => {
|
||||
rtabort!("no cleanup job");
|
||||
}
|
||||
Some(CleanupJob { task: ref mut task, .. }) => &mut **task,
|
||||
None => rtabort!("no cleanup job")
|
||||
};
|
||||
|
||||
let (current_task_context, next_task_context) =
|
||||
@ -852,7 +845,7 @@ impl Scheduler {
|
||||
|
||||
// * Utility Functions
|
||||
|
||||
pub fn sched_id(&self) -> uint { unsafe { cast::transmute(self) } }
|
||||
pub fn sched_id(&self) -> uint { self as *Scheduler as uint }
|
||||
|
||||
pub fn run_cleanup_job(&mut self) {
|
||||
let cleanup_job = self.cleanup_job.take_unwrap();
|
||||
|
@ -266,7 +266,7 @@ impl GreenTask {
|
||||
// context switches
|
||||
|
||||
pub fn as_uint(&self) -> uint {
|
||||
unsafe { cast::transmute(self) }
|
||||
self as *GreenTask as uint
|
||||
}
|
||||
|
||||
pub unsafe fn from_uint(val: uint) -> ~GreenTask { cast::transmute(val) }
|
||||
|
@ -14,7 +14,7 @@ use std::cast;
|
||||
use std::io::IoError;
|
||||
use std::libc;
|
||||
use std::libc::{c_char, c_int};
|
||||
use std::ptr::null;
|
||||
use std::ptr::{null, mut_null};
|
||||
|
||||
use super::net::sockaddr_to_addr;
|
||||
|
||||
@ -42,13 +42,13 @@ impl GetAddrInfoRequest {
|
||||
});
|
||||
|
||||
let hint_ptr = hint.as_ref().map_or(null(), |x| x as *libc::addrinfo);
|
||||
let res = null();
|
||||
let mut res = mut_null();
|
||||
|
||||
// Make the call
|
||||
let s = unsafe {
|
||||
let ch = if c_host.is_null() { null() } else { c_host.with_ref(|x| x) };
|
||||
let cs = if c_serv.is_null() { null() } else { c_serv.with_ref(|x| x) };
|
||||
getaddrinfo(ch, cs, hint_ptr, &res)
|
||||
getaddrinfo(ch, cs, hint_ptr, &mut res)
|
||||
};
|
||||
|
||||
// Error?
|
||||
@ -74,7 +74,7 @@ impl GetAddrInfoRequest {
|
||||
flags: (*rp).ai_flags as uint
|
||||
});
|
||||
|
||||
rp = (*rp).ai_next;
|
||||
rp = (*rp).ai_next as *mut libc::addrinfo;
|
||||
}
|
||||
}
|
||||
|
||||
@ -86,8 +86,8 @@ impl GetAddrInfoRequest {
|
||||
|
||||
extern "system" {
|
||||
fn getaddrinfo(node: *c_char, service: *c_char,
|
||||
hints: *libc::addrinfo, res: **libc::addrinfo) -> c_int;
|
||||
fn freeaddrinfo(res: *libc::addrinfo);
|
||||
hints: *libc::addrinfo, res: *mut *mut libc::addrinfo) -> c_int;
|
||||
fn freeaddrinfo(res: *mut libc::addrinfo);
|
||||
#[cfg(not(windows))]
|
||||
fn gai_strerror(errcode: c_int) -> *c_char;
|
||||
#[cfg(windows)]
|
||||
|
@ -91,7 +91,7 @@ impl FileDesc {
|
||||
#[cfg(not(windows))] type rlen = libc::size_t;
|
||||
let ret = retry(|| unsafe {
|
||||
libc::read(self.fd(),
|
||||
buf.as_ptr() as *mut libc::c_void,
|
||||
buf.as_mut_ptr() as *mut libc::c_void,
|
||||
buf.len() as rlen) as libc::c_int
|
||||
});
|
||||
if ret == 0 {
|
||||
|
@ -309,7 +309,7 @@ impl rtio::RtioTcpStream for TcpStream {
|
||||
let ret = retry(|| {
|
||||
unsafe {
|
||||
libc::recv(self.fd(),
|
||||
buf.as_ptr() as *mut libc::c_void,
|
||||
buf.as_mut_ptr() as *mut libc::c_void,
|
||||
buf.len() as wrlen,
|
||||
0) as libc::c_int
|
||||
}
|
||||
|
@ -186,7 +186,7 @@ impl rt::Runtime for Ops {
|
||||
cur_task.put_runtime(self as ~rt::Runtime);
|
||||
|
||||
unsafe {
|
||||
let cur_task_dupe = *cast::transmute::<&~Task, &uint>(&cur_task);
|
||||
let cur_task_dupe = &*cur_task as *Task;
|
||||
let task = BlockedTask::block(cur_task);
|
||||
|
||||
if times == 1 {
|
||||
@ -218,7 +218,7 @@ impl rt::Runtime for Ops {
|
||||
}
|
||||
}
|
||||
// re-acquire ownership of the task
|
||||
cur_task = cast::transmute::<uint, ~Task>(cur_task_dupe);
|
||||
cur_task = cast::transmute(cur_task_dupe);
|
||||
}
|
||||
|
||||
// put the task back in TLS, and everything is as it once was.
|
||||
|
@ -944,7 +944,7 @@ fn link_rlib(sess: Session,
|
||||
// into the archive.
|
||||
let bc = obj_filename.with_extension("bc");
|
||||
match fs::File::open(&bc).read_to_end().and_then(|data| {
|
||||
fs::File::create(&bc).write(flate::deflate_bytes(data))
|
||||
fs::File::create(&bc).write(flate::deflate_bytes(data).as_slice())
|
||||
}) {
|
||||
Ok(()) => {}
|
||||
Err(e) => {
|
||||
|
@ -58,7 +58,7 @@ pub fn run(sess: session::Session, llmod: ModuleRef,
|
||||
let bc = bc.expect("missing bytecode in archive!");
|
||||
let bc = time(sess.time_passes(), format!("inflate {}.bc", name), (), |_|
|
||||
flate::inflate_bytes(bc));
|
||||
let ptr = bc.as_ptr();
|
||||
let ptr = bc.as_slice().as_ptr();
|
||||
debug!("linking {}", name);
|
||||
time(sess.time_passes(), format!("ll link {}", name), (), |()| unsafe {
|
||||
if !llvm::LLVMRustLinkInExternalBitcode(llmod,
|
||||
|
@ -18,6 +18,7 @@ use metadata::loader;
|
||||
|
||||
use std::cell::RefCell;
|
||||
use collections::HashMap;
|
||||
use extra::c_vec::CVec;
|
||||
use syntax::ast;
|
||||
use syntax::parse::token::IdentInterner;
|
||||
|
||||
@ -28,7 +29,7 @@ use syntax::parse::token::IdentInterner;
|
||||
pub type cnum_map = @RefCell<HashMap<ast::CrateNum, ast::CrateNum>>;
|
||||
|
||||
pub enum MetadataBlob {
|
||||
MetadataVec(~[u8]),
|
||||
MetadataVec(CVec<u8>),
|
||||
MetadataArchive(loader::ArchiveMetadata),
|
||||
}
|
||||
|
||||
|
@ -2569,7 +2569,7 @@ pub fn write_metadata(cx: &CrateContext, krate: &ast::Crate) -> ~[u8] {
|
||||
let encode_parms = crate_ctxt_to_encode_parms(cx, encode_inlined_item);
|
||||
let metadata = encoder::encode_metadata(encode_parms, krate);
|
||||
let compressed = encoder::metadata_encoding_version +
|
||||
flate::deflate_bytes(metadata);
|
||||
flate::deflate_bytes(metadata).as_slice();
|
||||
let llmeta = C_bytes(compressed);
|
||||
let llconst = C_struct([llmeta], false);
|
||||
let name = format!("rust_metadata_{}_{}_{}", cx.link_meta.crateid.name,
|
||||
|
@ -123,7 +123,7 @@ impl QueuePool {
|
||||
unsafe {
|
||||
assert_eq!(uvll::uv_async_init(loop_.handle, handle, async_cb), 0);
|
||||
uvll::uv_unref(handle);
|
||||
let data: *c_void = *cast::transmute::<&~QueuePool, &*c_void>(&q);
|
||||
let data = &*q as *QueuePool as *c_void;
|
||||
uvll::set_data_for_uv_handle(handle, data);
|
||||
}
|
||||
|
||||
|
@ -161,9 +161,7 @@ pub mod reader {
|
||||
];
|
||||
|
||||
unsafe {
|
||||
let (ptr, _): (*u8, uint) = transmute(data);
|
||||
let ptr = ptr.offset(start as int);
|
||||
let ptr: *i32 = transmute(ptr);
|
||||
let ptr = data.as_ptr().offset(start as int) as *i32;
|
||||
let val = from_be32(*ptr) as u32;
|
||||
|
||||
let i = (val >> 28u) as uint;
|
||||
|
@ -350,10 +350,8 @@ impl Float for f32 {
|
||||
static EXP_MASK: u32 = 0x7f800000;
|
||||
static MAN_MASK: u32 = 0x007fffff;
|
||||
|
||||
match (
|
||||
unsafe { ::cast::transmute::<f32,u32>(*self) } & MAN_MASK,
|
||||
unsafe { ::cast::transmute::<f32,u32>(*self) } & EXP_MASK,
|
||||
) {
|
||||
let bits: u32 = unsafe {::cast::transmute(*self)};
|
||||
match (bits & MAN_MASK, bits & EXP_MASK) {
|
||||
(0, 0) => FPZero,
|
||||
(_, 0) => FPSubnormal,
|
||||
(0, EXP_MASK) => FPInfinite,
|
||||
|
@ -352,10 +352,8 @@ impl Float for f64 {
|
||||
static EXP_MASK: u64 = 0x7ff0000000000000;
|
||||
static MAN_MASK: u64 = 0x000fffffffffffff;
|
||||
|
||||
match (
|
||||
unsafe { ::cast::transmute::<f64,u64>(*self) } & MAN_MASK,
|
||||
unsafe { ::cast::transmute::<f64,u64>(*self) } & EXP_MASK,
|
||||
) {
|
||||
let bits: u64 = unsafe {::cast::transmute(*self)};
|
||||
match (bits & MAN_MASK, bits & EXP_MASK) {
|
||||
(0, 0) => FPZero,
|
||||
(_, 0) => FPSubnormal,
|
||||
(0, EXP_MASK) => FPInfinite,
|
||||
|
Loading…
x
Reference in New Issue
Block a user