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::{Some, None};
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 pub fn new() -> LocalHeap {
42 // Don't need synchronization for the single-threaded local heap
43 let synchronized = false as uintptr_t;
44 // XXX: These usually come from the environment
45 let detailed_leaks = false as uintptr_t;
46 let poison_on_free = false as uintptr_t;
47 let region = rust_new_memory_region(synchronized, detailed_leaks, poison_on_free);
48 assert!(region.is_not_null());
49 let boxed = rust_new_boxed_region(region, poison_on_free);
50 assert!(boxed.is_not_null());
52 memory_region: region,
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 pub fn realloc(&mut self, ptr: *OpaqueBox, size: uint) -> *OpaqueBox {
66 return rust_boxed_region_realloc(self.boxed_region, ptr, size as size_t);
70 pub fn free(&mut self, box: *OpaqueBox) {
72 return rust_boxed_region_free(self.boxed_region, box);
77 impl Drop for LocalHeap {
80 rust_delete_boxed_region(self.boxed_region);
81 rust_delete_memory_region(self.memory_region);
86 // A little compatibility function
87 pub unsafe fn local_free(ptr: *libc::c_char) {
88 // XXX: Unsafe borrow for speed. Lame.
89 match Local::try_unsafe_borrow::<Task>() {
91 (*task).heap.free(ptr as *libc::c_void);
93 None => rtabort!("local free outside of task")
97 pub fn live_allocs() -> *raw::Box<()> {
98 let region = do Local::borrow::<Task, *BoxedRegion> |task| {
99 task.heap.boxed_region
102 return unsafe { (*region).live_allocs };
107 fn rust_new_memory_region(synchronized: uintptr_t,
108 detailed_leaks: uintptr_t,
109 poison_on_free: uintptr_t) -> *MemoryRegion;
111 fn rust_delete_memory_region(region: *MemoryRegion);
113 fn rust_new_boxed_region(region: *MemoryRegion,
114 poison_on_free: uintptr_t) -> *BoxedRegion;
116 fn rust_delete_boxed_region(region: *BoxedRegion);
118 fn rust_boxed_region_malloc(region: *BoxedRegion,
120 size: size_t) -> *OpaqueBox;
122 fn rust_boxed_region_realloc(region: *BoxedRegion,
124 size: size_t) -> *OpaqueBox;
126 fn rust_boxed_region_free(region: *BoxedRegion, box: *OpaqueBox);
131 use extra::test::BenchHarness;
134 fn alloc_managed_small(bh: &mut BenchHarness) {
141 fn alloc_managed_big(bh: &mut BenchHarness) {