1 // Copyright 2013 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.
11 //! The local, garbage collected heap
14 use libc::{c_void, uintptr_t, size_t};
16 use option::{Option, None, Some};
21 type MemoryRegion = c_void;
23 struct Env { priv opaque: () }
27 backing_region: *MemoryRegion,
28 live_allocs: *raw::Box<()>,
31 pub type OpaqueBox = c_void;
32 pub type TypeDesc = c_void;
34 pub struct LocalHeap {
35 memory_region: *MemoryRegion,
36 boxed_region: *BoxedRegion
40 #[fixed_stack_segment] #[inline(never)]
41 pub fn new() -> LocalHeap {
43 // XXX: These usually come from the environment
44 let detailed_leaks = false as uintptr_t;
45 let poison_on_free = false as uintptr_t;
46 let region = rust_new_memory_region(detailed_leaks, poison_on_free);
47 assert!(region.is_not_null());
48 let boxed = rust_new_boxed_region(region, poison_on_free);
49 assert!(boxed.is_not_null());
51 memory_region: region,
57 #[fixed_stack_segment] #[inline(never)]
58 pub fn alloc(&mut self, td: *TypeDesc, size: uint) -> *OpaqueBox {
60 return rust_boxed_region_malloc(self.boxed_region, td, size as size_t);
64 #[fixed_stack_segment] #[inline(never)]
65 pub fn realloc(&mut self, ptr: *OpaqueBox, size: uint) -> *OpaqueBox {
67 return rust_boxed_region_realloc(self.boxed_region, ptr, size as size_t);
71 #[fixed_stack_segment] #[inline(never)]
72 pub fn free(&mut self, box: *OpaqueBox) {
74 return rust_boxed_region_free(self.boxed_region, box);
79 impl Drop for LocalHeap {
80 #[fixed_stack_segment] #[inline(never)]
83 rust_delete_boxed_region(self.boxed_region);
84 rust_delete_memory_region(self.memory_region);
89 // A little compatibility function
90 pub unsafe fn local_free(ptr: *libc::c_char) {
91 // XXX: Unsafe borrow for speed. Lame.
92 let task_ptr: Option<*mut Task> = Local::try_unsafe_borrow();
95 (*task).heap.free(ptr as *libc::c_void);
97 None => rtabort!("local free outside of task")
101 pub fn live_allocs() -> *raw::Box<()> {
102 let region = do Local::borrow |task: &mut Task| {
103 task.heap.boxed_region
106 return unsafe { (*region).live_allocs };
111 fn rust_new_memory_region(detailed_leaks: uintptr_t,
112 poison_on_free: uintptr_t) -> *MemoryRegion;
114 fn rust_delete_memory_region(region: *MemoryRegion);
116 fn rust_new_boxed_region(region: *MemoryRegion,
117 poison_on_free: uintptr_t) -> *BoxedRegion;
119 fn rust_delete_boxed_region(region: *BoxedRegion);
121 fn rust_boxed_region_malloc(region: *BoxedRegion,
123 size: size_t) -> *OpaqueBox;
125 fn rust_boxed_region_realloc(region: *BoxedRegion,
127 size: size_t) -> *OpaqueBox;
129 fn rust_boxed_region_free(region: *BoxedRegion, box: *OpaqueBox);
134 use extra::test::BenchHarness;
137 fn alloc_managed_small(bh: &mut BenchHarness) {
144 fn alloc_managed_big(bh: &mut BenchHarness) {