2013-04-21 21:03:52 -05:00
|
|
|
// Copyright 2013 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.
|
|
|
|
|
|
|
|
//! The local, garbage collected heap
|
|
|
|
|
2013-06-22 03:09:06 -05:00
|
|
|
use libc;
|
2013-04-21 21:03:52 -05:00
|
|
|
use libc::{c_void, uintptr_t, size_t};
|
|
|
|
use ops::Drop;
|
2013-08-08 13:38:10 -05:00
|
|
|
use option::{Option, None, Some};
|
2013-06-22 03:09:06 -05:00
|
|
|
use rt::local::Local;
|
|
|
|
use rt::task::Task;
|
2013-07-21 19:20:52 -05:00
|
|
|
use unstable::raw;
|
2013-04-21 21:03:52 -05:00
|
|
|
|
|
|
|
type MemoryRegion = c_void;
|
2013-06-22 03:09:06 -05:00
|
|
|
|
|
|
|
struct Env { priv opaque: () }
|
|
|
|
|
|
|
|
struct BoxedRegion {
|
|
|
|
env: *Env,
|
|
|
|
backing_region: *MemoryRegion,
|
2013-07-21 19:20:52 -05:00
|
|
|
live_allocs: *raw::Box<()>,
|
2013-06-22 03:09:06 -05:00
|
|
|
}
|
2013-04-21 21:03:52 -05:00
|
|
|
|
|
|
|
pub type OpaqueBox = c_void;
|
|
|
|
pub type TypeDesc = c_void;
|
|
|
|
|
|
|
|
pub struct LocalHeap {
|
|
|
|
memory_region: *MemoryRegion,
|
|
|
|
boxed_region: *BoxedRegion
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LocalHeap {
|
2013-08-14 20:41:40 -05:00
|
|
|
#[fixed_stack_segment] #[inline(never)]
|
2013-04-21 21:03:52 -05:00
|
|
|
pub fn new() -> LocalHeap {
|
|
|
|
unsafe {
|
|
|
|
// XXX: These usually come from the environment
|
|
|
|
let detailed_leaks = false as uintptr_t;
|
|
|
|
let poison_on_free = false as uintptr_t;
|
2013-08-23 00:42:36 -05:00
|
|
|
let region = rust_new_memory_region(detailed_leaks, poison_on_free);
|
2013-04-21 21:03:52 -05:00
|
|
|
assert!(region.is_not_null());
|
|
|
|
let boxed = rust_new_boxed_region(region, poison_on_free);
|
|
|
|
assert!(boxed.is_not_null());
|
|
|
|
LocalHeap {
|
|
|
|
memory_region: region,
|
|
|
|
boxed_region: boxed
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-14 20:41:40 -05:00
|
|
|
#[fixed_stack_segment] #[inline(never)]
|
2013-04-21 21:03:52 -05:00
|
|
|
pub fn alloc(&mut self, td: *TypeDesc, size: uint) -> *OpaqueBox {
|
|
|
|
unsafe {
|
|
|
|
return rust_boxed_region_malloc(self.boxed_region, td, size as size_t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-14 20:41:40 -05:00
|
|
|
#[fixed_stack_segment] #[inline(never)]
|
2013-06-21 21:40:00 -05:00
|
|
|
pub fn realloc(&mut self, ptr: *OpaqueBox, size: uint) -> *OpaqueBox {
|
|
|
|
unsafe {
|
|
|
|
return rust_boxed_region_realloc(self.boxed_region, ptr, size as size_t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-14 20:41:40 -05:00
|
|
|
#[fixed_stack_segment] #[inline(never)]
|
2013-04-21 21:03:52 -05:00
|
|
|
pub fn free(&mut self, box: *OpaqueBox) {
|
|
|
|
unsafe {
|
|
|
|
return rust_boxed_region_free(self.boxed_region, box);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for LocalHeap {
|
2013-08-14 20:41:40 -05:00
|
|
|
#[fixed_stack_segment] #[inline(never)]
|
2013-09-16 20:18:07 -05:00
|
|
|
fn drop(&mut self) {
|
2013-04-21 21:03:52 -05:00
|
|
|
unsafe {
|
|
|
|
rust_delete_boxed_region(self.boxed_region);
|
|
|
|
rust_delete_memory_region(self.memory_region);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-09 12:34:27 -05:00
|
|
|
pub unsafe fn local_malloc(td: *libc::c_char, size: libc::uintptr_t) -> *libc::c_char {
|
|
|
|
// XXX: Unsafe borrow for speed. Lame.
|
|
|
|
let task: Option<*mut Task> = Local::try_unsafe_borrow();
|
|
|
|
match task {
|
|
|
|
Some(task) => {
|
|
|
|
(*task).heap.alloc(td as *libc::c_void, size as uint) as *libc::c_char
|
|
|
|
}
|
|
|
|
None => rtabort!("local malloc outside of task")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-22 03:09:06 -05:00
|
|
|
// A little compatibility function
|
|
|
|
pub unsafe fn local_free(ptr: *libc::c_char) {
|
2013-08-12 21:09:46 -05:00
|
|
|
// XXX: Unsafe borrow for speed. Lame.
|
2013-08-08 13:38:10 -05:00
|
|
|
let task_ptr: Option<*mut Task> = Local::try_unsafe_borrow();
|
|
|
|
match task_ptr {
|
2013-08-12 21:09:46 -05:00
|
|
|
Some(task) => {
|
|
|
|
(*task).heap.free(ptr as *libc::c_void);
|
|
|
|
}
|
|
|
|
None => rtabort!("local free outside of task")
|
2013-06-22 03:09:06 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-21 19:20:52 -05:00
|
|
|
pub fn live_allocs() -> *raw::Box<()> {
|
2013-08-08 13:38:10 -05:00
|
|
|
let region = do Local::borrow |task: &mut Task| {
|
2013-08-01 01:12:20 -05:00
|
|
|
task.heap.boxed_region
|
2013-06-22 03:09:06 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
return unsafe { (*region).live_allocs };
|
|
|
|
}
|
|
|
|
|
2013-04-21 21:03:52 -05:00
|
|
|
extern {
|
2013-08-23 00:42:36 -05:00
|
|
|
fn rust_new_memory_region(detailed_leaks: uintptr_t,
|
2013-04-21 21:03:52 -05:00
|
|
|
poison_on_free: uintptr_t) -> *MemoryRegion;
|
|
|
|
fn rust_delete_memory_region(region: *MemoryRegion);
|
|
|
|
fn rust_new_boxed_region(region: *MemoryRegion,
|
|
|
|
poison_on_free: uintptr_t) -> *BoxedRegion;
|
|
|
|
fn rust_delete_boxed_region(region: *BoxedRegion);
|
|
|
|
fn rust_boxed_region_malloc(region: *BoxedRegion,
|
|
|
|
td: *TypeDesc,
|
|
|
|
size: size_t) -> *OpaqueBox;
|
2013-06-21 21:40:00 -05:00
|
|
|
fn rust_boxed_region_realloc(region: *BoxedRegion,
|
|
|
|
ptr: *OpaqueBox,
|
|
|
|
size: size_t) -> *OpaqueBox;
|
2013-04-21 21:03:52 -05:00
|
|
|
fn rust_boxed_region_free(region: *BoxedRegion, box: *OpaqueBox);
|
|
|
|
}
|
2013-07-22 13:42:47 -05:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod bench {
|
|
|
|
use extra::test::BenchHarness;
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn alloc_managed_small(bh: &mut BenchHarness) {
|
|
|
|
do bh.iter {
|
|
|
|
@10;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn alloc_managed_big(bh: &mut BenchHarness) {
|
|
|
|
do bh.iter {
|
|
|
|
@[10, ..1000];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|