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.
13 use libc::{c_char, size_t, STDERR_FILENO};
15 use io::{Writer, WriterUtil};
16 use option::{Option, None, Some};
19 use str::{OwnedStr, StrSlice};
22 use vec::ImmutableVector;
24 pub static FROZEN_BIT: uint = 1 << (uint::bits - 1);
25 pub static MUT_BIT: uint = 1 << (uint::bits - 2);
26 static ALL_BITS: uint = FROZEN_BIT | MUT_BIT;
30 box: *mut raw::Box<()>,
35 fn try_take_task_borrow_list() -> Option<~[BorrowRecord]> {
40 fn swap_task_borrow_list(_f: &fn(~[BorrowRecord]) -> ~[BorrowRecord]) {
44 pub unsafe fn clear_task_borrow_list() {
45 // pub because it is used by the box annihilator.
46 let _ = try_take_task_borrow_list();
49 unsafe fn fail_borrowed(box: *mut raw::Box<()>, file: *c_char, line: size_t) {
50 debug_borrow("fail_borrowed: ", box, 0, 0, file, line);
52 match try_take_task_borrow_list() {
53 None => { // not recording borrows
55 do msg.with_c_str |msg_p| {
56 sys::begin_unwind_(msg_p, file, line);
59 Some(borrow_list) => { // recording borrows
60 let mut msg = ~"borrowed";
62 for entry in borrow_list.rev_iter() {
65 let filename = str::raw::from_c_str(entry.file);
66 msg.push_str(filename);
67 msg.push_str(fmt!(":%u", entry.line as uint));
71 do msg.with_c_str |msg_p| {
72 sys::begin_unwind_(msg_p, file, line)
78 /// Because this code is so perf. sensitive, use a static constant so that
79 /// debug printouts are compiled out most of the time.
80 static ENABLE_DEBUG: bool = false;
83 unsafe fn debug_borrow<T>(tag: &'static str,
89 //! A useful debugging function that prints a pointer + tag + newline
90 //! without allocating memory.
94 debug_borrow_slow(tag, p, old_bits, new_bits, filename, line);
97 unsafe fn debug_borrow_slow<T>(tag: &'static str,
103 let dbg = STDERR_FILENO as io::fd_t;
105 dbg.write_hex(p as uint);
107 dbg.write_hex(old_bits);
109 dbg.write_hex(new_bits);
111 dbg.write_cstr(filename);
113 dbg.write_hex(line as uint);
119 fn write_hex(&self, val: uint);
120 unsafe fn write_cstr(&self, str: *c_char);
123 impl DebugPrints for io::fd_t {
124 fn write_hex(&self, mut i: uint) {
125 let letters = ['0', '1', '2', '3', '4', '5', '6', '7', '8',
126 '9', 'a', 'b', 'c', 'd', 'e', 'f'];
127 static UINT_NIBBLES: uint = ::uint::bytes << 1;
128 let mut buffer = [0_u8, ..UINT_NIBBLES+1];
129 let mut c = UINT_NIBBLES;
132 buffer[c] = letters[i & 0xF] as u8;
135 self.write(buffer.slice(0, UINT_NIBBLES));
138 unsafe fn write_cstr(&self, p: *c_char) {
139 #[fixed_stack_segment]; #[inline(never)];
144 let p: *u8 = transmute(p);
145 do vec::raw::buf_as_slice(p, len as uint) |s| {
152 pub unsafe fn borrow_as_imm(a: *u8, file: *c_char, line: size_t) -> uint {
153 let a = a as *mut raw::Box<()>;
154 let old_ref_count = (*a).ref_count;
155 let new_ref_count = old_ref_count | FROZEN_BIT;
157 debug_borrow("borrow_as_imm:", a, old_ref_count, new_ref_count, file, line);
159 if (old_ref_count & MUT_BIT) != 0 {
160 fail_borrowed(a, file, line);
163 (*a).ref_count = new_ref_count;
169 pub unsafe fn borrow_as_mut(a: *u8, file: *c_char, line: size_t) -> uint {
170 let a = a as *mut raw::Box<()>;
171 let old_ref_count = (*a).ref_count;
172 let new_ref_count = old_ref_count | MUT_BIT | FROZEN_BIT;
174 debug_borrow("borrow_as_mut:", a, old_ref_count, new_ref_count, file, line);
176 if (old_ref_count & (MUT_BIT|FROZEN_BIT)) != 0 {
177 fail_borrowed(a, file, line);
180 (*a).ref_count = new_ref_count;
185 pub unsafe fn record_borrow(a: *u8, old_ref_count: uint,
186 file: *c_char, line: size_t) {
187 if (old_ref_count & ALL_BITS) == 0 {
188 // was not borrowed before
189 let a = a as *mut raw::Box<()>;
190 debug_borrow("record_borrow:", a, old_ref_count, 0, file, line);
191 do swap_task_borrow_list |borrow_list| {
192 let mut borrow_list = borrow_list;
193 borrow_list.push(BorrowRecord {box: a, file: file, line: line});
199 pub unsafe fn unrecord_borrow(a: *u8, old_ref_count: uint,
200 file: *c_char, line: size_t) {
201 if (old_ref_count & ALL_BITS) == 0 {
202 // was not borrowed before, so we should find the record at
203 // the end of the list
204 let a = a as *mut raw::Box<()>;
205 debug_borrow("unrecord_borrow:", a, old_ref_count, 0, file, line);
206 do swap_task_borrow_list |borrow_list| {
207 let mut borrow_list = borrow_list;
208 assert!(!borrow_list.is_empty());
209 let br = borrow_list.pop();
210 if br.box != a || br.file != file || br.line != line {
211 let err = fmt!("wrong borrow found, br=%?", br);
212 do err.with_c_str |msg_p| {
213 sys::begin_unwind_(msg_p, file, line)
222 pub unsafe fn return_to_mut(a: *u8, orig_ref_count: uint,
223 file: *c_char, line: size_t) {
224 // Sometimes the box is null, if it is conditionally frozen.
227 let a = a as *mut raw::Box<()>;
228 let old_ref_count = (*a).ref_count;
230 (old_ref_count & !ALL_BITS) | (orig_ref_count & ALL_BITS);
232 debug_borrow("return_to_mut:",
233 a, old_ref_count, new_ref_count, file, line);
235 (*a).ref_count = new_ref_count;
240 pub unsafe fn check_not_borrowed(a: *u8,
243 let a = a as *mut raw::Box<()>;
244 let ref_count = (*a).ref_count;
245 debug_borrow("check_not_borrowed:", a, ref_count, 0, file, line);
246 if (ref_count & FROZEN_BIT) != 0 {
247 fail_borrowed(a, file, line);