1 // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
12 use libc::{c_char, c_void, size_t, STDERR_FILENO};
14 use io::{Writer, WriterUtil};
15 use managed::raw::BoxRepr;
16 use option::{Option, None, Some};
21 use vec::ImmutableVector;
23 #[allow(non_camel_case_types)]
24 type rust_task = c_void;
26 pub static FROZEN_BIT: uint = 1 << (uint::bits - 1);
27 pub static MUT_BIT: uint = 1 << (uint::bits - 2);
28 static ALL_BITS: uint = FROZEN_BIT | MUT_BIT;
37 fn try_take_task_borrow_list() -> Option<~[BorrowRecord]> {
39 let cur_task: *rust_task = rust_try_get_task();
40 if cur_task.is_not_null() {
41 let ptr = rust_take_task_borrow_list(cur_task);
45 let v: ~[BorrowRecord] = transmute(ptr);
54 fn swap_task_borrow_list(f: &fn(~[BorrowRecord]) -> ~[BorrowRecord]) {
56 let cur_task: *rust_task = rust_try_get_task();
57 if cur_task.is_not_null() {
58 let mut borrow_list: ~[BorrowRecord] = {
59 let ptr = rust_take_task_borrow_list(cur_task);
60 if ptr.is_null() { ~[] } else { transmute(ptr) }
62 borrow_list = f(borrow_list);
63 rust_set_task_borrow_list(cur_task, transmute(borrow_list));
68 pub unsafe fn clear_task_borrow_list() {
69 // pub because it is used by the box annihilator.
70 let _ = try_take_task_borrow_list();
73 unsafe fn fail_borrowed(box: *mut BoxRepr, file: *c_char, line: size_t) {
74 debug_borrow("fail_borrowed: ", box, 0, 0, file, line);
76 match try_take_task_borrow_list() {
77 None => { // not recording borrows
79 do str::as_buf(msg) |msg_p, _| {
80 sys::begin_unwind_(msg_p as *c_char, file, line);
83 Some(borrow_list) => { // recording borrows
84 let mut msg = ~"borrowed";
86 for borrow_list.rev_iter().advance |entry| {
89 let filename = str::raw::from_c_str(entry.file);
90 msg.push_str(filename);
91 msg.push_str(fmt!(":%u", entry.line as uint));
95 do str::as_buf(msg) |msg_p, _| {
96 sys::begin_unwind_(msg_p as *c_char, file, line)
102 /// Because this code is so perf. sensitive, use a static constant so that
103 /// debug printouts are compiled out most of the time.
104 static ENABLE_DEBUG: bool = false;
107 unsafe fn debug_borrow<T>(tag: &'static str,
113 //! A useful debugging function that prints a pointer + tag + newline
114 //! without allocating memory.
116 if ENABLE_DEBUG && ::rt::env::get().debug_borrow {
117 debug_borrow_slow(tag, p, old_bits, new_bits, filename, line);
120 unsafe fn debug_borrow_slow<T>(tag: &'static str,
126 let dbg = STDERR_FILENO as io::fd_t;
128 dbg.write_hex(p as uint);
130 dbg.write_hex(old_bits);
132 dbg.write_hex(new_bits);
134 dbg.write_cstr(filename);
136 dbg.write_hex(line as uint);
142 fn write_hex(&self, val: uint);
143 unsafe fn write_cstr(&self, str: *c_char);
146 impl DebugPrints for io::fd_t {
147 fn write_hex(&self, mut i: uint) {
148 let letters = ['0', '1', '2', '3', '4', '5', '6', '7', '8',
149 '9', 'a', 'b', 'c', 'd', 'e', 'f'];
150 static UINT_NIBBLES: uint = ::uint::bytes << 1;
151 let mut buffer = [0_u8, ..UINT_NIBBLES+1];
152 let mut c = UINT_NIBBLES;
155 buffer[c] = letters[i & 0xF] as u8;
158 self.write(buffer.slice(0, UINT_NIBBLES));
161 unsafe fn write_cstr(&self, p: *c_char) {
166 let p: *u8 = transmute(p);
167 do vec::raw::buf_as_slice(p, len as uint) |s| {
174 pub unsafe fn borrow_as_imm(a: *u8, file: *c_char, line: size_t) -> uint {
175 let a: *mut BoxRepr = transmute(a);
176 let old_ref_count = (*a).header.ref_count;
177 let new_ref_count = old_ref_count | FROZEN_BIT;
179 debug_borrow("borrow_as_imm:", a, old_ref_count, new_ref_count, file, line);
181 if (old_ref_count & MUT_BIT) != 0 {
182 fail_borrowed(a, file, line);
185 (*a).header.ref_count = new_ref_count;
191 pub unsafe fn borrow_as_mut(a: *u8, file: *c_char, line: size_t) -> uint {
192 let a: *mut BoxRepr = transmute(a);
193 let old_ref_count = (*a).header.ref_count;
194 let new_ref_count = old_ref_count | MUT_BIT | FROZEN_BIT;
196 debug_borrow("borrow_as_mut:", a, old_ref_count, new_ref_count, file, line);
198 if (old_ref_count & (MUT_BIT|FROZEN_BIT)) != 0 {
199 fail_borrowed(a, file, line);
202 (*a).header.ref_count = new_ref_count;
207 pub unsafe fn record_borrow(a: *u8, old_ref_count: uint,
208 file: *c_char, line: size_t) {
209 if (old_ref_count & ALL_BITS) == 0 {
210 // was not borrowed before
211 let a: *mut BoxRepr = transmute(a);
212 debug_borrow("record_borrow:", a, old_ref_count, 0, file, line);
213 do swap_task_borrow_list |borrow_list| {
214 let mut borrow_list = borrow_list;
215 borrow_list.push(BorrowRecord {box: a, file: file, line: line});
221 pub unsafe fn unrecord_borrow(a: *u8, old_ref_count: uint,
222 file: *c_char, line: size_t) {
223 if (old_ref_count & ALL_BITS) == 0 {
224 // was not borrowed before, so we should find the record at
225 // the end of the list
226 let a: *mut BoxRepr = transmute(a);
227 debug_borrow("unrecord_borrow:", a, old_ref_count, 0, file, line);
228 do swap_task_borrow_list |borrow_list| {
229 let mut borrow_list = borrow_list;
230 assert!(!borrow_list.is_empty());
231 let br = borrow_list.pop();
232 if br.box != a || br.file != file || br.line != line {
233 let err = fmt!("wrong borrow found, br=%?", br);
234 do str::as_buf(err) |msg_p, _| {
235 sys::begin_unwind_(msg_p as *c_char, file, line)
244 pub unsafe fn return_to_mut(a: *u8, orig_ref_count: uint,
245 file: *c_char, line: size_t) {
246 // Sometimes the box is null, if it is conditionally frozen.
249 let a: *mut BoxRepr = transmute(a);
250 let old_ref_count = (*a).header.ref_count;
252 (old_ref_count & !ALL_BITS) | (orig_ref_count & ALL_BITS);
254 debug_borrow("return_to_mut:",
255 a, old_ref_count, new_ref_count, file, line);
257 (*a).header.ref_count = new_ref_count;
262 pub unsafe fn check_not_borrowed(a: *u8,
265 let a: *mut BoxRepr = transmute(a);
266 let ref_count = (*a).header.ref_count;
267 debug_borrow("check_not_borrowed:", a, ref_count, 0, file, line);
268 if (ref_count & FROZEN_BIT) != 0 {
269 fail_borrowed(a, file, line);
276 pub fn rust_take_task_borrow_list(task: *rust_task) -> *c_void;
279 pub fn rust_set_task_borrow_list(task: *rust_task, map: *c_void);
282 pub fn rust_try_get_task() -> *rust_task;