core: remove unnecessary unsafe blocks/functions

This commit is contained in:
Alex Crichton 2013-04-09 01:31:23 -04:00
parent 4bfa3c6663
commit d9595d1737
17 changed files with 227 additions and 243 deletions

View File

@ -188,7 +188,6 @@ impl<T: Owned> Peekable<T> for Port<T> {
#[inline(always)] #[inline(always)]
fn port_peek<T:Owned>(self: &Port<T>) -> bool { fn port_peek<T:Owned>(self: &Port<T>) -> bool {
unsafe {
let mut endp = None; let mut endp = None;
endp <-> self.endp; endp <-> self.endp;
let peek = match &endp { let peek = match &endp {
@ -197,7 +196,6 @@ fn port_peek<T:Owned>(self: &Port<T>) -> bool {
}; };
self.endp <-> endp; self.endp <-> endp;
peek peek
}
} }
impl<T: Owned> Selectable for Port<T> { impl<T: Owned> Selectable for Port<T> {

View File

@ -1536,11 +1536,8 @@ pub fn with_bytes_writer(f: &fn(@Writer)) -> ~[u8] {
pub fn with_str_writer(f: &fn(@Writer)) -> ~str { pub fn with_str_writer(f: &fn(@Writer)) -> ~str {
let mut v = with_bytes_writer(f); let mut v = with_bytes_writer(f);
// FIXME (#3758): This should not be needed.
unsafe {
// Make sure the vector has a trailing null and is proper utf8. // Make sure the vector has a trailing null and is proper utf8.
v.push(0); v.push(0);
}
assert!(str::is_utf8(v)); assert!(str::is_utf8(v));
unsafe { ::cast::transmute(v) } unsafe { ::cast::transmute(v) }
@ -1640,7 +1637,6 @@ pub mod fsync {
// outer res // outer res
pub fn FILE_res_sync(file: &FILERes, opt_level: Option<Level>, pub fn FILE_res_sync(file: &FILERes, opt_level: Option<Level>,
blk: &fn(v: Res<*libc::FILE>)) { blk: &fn(v: Res<*libc::FILE>)) {
unsafe {
blk(Res(Arg { blk(Res(Arg {
val: file.f, opt_level: opt_level, val: file.f, opt_level: opt_level,
fsync_fn: |file, l| { fsync_fn: |file, l| {
@ -1650,7 +1646,6 @@ pub mod fsync {
} }
})); }));
} }
}
// fsync fd after executing blk // fsync fd after executing blk
pub fn fd_res_sync(fd: &FdRes, opt_level: Option<Level>, pub fn fd_res_sync(fd: &FdRes, opt_level: Option<Level>,

View File

@ -38,13 +38,13 @@ pub mod raw {
#[inline(always)] #[inline(always)]
pub fn ptr_eq<T>(a: @T, b: @T) -> bool { pub fn ptr_eq<T>(a: @T, b: @T) -> bool {
//! Determine if two shared boxes point to the same object //! Determine if two shared boxes point to the same object
unsafe { ptr::addr_of(&(*a)) == ptr::addr_of(&(*b)) } ptr::addr_of(&(*a)) == ptr::addr_of(&(*b))
} }
#[inline(always)] #[inline(always)]
pub fn mut_ptr_eq<T>(a: @mut T, b: @mut T) -> bool { pub fn mut_ptr_eq<T>(a: @mut T, b: @mut T) -> bool {
//! Determine if two mutable shared boxes point to the same object //! Determine if two mutable shared boxes point to the same object
unsafe { ptr::addr_of(&(*a)) == ptr::addr_of(&(*b)) } ptr::addr_of(&(*a)) == ptr::addr_of(&(*b))
} }
#[cfg(notest)] #[cfg(notest)]

View File

@ -369,27 +369,27 @@ pub fn is_NaN(x: float) -> bool { f64::is_NaN(x as f64) }
#[inline(always)] #[inline(always)]
pub fn abs(x: float) -> float { pub fn abs(x: float) -> float {
unsafe { f64::abs(x as f64) as float } f64::abs(x as f64) as float
} }
#[inline(always)] #[inline(always)]
pub fn sqrt(x: float) -> float { pub fn sqrt(x: float) -> float {
unsafe { f64::sqrt(x as f64) as float } f64::sqrt(x as f64) as float
} }
#[inline(always)] #[inline(always)]
pub fn atan(x: float) -> float { pub fn atan(x: float) -> float {
unsafe { f64::atan(x as f64) as float } f64::atan(x as f64) as float
} }
#[inline(always)] #[inline(always)]
pub fn sin(x: float) -> float { pub fn sin(x: float) -> float {
unsafe { f64::sin(x as f64) as float } f64::sin(x as f64) as float
} }
#[inline(always)] #[inline(always)]
pub fn cos(x: float) -> float { pub fn cos(x: float) -> float {
unsafe { f64::cos(x as f64) as float } f64::cos(x as f64) as float
} }
#[inline(always)] #[inline(always)]
pub fn tan(x: float) -> float { pub fn tan(x: float) -> float {
unsafe { f64::tan(x as f64) as float } f64::tan(x as f64) as float
} }
#[cfg(notest)] #[cfg(notest)]

View File

@ -389,7 +389,6 @@ impl GenericPath for PosixPath {
} }
fn dirname(&self) -> ~str { fn dirname(&self) -> ~str {
unsafe {
let s = self.dir_path().to_str(); let s = self.dir_path().to_str();
if s.len() == 0 { if s.len() == 0 {
~"." ~"."
@ -397,7 +396,6 @@ impl GenericPath for PosixPath {
s s
} }
} }
}
fn filename(&self) -> Option<~str> { fn filename(&self) -> Option<~str> {
match self.components.len() { match self.components.len() {
@ -439,11 +437,9 @@ impl GenericPath for PosixPath {
} }
fn with_filename(&self, f: &str) -> PosixPath { fn with_filename(&self, f: &str) -> PosixPath {
unsafe {
assert!(! str::any(f, |c| windows::is_sep(c as u8))); assert!(! str::any(f, |c| windows::is_sep(c as u8)));
self.dir_path().push(f) self.dir_path().push(f)
} }
}
fn with_filestem(&self, s: &str) -> PosixPath { fn with_filestem(&self, s: &str) -> PosixPath {
match self.filetype() { match self.filetype() {
@ -509,7 +505,7 @@ impl GenericPath for PosixPath {
for str::each_split_nonempty(*e, |c| windows::is_sep(c as u8)) |s| { for str::each_split_nonempty(*e, |c| windows::is_sep(c as u8)) |s| {
ss.push(s.to_owned()) ss.push(s.to_owned())
} }
unsafe { v.push_all_move(ss); } v.push_all_move(ss);
} }
PosixPath { is_absolute: self.is_absolute, PosixPath { is_absolute: self.is_absolute,
components: v } components: v }
@ -521,14 +517,14 @@ impl GenericPath for PosixPath {
for str::each_split_nonempty(s, |c| windows::is_sep(c as u8)) |s| { for str::each_split_nonempty(s, |c| windows::is_sep(c as u8)) |s| {
ss.push(s.to_owned()) ss.push(s.to_owned())
} }
unsafe { v.push_all_move(ss); } v.push_all_move(ss);
PosixPath { components: v, ..copy *self } PosixPath { components: v, ..copy *self }
} }
fn pop(&self) -> PosixPath { fn pop(&self) -> PosixPath {
let mut cs = copy self.components; let mut cs = copy self.components;
if cs.len() != 0 { if cs.len() != 0 {
unsafe { cs.pop(); } cs.pop();
} }
return PosixPath { return PosixPath {
is_absolute: self.is_absolute, is_absolute: self.is_absolute,
@ -607,7 +603,6 @@ impl GenericPath for WindowsPath {
} }
fn dirname(&self) -> ~str { fn dirname(&self) -> ~str {
unsafe {
let s = self.dir_path().to_str(); let s = self.dir_path().to_str();
if s.len() == 0 { if s.len() == 0 {
~"." ~"."
@ -615,7 +610,6 @@ impl GenericPath for WindowsPath {
s s
} }
} }
}
fn filename(&self) -> Option<~str> { fn filename(&self) -> Option<~str> {
match self.components.len() { match self.components.len() {
@ -770,7 +764,7 @@ impl GenericPath for WindowsPath {
for str::each_split_nonempty(*e, |c| windows::is_sep(c as u8)) |s| { for str::each_split_nonempty(*e, |c| windows::is_sep(c as u8)) |s| {
ss.push(s.to_owned()) ss.push(s.to_owned())
} }
unsafe { v.push_all_move(ss); } v.push_all_move(ss);
} }
// tedious, but as-is, we can't use ..self // tedious, but as-is, we can't use ..self
return WindowsPath { return WindowsPath {
@ -787,14 +781,14 @@ impl GenericPath for WindowsPath {
for str::each_split_nonempty(s, |c| windows::is_sep(c as u8)) |s| { for str::each_split_nonempty(s, |c| windows::is_sep(c as u8)) |s| {
ss.push(s.to_owned()) ss.push(s.to_owned())
} }
unsafe { v.push_all_move(ss); } v.push_all_move(ss);
return WindowsPath { components: v, ..copy *self } return WindowsPath { components: v, ..copy *self }
} }
fn pop(&self) -> WindowsPath { fn pop(&self) -> WindowsPath {
let mut cs = copy self.components; let mut cs = copy self.components;
if cs.len() != 0 { if cs.len() != 0 {
unsafe { cs.pop(); } cs.pop();
} }
return WindowsPath { return WindowsPath {
host: copy self.host, host: copy self.host,
@ -820,9 +814,7 @@ impl GenericPath for WindowsPath {
pub fn normalize(components: &[~str]) -> ~[~str] { pub fn normalize(components: &[~str]) -> ~[~str] {
let mut cs = ~[]; let mut cs = ~[];
unsafe {
for components.each |c| { for components.each |c| {
unsafe {
if *c == ~"." && components.len() > 1 { loop; } if *c == ~"." && components.len() > 1 { loop; }
if *c == ~"" { loop; } if *c == ~"" { loop; }
if *c == ~".." && cs.len() != 0 { if *c == ~".." && cs.len() != 0 {
@ -831,8 +823,6 @@ pub fn normalize(components: &[~str]) -> ~[~str] {
} }
cs.push(copy *c); cs.push(copy *c);
} }
}
}
cs cs
} }

View File

@ -55,17 +55,13 @@ pub fn addr_of<T>(val: &T) -> *T { unsafe { rusti::addr_of(*val) } }
/// Calculate the offset from a pointer /// Calculate the offset from a pointer
#[inline(always)] #[inline(always)]
pub fn offset<T>(ptr: *T, count: uint) -> *T { pub fn offset<T>(ptr: *T, count: uint) -> *T {
unsafe {
(ptr as uint + count * sys::size_of::<T>()) as *T (ptr as uint + count * sys::size_of::<T>()) as *T
}
} }
/// Calculate the offset from a const pointer /// Calculate the offset from a const pointer
#[inline(always)] #[inline(always)]
pub fn const_offset<T>(ptr: *const T, count: uint) -> *const T { pub fn const_offset<T>(ptr: *const T, count: uint) -> *const T {
unsafe {
(ptr as uint + count * sys::size_of::<T>()) as *T (ptr as uint + count * sys::size_of::<T>()) as *T
}
} }
/// Calculate the offset from a mut pointer /// Calculate the offset from a mut pointer

View File

@ -205,8 +205,6 @@ fn align_down(sp: *mut uint) -> *mut uint {
#[inline(always)] #[inline(always)]
pub fn mut_offset<T>(ptr: *mut T, count: int) -> *mut T { pub fn mut_offset<T>(ptr: *mut T, count: int) -> *mut T {
use core::sys::size_of; use core::sys::size_of;
unsafe {
(ptr as int + count * (size_of::<T>() as int)) as *mut T (ptr as int + count * (size_of::<T>() as int)) as *mut T
}
} }

View File

@ -21,17 +21,17 @@ pub type Key = pthread_key_t;
#[cfg(unix)] #[cfg(unix)]
pub unsafe fn create(key: &mut Key) { pub unsafe fn create(key: &mut Key) {
unsafe { assert!(0 == pthread_key_create(key, null())); } assert!(0 == pthread_key_create(key, null()));
} }
#[cfg(unix)] #[cfg(unix)]
pub unsafe fn set(key: Key, value: *mut c_void) { pub unsafe fn set(key: Key, value: *mut c_void) {
unsafe { assert!(0 == pthread_setspecific(key, value)); } assert!(0 == pthread_setspecific(key, value));
} }
#[cfg(unix)] #[cfg(unix)]
pub unsafe fn get(key: Key) -> *mut c_void { pub unsafe fn get(key: Key) -> *mut c_void {
unsafe { pthread_getspecific(key) } pthread_getspecific(key)
} }
#[cfg(target_os="macos")] #[cfg(target_os="macos")]

View File

@ -382,7 +382,6 @@ pub struct ProgramOutput {status: int, out: ~str, err: ~str}
* the contents of stdout and the contents of stderr. * the contents of stdout and the contents of stderr.
*/ */
pub fn program_output(prog: &str, args: &[~str]) -> ProgramOutput { pub fn program_output(prog: &str, args: &[~str]) -> ProgramOutput {
unsafe {
let pipe_in = os::pipe(); let pipe_in = os::pipe();
let pipe_out = os::pipe(); let pipe_out = os::pipe();
let pipe_err = os::pipe(); let pipe_err = os::pipe();
@ -439,7 +438,6 @@ pub fn program_output(prog: &str, args: &[~str]) -> ProgramOutput {
return ProgramOutput {status: status, return ProgramOutput {status: status,
out: outs, out: outs,
err: errs}; err: errs};
}
} }
pub fn writeclose(fd: c_int, s: ~str) { pub fn writeclose(fd: c_int, s: ~str) {

View File

@ -170,19 +170,17 @@ pub fn push_char(s: &mut ~str, ch: char) {
/// Convert a char to a string /// Convert a char to a string
pub fn from_char(ch: char) -> ~str { pub fn from_char(ch: char) -> ~str {
let mut buf = ~""; let mut buf = ~"";
unsafe { push_char(&mut buf, ch); } push_char(&mut buf, ch);
buf buf
} }
/// Convert a vector of chars to a string /// Convert a vector of chars to a string
pub fn from_chars(chs: &[char]) -> ~str { pub fn from_chars(chs: &[char]) -> ~str {
let mut buf = ~""; let mut buf = ~"";
unsafe {
reserve(&mut buf, chs.len()); reserve(&mut buf, chs.len());
for vec::each(chs) |ch| { for vec::each(chs) |ch| {
push_char(&mut buf, *ch); push_char(&mut buf, *ch);
} }
}
buf buf
} }
@ -226,9 +224,7 @@ pub fn push_str(lhs: &mut ~str, rhs: &str) {
#[inline(always)] #[inline(always)]
pub fn append(lhs: ~str, rhs: &str) -> ~str { pub fn append(lhs: ~str, rhs: &str) -> ~str {
let mut v = lhs; let mut v = lhs;
unsafe {
push_str_no_overallocate(&mut v, rhs); push_str_no_overallocate(&mut v, rhs);
}
v v
} }
@ -236,7 +232,7 @@ pub fn append(lhs: ~str, rhs: &str) -> ~str {
pub fn concat(v: &[~str]) -> ~str { pub fn concat(v: &[~str]) -> ~str {
let mut s: ~str = ~""; let mut s: ~str = ~"";
for vec::each(v) |ss| { for vec::each(v) |ss| {
unsafe { push_str(&mut s, *ss) }; push_str(&mut s, *ss);
} }
s s
} }
@ -245,8 +241,8 @@ pub fn concat(v: &[~str]) -> ~str {
pub fn connect(v: &[~str], sep: &str) -> ~str { pub fn connect(v: &[~str], sep: &str) -> ~str {
let mut s = ~"", first = true; let mut s = ~"", first = true;
for vec::each(v) |ss| { for vec::each(v) |ss| {
if first { first = false; } else { unsafe { push_str(&mut s, sep); } } if first { first = false; } else { push_str(&mut s, sep); }
unsafe { push_str(&mut s, *ss) }; push_str(&mut s, *ss);
} }
s s
} }
@ -255,8 +251,8 @@ pub fn connect(v: &[~str], sep: &str) -> ~str {
pub fn connect_slices(v: &[&str], sep: &str) -> ~str { pub fn connect_slices(v: &[&str], sep: &str) -> ~str {
let mut s = ~"", first = true; let mut s = ~"", first = true;
for vec::each(v) |ss| { for vec::each(v) |ss| {
if first { first = false; } else { unsafe { push_str(&mut s, sep); } } if first { first = false; } else { push_str(&mut s, sep); }
unsafe { push_str(&mut s, *ss) }; push_str(&mut s, *ss);
} }
s s
} }
@ -2251,7 +2247,6 @@ pub mod raw {
assert!((end <= n)); assert!((end <= n));
let mut v = vec::with_capacity(end - begin + 1u); let mut v = vec::with_capacity(end - begin + 1u);
unsafe {
do vec::as_imm_buf(v) |vbuf, _vlen| { do vec::as_imm_buf(v) |vbuf, _vlen| {
let vbuf = ::cast::transmute_mut_unsafe(vbuf); let vbuf = ::cast::transmute_mut_unsafe(vbuf);
let src = ptr::offset(sbuf, begin); let src = ptr::offset(sbuf, begin);
@ -2262,7 +2257,6 @@ pub mod raw {
::cast::transmute(v) ::cast::transmute(v)
} }
} }
}
/** /**
* Takes a bytewise (not UTF-8) slice from a string. * Takes a bytewise (not UTF-8) slice from a string.
@ -2304,7 +2298,7 @@ pub mod raw {
} }
/// Removes the last byte from a string and returns it. (Not UTF-8 safe). /// Removes the last byte from a string and returns it. (Not UTF-8 safe).
pub unsafe fn pop_byte(s: &mut ~str) -> u8 { pub fn pop_byte(s: &mut ~str) -> u8 {
let len = len(*s); let len = len(*s);
assert!((len > 0u)); assert!((len > 0u));
let b = s[len - 1u]; let b = s[len - 1u];
@ -2313,7 +2307,7 @@ pub mod raw {
} }
/// Removes the first byte from a string and returns it. (Not UTF-8 safe). /// Removes the first byte from a string and returns it. (Not UTF-8 safe).
pub unsafe fn shift_byte(s: &mut ~str) -> u8 { pub fn shift_byte(s: &mut ~str) -> u8 {
let len = len(*s); let len = len(*s);
assert!((len > 0u)); assert!((len > 0u));
let b = s[0]; let b = s[0];

View File

@ -127,11 +127,9 @@ pub fn refcount<T>(t: @T) -> uint {
} }
pub fn log_str<T>(t: &T) -> ~str { pub fn log_str<T>(t: &T) -> ~str {
unsafe {
do io::with_str_writer |wr| { do io::with_str_writer |wr| {
repr::write_repr(wr, t) repr::write_repr(wr, t)
} }
}
} }
/** Initiate task failure */ /** Initiate task failure */
@ -157,10 +155,8 @@ pub fn begin_unwind_(msg: *c_char, file: *c_char, line: size_t) -> ! {
} }
pub fn fail_assert(msg: &str, file: &str, line: uint) -> ! { pub fn fail_assert(msg: &str, file: &str, line: uint) -> ! {
unsafe {
let (msg, file) = (msg.to_owned(), file.to_owned()); let (msg, file) = (msg.to_owned(), file.to_owned());
begin_unwind(~"assertion failed: " + msg, file, line) begin_unwind(~"assertion failed: " + msg, file, line)
}
} }
#[cfg(test)] #[cfg(test)]

View File

@ -39,10 +39,9 @@ use result::Result;
use comm::{stream, Chan, GenericChan, GenericPort, Port}; use comm::{stream, Chan, GenericChan, GenericPort, Port};
use prelude::*; use prelude::*;
use result; use result;
use task::rt::{task_id, sched_id}; use task::rt::{task_id, sched_id, rust_task};
use util; use util;
use util::replace; use util::replace;
use unstable::finally::Finally;
#[cfg(test)] use comm::SharedChan; #[cfg(test)] use comm::SharedChan;
@ -566,28 +565,48 @@ pub fn get_scheduler() -> Scheduler {
* ~~~ * ~~~
*/ */
pub unsafe fn unkillable<U>(f: &fn() -> U) -> U { pub unsafe fn unkillable<U>(f: &fn() -> U) -> U {
struct AllowFailure {
t: *rust_task,
drop {
unsafe { unsafe {
rt::rust_task_allow_kill(self.t);
}
}
}
fn AllowFailure(t: *rust_task) -> AllowFailure{
AllowFailure {
t: t
}
}
let t = rt::rust_get_task(); let t = rt::rust_get_task();
let _allow_failure = AllowFailure(t);
rt::rust_task_inhibit_kill(t); rt::rust_task_inhibit_kill(t);
do (|| {
f() f()
}).finally {
rt::rust_task_allow_kill(t);
}
}
} }
/// The inverse of unkillable. Only ever to be used nested in unkillable(). /// The inverse of unkillable. Only ever to be used nested in unkillable().
pub unsafe fn rekillable<U>(f: &fn() -> U) -> U { pub unsafe fn rekillable<U>(f: &fn() -> U) -> U {
struct DisallowFailure {
t: *rust_task,
drop {
unsafe { unsafe {
rt::rust_task_inhibit_kill(self.t);
}
}
}
fn DisallowFailure(t: *rust_task) -> DisallowFailure {
DisallowFailure {
t: t
}
}
let t = rt::rust_get_task(); let t = rt::rust_get_task();
let _allow_failure = DisallowFailure(t);
rt::rust_task_allow_kill(t); rt::rust_task_allow_kill(t);
do (|| {
f() f()
}).finally {
rt::rust_task_inhibit_kill(t);
}
}
} }
/** /**
@ -595,17 +614,27 @@ pub unsafe fn rekillable<U>(f: &fn() -> U) -> U {
* For use with exclusive ARCs, which use pthread mutexes directly. * For use with exclusive ARCs, which use pthread mutexes directly.
*/ */
pub unsafe fn atomically<U>(f: &fn() -> U) -> U { pub unsafe fn atomically<U>(f: &fn() -> U) -> U {
struct DeferInterrupts {
t: *rust_task,
drop {
unsafe { unsafe {
rt::rust_task_allow_yield(self.t);
rt::rust_task_allow_kill(self.t);
}
}
}
fn DeferInterrupts(t: *rust_task) -> DeferInterrupts {
DeferInterrupts {
t: t
}
}
let t = rt::rust_get_task(); let t = rt::rust_get_task();
let _interrupts = DeferInterrupts(t);
rt::rust_task_inhibit_kill(t); rt::rust_task_inhibit_kill(t);
rt::rust_task_inhibit_yield(t); rt::rust_task_inhibit_yield(t);
do (|| {
f() f()
}).finally {
rt::rust_task_allow_yield(t);
rt::rust_task_allow_kill(t);
}
}
} }
#[test] #[should_fail] #[ignore(cfg(windows))] #[test] #[should_fail] #[ignore(cfg(windows))]

View File

@ -157,13 +157,13 @@ struct AncestorList(Option<unstable::Exclusive<AncestorNode>>);
// Accessors for taskgroup arcs and ancestor arcs that wrap the unsafety. // Accessors for taskgroup arcs and ancestor arcs that wrap the unsafety.
#[inline(always)] #[inline(always)]
fn access_group<U>(x: &TaskGroupArc, blk: &fn(TaskGroupInner) -> U) -> U { fn access_group<U>(x: &TaskGroupArc, blk: &fn(TaskGroupInner) -> U) -> U {
unsafe { x.with(blk) } x.with(blk)
} }
#[inline(always)] #[inline(always)]
fn access_ancestors<U>(x: &unstable::Exclusive<AncestorNode>, fn access_ancestors<U>(x: &unstable::Exclusive<AncestorNode>,
blk: &fn(x: &mut AncestorNode) -> U) -> U { blk: &fn(x: &mut AncestorNode) -> U) -> U {
unsafe { x.with(blk) } x.with(blk)
} }
// Iterates over an ancestor list. // Iterates over an ancestor list.

View File

@ -152,45 +152,37 @@ pub type SharedMutableState<T> = ArcDestruct<T>;
pub unsafe fn shared_mutable_state<T:Owned>(data: T) -> pub unsafe fn shared_mutable_state<T:Owned>(data: T) ->
SharedMutableState<T> { SharedMutableState<T> {
let data = ~ArcData { count: 1, data: Some(data) }; let data = ~ArcData { count: 1, data: Some(data) };
unsafe {
let ptr = cast::transmute(data); let ptr = cast::transmute(data);
ArcDestruct(ptr) ArcDestruct(ptr)
}
} }
#[inline(always)] #[inline(always)]
pub unsafe fn get_shared_mutable_state<T:Owned>( pub unsafe fn get_shared_mutable_state<T:Owned>(
rc: *SharedMutableState<T>) -> *mut T rc: *SharedMutableState<T>) -> *mut T
{ {
unsafe {
let ptr: ~ArcData<T> = cast::reinterpret_cast(&(*rc).data); let ptr: ~ArcData<T> = cast::reinterpret_cast(&(*rc).data);
assert!(ptr.count > 0); assert!(ptr.count > 0);
let r = cast::transmute(ptr.data.get_ref()); let r = cast::transmute(ptr.data.get_ref());
cast::forget(ptr); cast::forget(ptr);
return r; return r;
}
} }
#[inline(always)] #[inline(always)]
pub unsafe fn get_shared_immutable_state<'a,T:Owned>( pub unsafe fn get_shared_immutable_state<'a,T:Owned>(
rc: &'a SharedMutableState<T>) -> &'a T { rc: &'a SharedMutableState<T>) -> &'a T {
unsafe {
let ptr: ~ArcData<T> = cast::reinterpret_cast(&(*rc).data); let ptr: ~ArcData<T> = cast::reinterpret_cast(&(*rc).data);
assert!(ptr.count > 0); assert!(ptr.count > 0);
// Cast us back into the correct region // Cast us back into the correct region
let r = cast::transmute_region(ptr.data.get_ref()); let r = cast::transmute_region(ptr.data.get_ref());
cast::forget(ptr); cast::forget(ptr);
return r; return r;
}
} }
pub unsafe fn clone_shared_mutable_state<T:Owned>(rc: &SharedMutableState<T>) pub unsafe fn clone_shared_mutable_state<T:Owned>(rc: &SharedMutableState<T>)
-> SharedMutableState<T> { -> SharedMutableState<T> {
unsafe {
let mut ptr: ~ArcData<T> = cast::reinterpret_cast(&(*rc).data); let mut ptr: ~ArcData<T> = cast::reinterpret_cast(&(*rc).data);
let new_count = intrinsics::atomic_xadd(&mut ptr.count, 1) + 1; let new_count = intrinsics::atomic_xadd(&mut ptr.count, 1) + 1;
assert!(new_count >= 2); assert!(new_count >= 2);
cast::forget(ptr); cast::forget(ptr);
}
ArcDestruct((*rc).data) ArcDestruct((*rc).data)
} }

View File

@ -19,7 +19,6 @@ use ptr::null;
use intrinsic::TyDesc; use intrinsic::TyDesc;
pub unsafe fn malloc(td: *TypeDesc, size: uint) -> *c_void { pub unsafe fn malloc(td: *TypeDesc, size: uint) -> *c_void {
unsafe {
assert!(td.is_not_null()); assert!(td.is_not_null());
let total_size = get_box_size(size, (*td).align); let total_size = get_box_size(size, (*td).align);
@ -39,7 +38,6 @@ pub unsafe fn malloc(td: *TypeDesc, size: uint) -> *c_void {
atomic_xadd(exchange_count, 1); atomic_xadd(exchange_count, 1);
return transmute(box); return transmute(box);
}
} }
/** /**
Thin wrapper around libc::malloc, none of the box header Thin wrapper around libc::malloc, none of the box header

View File

@ -512,7 +512,7 @@ pub mod rt {
None None
} }
} else { Some('-') }; } else { Some('-') };
unsafe { pad(cv, s, head, PadSigned, buf) }; pad(cv, s, head, PadSigned, buf);
} }
pub fn conv_uint(cv: Conv, u: uint, buf: &mut ~str) { pub fn conv_uint(cv: Conv, u: uint, buf: &mut ~str) {
let prec = get_int_precision(cv); let prec = get_int_precision(cv);
@ -524,7 +524,7 @@ pub mod rt {
TyBits => uint_to_str_prec(u, 2, prec), TyBits => uint_to_str_prec(u, 2, prec),
TyOctal => uint_to_str_prec(u, 8, prec) TyOctal => uint_to_str_prec(u, 8, prec)
}; };
unsafe { pad(cv, rs, None, PadUnsigned, buf) }; pad(cv, rs, None, PadUnsigned, buf);
} }
pub fn conv_bool(cv: Conv, b: bool, buf: &mut ~str) { pub fn conv_bool(cv: Conv, b: bool, buf: &mut ~str) {
let s = if b { "true" } else { "false" }; let s = if b { "true" } else { "false" };
@ -533,7 +533,7 @@ pub mod rt {
conv_str(cv, s, buf); conv_str(cv, s, buf);
} }
pub fn conv_char(cv: Conv, c: char, buf: &mut ~str) { pub fn conv_char(cv: Conv, c: char, buf: &mut ~str) {
unsafe { pad(cv, "", Some(c), PadNozero, buf) }; pad(cv, "", Some(c), PadNozero, buf);
} }
pub fn conv_str(cv: Conv, s: &str, buf: &mut ~str) { pub fn conv_str(cv: Conv, s: &str, buf: &mut ~str) {
// For strings, precision is the maximum characters // For strings, precision is the maximum characters
@ -546,14 +546,14 @@ pub mod rt {
s s
} }
}; };
unsafe { pad(cv, unpadded, None, PadNozero, buf) }; pad(cv, unpadded, None, PadNozero, buf);
} }
pub fn conv_float(cv: Conv, f: float, buf: &mut ~str) { pub fn conv_float(cv: Conv, f: float, buf: &mut ~str) {
let (to_str, digits) = match cv.precision { let (to_str, digits) = match cv.precision {
CountIs(c) => (float::to_str_exact, c as uint), CountIs(c) => (float::to_str_exact, c as uint),
CountImplied => (float::to_str_digits, 6u) CountImplied => (float::to_str_digits, 6u)
}; };
let mut s = unsafe { to_str(f, digits) }; let mut s = to_str(f, digits);
let head = if 0.0 <= f { let head = if 0.0 <= f {
if have_flag(cv.flags, flag_sign_always) { if have_flag(cv.flags, flag_sign_always) {
Some('+') Some('+')
@ -563,7 +563,7 @@ pub mod rt {
None None
} }
} else { None }; } else { None };
unsafe { pad(cv, s, head, PadFloat, buf) }; pad(cv, s, head, PadFloat, buf);
} }
pub fn conv_poly<T>(cv: Conv, v: &T, buf: &mut ~str) { pub fn conv_poly<T>(cv: Conv, v: &T, buf: &mut ~str) {
let s = sys::log_str(v); let s = sys::log_str(v);

View File

@ -44,7 +44,7 @@ pub fn fail_(expr: *c_char, file: *c_char, line: size_t) -> ! {
} }
#[lang="fail_bounds_check"] #[lang="fail_bounds_check"]
pub unsafe fn fail_bounds_check(file: *c_char, line: size_t, pub fn fail_bounds_check(file: *c_char, line: size_t,
index: size_t, len: size_t) { index: size_t, len: size_t) {
let msg = fmt!("index out of bounds: the len is %d but the index is %d", let msg = fmt!("index out of bounds: the len is %d but the index is %d",
len as int, index as int); len as int, index as int);
@ -53,7 +53,7 @@ pub unsafe fn fail_bounds_check(file: *c_char, line: size_t,
} }
} }
pub unsafe fn fail_borrowed() { pub fn fail_borrowed() {
let msg = "borrowed"; let msg = "borrowed";
do str::as_buf(msg) |msg_p, _| { do str::as_buf(msg) |msg_p, _| {
do str::as_buf("???") |file_p, _| { do str::as_buf("???") |file_p, _| {