Skip to content

Commit 4601ea6

Browse files
committed
auto merge of #8487 : brson/rust/local-opts, r=brson
I did this once but acciddentally undid it in a later patch.
2 parents 44675ac + 95badab commit 4601ea6

File tree

4 files changed

+24
-16
lines changed

4 files changed

+24
-16
lines changed

src/libstd/rt/local.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,7 @@ impl Local for Task {
4545
}
4646
unsafe fn unsafe_borrow() -> *mut Task { local_ptr::unsafe_borrow() }
4747
unsafe fn try_unsafe_borrow() -> Option<*mut Task> {
48-
if Local::exists::<Task>() {
49-
Some(Local::unsafe_borrow())
50-
} else {
51-
None
52-
}
48+
local_ptr::try_unsafe_borrow()
5349
}
5450
}
5551

src/libstd/rt/local_heap.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use libc;
1414
use libc::{c_void, uintptr_t, size_t};
1515
use ops::Drop;
16+
use option::{Some, None};
1617
use rt::local::Local;
1718
use rt::task::Task;
1819
use unstable::raw;
@@ -84,8 +85,12 @@ impl Drop for LocalHeap {
8485

8586
// A little compatibility function
8687
pub unsafe fn local_free(ptr: *libc::c_char) {
87-
do Local::borrow::<Task,()> |task| {
88-
task.heap.free(ptr as *libc::c_void);
88+
// XXX: Unsafe borrow for speed. Lame.
89+
match Local::try_unsafe_borrow::<Task>() {
90+
Some(task) => {
91+
(*task).heap.free(ptr as *libc::c_void);
92+
}
93+
None => rtabort!("local free outside of task")
8994
}
9095
}
9196

src/libstd/rt/local_ptr.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,16 +97,23 @@ pub unsafe fn borrow<T>(f: &fn(&mut T)) {
9797
/// Because this leaves the value in thread-local storage it is possible
9898
/// For the Scheduler pointer to be aliased
9999
pub unsafe fn unsafe_borrow<T>() -> *mut T {
100+
match try_unsafe_borrow() {
101+
Some(p) => p,
102+
None => rtabort!("thread-local pointer is null. bogus!")
103+
}
104+
}
105+
106+
pub unsafe fn try_unsafe_borrow<T>() -> Option<*mut T> {
100107
let key = tls_key();
101108
let mut void_ptr: *mut c_void = tls::get(key);
102109
if void_ptr.is_null() {
103-
rtabort!("thread-local pointer is null. bogus!");
110+
return None;
104111
}
105112
{
106113
let ptr: *mut *mut c_void = &mut void_ptr;
107114
let ptr: *mut ~T = ptr as *mut ~T;
108115
let ptr: *mut T = &mut **ptr;
109-
return ptr;
116+
return Some(ptr);
110117
}
111118
}
112119

src/libstd/unstable/lang.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use c_str::ToCStr;
1414
use cast::transmute;
1515
use libc::{c_char, c_void, size_t, uintptr_t};
16+
use option::{Some, None};
1617
use sys;
1718
use rt::task::Task;
1819
use rt::local::Local;
@@ -35,14 +36,13 @@ pub fn fail_bounds_check(file: *c_char, line: size_t,
3536

3637
#[lang="malloc"]
3738
pub unsafe fn local_malloc(td: *c_char, size: uintptr_t) -> *c_char {
38-
let mut alloc = ::ptr::null();
39-
do Local::borrow::<Task,()> |task| {
40-
rtdebug!("task pointer: %x, heap pointer: %x",
41-
::borrow::to_uint(task),
42-
::borrow::to_uint(&task.heap));
43-
alloc = task.heap.alloc(td as *c_void, size as uint) as *c_char;
39+
// XXX: Unsafe borrow for speed. Lame.
40+
match Local::try_unsafe_borrow::<Task>() {
41+
Some(task) => {
42+
(*task).heap.alloc(td as *c_void, size as uint) as *c_char
43+
}
44+
None => rtabort!("local malloc outside of task")
4445
}
45-
return alloc;
4646
}
4747

4848
// NB: Calls to free CANNOT be allowed to fail, as throwing an exception from

0 commit comments

Comments
 (0)