2012-12-03 18:48:01 -06:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2012-07-10 17:52:05 -05:00
|
|
|
//! Runtime calls emitted by the compiler.
|
|
|
|
|
2013-05-01 12:48:00 -05:00
|
|
|
use uint;
|
2013-01-11 23:01:42 -06:00
|
|
|
use cast::transmute;
|
2013-03-15 14:24:24 -05:00
|
|
|
use libc::{c_char, c_uchar, c_void, size_t, uintptr_t, c_int, STDERR_FILENO};
|
2013-01-11 23:01:42 -06:00
|
|
|
use managed::raw::BoxRepr;
|
2012-12-23 16:41:37 -06:00
|
|
|
use str;
|
|
|
|
use sys;
|
2013-04-21 21:03:52 -05:00
|
|
|
use rt::{context, OldTaskContext};
|
2013-05-19 18:50:21 -05:00
|
|
|
use rt::task::Task;
|
|
|
|
use rt::local::Local;
|
2013-05-03 04:42:00 -05:00
|
|
|
use option::{Option, Some, None};
|
2013-05-04 13:25:15 -05:00
|
|
|
use io;
|
2013-05-07 17:54:06 -05:00
|
|
|
use rt::global_heap;
|
2012-07-10 17:52:05 -05:00
|
|
|
|
2013-02-26 23:10:03 -06:00
|
|
|
#[allow(non_camel_case_types)]
|
|
|
|
pub type rust_task = c_void;
|
2012-07-16 14:28:15 -05:00
|
|
|
|
2013-05-01 12:48:00 -05:00
|
|
|
pub static FROZEN_BIT: uint = 1 << (uint::bits - 1);
|
|
|
|
pub static MUT_BIT: uint = 1 << (uint::bits - 2);
|
|
|
|
static ALL_BITS: uint = FROZEN_BIT | MUT_BIT;
|
2013-01-11 23:01:42 -06:00
|
|
|
|
2013-03-05 13:57:50 -06:00
|
|
|
pub mod rustrt {
|
2013-05-01 09:29:47 -05:00
|
|
|
use unstable::lang::rust_task;
|
|
|
|
use libc::{c_void, c_char, uintptr_t};
|
2012-07-17 12:48:19 -05:00
|
|
|
|
2013-03-05 13:57:50 -06:00
|
|
|
pub extern {
|
|
|
|
#[rust_stack]
|
|
|
|
unsafe fn rust_upcall_malloc(td: *c_char, size: uintptr_t) -> *c_char;
|
|
|
|
|
|
|
|
#[rust_stack]
|
|
|
|
unsafe fn rust_upcall_free(ptr: *c_char);
|
2013-03-29 18:55:04 -05:00
|
|
|
|
|
|
|
#[fast_ffi]
|
|
|
|
unsafe fn rust_upcall_malloc_noswitch(td: *c_char,
|
|
|
|
size: uintptr_t)
|
|
|
|
-> *c_char;
|
|
|
|
|
|
|
|
#[fast_ffi]
|
|
|
|
unsafe fn rust_upcall_free_noswitch(ptr: *c_char);
|
2013-05-01 09:29:47 -05:00
|
|
|
|
|
|
|
#[rust_stack]
|
|
|
|
fn rust_take_task_borrow_list(task: *rust_task) -> *c_void;
|
|
|
|
|
|
|
|
#[rust_stack]
|
|
|
|
fn rust_set_task_borrow_list(task: *rust_task, map: *c_void);
|
2013-05-01 19:23:07 -05:00
|
|
|
|
2013-05-06 10:16:17 -05:00
|
|
|
#[rust_stack]
|
|
|
|
fn rust_try_get_task() -> *rust_task;
|
|
|
|
|
2013-05-01 19:23:07 -05:00
|
|
|
fn rust_dbg_breakpoint();
|
2013-03-05 13:57:50 -06:00
|
|
|
}
|
2012-07-10 17:52:05 -05:00
|
|
|
}
|
|
|
|
|
2013-01-07 14:21:34 -06:00
|
|
|
#[lang="fail_"]
|
2013-02-27 20:34:04 -06:00
|
|
|
pub fn fail_(expr: *c_char, file: *c_char, line: size_t) -> ! {
|
2012-12-10 19:22:10 -06:00
|
|
|
sys::begin_unwind_(expr, file, line);
|
2012-07-10 17:52:05 -05:00
|
|
|
}
|
|
|
|
|
2013-01-07 14:21:34 -06:00
|
|
|
#[lang="fail_bounds_check"]
|
2013-04-09 00:31:23 -05:00
|
|
|
pub fn fail_bounds_check(file: *c_char, line: size_t,
|
2013-05-01 08:14:47 -05:00
|
|
|
index: size_t, len: size_t) {
|
2012-09-29 06:34:11 -05:00
|
|
|
let msg = fmt!("index out of bounds: the len is %d but the index is %d",
|
|
|
|
len as int, index as int);
|
|
|
|
do str::as_buf(msg) |p, _len| {
|
2013-02-27 20:34:04 -06:00
|
|
|
fail_(p as *c_char, file, line);
|
2012-09-29 06:34:11 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-01 20:46:34 -05:00
|
|
|
#[deriving(Eq)]
|
2013-05-01 09:29:47 -05:00
|
|
|
struct BorrowRecord {
|
|
|
|
box: *mut BoxRepr,
|
|
|
|
file: *c_char,
|
|
|
|
line: size_t
|
|
|
|
}
|
|
|
|
|
2013-05-03 04:42:00 -05:00
|
|
|
fn try_take_task_borrow_list() -> Option<~[BorrowRecord]> {
|
|
|
|
unsafe {
|
2013-05-06 10:16:17 -05:00
|
|
|
let cur_task: *rust_task = rustrt::rust_try_get_task();
|
|
|
|
if cur_task.is_not_null() {
|
|
|
|
let ptr = rustrt::rust_take_task_borrow_list(cur_task);
|
|
|
|
if ptr.is_null() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
let v: ~[BorrowRecord] = transmute(ptr);
|
|
|
|
Some(v)
|
|
|
|
}
|
2013-05-03 04:42:00 -05:00
|
|
|
} else {
|
2013-05-06 10:16:17 -05:00
|
|
|
None
|
2013-05-03 04:42:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-01 09:29:47 -05:00
|
|
|
fn swap_task_borrow_list(f: &fn(~[BorrowRecord]) -> ~[BorrowRecord]) {
|
|
|
|
unsafe {
|
2013-05-06 10:16:17 -05:00
|
|
|
let cur_task: *rust_task = rustrt::rust_try_get_task();
|
|
|
|
if cur_task.is_not_null() {
|
|
|
|
let mut borrow_list: ~[BorrowRecord] = {
|
|
|
|
let ptr = rustrt::rust_take_task_borrow_list(cur_task);
|
|
|
|
if ptr.is_null() { ~[] } else { transmute(ptr) }
|
|
|
|
};
|
|
|
|
borrow_list = f(borrow_list);
|
|
|
|
rustrt::rust_set_task_borrow_list(cur_task, transmute(borrow_list));
|
|
|
|
}
|
2013-05-01 09:29:47 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-02 16:08:04 -05:00
|
|
|
pub unsafe fn clear_task_borrow_list() {
|
|
|
|
// pub because it is used by the box annihilator.
|
2013-05-03 04:42:00 -05:00
|
|
|
let _ = try_take_task_borrow_list();
|
2013-05-02 16:08:04 -05:00
|
|
|
}
|
|
|
|
|
2013-05-04 13:25:15 -05:00
|
|
|
unsafe fn fail_borrowed(box: *mut BoxRepr, file: *c_char, line: size_t) {
|
|
|
|
debug_borrow("fail_borrowed: ", box, 0, 0, file, line);
|
2013-05-01 19:23:07 -05:00
|
|
|
|
2013-05-03 04:42:00 -05:00
|
|
|
match try_take_task_borrow_list() {
|
|
|
|
None => { // not recording borrows
|
|
|
|
let msg = "borrowed";
|
|
|
|
do str::as_buf(msg) |msg_p, _| {
|
|
|
|
fail_(msg_p as *c_char, file, line);
|
|
|
|
}
|
2013-05-01 09:29:47 -05:00
|
|
|
}
|
2013-05-03 04:42:00 -05:00
|
|
|
Some(borrow_list) => { // recording borrows
|
2013-05-01 09:29:47 -05:00
|
|
|
let mut msg = ~"borrowed";
|
|
|
|
let mut sep = " at ";
|
|
|
|
for borrow_list.each_reverse |entry| {
|
|
|
|
if entry.box == box {
|
|
|
|
str::push_str(&mut msg, sep);
|
2013-05-06 10:16:17 -05:00
|
|
|
let filename = str::raw::from_c_str(entry.file);
|
2013-05-01 09:29:47 -05:00
|
|
|
str::push_str(&mut msg, filename);
|
2013-05-01 20:46:34 -05:00
|
|
|
str::push_str(&mut msg, fmt!(":%u", entry.line as uint));
|
2013-05-01 09:29:47 -05:00
|
|
|
sep = " and at ";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
do str::as_buf(msg) |msg_p, _| {
|
|
|
|
fail_(msg_p as *c_char, file, line)
|
|
|
|
}
|
|
|
|
}
|
2013-01-11 23:01:42 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-14 20:17:56 -06:00
|
|
|
// FIXME #4942: Make these signatures agree with exchange_alloc's signatures
|
2013-01-07 14:21:34 -06:00
|
|
|
#[lang="exchange_malloc"]
|
2013-03-28 10:46:08 -05:00
|
|
|
#[inline(always)]
|
2013-02-27 20:34:04 -06:00
|
|
|
pub unsafe fn exchange_malloc(td: *c_char, size: uintptr_t) -> *c_char {
|
2013-05-07 17:54:06 -05:00
|
|
|
transmute(global_heap::malloc(transmute(td), transmute(size)))
|
2013-03-15 14:24:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Because this code is so perf. sensitive, use a static constant so that
|
|
|
|
/// debug printouts are compiled out most of the time.
|
2013-05-05 06:43:43 -05:00
|
|
|
static ENABLE_DEBUG: bool = false;
|
2013-03-15 14:24:24 -05:00
|
|
|
|
2013-05-04 13:25:15 -05:00
|
|
|
#[inline]
|
|
|
|
unsafe fn debug_borrow<T>(tag: &'static str,
|
|
|
|
p: *const T,
|
|
|
|
old_bits: uint,
|
|
|
|
new_bits: uint,
|
|
|
|
filename: *c_char,
|
|
|
|
line: size_t) {
|
|
|
|
//! A useful debugging function that prints a pointer + tag + newline
|
|
|
|
//! without allocating memory.
|
|
|
|
|
|
|
|
if ENABLE_DEBUG && ::rt::env::get().debug_borrow {
|
|
|
|
debug_borrow_slow(tag, p, old_bits, new_bits, filename, line);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe fn debug_borrow_slow<T>(tag: &'static str,
|
|
|
|
p: *const T,
|
|
|
|
old_bits: uint,
|
|
|
|
new_bits: uint,
|
|
|
|
filename: *c_char,
|
|
|
|
line: size_t) {
|
|
|
|
let dbg = STDERR_FILENO as io::fd_t;
|
|
|
|
dbg.write_str(tag);
|
|
|
|
dbg.write_hex(p as uint);
|
|
|
|
dbg.write_str(" ");
|
|
|
|
dbg.write_hex(old_bits);
|
|
|
|
dbg.write_str(" ");
|
|
|
|
dbg.write_hex(new_bits);
|
|
|
|
dbg.write_str(" ");
|
|
|
|
dbg.write_cstr(filename);
|
|
|
|
dbg.write_str(":");
|
|
|
|
dbg.write_hex(line as uint);
|
|
|
|
dbg.write_str("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
trait DebugPrints {
|
|
|
|
fn write_hex(&self, val: uint);
|
|
|
|
unsafe fn write_cstr(&self, str: *c_char);
|
|
|
|
}
|
2013-03-15 14:24:24 -05:00
|
|
|
|
2013-05-04 13:25:15 -05:00
|
|
|
impl DebugPrints for io::fd_t {
|
|
|
|
fn write_hex(&self, mut i: uint) {
|
|
|
|
let letters = ['0', '1', '2', '3', '4', '5', '6', '7', '8',
|
|
|
|
'9', 'a', 'b', 'c', 'd', 'e', 'f'];
|
2013-03-15 14:24:24 -05:00
|
|
|
static uint_nibbles: uint = ::uint::bytes << 1;
|
|
|
|
let mut buffer = [0_u8, ..uint_nibbles+1];
|
|
|
|
let mut c = uint_nibbles;
|
|
|
|
while c > 0 {
|
|
|
|
c -= 1;
|
|
|
|
buffer[c] = letters[i & 0xF] as u8;
|
|
|
|
i >>= 4;
|
|
|
|
}
|
2013-05-04 13:25:15 -05:00
|
|
|
self.write(buffer.slice(0, uint_nibbles));
|
|
|
|
}
|
2013-03-15 14:24:24 -05:00
|
|
|
|
2013-05-04 13:25:15 -05:00
|
|
|
unsafe fn write_cstr(&self, p: *c_char) {
|
|
|
|
use libc::strlen;
|
|
|
|
use vec;
|
|
|
|
|
|
|
|
let len = strlen(p);
|
|
|
|
let p: *u8 = transmute(p);
|
|
|
|
do vec::raw::buf_as_slice(p, len as uint) |s| {
|
|
|
|
self.write(s);
|
|
|
|
}
|
2013-03-15 14:24:24 -05:00
|
|
|
}
|
2012-07-17 12:48:19 -05:00
|
|
|
}
|
|
|
|
|
2012-07-23 18:00:19 -05:00
|
|
|
// NB: Calls to free CANNOT be allowed to fail, as throwing an exception from
|
|
|
|
// inside a landing pad may corrupt the state of the exception handler. If a
|
|
|
|
// problem occurs, call exit instead.
|
2013-01-07 14:21:34 -06:00
|
|
|
#[lang="exchange_free"]
|
2013-03-28 10:46:08 -05:00
|
|
|
#[inline(always)]
|
2013-02-27 20:34:04 -06:00
|
|
|
pub unsafe fn exchange_free(ptr: *c_char) {
|
2013-05-07 17:54:06 -05:00
|
|
|
global_heap::free(transmute(ptr))
|
2012-07-17 12:48:19 -05:00
|
|
|
}
|
|
|
|
|
2013-01-07 14:21:34 -06:00
|
|
|
#[lang="malloc"]
|
2013-02-27 20:34:04 -06:00
|
|
|
pub unsafe fn local_malloc(td: *c_char, size: uintptr_t) -> *c_char {
|
2013-04-21 21:03:52 -05:00
|
|
|
match context() {
|
|
|
|
OldTaskContext => {
|
2013-04-23 17:16:04 -05:00
|
|
|
return rustrt::rust_upcall_malloc_noswitch(td, size);
|
2013-04-21 21:03:52 -05:00
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
let mut alloc = ::ptr::null();
|
2013-05-19 18:50:21 -05:00
|
|
|
do Local::borrow::<Task> |task| {
|
2013-05-19 03:04:01 -05:00
|
|
|
alloc = task.heap.alloc(td as *c_void, size as uint) as *c_char;
|
2013-04-21 21:03:52 -05:00
|
|
|
}
|
|
|
|
return alloc;
|
|
|
|
}
|
|
|
|
}
|
2012-07-17 12:48:19 -05:00
|
|
|
}
|
|
|
|
|
2012-07-23 18:00:19 -05:00
|
|
|
// NB: Calls to free CANNOT be allowed to fail, as throwing an exception from
|
|
|
|
// inside a landing pad may corrupt the state of the exception handler. If a
|
|
|
|
// problem occurs, call exit instead.
|
2013-01-07 14:21:34 -06:00
|
|
|
#[lang="free"]
|
2013-02-27 20:34:04 -06:00
|
|
|
pub unsafe fn local_free(ptr: *c_char) {
|
2013-04-21 21:03:52 -05:00
|
|
|
match context() {
|
|
|
|
OldTaskContext => {
|
2013-04-23 17:16:04 -05:00
|
|
|
rustrt::rust_upcall_free_noswitch(ptr);
|
2013-04-21 21:03:52 -05:00
|
|
|
}
|
|
|
|
_ => {
|
2013-05-19 18:50:21 -05:00
|
|
|
do Local::borrow::<Task> |task| {
|
2013-05-19 03:04:01 -05:00
|
|
|
task.heap.free(ptr as *c_void);
|
2013-04-21 21:03:52 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-07-17 12:48:19 -05:00
|
|
|
}
|
|
|
|
|
2013-05-01 09:29:47 -05:00
|
|
|
#[lang="borrow_as_imm"]
|
|
|
|
#[inline(always)]
|
2013-05-01 12:48:00 -05:00
|
|
|
pub unsafe fn borrow_as_imm(a: *u8, file: *c_char, line: size_t) -> uint {
|
2013-05-01 09:29:47 -05:00
|
|
|
let a: *mut BoxRepr = transmute(a);
|
2013-05-04 13:25:15 -05:00
|
|
|
let old_ref_count = (*a).header.ref_count;
|
|
|
|
let new_ref_count = old_ref_count | FROZEN_BIT;
|
2013-05-01 19:23:07 -05:00
|
|
|
|
2013-05-04 13:25:15 -05:00
|
|
|
debug_borrow("borrow_as_imm:", a, old_ref_count, new_ref_count, file, line);
|
2013-05-01 19:23:07 -05:00
|
|
|
|
2013-05-04 13:25:15 -05:00
|
|
|
if (old_ref_count & MUT_BIT) != 0 {
|
2013-05-01 12:48:00 -05:00
|
|
|
fail_borrowed(a, file, line);
|
2013-05-01 09:29:47 -05:00
|
|
|
}
|
2013-05-03 04:42:00 -05:00
|
|
|
|
2013-05-04 13:25:15 -05:00
|
|
|
(*a).header.ref_count = new_ref_count;
|
2013-05-03 14:12:04 -05:00
|
|
|
|
2013-05-04 13:25:15 -05:00
|
|
|
old_ref_count
|
2013-05-01 09:29:47 -05:00
|
|
|
}
|
|
|
|
|
2013-05-01 12:48:00 -05:00
|
|
|
#[lang="borrow_as_mut"]
|
|
|
|
#[inline(always)]
|
|
|
|
pub unsafe fn borrow_as_mut(a: *u8, file: *c_char, line: size_t) -> uint {
|
|
|
|
let a: *mut BoxRepr = transmute(a);
|
2013-05-04 13:25:15 -05:00
|
|
|
let old_ref_count = (*a).header.ref_count;
|
|
|
|
let new_ref_count = old_ref_count | MUT_BIT | FROZEN_BIT;
|
2013-05-01 12:48:00 -05:00
|
|
|
|
2013-05-04 13:25:15 -05:00
|
|
|
debug_borrow("borrow_as_mut:", a, old_ref_count, new_ref_count, file, line);
|
2013-05-01 19:23:07 -05:00
|
|
|
|
2013-05-04 13:25:15 -05:00
|
|
|
if (old_ref_count & (MUT_BIT|FROZEN_BIT)) != 0 {
|
2013-05-01 12:48:00 -05:00
|
|
|
fail_borrowed(a, file, line);
|
|
|
|
}
|
2013-05-03 14:12:04 -05:00
|
|
|
|
2013-05-04 13:25:15 -05:00
|
|
|
(*a).header.ref_count = new_ref_count;
|
2013-05-03 14:12:04 -05:00
|
|
|
|
2013-05-04 13:25:15 -05:00
|
|
|
old_ref_count
|
2013-05-01 12:48:00 -05:00
|
|
|
}
|
|
|
|
|
2013-05-03 04:42:00 -05:00
|
|
|
|
|
|
|
#[lang="record_borrow"]
|
|
|
|
pub unsafe fn record_borrow(a: *u8, old_ref_count: uint,
|
|
|
|
file: *c_char, line: size_t) {
|
|
|
|
if (old_ref_count & ALL_BITS) == 0 {
|
|
|
|
// was not borrowed before
|
|
|
|
let a: *mut BoxRepr = transmute(a);
|
2013-05-04 13:25:15 -05:00
|
|
|
debug_borrow("record_borrow:", a, old_ref_count, 0, file, line);
|
2013-05-03 04:42:00 -05:00
|
|
|
do swap_task_borrow_list |borrow_list| {
|
|
|
|
let mut borrow_list = borrow_list;
|
|
|
|
borrow_list.push(BorrowRecord {box: a, file: file, line: line});
|
|
|
|
borrow_list
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[lang="unrecord_borrow"]
|
|
|
|
pub unsafe fn unrecord_borrow(a: *u8, old_ref_count: uint,
|
|
|
|
file: *c_char, line: size_t) {
|
|
|
|
if (old_ref_count & ALL_BITS) == 0 {
|
2013-05-06 10:16:17 -05:00
|
|
|
// was not borrowed before, so we should find the record at
|
|
|
|
// the end of the list
|
2013-05-03 04:42:00 -05:00
|
|
|
let a: *mut BoxRepr = transmute(a);
|
2013-05-04 13:25:15 -05:00
|
|
|
debug_borrow("unrecord_borrow:", a, old_ref_count, 0, file, line);
|
2013-05-03 04:42:00 -05:00
|
|
|
do swap_task_borrow_list |borrow_list| {
|
|
|
|
let mut borrow_list = borrow_list;
|
2013-05-06 10:16:17 -05:00
|
|
|
assert!(!borrow_list.is_empty());
|
|
|
|
let br = borrow_list.pop();
|
|
|
|
if br.box != a || br.file != file || br.line != line {
|
|
|
|
let err = fmt!("wrong borrow found, br=%?", br);
|
|
|
|
do str::as_buf(err) |msg_p, _| {
|
|
|
|
fail_(msg_p as *c_char, file, line)
|
2013-05-03 04:42:00 -05:00
|
|
|
}
|
|
|
|
}
|
2013-05-06 10:16:17 -05:00
|
|
|
borrow_list
|
2013-05-03 04:42:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-01 12:48:00 -05:00
|
|
|
#[lang="return_to_mut"]
|
|
|
|
#[inline(always)]
|
2013-05-04 13:25:15 -05:00
|
|
|
pub unsafe fn return_to_mut(a: *u8, orig_ref_count: uint,
|
2013-05-01 20:46:34 -05:00
|
|
|
file: *c_char, line: size_t) {
|
2013-05-01 12:48:00 -05:00
|
|
|
// Sometimes the box is null, if it is conditionally frozen.
|
|
|
|
// See e.g. #4904.
|
|
|
|
if !a.is_null() {
|
|
|
|
let a: *mut BoxRepr = transmute(a);
|
2013-05-04 13:25:15 -05:00
|
|
|
let old_ref_count = (*a).header.ref_count;
|
|
|
|
let new_ref_count =
|
|
|
|
(old_ref_count & !ALL_BITS) | (orig_ref_count & ALL_BITS);
|
|
|
|
|
|
|
|
debug_borrow("return_to_mut:",
|
|
|
|
a, old_ref_count, new_ref_count, file, line);
|
|
|
|
|
|
|
|
(*a).header.ref_count = new_ref_count;
|
2013-05-01 12:48:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-01 08:14:47 -05:00
|
|
|
#[lang="check_not_borrowed"]
|
|
|
|
#[inline(always)]
|
|
|
|
pub unsafe fn check_not_borrowed(a: *u8,
|
|
|
|
file: *c_char,
|
|
|
|
line: size_t) {
|
|
|
|
let a: *mut BoxRepr = transmute(a);
|
2013-05-03 14:12:04 -05:00
|
|
|
let ref_count = (*a).header.ref_count;
|
2013-05-04 13:25:15 -05:00
|
|
|
debug_borrow("check_not_borrowed:", a, ref_count, 0, file, line);
|
2013-05-03 14:12:04 -05:00
|
|
|
if (ref_count & FROZEN_BIT) != 0 {
|
2013-05-01 09:29:47 -05:00
|
|
|
fail_borrowed(a, file, line);
|
2013-01-11 23:01:42 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-29 21:59:52 -06:00
|
|
|
#[lang="strdup_uniq"]
|
2013-03-28 10:46:08 -05:00
|
|
|
#[inline(always)]
|
2013-01-29 21:59:52 -06:00
|
|
|
pub unsafe fn strdup_uniq(ptr: *c_uchar, len: uint) -> ~str {
|
|
|
|
str::raw::from_buf_len(ptr, len)
|
|
|
|
}
|
|
|
|
|
2013-02-26 21:40:39 -06:00
|
|
|
#[lang="start"]
|
2013-03-30 21:59:21 -05:00
|
|
|
pub fn start(main: *u8, argc: int, argv: **c_char,
|
|
|
|
crate_map: *u8) -> int {
|
2013-05-08 16:52:30 -05:00
|
|
|
use rt;
|
|
|
|
use sys::Closure;
|
|
|
|
use ptr;
|
|
|
|
use cast;
|
2013-05-08 17:28:30 -05:00
|
|
|
use os;
|
2013-03-30 21:59:21 -05:00
|
|
|
|
|
|
|
unsafe {
|
2013-05-08 17:28:30 -05:00
|
|
|
let use_old_rt = os::getenv("RUST_NEWRT").is_none();
|
2013-03-30 22:00:19 -05:00
|
|
|
if use_old_rt {
|
2013-03-30 21:59:21 -05:00
|
|
|
return rust_start(main as *c_void, argc as c_int, argv,
|
|
|
|
crate_map as *c_void) as int;
|
|
|
|
} else {
|
2013-05-08 18:53:40 -05:00
|
|
|
return do rt::start(argc, argv as **u8, crate_map) {
|
2013-05-08 16:52:30 -05:00
|
|
|
unsafe {
|
|
|
|
// `main` is an `fn() -> ()` that doesn't take an environment
|
|
|
|
// XXX: Could also call this as an `extern "Rust" fn` once they work
|
|
|
|
let main = Closure {
|
|
|
|
code: main as *(),
|
|
|
|
env: ptr::null(),
|
|
|
|
};
|
|
|
|
let mainfn: &fn() = cast::transmute(main);
|
|
|
|
|
|
|
|
mainfn();
|
|
|
|
}
|
|
|
|
};
|
2013-03-30 21:59:21 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extern {
|
|
|
|
fn rust_start(main: *c_void, argc: c_int, argv: **c_char,
|
|
|
|
crate_map: *c_void) -> c_int;
|
|
|
|
}
|
|
|
|
}
|