1 // Copyright 2012 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.
12 //! The global (exchange) heap.
14 use libc::{c_void, size_t, free, malloc, realloc};
15 use ptr::{RawPtr, mut_null};
16 use intrinsics::abort;
21 pub fn get_box_size(body_size: uint, body_align: uint) -> uint {
22 let header_size = size_of::<raw::Box<()>>();
23 let total_size = align_to(header_size, body_align) + body_size;
27 // Rounds |size| to the nearest |alignment|. Invariant: |alignment| is a power
30 fn align_to(size: uint, align: uint) -> uint {
32 (size + align - 1) & !(align - 1)
35 /// A wrapper around libc::malloc, aborting on out-of-memory
37 pub unsafe fn malloc_raw(size: uint) -> *mut u8 {
38 // `malloc(0)` may allocate, but it may also return a null pointer
39 // http://pubs.opengroup.org/onlinepubs/9699919799/functions/malloc.html
43 let p = malloc(size as size_t);
45 // we need a non-allocating way to print an error here
52 /// A wrapper around libc::realloc, aborting on out-of-memory
54 pub unsafe fn realloc_raw(ptr: *mut u8, size: uint) -> *mut u8 {
55 // `realloc(ptr, 0)` may allocate, but it may also return a null pointer
56 // http://pubs.opengroup.org/onlinepubs/9699919799/functions/realloc.html
58 free(ptr as *mut c_void);
61 let p = realloc(ptr as *mut c_void, size as size_t);
63 // we need a non-allocating way to print an error here
70 /// The allocator for unique pointers without contained managed pointers.
71 #[cfg(not(test), stage0)]
72 #[lang="exchange_malloc"]
74 pub unsafe fn exchange_malloc(size: uint) -> *mut u8 {
75 // The compiler never calls `exchange_free` on Box<ZeroSizeType>, so
76 // zero-size allocations can point to this `static`. It would be incorrect
77 // to use a null pointer, due to enums assuming types like unique pointers
79 static EMPTY: () = ();
82 &EMPTY as *() as *mut u8
88 /// The allocator for unique pointers without contained managed pointers.
89 #[cfg(not(test), not(stage0))]
90 #[lang="exchange_malloc"]
92 pub unsafe fn exchange_malloc(size: uint, _align: uint) -> *mut u8 {
93 // The compiler never calls `exchange_free` on ~ZeroSizeType, so zero-size
94 // allocations can point to this `static`. It would be incorrect to use a null
95 // pointer, due to enums assuming types like unique pointers are never null.
96 static EMPTY: () = ();
99 &EMPTY as *() as *mut u8
107 #[lang="closure_exchange_malloc"]
109 pub unsafe fn closure_exchange_malloc_(drop_glue: fn(*mut u8), size: uint, align: uint) -> *u8 {
110 closure_exchange_malloc(drop_glue, size, align)
114 pub unsafe fn closure_exchange_malloc(drop_glue: fn(*mut u8), size: uint, align: uint) -> *u8 {
115 let total_size = get_box_size(size, align);
116 let p = malloc_raw(total_size);
118 let alloc = p as *mut raw::Box<()>;
119 (*alloc).drop_glue = drop_glue;
124 // NB: Calls to free CANNOT be allowed to fail, as throwing an exception from
125 // inside a landing pad may corrupt the state of the exception handler.
127 #[lang="exchange_free"]
129 pub unsafe fn exchange_free_(ptr: *u8) {
134 pub unsafe fn exchange_free(ptr: *u8) {
135 free(ptr as *mut c_void);
143 pub extern "C" fn rust_malloc(size: uint) -> *mut u8 {
144 unsafe { exchange_malloc(size) }
152 pub extern "C" fn rust_malloc(size: uint, align: uint) -> *mut u8 {
153 unsafe { exchange_malloc(size, align) }
160 pub extern "C" fn rust_free(ptr: *u8) {
161 unsafe { exchange_free(ptr) }
167 use self::test::Bencher;
170 fn alloc_owned_small(b: &mut Bencher) {
177 fn alloc_owned_big(b: &mut Bencher) {