|
| 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. |
| 4 | +// |
| 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. |
| 10 | + |
| 11 | +//! The local, garbage collected heap |
| 12 | +
|
| 13 | +use libc::{c_void, uintptr_t, size_t}; |
| 14 | +use ops::Drop; |
| 15 | + |
| 16 | +type MemoryRegion = c_void; |
| 17 | +type BoxedRegion = c_void; |
| 18 | + |
| 19 | +pub type OpaqueBox = c_void; |
| 20 | +pub type TypeDesc = c_void; |
| 21 | + |
| 22 | +pub struct LocalHeap { |
| 23 | + memory_region: *MemoryRegion, |
| 24 | + boxed_region: *BoxedRegion |
| 25 | +} |
| 26 | + |
| 27 | +impl LocalHeap { |
| 28 | + pub fn new() -> LocalHeap { |
| 29 | + unsafe { |
| 30 | + // Don't need synchronization for the single-threaded local heap |
| 31 | + let synchronized = false as uintptr_t; |
| 32 | + // XXX: These usually come from the environment |
| 33 | + let detailed_leaks = false as uintptr_t; |
| 34 | + let poison_on_free = false as uintptr_t; |
| 35 | + let region = rust_new_memory_region(synchronized, detailed_leaks, poison_on_free); |
| 36 | + assert!(region.is_not_null()); |
| 37 | + let boxed = rust_new_boxed_region(region, poison_on_free); |
| 38 | + assert!(boxed.is_not_null()); |
| 39 | + LocalHeap { |
| 40 | + memory_region: region, |
| 41 | + boxed_region: boxed |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + pub fn alloc(&mut self, td: *TypeDesc, size: uint) -> *OpaqueBox { |
| 47 | + unsafe { |
| 48 | + return rust_boxed_region_malloc(self.boxed_region, td, size as size_t); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + pub fn free(&mut self, box: *OpaqueBox) { |
| 53 | + unsafe { |
| 54 | + return rust_boxed_region_free(self.boxed_region, box); |
| 55 | + } |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +impl Drop for LocalHeap { |
| 60 | + fn finalize(&self) { |
| 61 | + unsafe { |
| 62 | + rust_delete_boxed_region(self.boxed_region); |
| 63 | + rust_delete_memory_region(self.memory_region); |
| 64 | + } |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +extern { |
| 69 | + fn rust_new_memory_region(synchronized: uintptr_t, |
| 70 | + detailed_leaks: uintptr_t, |
| 71 | + poison_on_free: uintptr_t) -> *MemoryRegion; |
| 72 | + fn rust_delete_memory_region(region: *MemoryRegion); |
| 73 | + fn rust_new_boxed_region(region: *MemoryRegion, |
| 74 | + poison_on_free: uintptr_t) -> *BoxedRegion; |
| 75 | + fn rust_delete_boxed_region(region: *BoxedRegion); |
| 76 | + fn rust_boxed_region_malloc(region: *BoxedRegion, |
| 77 | + td: *TypeDesc, |
| 78 | + size: size_t) -> *OpaqueBox; |
| 79 | + fn rust_boxed_region_free(region: *BoxedRegion, box: *OpaqueBox); |
| 80 | +} |
| 81 | + |
0 commit comments