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-09-19 18:52:32 -05:00
|
|
|
#[doc(hidden)];
|
|
|
|
|
2013-10-26 03:10:39 -05:00
|
|
|
use ptr;
|
2014-02-16 02:04:33 -06:00
|
|
|
use raw;
|
2012-09-14 18:12:18 -05:00
|
|
|
|
2014-01-15 06:47:37 -06:00
|
|
|
static RC_IMMORTAL : uint = 0x77777777;
|
|
|
|
|
2012-09-14 18:12:18 -05:00
|
|
|
/*
|
|
|
|
* Box annihilation
|
|
|
|
*
|
|
|
|
* This runs at task death to free all boxes.
|
|
|
|
*/
|
|
|
|
|
2013-05-02 17:33:18 -05:00
|
|
|
unsafe fn each_live_alloc(read_next_before: bool,
|
2014-01-15 06:47:37 -06:00
|
|
|
f: |alloc: *mut raw::Box<()>| -> bool)
|
2013-11-18 23:15:42 -06:00
|
|
|
-> bool {
|
2013-05-02 17:33:18 -05:00
|
|
|
//! Walks the internal list of allocations
|
|
|
|
|
2013-06-22 03:09:06 -05:00
|
|
|
use rt::local_heap;
|
2013-05-02 17:33:18 -05:00
|
|
|
|
2013-12-11 19:04:50 -06:00
|
|
|
let mut alloc = local_heap::live_allocs();
|
|
|
|
while alloc != ptr::mut_null() {
|
|
|
|
let next_before = (*alloc).next;
|
2013-05-02 17:33:18 -05:00
|
|
|
|
2014-01-15 06:47:37 -06:00
|
|
|
if !f(alloc) {
|
2013-05-02 17:33:18 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if read_next_before {
|
2013-12-11 19:04:50 -06:00
|
|
|
alloc = next_before;
|
2013-05-02 17:33:18 -05:00
|
|
|
} else {
|
2013-12-11 19:04:50 -06:00
|
|
|
alloc = (*alloc).next;
|
2013-05-02 17:33:18 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2013-02-20 17:02:21 -06:00
|
|
|
|
|
|
|
#[cfg(unix)]
|
|
|
|
fn debug_mem() -> bool {
|
2014-01-26 02:43:42 -06:00
|
|
|
// FIXME: Need to port the environment struct to newsched
|
2013-08-01 01:12:20 -05:00
|
|
|
false
|
2013-02-20 17:02:21 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
fn debug_mem() -> bool {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
2012-09-14 18:58:23 -05:00
|
|
|
/// Destroys all managed memory (i.e. @ boxes) held by the current task.
|
2014-02-05 22:05:30 -06:00
|
|
|
pub unsafe fn annihilate() {
|
|
|
|
use rt::local_heap::local_free;
|
|
|
|
|
|
|
|
let mut n_total_boxes = 0u;
|
|
|
|
|
|
|
|
// Pass 1: Make all boxes immortal.
|
|
|
|
//
|
|
|
|
// In this pass, nothing gets freed, so it does not matter whether
|
|
|
|
// we read the next field before or after the callback.
|
|
|
|
each_live_alloc(true, |alloc| {
|
|
|
|
n_total_boxes += 1;
|
|
|
|
(*alloc).ref_count = RC_IMMORTAL;
|
|
|
|
true
|
|
|
|
});
|
|
|
|
|
|
|
|
// Pass 2: Drop all boxes.
|
|
|
|
//
|
|
|
|
// In this pass, unique-managed boxes may get freed, but not
|
|
|
|
// managed boxes, so we must read the `next` field *after* the
|
|
|
|
// callback, as the original value may have been freed.
|
|
|
|
each_live_alloc(false, |alloc| {
|
|
|
|
let drop_glue = (*alloc).drop_glue;
|
|
|
|
let data = &mut (*alloc).data as *mut ();
|
|
|
|
drop_glue(data as *mut u8);
|
|
|
|
true
|
|
|
|
});
|
|
|
|
|
|
|
|
// Pass 3: Free all boxes.
|
|
|
|
//
|
|
|
|
// In this pass, managed boxes may get freed (but not
|
|
|
|
// unique-managed boxes, though I think that none of those are
|
|
|
|
// left), so we must read the `next` field before, since it will
|
|
|
|
// not be valid after.
|
|
|
|
each_live_alloc(true, |alloc| {
|
|
|
|
local_free(alloc as *u8);
|
|
|
|
true
|
|
|
|
});
|
|
|
|
|
|
|
|
if debug_mem() {
|
|
|
|
// We do logging here w/o allocation.
|
|
|
|
debug!("total boxes annihilated: {}", n_total_boxes);
|
2012-09-14 18:12:18 -05:00
|
|
|
}
|
|
|
|
}
|